forked from kravietz/pam_tacplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tacc.c
554 lines (469 loc) · 13.2 KB
/
tacc.c
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* tacc.c TACACS+ PAP authentication client
*
* Copyright 1997-98 by Pawel Krawczyk <[email protected]>
* Portions copyright (c) 1989 Carnegie Mellon University.
*
* See http://www.ceti.com.pl/~kravietz/progs/tacacs.html
* for details.
*
*/
#include <stdio.h>
#include <pwd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <utmp.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#include <getopt.h>
#include <ctype.h>
#include <openssl/rand.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "tacplus.h"
#include "libtac.h"
/* Prompt displayed when asking for password */
#define PASSWORD_PROMPT "Password: "
/* if defined, given command will be run after
* successful authentication and proper wtmp
* entries will be made
*/
#define DEFAULT_COMMAND "/usr/sbin/pppd -detach"
/* message that will be displayed to user
* before starting COMMAND
*/
#define COMMAND_MESSAGE "Starting PPP\n"
/* timeout for reading password from user (seconds) */
#define GETPASS_TIMEOUT 60
/* end of CONFIGURABLE PARAMETERS */
/* prototypes */
void sighandler(int sig);
void showusage(char *argv0);
unsigned long getservername(char *serv);
void showusage(char *progname);
void showversion(char *progname);
void authenticate(const struct addrinfo *tac_server, const char *tac_secret,
const char *user, const char *pass, const char *tty,
const char *remote_addr);
void timeout_handler(int signum);
#define EXIT_OK 0
#define EXIT_FAIL 1 /* AAA failure (or server error) */
#define EXIT_ERR 2 /* local error */
#define USE_SYSTEM 1
/* globals */
int tac_encryption = 1;
typedef unsigned char flag;
flag quiet = 0;
char *user = NULL; /* global, because of signal handler */
/* command line options */
static struct option long_options[] =
{
/* operation */
{ "authenticate", no_argument, NULL, 'T' }, { "authorize", no_argument,
NULL, 'R' }, { "account", no_argument, NULL, 'A' }, { "version",
no_argument, NULL, 'V' }, { "help", no_argument, NULL, 'h' },
/* data */
{ "username", required_argument, NULL, 'u' }, { "remote",
required_argument, NULL, 'r' }, { "password", required_argument,
NULL, 'p' }, { "server", required_argument, NULL, 's' }, {
"secret", required_argument, NULL, 'k' }, { "command",
required_argument, NULL, 'c' }, { "exec", required_argument,
NULL, 'c' }, { "service", required_argument, NULL, 'S' }, {
"protocol", required_argument, NULL, 'P' }, { "remote",
required_argument, NULL, 'r' }, { "login", required_argument,
NULL, 'L' },
/* modifiers */
{ "quiet", no_argument, NULL, 'q' },
{ "silent", no_argument, NULL, 'q' }, { "no-wtmp", no_argument,
NULL, 'w' }, { "no-encrypt", no_argument, NULL, 'n' }, {
0, 0, 0, 0 } };
/* command line letters */
char *opt_string = "TRAVhu:p:s:k:c:qr:wnS:P:L:";
int main(int argc, char **argv) {
char *pass = NULL;
char *tty;
char *command = NULL;
char *remote_addr = NULL;
char *service = NULL;
char *protocol = NULL;
struct addrinfo *tac_server;
char *tac_server_name = NULL;
char *tac_secret = NULL;
int tac_fd;
short int task_id = 0;
char buf[40];
int ret;
#ifndef USE_SYSTEM
pid_t pid;
#endif
char *msg;
struct areply arep;
/* options */
flag log_wtmp = 1;
flag do_author = 0;
flag do_authen = 0;
flag do_account = 0;
flag login_mode = 0;
/* check argc */
if (argc < 2) {
showusage(argv[0]);
exit(EXIT_ERR);
}
/* check for login mode */
if (argc == 2 && isalpha(*argv[1])) {
user = argv[1];
do_author = do_authen = do_account = 1;
command = DEFAULT_COMMAND;
login_mode = 1;
} else {
int c;
int opt_index;
while ((c = getopt_long(argc, argv, opt_string, long_options,
&opt_index)) != EOF) {
switch (c) {
case 'T':
do_authen = 1;
break;
case 'R':
do_author = 1;
break;
case 'A':
do_account = 1;
break;
case 'V':
showversion(argv[0]);
case 'h':
showusage(argv[0]);
case 'u':
user = optarg;
break;
case 'r':
remote_addr = optarg;
break;
case 'L':
// tac_login is a global variable initialized in libtac
bzero(tac_login, sizeof(tac_login));
strncpy(tac_login, optarg, sizeof(tac_login) - 1);
break;
case 'p':
pass = optarg;
break;
case 's':
tac_server_name = optarg;
break;
case 'k':
tac_secret = optarg;
break;
case 'c':
command = optarg;
break;
case 'S':
service = optarg;
break;
case 'P':
protocol = optarg;
break;
case 'q':
quiet = 1;
break;
case 'w':
log_wtmp = 0;
break;
case 'n':
tac_encryption = 0;
break;
}
}
}
/* check available information and set to defaults if needed */
if (do_authen + do_author + do_account == 0) {
printf("error: one of -TRAVh options is required\n");
exit(EXIT_ERR);
}
if (user == NULL) {
printf("error: username is required.\n");
exit(EXIT_ERR);
}
if (remote_addr == NULL) {
printf("error: remote address is required.\n");
exit(EXIT_ERR);
}
if (service == NULL) {
printf("error: service is required.\n");
exit(EXIT_ERR);
}
if (protocol == NULL) {
printf("error: protocol is required.\n");
exit(EXIT_ERR);
}
if (tac_server_name == NULL) {
printf("error: server name is required.\n");
exit(EXIT_ERR);
}
struct addrinfo hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
ret = getaddrinfo(tac_server_name, "tacacs", &hints, &tac_server);
if (ret != 0) {
printf("error: resolving name %s: %s", tac_server_name,
gai_strerror(ret));
exit(EXIT_ERR);
}
if (tac_secret == NULL) {
printf("error: server secret is required.\n");
exit(EXIT_ERR);
}
if (pass == NULL) {
signal(SIGALRM, timeout_handler);
alarm(GETPASS_TIMEOUT);
pass = getpass(PASSWORD_PROMPT);
alarm(0);
signal(SIGALRM, SIG_DFL);
if (!strlen(pass))
exit(EXIT_ERR);
}
tty = ttyname(0);
if (strncmp(tty, "/dev/", 5) == 0)
tty += 5;
/* open syslog before any TACACS+ calls */
openlog("tacc", LOG_CONS | LOG_PID, LOG_AUTHPRIV);
if (do_authen)
authenticate(tac_server, tac_secret, user, pass, tty, remote_addr);
if (do_author) {
/* authorize user */
struct tac_attrib *attr = NULL;
tac_add_attrib(&attr, "service", service);
tac_add_attrib(&attr, "protocol", protocol);
tac_fd = tac_connect_single(tac_server, tac_secret, NULL, 60);
if (tac_fd < 0) {
if (!quiet)
printf("Error connecting to TACACS+ server: %m\n");
exit(EXIT_ERR);
}
tac_author_send(tac_fd, user, tty, remote_addr, attr);
tac_author_read(tac_fd, &arep);
if (arep.status != AUTHOR_STATUS_PASS_ADD
&& arep.status != AUTHOR_STATUS_PASS_REPL) {
if (!quiet)
printf("Authorization FAILED: %s\n", arep.msg);
exit(EXIT_FAIL);
} else {
if (!quiet)
printf("Authorization OK: %s\n", arep.msg);
}
tac_free_attrib(&attr);
}
/* we no longer need the password in our address space */
bzero(pass, strlen(pass));
pass = NULL;
if (do_account) {
/* start accounting */
struct tac_attrib *attr = NULL;
sprintf(buf, "%lu", time(0));
tac_add_attrib(&attr, "start_time", buf);
RAND_pseudo_bytes((unsigned char *) &task_id, sizeof(task_id));
sprintf(buf, "%hu", task_id);
tac_add_attrib(&attr, "task_id", buf);
tac_add_attrib(&attr, "service", service);
tac_add_attrib(&attr, "protocol", protocol);
tac_fd = tac_connect_single(tac_server, tac_secret, NULL, 60);
if (tac_fd < 0) {
if (!quiet)
printf("Error connecting to TACACS+ server: %m\n");
exit(EXIT_ERR);
}
tac_acct_send(tac_fd, TAC_PLUS_ACCT_FLAG_START, user, tty, remote_addr,
attr);
ret = tac_acct_read(tac_fd, &arep);
if (ret == 0) {
if (!quiet)
printf("Accounting: START failed: %s\n", arep.msg);
syslog(LOG_INFO, "TACACS+ accounting start failed: %s", arep.msg);
} else if (!login_mode && !quiet)
printf("Accounting: START OK\n");
close(tac_fd);
tac_free_attrib(&attr);
}
/* log in local utmp */
#ifdef HAVE_LOGWTMP
if (log_wtmp)
logwtmp(tty, user, "dialup");
#endif
if (command != NULL) {
int ret;
syslog(LOG_DEBUG, "starting %s for %s", command, user);
signal(SIGHUP, SIG_IGN);
signal(SIGTERM, SIG_IGN);
signal(SIGINT, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
#ifdef COMMAND_MESSAGE
printf(COMMAND_MESSAGE);
#endif
#if USE_SYSTEM
ret = system(command);
if (ret < 0)
syslog(LOG_WARNING, "command failed: %m");
else
syslog(LOG_NOTICE, "command exit code %u", ret);
#else
pid=fork();
if(pid == 0) {
/* child */
execl(DEFAULT_COMMAND, DEFAULT_COMMAND, ARGS, NULL);
syslog(LOG_ERR, "execl() failed: %m");
_exit(EXIT_FAIL);
}
if(pid < 0) {
/* error */
syslog(LOG_ERR, "fork failed: %m");
exit(EXIT_FAIL);
}
if(pid > 0) {
/* parent */
int st, r;
r=wait(&st);
}
#endif
}
if (do_account) {
/* stop accounting */
struct tac_attrib *attr = NULL;
sprintf(buf, "%lu", time(0));
tac_add_attrib(&attr, "stop_time", buf);
sprintf(buf, "%hu", task_id);
tac_add_attrib(&attr, "task_id", buf);
tac_fd = tac_connect_single(tac_server, tac_secret, NULL, 60);
if (tac_fd < 0) {
if (!quiet)
printf("Error connecting to TACACS+ server: %m\n");
exit(EXIT_ERR);
}
tac_acct_send(tac_fd, TAC_PLUS_ACCT_FLAG_STOP, user, tty, remote_addr,
attr);
ret = tac_acct_read(tac_fd, &arep);
if (ret == 0) {
if (!quiet)
printf("Accounting: STOP failed: %s", arep.msg);
syslog(LOG_INFO, "TACACS+ accounting stop failed: %s\n", arep.msg);
} else if (!login_mode && !quiet)
printf("Accounting: STOP OK\n");
close(tac_fd);
tac_free_attrib(&attr);
}
/* logout from utmp */
#ifdef HAVE_LOGWTMP
if (log_wtmp)
logwtmp(tty, "", "");
#endif
exit(EXIT_OK);
}
void sighandler(int sig) {
TACDEBUG((LOG_DEBUG, "caught signal %d", sig));
}
void authenticate(const struct addrinfo *tac_server, const char *tac_secret,
const char *user, const char *pass, const char *tty,
const char *remote_addr) {
int tac_fd;
char *msg;
int ret;
struct areply arep;
tac_fd = tac_connect_single(tac_server, tac_secret, NULL, 60);
if (tac_fd < 0) {
if (!quiet)
printf("Error connecting to TACACS+ server: %m\n");
exit(EXIT_ERR);
}
/* start authentication */
if (tac_authen_send(tac_fd, user, pass, tty, remote_addr,
TAC_PLUS_AUTHEN_LOGIN) < 0) {
if (!quiet)
printf("Error sending query to TACACS+ server\n");
exit(EXIT_ERR);
}
ret = tac_authen_read(tac_fd, &arep);
if (ret != TAC_PLUS_AUTHEN_STATUS_PASS) {
if (!quiet)
printf("Authentication FAILED: %s\n", arep.msg);
syslog(LOG_ERR, "authentication failed for %s: %s", user, arep.msg);
exit(EXIT_FAIL);
}
if (!quiet)
printf("Authentication OK\n");
syslog(LOG_INFO, "authentication OK for %s", user);
close(tac_fd);
}
void showusage(char *progname) {
char *a;
a = rindex(progname, '/');
progname = (a == NULL) ? progname : ++a;
printf("%s -- simple TACACS+ client and login, version %u.%u.%u\n",
progname, tac_ver_major, tac_ver_minor, tac_ver_patch);
printf("Copyright 1997-2016 by Pawel Krawczyk <[email protected]>\n");
printf("Usage: %s option [option, ...]\n\n", progname);
printf(" Action:\n");
printf(
" -T, --authenticate perform authentication with username and password\n");
printf(
" -R, --authorize perform authorization for requested service\n");
printf(" -A, --account account session beginning and end\n");
printf(" -h, --help display this help and exit\n");
printf(" -V, --version display version number and exit\n\n");
printf(" Data:\n");
printf(" -u, --username remote user name\n");
printf(" -p, --password remote user password\n");
printf(" -s, --server server IP address or FQDN\n");
printf(" -r, --remote remote client's IP address\n");
printf(" -S, --service requested service (e.g. ppp)\n");
printf(" -P, --protocol requested protocl (e.g. ip)\n");
printf(" -L, --login TACACS+ login mode (e.g. chap, login)\n");
printf(" -k, --secret server encryption key\n");
printf(" -c, --command command to execute after successful AAA\n");
printf(" --exec alias for --command\n\n");
printf(" Modifiers:\n");
printf(
" -q, --quiet don't display messages to screen (but still\n");
printf(" --silent report them via syslog(3))\n");
printf(" -w, --no-wtmp don't write records to wtmp(5)\n");
printf(
" -n, --no-encrypt don't encrypt AAA packets sent to servers\n\n");
printf("Example usage:\n\n");
printf(
" tacc -TRA -u test1 -p test1 -s localhost -r 1.1.1.1 -k test1 -S ppp -P ip\n");
exit(EXIT_ERR);
}
void showversion(char *progname) {
char *a;
a = rindex(progname, '/');
progname = (a == NULL) ? progname : ++a;
printf("%s %u.%u.%u\n", progname, tac_ver_major, tac_ver_minor,
tac_ver_patch);
exit(EXIT_OK);
}
unsigned long getservername(char *serv) {
struct in_addr addr;
struct hostent *h;
if (inet_aton(serv, &addr) == 0) {
if ((h = gethostbyname(serv)) == NULL) {
herror("gethostbyname");
} else {
bcopy(h->h_addr, (char *)&addr, sizeof(struct in_addr));
return(addr.s_addr);
}
} else
return(addr.s_addr);
return (-1);
}
void timeout_handler(int signum) {
syslog(LOG_ERR, "timeout reading password from user %s", user);
}