summaryrefslogtreecommitdiff
path: root/tacplus-daemon/transaction.c
blob: 4367b58145f1e128ed05c0bab9ddc2c0976df6db (plain)
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
/*
	TACACS+ D-Bus Daemon code

	Copyright (c) 2018-2019 AT&T Intellectual Property.

	SPDX-License-Identifier: GPL-2.0-only
*/

#include <assert.h>
#include <stdbool.h>
#include <syslog.h>

#include <libtac.h>
#include <tacplus.h>

#include "global.h"
#include "statistics.h"
#include "tacplus_srv_conn.h"
#include "transaction.h"
#include "transaction_private.h"

#define OPTIONAL_ATTR_SEP '*'

const char *transaction_type_str(transaction_type_t type)
{
	switch (type) {
		case TRANSACTION_ACCOUNT:
			return "accounting";
		case TRANSACTION_AUTHEN:
			return "authentication";
		case TRANSACTION_AUTHOR:
			return "authorization";
		case TRANSACTION_INVALID:
			return "invalid";
		default:
			break;
	}

	syslog(LOG_ERR, "Unknown transaction type %d", type);
	return "unknown";
}

struct transaction *transaction_new(transaction_type_t type)
{
	struct transaction *t;

	t = calloc(1, sizeof(*t));
	if (!t)
		return NULL;

	t->type = type;
	return t;
}

void transaction_free(struct transaction **t)
{
	if (t && *t) {
		switch ((*t)->type) {
			case TRANSACTION_AUTHOR:
				transaction_attrib_free(&(*t)->response.author.attrs);
				break;
			case TRANSACTION_AUTHEN:
			case TRANSACTION_ACCOUNT:
			case TRANSACTION_INVALID:
				break;
		}
		free(*t);
		*t = NULL;
	}
}

struct transaction_attrib *
transaction_attrib_new(const char *av_pair)
{
	if (! av_pair || strlen(av_pair) == 0)
		return NULL;

	/*
	 * We need an attribute name, and separators are not allowed to be part
	 * of the name. Therefore if the first character is a separator just fail.
	 */
	if (av_pair[0] == '*' || av_pair[0] == '=')
		return NULL;

	struct transaction_attrib *attr = calloc(1, sizeof(struct transaction_attrib));
	if (! attr)
		goto malloc_fail;

	char *mand_sep = strchr(av_pair, '=');
	char *opt_sep  = strchr(av_pair, '*');
	char *sep = NULL;

	/*
	 * If both separators are found it means that one or both of them is
	 * present in the attribute value as well as the name. Therefore take
	 * the first one which occurs as the separator.
	 */
	if (mand_sep && opt_sep)
		sep = mand_sep < opt_sep ? mand_sep : opt_sep;
	else if (mand_sep)
		sep = mand_sep;
	else if (opt_sep)
		sep = opt_sep;

	if (sep) {
		attr->name = strndup(av_pair, (sep + 1) - av_pair);
		attr->value = strdup(sep + 1);
	}
	else {
		/* If there is no separator found treat it as a mandatory attribute */
		if ((attr->name = calloc(1, strlen(av_pair) + 2))) {
			strcat((char *) attr->name, av_pair);
			strcat((char *) attr->name, "=");
		}
		attr->value = strdup("");
	}

	if (attr->name && attr->value)
		return attr;

malloc_fail:
	syslog(LOG_ERR, "tacplus_attrib memory allocation failure!");
	transaction_attrib_free(&attr);
	return NULL;
}

void transaction_attrib_free(struct transaction_attrib **head)
{
	if (head && *head) {
		struct transaction_attrib *attr = *head, *next;
		*head = NULL;

		do {
			next = attr->next;
			free((void *) attr->name);
			free((void *) attr->value);
			free(attr);
		} while ((attr = next) != NULL);
	}
}

struct transaction_attrib *transaction_attrib_from_tac_attrib(const struct tac_attrib *tac_attr)
{
	struct transaction_attrib *head = NULL, *tail;

	for (; tac_attr != NULL; tac_attr = tac_attr->next) {
		struct transaction_attrib *attr = transaction_attrib_new(tac_attr->attr);
		if (! attr)
			continue;

		if (head) {
			tail->next = attr;
			tail = attr;
		}
		else {
			head = tail = attr;
		}
	}

	return head;
}

static int tacplus_add_attrib(struct tac_attrib **attr, char *name,
							  char *value, bool truncate)
{
	syslog(LOG_DEBUG, "Appending mandatory attribute %s: %s", name, value);

	return truncate ? tac_add_attrib_truncate(attr, name, value) :
					  tac_add_attrib(attr, name, value);
}

static int tacplus_add_optional_attrib(struct tac_attrib **attr, char *name,
				       char *value, bool truncate)
{
	syslog(LOG_DEBUG, "Appending optional attribute %s: %s", name, value);

	return truncate ? tac_add_attrib_pair_truncate(attr, name,
						       OPTIONAL_ATTR_SEP, value) :
		tac_add_attrib_pair(attr, name, OPTIONAL_ATTR_SEP, value);
}

int tacplus_author_send(struct transaction *t)
{
	struct tac_attrib *attr = NULL;
	char *addr_str;
	struct tac_session_extra *extra;
	struct areply author_rep = { .status = TAC_PLUS_AUTHOR_STATUS_ERROR };

	assert(t->type == TRANSACTION_AUTHOR);
	t->response.author.status = author_rep.status;

	/* Attempt to populate cmd and args before connecting to server */
	char **cmd_arg = t->request.author.cmd;
	if (cmd_arg && *cmd_arg) {
		if (tacplus_add_attrib(&attr, "cmd",  *cmd_arg, false) < 0)
			goto unable_to_send;

		for (cmd_arg++; cmd_arg && *cmd_arg; cmd_arg++) {
			if (tacplus_add_attrib(&attr, "cmd-arg",  *cmd_arg, false) < 0)
				goto unable_to_send;
		}
	}

	if (tacplus_connect() == false) {
		syslog(LOG_NOTICE, "Failed to connect to a TACACS+ server for "
						   "authorization transaction");
		goto finish;
	}

	struct tac_session_extra _extra = {};
	extra = tacplus_current_session_extra(connControl->opts, &_extra);

	if (tacplus_add_attrib(&attr, "protocol",
			       t->request.author.protocol, false) < 0)
		goto unable_to_send;

	if (tacplus_add_attrib(&attr, "service",
			       t->request.author.service, false) < 0)
		goto unable_to_send;

	if (t->request.author.secrets &&
		tacplus_add_optional_attrib(&attr, "secrets",
					t->request.author.secrets, false) < 0)
		goto unable_to_send;

	addr_str = addrinfo_to_string(extra->server->addrs);

	syslog(LOG_DEBUG, "Sending authorization request to %s",
		strOrNil(addr_str));

	free(addr_str);

	t->response.author.status = tac_author_send(extra->server->fd, t->request.author.login,
									t->request.author.tty, t->request.author.r_addr, attr);
	if (t->response.author.status < 0)
	{
		syslog(LOG_NOTICE, "Error sending authorization request for user: %s <%d>\n",
						    t->request.author.login, t->response.author.status);
		tacplus_server_activate_hold_down(extra->server);
		goto finish;
	} else {
		inc_author_requests(extra->server_id);
	}

	t->response.author.status = tac_author_read_timeout(extra->server->fd, &author_rep, extra->server->timeout);

	if (author_rep.status < 0) {
		syslog(LOG_NOTICE, "Failed to read authorization response for user: %s <%d>\n",
							t->request.author.login, author_rep.status);
		tacplus_server_activate_hold_down(extra->server);
		goto finish;
	}

	switch (author_rep.status) {
		case TAC_PLUS_AUTHOR_STATUS_PASS_ADD:
			syslog(LOG_DEBUG, "Authorization received: PASS_ADD");
			break;

		case TAC_PLUS_AUTHOR_STATUS_PASS_REPL:
			syslog(LOG_DEBUG, "Authorization received: PASS_REPLACE");
			break;

		case TAC_PLUS_AUTHOR_STATUS_FAIL:
			syslog(LOG_DEBUG, "Authorization received: FAILED");
			break;

		case TAC_PLUS_AUTHOR_STATUS_ERROR:
			syslog(LOG_DEBUG, "Authorization received: ERROR");
			break;

		case TAC_PLUS_AUTHOR_STATUS_FOLLOW:
			syslog(LOG_DEBUG, "Authorization received: FOLLOW");
			break;

		default:
			syslog(LOG_DEBUG, "Authorization received: UNKNOWN");
			break;
	}

	inc_author_replies(extra->server_id);
	goto finish;

unable_to_send:
	syslog(LOG_NOTICE, "Unable to send authorization request");

finish:
	t->response.author.attrs = transaction_attrib_from_tac_attrib(author_rep.attr);

	tacplus_close();
	tac_free_attrib(&attr);
	tac_free_attrib(&author_rep.attr);
	free(author_rep.msg);
	return t->response.author.status;
}

static int tacplus_acct_send_per_session(struct transaction *t)
{
	struct tac_attrib *attr;
	struct tac_session_extra *extra;
	char *addr_str;
	struct areply re = {0};

	struct tac_session_extra _extra = {};
	extra = tacplus_current_session_extra (connControl->opts, &_extra);

	attr = NULL;

	if (t->request.account.task_id)
		if (tacplus_add_attrib(&attr, "task_id", t->request.account.task_id, false) < 0)
			goto unable_to_send;

	if (t->request.account.start_time)
		if (tacplus_add_attrib(&attr, "start_time", t->request.account.start_time, false) < 0)
			goto unable_to_send;

	if (t->request.account.stop_time)
		if (tacplus_add_attrib(&attr, "stop_time", t->request.account.stop_time, false) < 0)
			goto unable_to_send;

	if (t->request.account.service)
		if (tacplus_add_attrib(&attr, "service", t->request.account.service, false) < 0)
			goto unable_to_send;

	if (t->request.account.protocol)
		if (tacplus_add_attrib(&attr, "protocol", t->request.account.protocol, false) < 0)
			goto unable_to_send;

	char **cmd_arg = t->request.account.command;
	if (cmd_arg && *cmd_arg) {
		if (tacplus_add_attrib(&attr, "cmd",  *cmd_arg, true) < 0)
			goto unable_to_send;

		for (cmd_arg++; cmd_arg && *cmd_arg; cmd_arg++) {
			if (tacplus_add_attrib(&attr, "cmd-arg",  *cmd_arg, true) < 0)
				goto unable_to_send;
		}
	}

	addr_str = addrinfo_to_string(extra->server->addrs);

	syslog(LOG_DEBUG, "Sending accounting request to %s",
			strOrNil(addr_str));

	free(addr_str);

	if (tac_acct_send(extra->server->fd, t->request.account.account_flag, t->request.account.name,
					  t->request.account.tty, t->request.account.r_addr, attr) < 0) {
		syslog(LOG_ERR, "Error sending accounting request");
		tacplus_server_activate_hold_down(extra->server);
		t->response.account.status = TAC_PLUS_ACCT_STATUS_ERROR;
		goto finish;
	} else {
		inc_acct_requests(extra->server_id);
	}

	t->response.account.status = tac_acct_read_timeout(extra->server->fd, &re, extra->server->timeout);
	if (t->response.account.status < 0) {
		syslog(LOG_ERR, "Error reading accounting reply: %d", t->response.account.status);
		tacplus_server_activate_hold_down(extra->server);
		goto finish;
	}

	switch (t->response.account.status) {
		case TAC_PLUS_ACCT_STATUS_SUCCESS:
			syslog(LOG_DEBUG, "Accounting received: SUCCESS");
			break;

		case TAC_PLUS_ACCT_STATUS_ERROR:
			syslog(LOG_DEBUG, "Accounting received: ERROR");
			break;

		case  TAC_PLUS_ACCT_STATUS_FOLLOW:
			syslog(LOG_DEBUG, "Accounting received: FOLLOW");
			break;

		default:
			syslog(LOG_DEBUG, "Accounting received: UNKNOWN");
			break;
	}

	inc_acct_replies(extra->server_id);
	goto finish;

unable_to_send:
	syslog(LOG_NOTICE, "Unable to send accounting request");

finish:
	tac_free_attrib(&attr);
	tac_free_attrib(&re.attr);
	free(re.msg);
	return t->response.account.status;
}

int tacplus_acct_send(struct transaction *t)
{
	assert(t->type == TRANSACTION_ACCOUNT);
	t->response.account.status = TAC_PLUS_ACCT_STATUS_ERROR;

	if (connControl->opts->broadcast) {
		syslog(LOG_ERR, "Broadcast mode is not supported!");
		goto finish;
	}

	if (tacplus_connect() == false) {
		syslog(LOG_NOTICE, "Failed to connect to a TACACS+ server for "
						   "accounting transaction");
		goto finish;
	}

	tacplus_acct_send_per_session(t);
	tacplus_close();

finish:
	return t->response.account.status;
}

int tacplus_authen_send(struct transaction *t)
{
	char *addr_str;
	struct tac_session_extra *extra;

	assert(t->type == TRANSACTION_AUTHEN);
	t->response.authen.status = TAC_PLUS_AUTHEN_STATUS_ERROR;

	if (tacplus_connect() == false) {
		syslog(LOG_NOTICE, "Failed to connect to a TACACS+ server for "
						   "authentication transaction");
		goto finish;
	}

	struct tac_session_extra _extra = {};
	extra = tacplus_current_session_extra (connControl->opts, &_extra);

	addr_str = addrinfo_to_string(extra->server->addrs);

	syslog(LOG_DEBUG, "Sending authentication request to %s",
		strOrNil(addr_str));

	free(addr_str);

	if (tac_authen_send(extra->server->fd, t->request.authen.user,
						t->request.authen.password, t->request.authen.tty,
						t->request.authen.r_addr) < 0)
	{
		syslog(LOG_ERR, "Error sending authentication request");
		t->response.authen.status = TAC_PLUS_AUTHEN_STATUS_ERROR;
		tacplus_server_activate_hold_down(extra->server);
		goto finish;
	}
	else {
		inc_authen_requests(extra->server_id);
	}

	bool auth_in_prog = true;

	while (auth_in_prog) {
		t->response.authen.status = tac_authen_read_timeout(extra->server->fd, extra->server->timeout);
		if (t->response.authen.status < 0) {
			syslog(LOG_ERR, "Error reading authentication reply: %d",
							t->response.authen.status);
			tacplus_server_activate_hold_down(extra->server);
			goto finish;
		}

		switch (t->response.authen.status) {
			case TAC_PLUS_AUTHEN_STATUS_PASS:
				auth_in_prog = false;
				syslog(LOG_DEBUG, "Authentication received: PASS");
				break;

			case TAC_PLUS_AUTHEN_STATUS_FAIL:
				auth_in_prog = false;
				syslog(LOG_DEBUG, "Authentication received: FAIL");
				break;

			case TAC_PLUS_AUTHEN_STATUS_GETDATA:
				/* FALLTHRU */
			case TAC_PLUS_AUTHEN_STATUS_GETUSER:
				syslog(LOG_DEBUG, "Authentication received: CONTINUE");

				/* FALLTHRU */
			case TAC_PLUS_AUTHEN_STATUS_RESTART:
				auth_in_prog = false;
				syslog(LOG_DEBUG, "Authentication received: RESTART");
				break;

			case TAC_PLUS_AUTHEN_STATUS_FOLLOW:
				auth_in_prog = false;
				syslog(LOG_DEBUG, "Authentication received: FOLLOW");
				break;

			case TAC_PLUS_AUTHEN_STATUS_GETPASS:
				if (tac_cont_send(extra->server->fd, t->request.authen.password) < 0) {
					syslog(LOG_NOTICE, "Could not send TACACS+ password for user %s.\n", t->request.authen.user);
					/* This means the dbus client will receive a return 
					 * value of TAC_PLUS_AUTHEN_STATUS_GETPASS.
					 */
					goto finish;
				}
				/* continue the loop and read the TACACS+ server response to the supplied password */
				break;

			case TAC_PLUS_AUTHEN_STATUS_ERROR:
				auth_in_prog = false;
				syslog(LOG_DEBUG, "Authentication received: ERROR");
				break;

			default:
				auth_in_prog = false;
				break;
		}
	}

	inc_authen_replies(extra->server_id);

finish:
	tacplus_close();
	return t->response.authen.status;
}