forked from kravietz/pam_tacplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tacc.c
616 lines (518 loc) · 17.9 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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* 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 <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/wait.h>
#include <time.h>
#include <getopt.h>
#include <ctype.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if defined(HAVE_PUTUTXLINE)
#include <utmpx.h>
#elif defined(HAVE_LOGWTMP)
#include <utmp.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 *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 *g_user = NULL; /* global, because of signal handler */
#if defined(HAVE_PUTUTXLINE)
struct utmpx utmpx;
#endif
/* take the length of a string constant without the NUL */
#define C_STRLEN(str) (sizeof("" str) - 1)
/* 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'},
{"tty", required_argument, NULL, 'y'},
/* 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:y:";
int main(int argc, char **argv) {
char *pass = NULL;
char *tty = NULL;
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
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])) {
g_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]);
/*NOTREACHED*/
break;
case 'h':
showusage(argv[0]);
/*NOTREACHED*/
break;
case 'u':
g_user = optarg;
break;
case 'r':
remote_addr = optarg;
break;
case 'L':
// tac_login is a global variable initialized in libtac
xstrcpy(tac_login, optarg, sizeof(tac_login));
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;
case 'y':
tty = optarg;
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 (g_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);
}
if (tty == NULL) {
printf("error: tty name is required.\n");
exit(EXIT_ERR);
}
/* open syslog before any TACACS+ calls */
openlog("tacc", LOG_CONS | LOG_PID, LOG_AUTHPRIV);
if (do_authen)
authenticate(tac_server, tac_secret, g_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, g_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);
// this is not crypto but merely an identifier
long rnd_id = random();
memcpy(&task_id, &rnd_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, g_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 */
if (log_wtmp) {
#if defined(HAVE_PUTUTXLINE)
struct timeval tv;
gettimeofday(&tv, NULL);
memset(&utmpx, 0, sizeof(utmpx));
utmpx.ut_type = USER_PROCESS;
utmpx.ut_pid = getpid();
xstrcpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
strncpy(utmpx.ut_id, tty + C_STRLEN("tty"), sizeof(utmpx.ut_id));
xstrcpy(utmpx.ut_host, "dialup", sizeof(utmpx.ut_host));
utmpx.ut_tv.tv_sec = tv.tv_sec;
utmpx.ut_tv.tv_usec = tv.tv_usec;
xstrcpy(utmpx.ut_user, g_user, sizeof(utmpx.ut_user));
/* ut_addr unused ... */
setutxent();
pututxline(&utmpx);
#elif defined(HAVE_LOGWTMP)
logwtmp(tty, g_user, "dialup");
#endif
}
if (command != NULL) {
int ret;
syslog(LOG_DEBUG, "starting %s for %s", command, g_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, g_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 */
if (log_wtmp) {
#if defined(HAVE_PUTUTXLINE)
utmpx.ut_type = DEAD_PROCESS;
memset(utmpx.ut_line, 0, sizeof(utmpx.ut_line));
memset(utmpx.ut_user, 0, sizeof(utmpx.ut_user));
memset(utmpx.ut_host, 0, sizeof(utmpx.ut_host));
utmpx.ut_tv.tv_sec = utmpx.ut_tv.tv_usec = 0;
setutxent();
pututxline(&utmpx);
#elif defined(HAVE_LOGWTMP)
logwtmp(tty, "", "");
#endif
}
exit(EXIT_OK);
}
void sighandler(int sig __Unused) {
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;
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_GETPASS) {
if (tac_cont_send(tac_fd, pass) < 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(" -y, --tty remote user tty or port\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 -y ttyS17\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);
}
void timeout_handler(int signum __Unused) {
syslog(LOG_ERR, "timeout reading password from user %s", g_user);
}
#ifdef TACDEBUG_AT_RUNTIME
void logmsg(int level __Unused, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
#endif