-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_tacplus.c
397 lines (342 loc) · 11.4 KB
/
bash_tacplus.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
/* bash_tacplus.c - Bash plugin for TACACS+ protocol.
* Copyright (C) 2021, Liu Hua <[email protected]>
*
* TACACS+ work based on pam_tacplus.c
* Copyright (C) 2010, Pawel Krawczyk <[email protected]> and
* Jeroen Nijhof <[email protected]>
*
* TACACS+ authorization work based on tacplus-auth.c
* https://github.com/daveolson53/tacplus-auth/blob/master/tacplus-auth.c
* Copyright 2016 Cumulus Networks, Inc. All rights reserved.
* Author: Dave Olson <[email protected]>
* Cumulus Networks, Inc.
* 185 E. Dana Street
* Mountain View, CA 94041
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program - see the file COPYING.
*
* See `CHANGES' file for revision history.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/types.h>
/* Tacacs+ lib */
#include <libtac/libtac.h>
/* Tacacs+ support lib */
#include <libtac/support.h>
/* Output syslog to mock method when build with UT */
#if defined (BASH_PLUGIN_UT)
# define syslog mock_syslog
#endif
/* Config file path */
const char *tacacs_config_file = "/etc/tacplus_servers";
/* Config file attribute */
struct stat config_file_attr;
/* Tacacs server config data */
typedef struct {
struct addrinfo *address;
const char *key;
} tacacs_server_t;
/* Tacacs control flag */
int tacacs_ctrl;
/*
* Output verbose log.
*/
void output_verbose(const char *format, ...)
{
fprintf (stderr, "TACACS+: ");
syslog(LOG_INFO,"TACACS+: ");
// convert log to a string because va args resoursive issue:
// http://www.c-faq.com/varargs/handoff.html
char logBuffer[512];
va_list args;
va_start (args, format);
vsnprintf(logBuffer, sizeof(logBuffer), format, args);
va_end (args);
fprintf (stderr, logBuffer);
syslog(LOG_INFO, logBuffer);
}
/*
* Output error message.
*/
void output_error (const char *format, ...)
{
fprintf (stderr, "TACACS+: ");
syslog(LOG_ERR,"TACACS+: ");
// convert log to a string because va args resoursive issue:
// http://www.c-faq.com/varargs/handoff.html
char logBuffer[512];
va_list args;
va_start (args, format);
vsnprintf(logBuffer, sizeof(logBuffer), format, args);
va_end (args);
fprintf (stderr, logBuffer);
syslog(LOG_ERR, logBuffer);
}
/*
* Output debug message.
*/
void output_debug (const char *format, ...)
{
if ((tacacs_ctrl & PAM_TAC_DEBUG) == 0) {
return;
}
// convert log to a string because va args resoursive issue:
// http://www.c-faq.com/varargs/handoff.html
char logBuffer[512];
va_list args;
va_start (args, format);
vsnprintf(logBuffer, sizeof(logBuffer), format, args);
va_end (args);
output_error (logBuffer);
}
/*
* Send authorization message.
* This method based on send_auth_msg in https://github.com/daveolson53/tacplus-auth/blob/master/tacplus-auth.c
*/
int send_authorization_message(
int tac_fd,
const char *user,
const char *tty,
const char *host,
uint16_t taskid,
const char *cmd,
char **args,
int argc)
{
char buf[128];
struct tac_attrib *attr;
int retval;
struct areply re;
int i;
attr=(struct tac_attrib *)xcalloc(1, sizeof(struct tac_attrib));
snprintf(buf, sizeof buf, "%hu", taskid);
tac_add_attrib(&attr, "task_id", buf);
tac_add_attrib(&attr, "protocol", "ssh");
tac_add_attrib(&attr, "service", "shell");
tac_add_attrib(&attr, "cmd", (char*)cmd);
for(i=1; i<argc; i++) {
// TACACS protocol allow max 255 bytes per argument. 'cmd-arg' will take 7 bytes.
char tbuf[248];
const char *arg;
if(strlen(args[i]) >= sizeof(tbuf)) {
snprintf(tbuf, sizeof tbuf, "%s", args[i]);
arg = tbuf;
}
else {
arg = args[i];
}
tac_add_attrib(&attr, "cmd-arg", (char *)arg);
}
re.msg = NULL;
retval = tac_author_send(tac_fd, (char *)user, (char *)tty, (char *)host, attr);
if(retval < 0) {
output_error("send of authorization message failed: %s\n", strerror(errno));
}
else {
retval = tac_author_read(tac_fd, &re);
if (retval < 0) {
output_debug("authorization response failed: %d\n", retval);
}
else if(re.status == AUTHOR_STATUS_PASS_ADD ||
re.status == AUTHOR_STATUS_PASS_REPL) {
retval = 0;
}
else {
output_debug("command not authorized (%d)\n", re.status);
retval = 1;
}
}
tac_free_attrib(&attr);
if(re.msg != NULL) {
free(re.msg);
}
return retval;
}
/*
* Send tacacs authorization request.
* This method based on send_tacacs_auth in https://github.com/daveolson53/tacplus-auth/blob/master/tacplus-auth.c
*/
int tacacs_authorization(
const char *user,
const char *tty,
const char *host,
const char *cmd,
char **args,
int argc)
{
int result = 1, server_idx, server_fd, connected_servers=0;
uint16_t task_id = (uint16_t)getpid();
for(server_idx = 0; server_idx < tac_srv_no; server_idx++) {
server_fd = tac_connect_single(tac_srv[server_idx].addr, tac_srv[server_idx].key, &tac_source_addr, tac_timeout, __vrfname);
if(server_fd < 0) {
// connect to tacacs server failed
output_debug("Failed to connecting to %s to request authorization for %s: %s\n", tac_ntop(tac_srv[server_idx].addr->ai_addr), cmd, strerror(errno));
continue;
}
// increase connected servers
connected_servers++;
result = send_authorization_message(server_fd, user, tty, host, task_id, cmd, args, argc);
close(server_fd);
if(result) {
// authorization failed
output_debug("%s not authorized from %s\n", cmd, tac_ntop(tac_srv[server_idx].addr->ai_addr));
}
else {
// authorization successed
output_debug("%s authorized command %s\n", cmd, tac_ntop(tac_srv[server_idx].addr->ai_addr));
break;
}
}
// can't connect to any server
if(!connected_servers) {
result = -2;
output_debug("Failed to connect to TACACS server(s)\n");
}
return result;
}
/*
* Send authorization request.
* This method based on build_auth_req in https://github.com/daveolson53/tacplus-auth/blob/master/tacplus-auth.c
*/
int authorization_with_host_and_tty(const char *user, const char *cmd, char **argv, int argc)
{
// try get host name
char hostname[64];
memset(&hostname, 0, sizeof(hostname));
(void)gethostname(hostname, sizeof(hostname) -1);
if (!hostname[0]) {
snprintf(hostname, sizeof(hostname), "UNK");
output_debug("Failed to determine hostname, passing %s\n", hostname);
}
// try get tty name
char ttyname[64];
memset(&ttyname, 0, sizeof(ttyname));
int i;
for(i=0; i<3; i++) {
int result;
if (isatty(i)) {
result = ttyname_r(i, ttyname, sizeof(ttyname) -1);
if (result) {
output_debug("Failed to get tty name for fd %d: %s\n", i, strerror(result));
}
break;
}
}
if (!ttyname[0]) {
snprintf(ttyname, sizeof(ttyname), "UNK");
output_debug("Failed to determine tty, passing %s\n", ttyname);
}
// send tacacs authorization request
return tacacs_authorization(user, ttyname, hostname, cmd, argv, argc);
}
/*
* Load tacacs config.
*/
void load_tacacs_config()
{
// load config file: tacacs_config_file
tacacs_ctrl = parse_config_file (tacacs_config_file);
output_verbose("tacacs config updated:\n");
int server_idx;
for(server_idx = 0; server_idx < tac_srv_no; server_idx++) {
output_verbose("Server %d, address:%s, key:%s\n", server_idx, tac_ntop(tac_srv[server_idx].addr->ai_addr),tac_srv[server_idx].key);
}
}
/*
* Load tacacs config.
*/
void check_and_load_changed_tacacs_config()
{
struct stat attr;
// get config file stat, check if file changed
stat(tacacs_config_file, &attr);
char date[36];
strftime(date, 36, "%d.%m.%Y %H:%M:%S", localtime(&(attr.st_mtime)));
if (difftime(attr.st_mtime, config_file_attr.st_mtime) == 0) {
output_verbose("tacacs config file not change: last modified time: %s.\n", date);
return;
}
output_verbose("tacacs config file changed: last modified time: %s.\n", date);
// config file changed, update file stat and reload config.
config_file_attr = attr;
// load config file
load_tacacs_config();
}
/*
* Tacacs plugin initialization.
*/
void plugin_init ()
{
// get config file stat, will use this to check config file changed
stat(tacacs_config_file, &config_file_attr);
// load config file: tacacs_config_file
load_tacacs_config();
output_verbose("tacacs plugin initialized.\n");
}
/*
* Tacacs plugin release.
*/
void plugin_uninit ()
{
output_verbose("tacacs plugin un-initialize.");
}
/*
* Tacacs authorization.
*/
int on_shell_execve (char *user, int shell_level, char *cmd, char **argv)
{
output_verbose("Authorization parameters:\n");
output_verbose(" Shell level: %d\n", shell_level);
output_verbose(" Current user: %s\n", user);
output_verbose(" Command full path: %s\n", cmd);
output_verbose(" Parameters:\n");
char **parameter_array_pointer = argv;
int argc = 0;
while (*parameter_array_pointer != 0) {
// output parameter
output_verbose(" %s\n", *parameter_array_pointer);
// move to next parameter
parameter_array_pointer++;
argc++;
}
// when shell_level > 1, it's a recursive command in shell script.
if (shell_level > 2) {
output_verbose("Recursive command %s ignored.\n", cmd);
return 0;
}
// reload config file when tacacs config changed
check_and_load_changed_tacacs_config();
int ret = authorization_with_host_and_tty(user, cmd, argv, argc);
switch (ret) {
case 0:
output_verbose("%s authorize successed by TACACS+ with given arguments\n", cmd);
break;
case -2:
/* -2 means no servers, so already a message */
output_verbose("%s not authorized by TACACS+ with given arguments, not executing\n", cmd);
break;
default:
output_verbose("%s authorize failed by TACACS+ with given arguments, not executing\n", cmd);
break;
}
return ret;
}