File: tac_client.c

package info (click to toggle)
libauthen-tacacsplus-perl 0.28-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 228 kB
  • sloc: ansic: 1,291; perl: 154; makefile: 5
file content (362 lines) | stat: -rw-r--r-- 9,702 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

/* (C) 1997 Mike Shoyher msh@corbina.net, msh@apache.lexa.ru */

#include<netdb.h>
#include<stdio.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>
#include <sys/types.h> 
#include <sys/time.h>  
#include <unistd.h>  
#include <time.h>  
#include"tac_plus.h"
#include "tacplus_client.h"

int tac_sequence;
int tac_session_id;
int tac_maxtry=3;
int tac_timeout=15;
int tac_fd;                  
char tac_key[128];
struct sockaddr_in tac_port; 
struct hostent *tac_h;       
struct servent *tac_serv;    
char ourhost[128];
int ourhost_len;
char* ourtty="Virtual00";
int ourtty_len;
char* tac_err="NONE";

void fill_tac_hdr(struct tac_plus_pak_hdr* hdr)
{
    hdr->version=TAC_PLUS_VER_0;   
    hdr->type=TAC_PLUS_AUTHEN;     
    hdr->seq_no=tac_sequence;      
    hdr->encryption=TAC_PLUS_CLEAR;
    hdr->session_id=tac_session_id;
}

int send_data(void* buf, int buf_len, int fd);
int read_reply(void** data);

/* Need to specify the lengths, becuase CHAP passwords etc 
   may have NULs in them */
void send_auth_cont(char* msg, int msg_len);
int make_auth(char* username, int user_len,
	      char* password, int password_len,
	      int authen_type)
{
    struct tac_plus_pak_hdr hdr;
    struct authen_start as;
    int datalength;
    void* buf;
    void* data;
    int data_len;
    int buf_len;
    struct authen_reply* ar;

    fill_tac_hdr(&hdr);

    datalength=TAC_AUTHEN_START_FIXED_FIELDS_SIZE;


    as.action= TAC_PLUS_AUTHEN_LOGIN ;
    as.priv_lvl= TAC_PLUS_PRIV_LVL_MIN ;
    as.authen_type = authen_type ;
    as.service= TAC_PLUS_AUTHEN_SVC_LOGIN ;
    as.user_len=as.port_len=as.rem_addr_len=as.data_len=0;

    if (authen_type != TAC_PLUS_AUTHEN_TYPE_ASCII)
    {
	/* This will be a version 1 request with the username
	   and password in the start */
	hdr.version=TAC_PLUS_VER_1;
	as.user_len = user_len;
	as.data_len = password_len;
	
    }
    buf=malloc(buf_len=TAC_PLUS_HDR_SIZE+TAC_AUTHEN_START_FIXED_FIELDS_SIZE+ourtty_len+ourhost_len+as.user_len+as.data_len);

    /* Append user name if required */
    bcopy(username,buf+datalength+TAC_PLUS_HDR_SIZE,as.user_len);
    datalength+=(as.user_len);

    bcopy(ourtty,buf+datalength+TAC_PLUS_HDR_SIZE,ourtty_len);
    datalength+=(ourtty_len);
    as.port_len=ourtty_len;

    bcopy(ourhost,buf+datalength+TAC_PLUS_HDR_SIZE,ourhost_len);      
    datalength+=(ourhost_len); 
    as.rem_addr_len=ourhost_len;

    /* Append password if required */
    bcopy(password,buf+datalength+TAC_PLUS_HDR_SIZE,as.data_len);
    datalength+=(as.data_len);

    hdr.datalength= htonl(datalength) ;
    bcopy(&hdr,buf,TAC_PLUS_HDR_SIZE);
    bcopy(&as,buf+TAC_PLUS_HDR_SIZE,TAC_AUTHEN_START_FIXED_FIELDS_SIZE);
    md5_xor(buf, buf+TAC_PLUS_HDR_SIZE, tac_key);
    send_data(buf,buf_len,tac_fd);
    free(buf);

    while ((data_len=read_reply(&data))!=-1){

	ar=data;
	switch (ar->status) {
	case TAC_PLUS_AUTHEN_STATUS_GETUSER:
	    free(data);
	    send_auth_cont(username, user_len);
	    break;
	case TAC_PLUS_AUTHEN_STATUS_GETPASS:
	    free(data);
	    send_auth_cont(password, password_len);   
	    break;                      
	case TAC_PLUS_AUTHEN_STATUS_PASS:
	    return 1;
	case TAC_PLUS_AUTHEN_STATUS_FAIL: 
	    tac_err="Authentication failed";
	    return 0;                 
	default :
	    tac_err="Protocol error";
	    return 0;
	}

    }
    tac_err="Unknown error";
    return 0;
}

void send_auth_cont(char* msg, int msg_len)
{
struct tac_plus_pak_hdr hdr;
struct authen_cont ac;     
int datalength;             
void* buf;                  
int buf_len;

buf_len=TAC_PLUS_HDR_SIZE+TAC_AUTHEN_CONT_FIXED_FIELDS_SIZE+msg_len;
buf=malloc(buf_len);
fill_tac_hdr(&hdr);
ac.user_data_len=ac.flags=0;
ac.user_msg_len=htons(msg_len);
bcopy(msg,buf+TAC_PLUS_HDR_SIZE+TAC_AUTHEN_CONT_FIXED_FIELDS_SIZE,msg_len); 
datalength=msg_len+TAC_AUTHEN_CONT_FIXED_FIELDS_SIZE;                                  
hdr.datalength=htonl(datalength);
bcopy(&hdr,buf,TAC_PLUS_HDR_SIZE);                                    
bcopy(&ac,buf+TAC_PLUS_HDR_SIZE,TAC_AUTHEN_CONT_FIXED_FIELDS_SIZE);  
md5_xor(buf, buf+TAC_PLUS_HDR_SIZE, tac_key);                         
send_data(buf,buf_len,tac_fd);                                        
free(buf);
}

void myerror (char* s)
{
printf("%s\n",s);
exit(1);
}

int init_tac_session(char* host_name, char* port_name, char* key, int timeout)
{

int flags;
int res;
int optval;
socklen_t len;
fd_set wset;
struct timeval tv;

gethostname(ourhost,127);
ourhost_len=strlen(ourhost);
ourtty_len=strlen(ourtty);
srand(time(NULL));
if (timeout>0) tac_timeout=timeout;
strcpy(tac_key,key);
tac_session_id=rand();
tac_sequence=1;
tac_port.sin_family = AF_INET; 
if (*host_name>='0' && *host_name<='9') {                              
     tac_port.sin_addr.s_addr = inet_addr(host_name);                     
} else {                                                                
     tac_h = gethostbyname(host_name);                                    
     if (!tac_h) {                                                         
	 /*myerror("Cannot resolve host name");*/
	 tac_err="Cannot resolve host name";
         return -1;                                                   
     }                                                              
     tac_port.sin_addr = *((struct in_addr *) tac_h->h_addr);                 
}                                                                  
if (port_name==NULL) port_name="tacacs";
if (*port_name>='0' && *port_name<='9') {
  tac_port.sin_port=htons (atoi(port_name));
} else {
    tac_serv=getservbyname(port_name,"tcp");
    if (tac_serv)
      tac_port.sin_port=tac_serv->s_port;
    else {
        /*myerror("Unknown port");*/
	tac_err="Unknown port";
        return -1;
    }
}
if((tac_fd = socket (AF_INET, SOCK_STREAM, 0))<0) return -1;

// get flags
flags = fcntl(tac_fd, F_GETFL, 0);
if( flags < 0 ) {
	//fprintf( stderr, "fcntl: %s\n", strerror(errno) );
	tac_err = "socket error";
	close(tac_fd);
	return -1;
}

// set socket to nonblock
res = fcntl( tac_fd, F_SETFL, flags | O_NONBLOCK );
if( res < 0 ) {
	//fprintf( stderr, "fcntl: %s\n", strerror(errno) );
	tac_err = "socket error";
	close(tac_fd);
	return -1;
}

// connect
res = connect (tac_fd, (struct sockaddr *) &tac_port, sizeof tac_port);

// connection not established, but in progress
if( res < 0 && (errno != EINPROGRESS) ) {
	tac_err = "connection failed";
	close(tac_fd);
	return -1;
}

// wait for connection or timeout
if( res != 0 ) {
	FD_ZERO(&wset);
	FD_SET(tac_fd, &wset);
	tv.tv_sec  = timeout;
	tv.tv_usec = 0;

	res = select( tac_fd+1, NULL, &wset, NULL, &tv );
	if( res < 0 ) {
		tac_err = "select failed";
		close(tac_fd);
		return -1;
	}
	else if( res == 0 ) {
		tac_err = "timeout";
		close(tac_fd);
		return -1;
	}
	if( res > 0 ) {
		// socket is ready for writing. Check if connection was
		// established successfully
		len = sizeof(optval);
		if( getsockopt( tac_fd, SOL_SOCKET, SO_ERROR, (void *)&optval, &len ) > 0 ) {
			tac_err = "getsockopt failed";
			close(tac_fd);
			return -1;
		}
		if( optval != 0 ) {
			tac_err = "connection failed";
			close(tac_fd);
			return -1;
		}
		// optval == 0 --> no error, connection established
	}
}
	
return tac_fd;
}

int send_data(void* buf, int buf_len, int fd)
{
fd_set fds;
int rr;
int try;
struct timeval tv;

FD_ZERO(&fds);
FD_SET(fd,&fds);
tv.tv_sec = tac_timeout;
tv.tv_usec = 0;     
for(try=0;try<tac_maxtry;try++){
	rr=select(fd+1,NULL,&fds,NULL,&tv);
	if (FD_ISSET(fd,&fds)) {
        	if (buf_len!=write(fd,buf,buf_len)) continue;
		return 0;
	}
	myerror("Write error");
	return -1;
}
 return 1;
}

int read_data(void* buf, int buf_len, int fd)                 
{                                                             
    fd_set fds;                                                   
    int rr;                                                       
    int try;   
    int have_len = 0;
    struct timeval tv;                                            
    
    FD_ZERO(&fds);                                                
    FD_SET(fd,&fds);                                              
    tv.tv_sec = tac_timeout;                                      
    tv.tv_usec = 0;
    try = 0;
    while (have_len < buf_len && try++ < tac_maxtry)
    {                              
        rr = select(fd+1, &fds, NULL, NULL, &tv);                   
        if (FD_ISSET(fd, &fds)) 
	{                              
	    ssize_t read_len = read(fd,buf+have_len,buf_len-have_len);
	    if (read_len == 0)
	    {
		// Unexpected end-of file. Server disconnected us?
		return -1;
	    }
	    else if (read_len == -1)
	    {
		myerror("read error");
		return -1;
	    }
	    have_len += read_len;
	    if (buf_len != have_len) 
		continue; 

	    return 0;                                     
        }                                                     
    }                                                             
    myerror("too many retries");                               
    return -1;                                                              
}                                                             

int read_reply(void** data)
{
    int data_len;
    struct tac_plus_pak_hdr hdr;
    
    if (read_data(&hdr,TAC_PLUS_HDR_SIZE,tac_fd)==-1) return -1;
    data_len=ntohl(hdr.datalength);
    tac_sequence=hdr.seq_no+1;
    *data=malloc(data_len);
    if (read_data(*data,data_len,tac_fd)==-1) return -1;
    md5_xor(&hdr, *data, tac_key); 
    return data_len;
}

void deinit_tac_session()
{
	shutdown(tac_fd,2);
	close(tac_fd);
}


void report(int __pri, const char *__fmt, ...)
{
}

int debug;