Debian Codegraph @

contrib/mod_radius.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_radius -- a module for RADIUS authentication and accounting
3 * Copyright (c) 2001-2025 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders gives permission to link this program
20 * with OpenSSL, and distribute the resulting executable, without including
21 * the source code for OpenSSL in the source distribution.
22 *
23 * This is mod_radius, contrib software for proftpd 1.2 and above.
24 * For more information contact TJ Saunders <[email protected]>.
25 *
26 * This module is based in part on code in Alan DeKok's ([email protected])
27 * mod_auth_radius for Apache, in part on the FreeRADIUS project's code.
28 */
29
30 #define MOD_RADIUS_VERSION "mod_radius/0.9.3"
31
32 #include "conf.h"
33 #include "privs.h"
34
35 /* RADIUS information */
36
37 /* From RFC2865, RFC2866 */
38 #define RADIUS_AUTH_PORT 1812
39 #define RADIUS_ACCT_PORT 1813
40
41 #define RADIUS_PASSWD_LEN 16
42 #define RADIUS_VECTOR_LEN 16
43
44 /* From RFC2138 */
45 #define RADIUS_STRING_LEN 254
46
47 /* RADIUS attribute structures */
48 typedef struct {
49 unsigned char type;
50 unsigned char length;
51 unsigned char data[1];
52 } radius_attrib_t;
53
54 /* RADIUS packet header */
55 typedef struct {
56 unsigned char code;
57 unsigned char id;
58 unsigned short length;
59 unsigned char digest[RADIUS_VECTOR_LEN];
60 unsigned char data[2];
61
62 char _pad[PR_TUNABLE_BUFFER_SIZE];
63 } radius_packet_t;
64
65 #define RADIUS_HEADER_LEN 20
66
67 /* RADIUS ID Definitions (see RFC 2865, 2866) */
68 #define RADIUS_AUTH_REQUEST 1
69 #define RADIUS_AUTH_ACCEPT 2
70 #define RADIUS_AUTH_REJECT 3
71 #define RADIUS_ACCT_REQUEST 4
72 #define RADIUS_ACCT_RESPONSE 5
73 #define RADIUS_ACCT_STATUS 6
74 #define RADIUS_AUTH_CHALLENGE 11
75
76 /* RADIUS Attribute Definitions (see RFC 2865, 2866) */
77 #define RADIUS_USER_NAME 1
78 #define RADIUS_PASSWORD 2
79 #define RADIUS_NAS_IP_ADDRESS 4
80 #define RADIUS_NAS_PORT 5
81 #define RADIUS_SERVICE_TYPE 6
82 #define RADIUS_OLD_PASSWORD 17
83 #define RADIUS_REPLY_MESSAGE 18
84 #define RADIUS_STATE 24
85 #define RADIUS_CLASS 25
86 #define RADIUS_VENDOR_SPECIFIC 26
87 #define RADIUS_SESSION_TIMEOUT 27
88 #define RADIUS_IDLE_TIMEOUT 28
89 #define RADIUS_CALLING_STATION_ID 31
90 #define RADIUS_NAS_IDENTIFIER 32
91 #define RADIUS_ACCT_STATUS_TYPE 40
92 #define RADIUS_ACCT_INPUT_OCTETS 42
93 #define RADIUS_ACCT_OUTPUT_OCTETS 43
94 #define RADIUS_ACCT_SESSION_ID 44
95 #define RADIUS_ACCT_AUTHENTIC 45
96 #define RADIUS_ACCT_SESSION_TIME 46
97 #define RADIUS_ACCT_TERMINATE_CAUSE 49
98 #define RADIUS_ACCT_EVENT_TS 55
99 #define RADIUS_NAS_PORT_TYPE 61
100 #define RADIUS_MESSAGE_AUTHENTICATOR 80
101 #define RADIUS_NAS_IPV6_ADDRESS 95
102
103 /* RADIUS service types */
104 #define RADIUS_SVC_LOGIN 1
105 #define RADIUS_SVC_AUTHENTICATE_ONLY 8
106
107 /* RADIUS status types */
108 #define RADIUS_ACCT_STATUS_START 1
109 #define RADIUS_ACCT_STATUS_STOP 2
110 #define RADIUS_ACCT_STATUS_ALIVE 3
111
112 /* RADIUS NAS port types */
113 #define RADIUS_NAS_PORT_TYPE_VIRTUAL 5
114
115 /* RADIUS authentication types */
116 #define RADIUS_AUTH_NONE 0
117 #define RADIUS_AUTH_RADIUS 1
118 #define RADIUS_AUTH_LOCAL 2
119
120 /* RADIUS Acct-Terminate-Cause types */
121 #define RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST 1
122 #define RADIUS_ACCT_TERMINATE_CAUSE_LOST_SERVICE 3
123 #define RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT 4
124 #define RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT 5
125 #define RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_RESET 6
126 #define RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT 7
127 #define RADIUS_ACCT_TERMINATE_CAUSE_SERVICE_UNAVAIL 15
128 #define RADIUS_ACCT_TERMINATE_CAUSE_USER_ERROR 16
129
130 /* The RFC says 4096 octets max, and most packets are less than 256.
131 * However, this number is just larger than the maximum MTU of just
132 * most types of networks, except maybe for gigabit ethernet.
133 */
134 #define RADIUS_PACKET_LEN 1600
135
136 /* Miscellaneous default values */
137 #define DEFAULT_RADIUS_TIMEOUT 10
138
139 #define RADIUS_ATTRIB_LEN(attr) ((attr)->length)
140
141 /* Adjust the VSA length (I'm not sure why this is necessary, but a reading
142 * of the FreeRADIUS sources show it to be. Weird.)
143 */
144 #define RADIUS_VSA_ATTRIB_LEN(attr) ((attr)->length - 2)
145
146 typedef struct radius_server_obj {
147
148 /* Next server in line */
149 struct radius_server_obj *next;
150
151 /* Memory pool for this object */
152 pool *pool;
153
154 /* RADIUS server IP address */
155 const pr_netaddr_t *addr;
156
157 /* RADIUS server port */
158 unsigned short port;
159
160 /* RADIUS server shared secret */
161 unsigned char *secret;
162 size_t secret_len;
163
164 /* How long to wait for RADIUS responses */
165 unsigned int timeout;
166
167 } radius_server_t;
168
169 module radius_module;
170
171 static pool *radius_pool = NULL;
172 static int radius_engine = FALSE;
173 static radius_server_t *radius_acct_server = NULL;
174 static radius_server_t *radius_auth_server = NULL;
175 static int radius_logfd = -1;
176
177 /* mod_radius option flags */
178 #define RADIUS_OPT_IGNORE_REPLY_MESSAGE_ATTR 0x0001
179 #define RADIUS_OPT_IGNORE_CLASS_ATTR 0x0002
180 #define RADIUS_OPT_IGNORE_SESSION_TIMEOUT_ATTR 0x0004
181 #define RADIUS_OPT_IGNORE_IDLE_TIMEOUT_ATTR 0x0008
182 #define RADIUS_OPT_REQUIRE_MAC 0x0010
183
184 static unsigned long radius_opts = 0UL;
185
186 static struct sockaddr radius_local_sock, radius_remote_sock;
187
188 /* For tracking various values not stored in the session struct */
189 static const char *radius_nas_identifier_config = NULL;
190 static char *radius_realm = NULL;
191 static time_t radius_session_start = 0;
192 static int radius_session_authtype = RADIUS_AUTH_LOCAL;
193 static unsigned char radius_auth_ok = FALSE;
194 static unsigned char radius_auth_reject = FALSE;
195
196 /* For tracking the Class attribute, for sending in accounting requests. */
197 static char *radius_acct_class = NULL;
198 static size_t radius_acct_classlen = 0;
199 static char *radius_acct_user = NULL;
200 static size_t radius_acct_userlen = 0;
201
202 /* "Fake" user/group information for RADIUS users. */
203 static unsigned char radius_have_user_info = FALSE;
204 static struct passwd radius_passwd;
205
206 static unsigned char radius_have_group_info = FALSE;
207 static char *radius_prime_group_name = NULL;
208 static unsigned int radius_addl_group_count = 0;
209 static unsigned int radius_getgroups_count = 0;
210 static char **radius_addl_group_names = NULL;
211 static char *radius_addl_group_names_str = NULL;
212 static gid_t *radius_addl_group_ids = NULL;
213 static char *radius_addl_group_ids_str = NULL;
214
215 /* Quota info */
216 static unsigned char radius_have_quota_info = FALSE;
217 static char *radius_quota_per_sess = NULL;
218 static char *radius_quota_limit_type = NULL;
219 static char *radius_quota_bytes_in = NULL;
220 static char *radius_quota_bytes_out = NULL;
221 static char *radius_quota_bytes_xfer = NULL;
222 static char *radius_quota_files_in = NULL;
223 static char *radius_quota_files_out = NULL;
224 static char *radius_quota_files_xfer = NULL;
225
226 /* Other info */
227 static unsigned char radius_have_other_info = FALSE;
228
229 /* Vendor information, defaults to Unix (Vendor-Id of 4) */
230 static const char *radius_vendor_name = "Unix";
231 static unsigned int radius_vendor_id = 4;
232
233 /* Custom VSA IDs that may be used for server-supplied RadiusUserInfo
234 * parameters.
235 */
236 static int radius_uid_attr_id = 0;
237 static int radius_gid_attr_id = 0;
238 static int radius_home_attr_id = 0;
239 static int radius_shell_attr_id = 0;
240
241 /* Custom VSA IDs that may be used for server-supplied RadiusGroupInfo
242 * parameters.
243 */
244 static int radius_prime_group_name_attr_id = 0;
245 static int radius_addl_group_names_attr_id = 0;
246 static int radius_addl_group_ids_attr_id = 0;
247
248 /* Custom VSA IDs that may be used for server-supplied QuotaLimitTable
249 * parameters.
250 */
251 static int radius_quota_per_sess_attr_id = 0;
252 static int radius_quota_limit_type_attr_id = 0;
253 static int radius_quota_bytes_in_attr_id = 0;
254 static int radius_quota_bytes_out_attr_id = 0;
255 static int radius_quota_bytes_xfer_attr_id = 0;
256 static int radius_quota_files_in_attr_id = 0;
257 static int radius_quota_files_out_attr_id = 0;
258 static int radius_quota_files_xfer_attr_id = 0;
259
260 /* For tracking the ID of the last accounting packet (to prevent the
261 * same ID from being reused).
262 */
263 static unsigned char radius_last_acct_pkt_id = 0;
264
265 static const char *trace_channel = "radius";
266
267 /* Convenience macros. */
268 #define RADIUS_IS_VAR(str) \
269 ((str[0] == '$') && (str[1] == '(') && (str[strlen(str)-1] == ')'))
270
271 /* Function prototypes. */
272 static radius_attrib_t *radius_add_attrib(radius_packet_t *, unsigned char,
273 const unsigned char *, size_t);
274 static void radius_add_passwd(radius_packet_t *, unsigned char,
275 const unsigned char *, unsigned char *, size_t);
276 static void radius_build_packet(radius_packet_t *, const unsigned char *,
277 const unsigned char *, unsigned char *, size_t);
278 static unsigned char radius_have_var(char *);
279 static radius_attrib_t *radius_get_attrib(radius_packet_t *, unsigned char);
280 static radius_attrib_t *radius_get_next_attrib(radius_packet_t *,
281 unsigned char, unsigned int *, radius_attrib_t *);
282 static void radius_get_rnd_digest(radius_packet_t *);
283 static radius_attrib_t *radius_get_vendor_attrib(radius_packet_t *,
284 unsigned char);
285 static void radius_set_acct_digest(radius_packet_t *, const unsigned char *,
286 size_t);
287 static void radius_set_auth_mac(radius_packet_t *, const unsigned char *,
288 size_t);
289 static radius_server_t *radius_make_server(pool *);
290 static int radius_openlog(void);
291 static int radius_open_socket(void);
292 static unsigned char radius_parse_gids_str(pool *, char *, gid_t **,
293 unsigned int *);
294 static unsigned char radius_parse_groups_str(pool *, char *, char ***,
295 unsigned int *);
296 static int radius_parse_var(char *, int *, char **);
297 static int radius_process_accept_packet(radius_packet_t *,
298 const unsigned char *, size_t);
299 static int radius_process_reject_packet(radius_packet_t *,
300 const unsigned char *, size_t);
301 static void radius_process_group_info(config_rec *);
302 static void radius_process_quota_info(config_rec *);
303 static void radius_process_user_info(config_rec *);
304 static radius_packet_t *radius_recv_packet(int, unsigned int);
305 static int radius_send_packet(int, radius_packet_t *, radius_server_t *);
306 static int radius_start_accting(void);
307 static int radius_stop_accting(void);
308 static int radius_verify_auth_mac(radius_packet_t *, const char *,
309 const unsigned char *, size_t);
310 static int radius_verify_packet(radius_packet_t *, radius_packet_t *,
311 const unsigned char *, size_t);
312 static int radius_sess_init(void);
313
314 /* Support functions
315 */
316
317 static char *radius_argsep(char **arg) {
318 char *ret = NULL, *dst = NULL;
319 char quote_mode = 0;
320
321 if (!arg || !*arg || !**arg)
322 return NULL;
323
324 while (**arg && PR_ISSPACE(**arg)) {
325 (*arg)++;
326 }
327
328 if (!**arg)
329 return NULL;
330
331 ret = dst = *arg;
332
333 if (**arg == '\"') {
334 quote_mode++;
335 (*arg)++;
336 }
337
338 while (**arg && **arg != ',' &&
339 (quote_mode ? (**arg != '\"') : (!PR_ISSPACE(**arg)))) {
340
341 if (**arg == '\\' && quote_mode) {
342
343 /* escaped char */
344 if (*((*arg) + 1))
345 *dst = *(++(*arg));
346 }
347
348 *dst++ = **arg;
349 ++(*arg);
350 }
351
352 if (**arg)
353 (*arg)++;
354
355 *dst = '\0';
356 return ret;
357 }
358
359 /* Check a "$(attribute-id:default)" string for validity. */
360 static unsigned char radius_have_var(char *var) {
361 int id = 0;
362 char *ptr = NULL;
363 size_t varlen;
364
365 varlen = strlen(var);
366
367 /* Must be at least six characters. */
368 if (varlen < 7) {
369 return FALSE;
370 }
371
372 /* Must start with '$(', and end with ')'. */
373 if (RADIUS_IS_VAR(var) == FALSE) {
374 return FALSE;
375 }
376
377 /* Must have a ':'. */
378 ptr = strchr(var, ':');
379 if (ptr == NULL) {
380 return FALSE;
381 }
382
383 /* ':' must be between '(' and ')'. */
384 if (ptr < (var + 3) ||
385 ptr > &var[varlen-2]) {
386 return FALSE;
387 }
388
389 /* Parse out the component int/string. */
390 radius_parse_var(var, &id, NULL);
391
392 /* Int must be greater than zero. */
393 if (id < 1) {
394 return FALSE;
395 }
396
397 return TRUE;
398 }
399
400 /* Separate the given "$(attribute-id:default)" string into its constituent
401 * custom attribute ID (int) and default (string) components.
402 */
403 static int radius_parse_var(char *var, int *attr_id, char **attr_default) {
404 pool *tmp_pool;
405 char *var_cpy, *ptr = NULL;
406 size_t var_len, var_cpylen;
407
408 if (var == NULL) {
409 errno = EINVAL;
410 return -1;
411 }
412
413 var_len = var_cpylen = strlen(var);
414 if (var_len == 0) {
415 /* Empty string; nothing to do. */
416 return 0;
417 }
418
419 tmp_pool = make_sub_pool(radius_pool);
420 var_cpy = pstrdup(tmp_pool, var);
421
422 /* First, strip off the "$()" variable characters. */
423 var_cpy[var_cpylen-1] = '\0';
424 var_cpy += 2;
425
426 /* Find the delimiting ':' */
427 ptr = strchr(var_cpy, ':');
428 if (ptr != NULL) {
429 *ptr++ = '\0';
430 }
431
432 if (attr_id) {
433 *attr_id = atoi(var_cpy);
434 }
435
436 if (attr_default) {
437 ptr = strchr(var, ':');
438
439 /* Note: this works because the calling of this function by
440 * radius_have_var(), which occurs during the parsing process, uses
441 * a NULL for this portion, so that the string stored in the config_rec
442 * is not actually manipulated, as is done here.
443 */
444 var[var_len-1] = '\0';
445
446 if (ptr != NULL) {
447 *attr_default = ++ptr;
448 }
449 }
450
451 /* Clean up. */
452 destroy_pool(tmp_pool);
453 return 0;
454 }
455
456 static unsigned char radius_parse_gids_str(pool *p, char *gids_str,
457 gid_t **gids, unsigned int *ngids) {
458 char *val = NULL;
459 array_header *group_ids = make_array(p, 0, sizeof(gid_t));
460
461 /* Add each GID to the array. */
462 while ((val = radius_argsep(&gids_str)) != NULL) {
463 gid_t gid;
464 char *endp = NULL;
465
466 pr_signals_handle();
467
468 /* Make sure the given ID is a valid number. */
469 gid = strtoul(val, &endp, 10);
470
471 if (endp && *endp) {
472 pr_log_pri(PR_LOG_NOTICE, "RadiusGroupInfo badly formed group ID: %s",
473 val);
474 return FALSE;
475 }
476
477 /* Push the ID into the ID array. */
478 *((gid_t *) push_array(group_ids)) = gid;
479 }
480
481 *gids = (gid_t *) group_ids->elts;
482 *ngids = group_ids->nelts;
483
484 return TRUE;
485 }
486
487 static unsigned char radius_parse_groups_str(pool *p, char *groups_str,
488 char ***groups, unsigned int *ngroups) {
489 char *name = NULL;
490 array_header *group_names = make_array(p, 0, sizeof(char *));
491
492 /* Add each name to the array. */
493 while ((name = radius_argsep(&groups_str)) != NULL) {
494 char *tmp;
495
496 pr_signals_handle();
497 tmp = pstrdup(p, name);
498
499 /* Push the name into the name array. */
500 *((char **) push_array(group_names)) = tmp;
501 }
502
503 *groups = (char **) group_names->elts;
504 *ngroups = group_names->nelts;
505
506 return TRUE;
507 }
508
509 static int radius_process_standard_attribs(radius_packet_t *pkt,
510 const unsigned char *secret, size_t secret_len) {
511 int attrib_count = 0;
512 radius_attrib_t *attrib = NULL;
513 unsigned char attrib_len;
514
515 pr_trace_msg(trace_channel, 2, "parsing packet for standard attribute IDs");
516
517 if (radius_verify_auth_mac(pkt, "Access-Accept", secret, secret_len) < 0) {
518 return -1;
519 }
520
521 /* TODO: Should we handle the Service-Type attribute here, make sure that it
522 * is a) a service type we implement, and b) the service type that we
523 * requested?
524 */
525
526 /* Handle any CLASS attribute. */
527 if (!(radius_opts & RADIUS_OPT_IGNORE_CLASS_ATTR)) {
528 attrib = radius_get_attrib(pkt, RADIUS_CLASS);
529 if (attrib != NULL) {
530 attrib_len = RADIUS_ATTRIB_LEN(attrib);
531 if (attrib_len > 0) {
532 char *class = NULL;
533
534 class = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
535 pr_trace_msg(trace_channel, 7,
536 "found Class attribute in Access-Accept message: %s", class);
537 radius_acct_class = class;
538 radius_acct_classlen = attrib_len;
539 }
540
541 attrib_count++;
542
543 } else {
544 pr_trace_msg(trace_channel, 6,
545 "Access-Accept packet lacks Class attribute (%d)", RADIUS_CLASS);
546 }
547 }
548
549 /* Handle any User-Name attribute, per RFC 2865, Section 5.1. */
550 attrib = radius_get_attrib(pkt, RADIUS_USER_NAME);
551 if (attrib != NULL) {
552 attrib_len = RADIUS_ATTRIB_LEN(attrib);
553 if (attrib_len > 0) {
554 char *user_name = NULL;
555
556 user_name = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
557 pr_trace_msg(trace_channel, 7,
558 "found User-Name attribute in Access-Accept message: %s", user_name);
559 radius_acct_user = user_name;
560 radius_acct_userlen = attrib_len;
561 }
562
563 attrib_count++;
564
565 } else {
566 pr_trace_msg(trace_channel, 6,
567 "Access-Accept packet lacks User-Name attribute (%d)", RADIUS_USER_NAME);
568 }
569
570 /* Handle any REPLY_MESSAGE attributes. */
571 if (!(radius_opts & RADIUS_OPT_IGNORE_REPLY_MESSAGE_ATTR)) {
572 unsigned int pkt_len = 0;
573
574 attrib = radius_get_next_attrib(pkt, RADIUS_REPLY_MESSAGE, &pkt_len, NULL);
575 while (attrib != NULL) {
576 pr_signals_handle();
577
578 attrib_len = RADIUS_ATTRIB_LEN(attrib);
579 if (attrib_len > 0) {
580 char *reply_msg = NULL;
581
582 reply_msg = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
583 pr_trace_msg(trace_channel, 7,
584 "found REPLY_MESSAGE attribute in Access-Accept message: '%s'",
585 reply_msg);
586 pr_response_add(R_DUP, "%s", reply_msg);
587 }
588
589 attrib_count++;
590
591 if (pkt_len == 0) {
592 break;
593 }
594
595 attrib = radius_get_next_attrib(pkt, RADIUS_REPLY_MESSAGE, &pkt_len,
596 attrib);
597 }
598
599 if (attrib_count == 0) {
600 pr_trace_msg(trace_channel, 6,
601 "Access-Accept packet lacks Reply-Message attribute (%d)",
602 RADIUS_REPLY_MESSAGE);
603 }
604 }
605
606 /* Handle any IDLE_TIMEOUT attribute. */
607 if (!(radius_opts & RADIUS_OPT_IGNORE_IDLE_TIMEOUT_ATTR)) {
608 attrib = radius_get_attrib(pkt, RADIUS_IDLE_TIMEOUT);
609 if (attrib != NULL) {
610 attrib_len = RADIUS_ATTRIB_LEN(attrib);
611 if (attrib_len > 0) {
612 int timeout = -1;
613
614 if (attrib_len > sizeof(timeout)) {
615 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
616 "invalid attribute length (%u) for Idle-Timeout, truncating",
617 attrib_len);
618 attrib_len = sizeof(timeout);
619 }
620
621 memcpy(&timeout, attrib->data, attrib_len);
622 timeout = ntohl(timeout);
623
624 if (timeout < 0) {
625 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
626 "packet includes Idle-Timeout attribute %d for illegal timeout: %d",
627 RADIUS_IDLE_TIMEOUT, timeout);
628
629 } else {
630 config_rec *c;
631
632 pr_trace_msg(trace_channel, 2,
633 "packet includes Idle-Timeout attribute %d for timeout: %d",
634 RADIUS_IDLE_TIMEOUT, timeout);
635 remove_config(main_server->conf, "TimeoutIdle", TRUE);
636
637 c = pr_config_add_set(&main_server->conf, "TimeoutIdle",
638 PR_CONFIG_FL_INSERT_HEAD);
639 c->config_type = CONF_PARAM;
640 c->argc = 1;
641 c->argv[0] = palloc(c->pool, sizeof(int));
642 *((int *) c->argv[0]) = timeout;
643
644 attrib_count++;
645 }
646 }
647
648 } else {
649 pr_trace_msg(trace_channel, 6,
650 "Access-Accept packet lacks Idle-Timeout attribute (%d)",
651 RADIUS_IDLE_TIMEOUT);
652 }
653 }
654
655 /* Handle any SESSION_TIMEOUT attribute. */
656 if (!(radius_opts & RADIUS_OPT_IGNORE_SESSION_TIMEOUT_ATTR)) {
657 attrib = radius_get_attrib(pkt, RADIUS_SESSION_TIMEOUT);
658 if (attrib != NULL) {
659 attrib_len = RADIUS_ATTRIB_LEN(attrib);
660 if (attrib_len > 0) {
661 int timeout = -1;
662
663 if (attrib_len > sizeof(timeout)) {
664 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
665 "invalid attribute length (%u) for Session-Timeout, truncating",
666 attrib_len);
667 attrib_len = sizeof(timeout);
668 }
669
670 memcpy(&timeout, attrib->data, attrib_len);
671 timeout = ntohl(timeout);
672
673 if (timeout < 0) {
674 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
675 "packet includes Session-Timeout attribute %d for illegal "
676 "timeout: %d", RADIUS_SESSION_TIMEOUT, timeout);
677
678 } else {
679 config_rec *c;
680
681 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
682 "packet includes Session-Timeout attribute %d for timeout: %d",
683 RADIUS_SESSION_TIMEOUT, timeout);
684 remove_config(main_server->conf, "TimeoutSession", TRUE);
685
686 c = pr_config_add_set(&main_server->conf, "TimeoutSession",
687 PR_CONFIG_FL_INSERT_HEAD);
688 c->config_type = CONF_PARAM;
689 c->argc = 2;
690 c->argv[0] = palloc(c->pool, sizeof(int));
691 *((int *) c->argv[0]) = timeout;
692 c->argv[1] = palloc(c->pool, sizeof(unsigned int));
693 *((unsigned int *) c->argv[1]) = 0;
694
695 attrib_count++;
696 }
697 }
698
699 } else {
700 pr_trace_msg(trace_channel, 6,
701 "Access-Accept packet lacks Session-Timeout attribute (%d)",
702 RADIUS_SESSION_TIMEOUT);
703 }
704 }
705
706 return attrib_count;
707 }
708
709 static int radius_process_user_info_attribs(radius_packet_t *pkt) {
710 int attrib_count = 0;
711
712 if (radius_uid_attr_id || radius_gid_attr_id ||
713 radius_home_attr_id || radius_shell_attr_id) {
714 pr_trace_msg(trace_channel, 2,
715 "parsing packet for RadiusUserInfo attributes");
716
717 /* These custom values will been supplied in the configuration file, and
718 * set when the RadiusUserInfo config_rec is retrieved, during
719 * session initialization.
720 */
721
722 if (radius_uid_attr_id) {
723 radius_attrib_t *attrib;
724
725 attrib = radius_get_vendor_attrib(pkt, radius_uid_attr_id);
726 if (attrib) {
727 unsigned char attrib_len;
728 int uid = -1;
729
730 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
731
732 /* Parse the attribute value into an int, then cast it into the
733 * radius_passwd.pw_uid field. Make sure it's a sane UID
734 * (ie non-negative).
735 */
736
737 if (attrib_len > sizeof(uid)) {
738 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
739 "invalid attribute length (%u) for user ID, truncating",
740 attrib_len);
741 attrib_len = sizeof(uid);
742 }
743
744 memcpy(&uid, attrib->data, attrib_len);
745 uid = ntohl(uid);
746
747 if (uid < 0) {
748 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
749 "packet includes '%s' Vendor-Specific Attribute %d for illegal "
750 "user ID: %d", radius_vendor_name, radius_uid_attr_id, uid);
751
752 } else {
753 radius_passwd.pw_uid = uid;
754
755 pr_trace_msg(trace_channel, 3,
756 "packet includes '%s' Vendor-Specific Attribute %d for user ID: %d",
757 radius_vendor_name, radius_uid_attr_id,
758 radius_passwd.pw_uid);
759 attrib_count++;
760 }
761
762 } else {
763 pr_trace_msg(trace_channel, 6,
764 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d "
765 "for user ID; defaulting to '%u'", radius_vendor_name,
766 radius_uid_attr_id, radius_passwd.pw_uid);
767 }
768 }
769
770 if (radius_gid_attr_id) {
771 radius_attrib_t *attrib;
772
773 attrib = radius_get_vendor_attrib(pkt, radius_gid_attr_id);
774 if (attrib) {
775 unsigned char attrib_len;
776 int gid = -1;
777
778 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
779
780 /* Parse the attribute value into an int, then cast it into the
781 * radius_passwd.pw_gid field. Make sure it's a sane GID
782 * (ie non-negative).
783 */
784
785 if (attrib_len > sizeof(gid)) {
786 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
787 "invalid attribute length (%u) for group ID, truncating",
788 attrib_len);
789 attrib_len = sizeof(gid);
790 }
791
792 memcpy(&gid, attrib->data, attrib_len);
793 gid = ntohl(gid);
794
795 if (gid < 0) {
796 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
797 "packet includes '%s' Vendor-Specific Attribute %d for illegal "
798 "group ID: %d", radius_vendor_name, radius_gid_attr_id, gid);
799
800 } else {
801 radius_passwd.pw_gid = gid;
802
803 pr_trace_msg(trace_channel, 3,
804 "packet includes '%s' Vendor-Specific Attribute %d for group "
805 "ID: %d", radius_vendor_name, radius_gid_attr_id,
806 radius_passwd.pw_gid);
807 attrib_count++;
808 }
809
810 } else {
811 pr_trace_msg(trace_channel, 6,
812 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d "
813 "for group ID; defaulting to '%u'", radius_vendor_name,
814 radius_gid_attr_id, radius_passwd.pw_gid);
815 }
816 }
817
818 if (radius_home_attr_id) {
819 radius_attrib_t *attrib;
820
821 attrib = radius_get_vendor_attrib(pkt, radius_home_attr_id);
822 if (attrib) {
823 unsigned char attrib_len;
824 char *home;
825
826 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
827
828 home = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
829 if (*home != '/') {
830 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
831 "packet includes '%s' Vendor-Specific Attribute %d for illegal "
832 "home: '%s'", radius_vendor_name, radius_home_attr_id, home);
833
834 } else {
835 radius_passwd.pw_dir = home;
836
837 pr_trace_msg(trace_channel, 3,
838 "packet includes '%s' Vendor-Specific Attribute %d for home "
839 "directory: '%s'", radius_vendor_name, radius_home_attr_id,
840 radius_passwd.pw_dir);
841 attrib_count++;
842 }
843
844 } else {
845 pr_trace_msg(trace_channel, 6,
846 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
847 "home directory; defaulting to '%s'", radius_vendor_name,
848 radius_home_attr_id, radius_passwd.pw_dir);
849 }
850 }
851
852 if (radius_shell_attr_id) {
853 radius_attrib_t *attrib;
854
855 attrib = radius_get_vendor_attrib(pkt, radius_shell_attr_id);
856 if (attrib) {
857 unsigned char attrib_len;
858 char *shell;
859
860 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
861
862 shell = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
863 if (*shell != '/') {
864 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
865 "packet includes '%s' Vendor-Specific Attribute %d for illegal "
866 "shell: '%s'", radius_vendor_name, radius_shell_attr_id, shell);
867
868 } else {
869 radius_passwd.pw_shell = shell;
870
871 pr_trace_msg(trace_channel, 3,
872 "packet includes '%s' Vendor-Specific Attribute %d for "
873 "shell: '%s'", radius_vendor_name, radius_shell_attr_id,
874 radius_passwd.pw_shell);
875 attrib_count++;
876 }
877
878 } else {
879 pr_trace_msg(trace_channel, 6,
880 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
881 "shell; defaulting to '%s'", radius_vendor_name, radius_shell_attr_id,
882 radius_passwd.pw_shell);
883 }
884 }
885 }
886
887 return attrib_count;
888 }
889
890 static int radius_process_group_info_attribs(radius_packet_t *pkt) {
891 int attrib_count = 0;
892
893 if (radius_prime_group_name_attr_id ||
894 radius_addl_group_names_attr_id ||
895 radius_addl_group_ids_attr_id) {
896 unsigned int ngroups = 0, ngids = 0;
897 char **groups = NULL;
898 gid_t *gids = NULL;
899
900 pr_trace_msg(trace_channel, 2,
901 "parsing packet for RadiusGroupInfo attributes");
902
903 if (radius_prime_group_name_attr_id) {
904 radius_attrib_t *attrib;
905
906 attrib = radius_get_vendor_attrib(pkt, radius_prime_group_name_attr_id);
907 if (attrib) {
908 unsigned char attrib_len;
909 char *group_name;
910
911 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
912
913 group_name = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
914 radius_prime_group_name = pstrdup(radius_pool, group_name);
915
916 pr_trace_msg(trace_channel, 3,
917 "packet includes '%s' Vendor-Specific Attribute %d for primary "
918 "group name: '%s'", radius_vendor_name,
919 radius_prime_group_name_attr_id, radius_prime_group_name);
920 attrib_count++;
921
922 } else {
923 pr_trace_msg(trace_channel, 6,
924 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
925 "prime group name; defaulting to '%s'", radius_vendor_name,
926 radius_prime_group_name_attr_id, radius_prime_group_name);
927 }
928 }
929
930 if (radius_addl_group_names_attr_id) {
931 radius_attrib_t *attrib;
932
933 attrib = radius_get_vendor_attrib(pkt, radius_addl_group_names_attr_id);
934 if (attrib) {
935 unsigned char attrib_len;
936 char *group_names, *group_names_str;
937
938 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
939 group_names = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
940
941 /* Make a copy of the string, for parsing purposes. The parsing
942 * of this string will consume it.
943 */
944 group_names_str = pstrdup(radius_pool, group_names);
945
946 if (!radius_parse_groups_str(radius_pool, group_names_str, &groups,
947 &ngroups)) {
948 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
949 "packet includes '%s' Vendor-Specific Attribute %d for illegal "
950 "additional group names: '%s'", radius_vendor_name,
951 radius_addl_group_names_attr_id, group_names);
952
953 } else {
954 pr_trace_msg(trace_channel, 3,
955 "packet includes '%s' Vendor-Specific Attribute %d for "
956 "additional group names: '%s'", radius_vendor_name,
957 radius_addl_group_names_attr_id, group_names);
958 }
959
960 attrib_count++;
961
962 } else {
963 pr_trace_msg(trace_channel, 6,
964 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
965 "additional group names; defaulting to '%s'", radius_vendor_name,
966 radius_addl_group_names_attr_id, radius_addl_group_names_str);
967 }
968 }
969
970 if (radius_addl_group_ids_attr_id) {
971 radius_attrib_t *attrib;
972
973 attrib = radius_get_vendor_attrib(pkt, radius_addl_group_ids_attr_id);
974 if (attrib) {
975 unsigned char attrib_len;
976 char *group_ids, *group_ids_str;
977
978 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
979 group_ids = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
980
981 /* Make a copy of the string, for parsing purposes. The parsing
982 * of this string will consume it.
983 */
984 group_ids_str = pstrdup(radius_pool, group_ids);
985
986 if (!radius_parse_gids_str(radius_pool, group_ids_str, &gids, &ngids)) {
987 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
988 "packet includes '%s' Vendor-Specific Attribute %d for illegal "
989 "additional group IDs: '%s'", radius_vendor_name,
990 radius_addl_group_ids_attr_id, group_ids);
991
992 } else {
993 pr_trace_msg(trace_channel, 3,
994 "packet includes '%s' Vendor-Specific Attribute %d for additional "
995 "group IDs: '%s'", radius_vendor_name,
996 radius_addl_group_ids_attr_id, group_ids);
997 }
998
999 attrib_count++;
1000
1001 } else {
1002 pr_trace_msg(trace_channel, 6,
1003 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
1004 "additional group IDs; defaulting to '%s'", radius_vendor_name,
1005 radius_addl_group_ids_attr_id, radius_addl_group_ids_str);
1006 }
1007 }
1008
1009 /* One last RadiusGroupInfo check: does the number of returned group
1010 * names match the number of returned group IDs?
1011 */
1012 if (ngroups == ngids) {
1013 radius_have_group_info = TRUE;
1014 radius_addl_group_count = ngroups;
1015 radius_addl_group_names = groups;
1016 radius_addl_group_ids = gids;
1017
1018 } else {
1019 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1020 "server provided mismatched number of group names (%u) and group "
1021 "IDs (%u), ignoring them", ngroups, ngids);
1022 }
1023 }
1024
1025 return attrib_count;
1026 }
1027
1028 static int radius_process_quota_info_attribs(radius_packet_t *pkt) {
1029 int attrib_count = 0;
1030
1031 if (radius_quota_per_sess_attr_id ||
1032 radius_quota_limit_type_attr_id ||
1033 radius_quota_bytes_in_attr_id ||
1034 radius_quota_bytes_out_attr_id ||
1035 radius_quota_bytes_xfer_attr_id ||
1036 radius_quota_files_in_attr_id ||
1037 radius_quota_files_out_attr_id ||
1038 radius_quota_files_xfer_attr_id) {
1039
1040 pr_trace_msg(trace_channel, 2,
1041 "parsing packet for RadiusQuotaInfo attributes");
1042
1043 if (radius_quota_per_sess_attr_id) {
1044 radius_attrib_t *attrib;
1045
1046 attrib = radius_get_vendor_attrib(pkt, radius_quota_per_sess_attr_id);
1047 if (attrib) {
1048 unsigned char attrib_len;
1049 char *per_sess;
1050
1051 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
1052 per_sess = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
1053
1054 radius_quota_per_sess = per_sess;
1055
1056 pr_trace_msg(trace_channel, 2,
1057 "packet includes '%s' Vendor-Specific Attribute %d for quota "
1058 "per-session: '%s'", radius_vendor_name,
1059 radius_quota_per_sess_attr_id, radius_quota_per_sess);
1060 attrib_count++;
1061
1062 } else {
1063 pr_trace_msg(trace_channel, 6,
1064 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
1065 "quota per-session; defaulting to '%s'", radius_vendor_name,
1066 radius_quota_per_sess_attr_id, radius_quota_per_sess);
1067 }
1068 }
1069
1070 if (radius_quota_limit_type_attr_id) {
1071 radius_attrib_t *attrib;
1072
1073 attrib = radius_get_vendor_attrib(pkt, radius_quota_limit_type_attr_id);
1074 if (attrib) {
1075 unsigned char attrib_len;
1076 char *limit_type;
1077
1078 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
1079 limit_type = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
1080 radius_quota_limit_type = limit_type;
1081
1082 pr_trace_msg(trace_channel, 2,
1083 "packet includes '%s' Vendor-Specific Attribute %d for quota limit "
1084 "type: '%s'", radius_vendor_name, radius_quota_limit_type_attr_id,
1085 radius_quota_limit_type);
1086 attrib_count++;
1087
1088 } else {
1089 pr_trace_msg(trace_channel, 6,
1090 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
1091 "quota limit type; defaulting to '%s'", radius_vendor_name,
1092 radius_quota_limit_type_attr_id, radius_quota_limit_type);
1093 }
1094 }
1095
1096 if (radius_quota_bytes_in_attr_id) {
1097 radius_attrib_t *attrib;
1098
1099 attrib = radius_get_vendor_attrib(pkt, radius_quota_bytes_in_attr_id);
1100 if (attrib) {
1101 unsigned char attrib_len;
1102 char *bytes_in;
1103
1104 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
1105 bytes_in = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
1106 radius_quota_bytes_in = bytes_in;
1107
1108 pr_trace_msg(trace_channel, 3,
1109 "packet includes '%s' Vendor-Specific Attribute %d for quota bytes "
1110 "in available: '%s'", radius_vendor_name,
1111 radius_quota_bytes_in_attr_id, radius_quota_bytes_in);
1112 attrib_count++;
1113
1114 } else {
1115 pr_trace_msg(trace_channel, 6,
1116 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
1117 "quota bytes in available; defaulting to '%s'", radius_vendor_name,
1118 radius_quota_bytes_in_attr_id, radius_quota_bytes_in);
1119 }
1120 }
1121
1122 if (radius_quota_bytes_out_attr_id) {
1123 radius_attrib_t *attrib;
1124
1125 attrib = radius_get_vendor_attrib(pkt, radius_quota_bytes_out_attr_id);
1126 if (attrib) {
1127 unsigned char attrib_len;
1128 char *bytes_out;
1129
1130 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
1131 bytes_out = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
1132 radius_quota_bytes_out = bytes_out;
1133
1134 pr_trace_msg(trace_channel, 3,
1135 "packet includes '%s' Vendor-Specific Attribute %d for quota bytes "
1136 "out available: '%s'", radius_vendor_name,
1137 radius_quota_bytes_out_attr_id, radius_quota_bytes_out);
1138 attrib_count++;
1139
1140 } else {
1141 pr_trace_msg(trace_channel, 6,
1142 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
1143 "quota bytes out available; defaulting to '%s'", radius_vendor_name,
1144 radius_quota_bytes_out_attr_id, radius_quota_bytes_out);
1145 }
1146 }
1147
1148 if (radius_quota_bytes_xfer_attr_id) {
1149 radius_attrib_t *attrib;
1150
1151 attrib = radius_get_vendor_attrib(pkt, radius_quota_bytes_xfer_attr_id);
1152 if (attrib) {
1153 unsigned char attrib_len;
1154 char *bytes_xfer;
1155
1156 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
1157 bytes_xfer = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
1158 radius_quota_bytes_xfer = bytes_xfer;
1159
1160 pr_trace_msg(trace_channel, 3,
1161 "packet includes '%s' Vendor-Specific Attribute %d for quota bytes "
1162 "xfer available: '%s'", radius_vendor_name,
1163 radius_quota_bytes_xfer_attr_id, radius_quota_bytes_xfer);
1164 attrib_count++;
1165
1166 } else {
1167 pr_trace_msg(trace_channel, 6,
1168 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
1169 "quota bytes xfer available; defaulting to '%s'", radius_vendor_name,
1170 radius_quota_bytes_xfer_attr_id, radius_quota_bytes_xfer);
1171 }
1172 }
1173
1174 if (radius_quota_files_in_attr_id) {
1175 radius_attrib_t *attrib;
1176
1177 attrib = radius_get_vendor_attrib(pkt, radius_quota_files_in_attr_id);
1178 if (attrib) {
1179 unsigned char attrib_len;
1180 char *files_in;
1181
1182 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
1183 files_in = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
1184 radius_quota_files_in = files_in;
1185
1186 pr_trace_msg(trace_channel, 3,
1187 "packet includes '%s' Vendor-Specific Attribute %d for quota files "
1188 "in available: '%s'", radius_vendor_name,
1189 radius_quota_files_in_attr_id, radius_quota_files_in);
1190 attrib_count++;
1191
1192 } else {
1193 pr_trace_msg(trace_channel, 6,
1194 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
1195 "quota files in available; defaulting to '%s'", radius_vendor_name,
1196 radius_quota_files_in_attr_id, radius_quota_files_in);
1197 }
1198 }
1199
1200 if (radius_quota_files_out_attr_id) {
1201 radius_attrib_t *attrib;
1202
1203 attrib = radius_get_vendor_attrib(pkt, radius_quota_files_out_attr_id);
1204 if (attrib) {
1205 unsigned char attrib_len;
1206 char *files_out;
1207
1208 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
1209 files_out = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
1210 radius_quota_files_out = files_out;
1211
1212 pr_trace_msg(trace_channel, 3,
1213 "packet includes '%s' Vendor-Specific Attribute %d for quota files "
1214 "out available: '%s'", radius_vendor_name,
1215 radius_quota_files_out_attr_id, radius_quota_files_out);
1216 attrib_count++;
1217
1218 } else {
1219 pr_trace_msg(trace_channel, 6,
1220 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
1221 "quota files out available; defaulting to '%s'", radius_vendor_name,
1222 radius_quota_files_out_attr_id, radius_quota_files_out);
1223 }
1224 }
1225
1226 if (radius_quota_files_xfer_attr_id) {
1227 radius_attrib_t *attrib;
1228
1229 attrib = radius_get_vendor_attrib(pkt, radius_quota_files_xfer_attr_id);
1230 if (attrib) {
1231 unsigned char attrib_len;
1232 char *files_xfer;
1233
1234 attrib_len = RADIUS_VSA_ATTRIB_LEN(attrib);
1235 files_xfer = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
1236 radius_quota_files_xfer = files_xfer;
1237
1238 pr_trace_msg(trace_channel, 3,
1239 "packet includes '%s' Vendor-Specific Attribute %d for quota files "
1240 "xfer available: '%s'", radius_vendor_name,
1241 radius_quota_files_xfer_attr_id, radius_quota_files_xfer);
1242 attrib_count++;
1243
1244 } else {
1245 pr_trace_msg(trace_channel, 6,
1246 "Access-Accept packet lacks '%s' Vendor-Specific Attribute %d for "
1247 "quota files xfer available; defaulting to '%s'", radius_vendor_name,
1248 radius_quota_files_xfer_attr_id, radius_quota_files_xfer);
1249 }
1250 }
1251 }
1252
1253 return attrib_count;
1254 }
1255
1256 static int radius_process_accept_packet(radius_packet_t *pkt,
1257 const unsigned char *secret, size_t secret_len) {
1258 int attrib_count = 0, res;;
1259
1260 res = radius_process_standard_attribs(pkt, secret, secret_len);
1261 if (res < 0) {
1262 return -1;
1263 }
1264
1265 attrib_count += res;
1266
1267 /* Now, parse the packet for any server-supplied RadiusUserInfo attributes,
1268 * if RadiusUserInfo is indeed in effect.
1269 */
1270
1271 if (radius_have_user_info == FALSE &&
1272 radius_have_group_info == FALSE &&
1273 radius_have_quota_info == FALSE) {
1274 /* Return now if there's no reason for doing extra work. */
1275 return attrib_count;
1276 }
1277
1278 attrib_count += radius_process_user_info_attribs(pkt);
1279 attrib_count += radius_process_group_info_attribs(pkt);
1280 attrib_count += radius_process_quota_info_attribs(pkt);
1281
1282 return attrib_count;
1283 }
1284
1285 static int radius_process_reject_packet(radius_packet_t *pkt,
1286 const unsigned char *secret, size_t secret_len) {
1287 int attrib_count = 0;
1288
1289 if (radius_verify_auth_mac(pkt, "Access-Reject", secret, secret_len) < 0) {
1290 return -1;
1291 }
1292
1293 /* Handle any REPLY_MESSAGE attributes. */
1294 if (!(radius_opts & RADIUS_OPT_IGNORE_REPLY_MESSAGE_ATTR)) {
1295 radius_attrib_t *attrib = NULL;
1296 unsigned int pkt_len = 0;
1297
1298 attrib = radius_get_next_attrib(pkt, RADIUS_REPLY_MESSAGE, &pkt_len,
1299 NULL);
1300 while (attrib != NULL) {
1301 unsigned char attrib_len;
1302
1303 pr_signals_handle();
1304
1305 attrib_len = RADIUS_ATTRIB_LEN(attrib);
1306 if (attrib_len > 0) {
1307 char *reply_msg = NULL;
1308
1309 reply_msg = pstrndup(radius_pool, (char *) attrib->data, attrib_len);
1310
1311 pr_trace_msg(trace_channel, 7,
1312 "found REPLY_MESSAGE attribute in Access-Reject message: '%s'",
1313 reply_msg);
1314 pr_response_add_err(R_DUP, "%s", reply_msg);
1315 }
1316
1317 attrib_count++;
1318
1319 if (pkt_len == 0) {
1320 break;
1321 }
1322
1323 attrib = radius_get_next_attrib(pkt, RADIUS_REPLY_MESSAGE, &pkt_len,
1324 attrib);
1325 }
1326 }
1327
1328 return attrib_count;
1329 }
1330
1331 static void radius_process_group_info(config_rec *c) {
1332 char *param = NULL;
1333 unsigned char have_illegal_value = FALSE;
1334 unsigned int ngroups = 0, ngids = 0;
1335 char **groups = NULL;
1336 gid_t *gids = NULL;
1337
1338 /* Parse out any configured attribute/defaults here. The stored strings will
1339 * already have been sanitized by the configuration handler, so I don't
1340 * need to worry about that here.
1341 */
1342
1343 param = (char *) c->argv[0];
1344 if (RADIUS_IS_VAR(param) == TRUE) {
1345 radius_parse_var(param, &radius_prime_group_name_attr_id,
1346 &radius_prime_group_name);
1347
1348 } else {
1349 radius_prime_group_name = param;
1350 }
1351
1352 /* If the group name count is zero, then I know that the data will be
1353 * contained in a VSA. Otherwise, the group names have already been parsed.
1354 */
1355 if (*((unsigned int *) c->argv[1]) == 0) {
1356 param = (char *) c->argv[2];
1357
1358 radius_parse_var(param, &radius_addl_group_names_attr_id,
1359 &radius_addl_group_names_str);
1360
1361 /* Now, parse the default value provided. */
1362 if (!radius_parse_groups_str(c->pool, radius_addl_group_names_str,
1363 &groups, &ngroups)) {
1364 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1365 "badly formatted RadiusGroupInfo default additional group names");
1366 have_illegal_value = TRUE;
1367 }
1368
1369 } else {
1370 ngroups = *((unsigned int *) c->argv[1]);
1371 groups = (char **) c->argv[2];
1372 }
1373
1374 if (*((unsigned int *) c->argv[3]) == 0) {
1375 param = (char *) c->argv[4];
1376
1377 radius_parse_var(param, &radius_addl_group_ids_attr_id,
1378 &radius_addl_group_ids_str);
1379
1380 /* Similarly, parse the default value provided. */
1381 if (!radius_parse_gids_str(c->pool, radius_addl_group_ids_str,
1382 &gids, &ngids)) {
1383 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1384 "badly formatted RadiusGroupInfo default additional group IDs");
1385 have_illegal_value = TRUE;
1386 }
1387
1388 } else {
1389 ngids = *((unsigned int *) c->argv[3]);
1390 gids = (gid_t *) c->argv[4];
1391 }
1392
1393 if (!have_illegal_value &&
1394 ngroups != ngids) {
1395 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1396 "mismatched number of RadiusGroupInfo default additional group "
1397 "names (%u) and IDs (%u)", ngroups, ngids);
1398 have_illegal_value = TRUE;
1399 }
1400
1401 if (!have_illegal_value) {
1402 radius_have_group_info = TRUE;
1403 radius_addl_group_count = ngroups;
1404 radius_addl_group_names = groups;
1405 radius_addl_group_ids = gids;
1406
1407 } else {
1408 radius_have_group_info = FALSE;
1409 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1410 "error with RadiusGroupInfo parameters, ignoring them");
1411 }
1412 }
1413
1414 static void radius_process_quota_info(config_rec *c) {
1415 char *param = NULL;
1416 unsigned char have_illegal_value = FALSE;
1417
1418 /* Parse out any configured attribute/defaults here. The stored strings will
1419 * already have been sanitized by the configuration handler, so I don't
1420 * need to worry about that here.
1421 */
1422
1423 param = (char *) c->argv[0];
1424 if (RADIUS_IS_VAR(param) == TRUE) {
1425 radius_parse_var(param, &radius_quota_per_sess_attr_id,
1426 &radius_quota_per_sess);
1427
1428 } else {
1429 radius_quota_per_sess = param;
1430
1431 if (strcasecmp(param, "false") != 0 &&
1432 strcasecmp(param, "true") != 0) {
1433 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1434 "illegal RadiusQuotaInfo per-session value: '%s'", param);
1435 have_illegal_value = TRUE;
1436
1437 } else {
1438 pr_trace_msg(trace_channel, 17,
1439 "found RadiusQuotaInfo per-session value: %s", param);
1440 }
1441 }
1442
1443 param = (char *) c->argv[1];
1444 if (RADIUS_IS_VAR(param) == TRUE) {
1445 radius_parse_var(param, &radius_quota_limit_type_attr_id,
1446 &radius_quota_limit_type);
1447
1448 } else {
1449 radius_quota_limit_type = param;
1450
1451 if (strcasecmp(param, "hard") != 0 &&
1452 strcasecmp(param, "soft") != 0) {
1453 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1454 "illegal RadiusQuotaInfo limit type value: '%s'", param);
1455 have_illegal_value = TRUE;
1456
1457 } else {
1458 pr_trace_msg(trace_channel, 17,
1459 "found RadiusQuotaInfo limit type value: %s", param);
1460 }
1461 }
1462
1463 param = (char *) c->argv[2];
1464 if (RADIUS_IS_VAR(param) == TRUE) {
1465 radius_parse_var(param, &radius_quota_bytes_in_attr_id,
1466 &radius_quota_bytes_in);
1467
1468 } else {
1469 char *endp = NULL;
1470
1471 if (strtod(param, &endp) < 0) {
1472 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1473 "illegal RadiusQuotaInfo bytes in value: negative number");
1474 have_illegal_value = TRUE;
1475 }
1476
1477 if (endp && *endp) {
1478 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1479 "illegal RadiusQuotaInfo bytes in value: '%s' not a number", param);
1480 have_illegal_value = TRUE;
1481
1482 } else {
1483 pr_trace_msg(trace_channel, 17,
1484 "found RadiusQuotaInfo bytes in value: %s", param);
1485 }
1486
1487 radius_quota_bytes_in = param;
1488 }
1489
1490 param = (char *) c->argv[3];
1491 if (RADIUS_IS_VAR(param) == TRUE) {
1492 radius_parse_var(param, &radius_quota_bytes_out_attr_id,
1493 &radius_quota_bytes_out);
1494
1495 } else {
1496 char *endp = NULL;
1497
1498 if (strtod(param, &endp) < 0) {
1499 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1500 "illegal RadiusQuotaInfo bytes out value: negative number");
1501 have_illegal_value = TRUE;
1502 }
1503
1504 if (endp && *endp) {
1505 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1506 "illegal RadiusQuotaInfo bytes out value: '%s' not a number", param);
1507 have_illegal_value = TRUE;
1508
1509 } else {
1510 pr_trace_msg(trace_channel, 17,
1511 "found RadiusQuotaInfo bytes out value: %s", param);
1512 }
1513
1514 radius_quota_bytes_out = param;
1515 }
1516
1517 param = (char *) c->argv[4];
1518 if (RADIUS_IS_VAR(param) == TRUE) {
1519 radius_parse_var(param, &radius_quota_bytes_xfer_attr_id,
1520 &radius_quota_bytes_xfer);
1521
1522 } else {
1523 char *endp = NULL;
1524
1525 if (strtod(param, &endp) < 0) {
1526 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1527 "illegal RadiusQuotaInfo bytes xfer value: negative number");
1528 have_illegal_value = TRUE;
1529 }
1530
1531 if (endp && *endp) {
1532 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1533 "illegal RadiusQuotaInfo bytes xfer value: '%s' not a number", param);
1534 have_illegal_value = TRUE;
1535
1536 } else {
1537 pr_trace_msg(trace_channel, 17,
1538 "found RadiusQuotaInfo bytes xfer value: %s", param);
1539 }
1540
1541 radius_quota_bytes_xfer = param;
1542 }
1543
1544 param = (char *) c->argv[5];
1545 if (RADIUS_IS_VAR(param) == TRUE) {
1546 radius_parse_var(param, &radius_quota_files_in_attr_id,
1547 &radius_quota_files_in);
1548
1549 } else {
1550 char *endp = NULL;
1551 unsigned long res;
1552
1553 res = strtoul(param, &endp, 10);
1554 if (endp && *endp) {
1555 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1556 "illegal RadiusQuotaInfo files in value: '%s' not a number",
1557 param);
1558 have_illegal_value = TRUE;
1559
1560 } else {
1561 pr_trace_msg(trace_channel, 17,
1562 "found RadiusQuotaInfo files in value: %lu", res);
1563 }
1564
1565 radius_quota_files_in = param;
1566 }
1567
1568 param = (char *) c->argv[6];
1569 if (RADIUS_IS_VAR(param) == TRUE) {
1570 radius_parse_var(param, &radius_quota_files_out_attr_id,
1571 &radius_quota_files_out);
1572
1573 } else {
1574 char *endp = NULL;
1575 unsigned long res;
1576
1577 res = strtoul(param, &endp, 10);
1578 if (endp && *endp) {
1579 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1580 "illegal RadiusQuotaInfo files out value: '%s' not a number", param);
1581 have_illegal_value = TRUE;
1582
1583 } else {
1584 pr_trace_msg(trace_channel, 17,
1585 "found RadiusQuotaInfo files out value: %lu", res);
1586 }
1587
1588 radius_quota_files_out = param;
1589 }
1590
1591 param = (char *) c->argv[7];
1592 if (RADIUS_IS_VAR(param) == TRUE) {
1593 radius_parse_var(param, &radius_quota_files_xfer_attr_id,
1594 &radius_quota_files_xfer);
1595
1596 } else {
1597 char *endp = NULL;
1598 unsigned long res;
1599
1600 res = strtoul(param, &endp, 10);
1601 if (endp && *endp) {
1602 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1603 "illegal RadiusQuotaInfo files xfer value: '%s' not a number", param);
1604 have_illegal_value = TRUE;
1605
1606 } else {
1607 pr_trace_msg(trace_channel, 17,
1608 "found RadiusQuotaInfo files xfer value: %lu", res);
1609 }
1610
1611 radius_quota_files_xfer = param;
1612 }
1613
1614 if (!have_illegal_value) {
1615 radius_have_quota_info = TRUE;
1616
1617 } else {
1618 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1619 "error with RadiusQuotaInfo parameters, ignoring them");
1620 }
1621 }
1622
1623 static void radius_process_user_info(config_rec *c) {
1624 char *param = NULL;
1625 unsigned char have_illegal_value = FALSE;
1626
1627 /* radius_passwd.pw_name will be filled in later, after successful
1628 * authentication. radius_passwd.pw_gecos will always be NULL, as there
1629 * is no practical need for this information.
1630 */
1631
1632 radius_passwd.pw_uid = -1;
1633 radius_passwd.pw_gid = -1;
1634 radius_passwd.pw_passwd = NULL;
1635 radius_passwd.pw_gecos = NULL;
1636
1637 /* Parse out any configured attribute/defaults here. The stored strings will
1638 * already have been sanitized by the configuration handler, so I don't
1639 * need to worry about that here.
1640 */
1641
1642 /* Process the UID string. */
1643 param = (char *) c->argv[0];
1644
1645 if (RADIUS_IS_VAR(param) == TRUE) {
1646 char *endp = NULL, *value = NULL;
1647
1648 radius_parse_var(param, &radius_uid_attr_id, &value);
1649 if (value != NULL) {
1650 radius_passwd.pw_uid = (uid_t) strtoul(value, &endp, 10);
1651 }
1652
1653 if (radius_passwd.pw_uid == (uid_t) -1) {
1654 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1655 "illegal RadiusUserInfo default UID value: -1 not allowed");
1656 have_illegal_value = TRUE;
1657 }
1658
1659 if (endp && *endp) {
1660 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1661 "illegal RadiusUserInfo default UID value: '%s' not a number", value);
1662 have_illegal_value = TRUE;
1663 }
1664
1665 } else {
1666 char *endp = NULL;
1667
1668 radius_passwd.pw_uid = (uid_t) strtoul(param, &endp, 10);
1669
1670 if (radius_passwd.pw_uid == (uid_t) -1) {
1671 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1672 "illegal RadiusUserInfo UID value: -1 not allowed");
1673 have_illegal_value = TRUE;
1674 }
1675
1676 if (endp && *endp) {
1677 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1678 "illegal RadiusUserInfo UID value: '%s' not a number", param);
1679 have_illegal_value = TRUE;
1680 }
1681 }
1682
1683 /* Process the GID string. */
1684 param = (char *) c->argv[1];
1685
1686 if (RADIUS_IS_VAR(param) == TRUE) {
1687 char *endp = NULL, *value = NULL;
1688
1689 radius_parse_var(param, &radius_gid_attr_id, &value);
1690
1691 if (value != NULL) {
1692 radius_passwd.pw_gid = (gid_t) strtoul(value, &endp, 10);
1693 }
1694
1695 if (radius_passwd.pw_gid == (gid_t) -1) {
1696 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1697 "illegal RadiusUserInfo default GID value: -1 not allowed");
1698 have_illegal_value = TRUE;
1699 }
1700
1701 if (endp && *endp) {
1702 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1703 "illegal RadiusUserInfo default GID value: '%s' not a number", value);
1704 have_illegal_value = TRUE;
1705 }
1706
1707 } else {
1708 char *endp = NULL;
1709
1710 radius_passwd.pw_gid = (gid_t) strtoul(param, &endp, 10);
1711
1712 if (radius_passwd.pw_gid == (gid_t) -1) {
1713 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1714 "illegal RadiusUserInfo GID value: -1 not allowed");
1715 have_illegal_value = TRUE;
1716 }
1717
1718 if (endp && *endp) {
1719 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1720 "illegal RadiusUserInfo GID value: '%s' not a number", param);
1721 have_illegal_value = TRUE;
1722 }
1723 }
1724
1725 /* Parse the home directory string. */
1726 param = (char *) c->argv[2];
1727
1728 if (RADIUS_IS_VAR(param) == TRUE) {
1729 radius_parse_var(param, &radius_home_attr_id, &radius_passwd.pw_dir);
1730
1731 if (*radius_passwd.pw_dir != '/') {
1732 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1733 "illegal RadiusUserInfo default home value: '%s' not an absolute path",
1734 radius_passwd.pw_dir);
1735 have_illegal_value = TRUE;
1736 }
1737
1738 } else {
1739
1740 /* Param already checked in this case. */
1741 radius_passwd.pw_dir = param;
1742 }
1743
1744 /* Process the shell string. */
1745 param = (char *) c->argv[3];
1746
1747 if (RADIUS_IS_VAR(param) == TRUE) {
1748 radius_parse_var(param, &radius_shell_attr_id, &radius_passwd.pw_shell);
1749
1750 if (*radius_passwd.pw_shell != '/') {
1751 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1752 "illegal RadiusUserInfo default shell value: '%s' not an absolute path",
1753 radius_passwd.pw_shell);
1754 have_illegal_value = TRUE;
1755 }
1756
1757 } else {
1758
1759 /* Param already checked in this case. */
1760 radius_passwd.pw_shell = param;
1761 }
1762
1763 if (have_illegal_value == FALSE) {
1764 radius_have_user_info = TRUE;
1765
1766 } else {
1767 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
1768 "error with RadiusUserInfo parameters, ignoring them");
1769 }
1770 }
1771
1772 static void radius_reset(void) {
1773 /* Clear/reset user info */
1774 radius_have_user_info = FALSE;
1775
1776 /* Clear/reset group info */
1777 radius_have_group_info = FALSE;
1778 radius_prime_group_name = NULL;
1779 radius_addl_group_count = 0;
1780 radius_getgroups_count = 0;
1781 radius_addl_group_names = NULL;
1782 radius_addl_group_names_str = NULL;
1783 radius_addl_group_ids = NULL;
1784 radius_addl_group_ids_str = NULL;
1785
1786 /* Clear/reset quota info */
1787 radius_have_quota_info = FALSE;
1788 radius_quota_per_sess = NULL;
1789 radius_quota_limit_type = NULL;
1790 radius_quota_bytes_in = NULL;
1791 radius_quota_bytes_out = NULL;
1792 radius_quota_bytes_xfer = NULL;
1793 radius_quota_files_in = NULL;
1794 radius_quota_files_out = NULL;
1795
1796 /* Clear/reset quota info */
1797 radius_have_quota_info = FALSE;
1798 radius_quota_per_sess = NULL;
1799 radius_quota_limit_type = NULL;
1800 radius_quota_bytes_in = NULL;
1801 radius_quota_bytes_out = NULL;
1802 radius_quota_bytes_xfer = NULL;
1803 radius_quota_files_in = NULL;
1804 radius_quota_files_out = NULL;
1805 radius_quota_files_xfer = NULL;
1806
1807 /* Clear/reset other info */
1808 radius_have_other_info = FALSE;
1809 }
1810
1811 static unsigned char *radius_xor(unsigned char *p, unsigned char *q,
1812 size_t len) {
1813 register size_t i = 0;
1814 unsigned char *tmp = p;
1815
1816 for (i = 0; i < len; i++) {
1817 *(p++) ^= *(q++);
1818 }
1819
1820 return tmp;
1821 }
1822
1823 #if defined(PR_USE_OPENSSL)
1824 # include <openssl/err.h>
1825 # include <openssl/md5.h>
1826 # include <openssl/hmac.h>
1827
1828 #else
1829 /* Built-in MD5 */
1830
1831 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991.
1832 * All rights reserved.
1833 *
1834 * License to copy and use this software is granted provided that it
1835 * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
1836 * Algorithm" in all material mentioning or referencing this software
1837 * or this function.
1838 *
1839 * License is also granted to make and use derivative works provided
1840 * that such works are identified as "derived from the RSA Data
1841 * Security, Inc. MD5 Message-Digest Algorithm" in all material
1842 * mentioning or referencing the derived work.
1843 *
1844 * RSA Data Security, Inc. makes no representations concerning either
1845 * the merchantability of this software or the suitability of this
1846 * software for any particular purpose. It is provided "as is"
1847 * without express or implied warranty of any kind.
1848 *
1849 * These notices must be retained in any copies of any part of this
1850 * documentation and/or software.
1851 */
1852
1853 /* MD5 context */
1854 typedef struct {
1855
1856 /* state (ABCD) */
1857 uint32_t state[4];
1858
1859 /* number of bits, module 2^64 (LSB first) */
1860 uint32_t count[2];
1861
1862 /* input buffer */
1863 unsigned char buffer[64];
1864 } MD5_CTX;
1865
1866 static void MD5_Init(MD5_CTX *);
1867 static void MD5_Update(MD5_CTX *, unsigned char *, size_t);
1868 static void MD5_Final(unsigned char *, MD5_CTX *);
1869
1870 /* Note: these MD5 routines are taken from RFC 1321 */
1871
1872 #ifdef HAVE_MEMCPY
1873 # define MD5_memcpy(a, b, c) memcpy((a), (b), (c))
1874 # define MD5_memset(a, b, c) memset((a), (b), (c))
1875 #endif
1876
1877 /* Constants for MD5Transform routine.
1878 */
1879
1880 #define S11 7
1881 #define S12 12
1882 #define S13 17
1883 #define S14 22
1884 #define S21 5
1885 #define S22 9
1886 #define S23 14
1887 #define S24 20
1888 #define S31 4
1889 #define S32 11
1890 #define S33 16
1891 #define S34 23
1892 #define S41 6
1893 #define S42 10
1894 #define S43 15
1895 #define S44 21
1896
1897 static void MD5Transform(uint32_t *, unsigned char[64]);
1898 static void Encode(unsigned char *, uint32_t *, unsigned int);
1899 static void Decode(uint32_t *, unsigned char *, unsigned int);
1900
1901 #ifndef HAVE_MEMCPY
1902 static void MD5_memcpy(unsigned char *, unsigned char *, unsigned int);
1903 static void MD5_memset(unsigned char *, int, unsigned int);
1904 #endif
1905
1906 static unsigned char PADDING[64] = {
1907 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1908 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1909 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1910 };
1911
1912 /* F, G, H and I are basic MD5 functions.
1913 */
1914 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1915 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1916 #define H(x, y, z) ((x) ^ (y) ^ (z))
1917 #define I(x, y, z) ((y) ^ ((x) | (~z)))
1918
1919 /* ROTATE_LEFT rotates x left n bits.
1920 */
1921 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
1922
1923 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
1924 * Rotation is separate from addition to prevent recomputation.
1925 */
1926 #define FF(a, b, c, d, x, s, ac) { \
1927 (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
1928 (a) = ROTATE_LEFT ((a), (s)); \
1929 (a) += (b); \
1930 }
1931 #define GG(a, b, c, d, x, s, ac) { \
1932 (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
1933 (a) = ROTATE_LEFT ((a), (s)); \
1934 (a) += (b); \
1935 }
1936 #define HH(a, b, c, d, x, s, ac) { \
1937 (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
1938 (a) = ROTATE_LEFT ((a), (s)); \
1939 (a) += (b); \
1940 }
1941 #define II(a, b, c, d, x, s, ac) { \
1942 (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
1943 (a) = ROTATE_LEFT ((a), (s)); \
1944 (a) += (b); \
1945 }
1946
1947 /* MD5 initialization. Begins an MD5 operation, writing a new context.
1948 */
1949 static void MD5_Init(MD5_CTX *context) {
1950 context->count[0] = context->count[1] = 0;
1951
1952 /* Load magic initialization constants.
1953 */
1954 context->state[0] = 0x67452301;
1955 context->state[1] = 0xefcdab89;
1956 context->state[2] = 0x98badcfe;
1957 context->state[3] = 0x10325476;
1958 }
1959
1960 /* MD5 block update operation. Continues an MD5 message-digest
1961 * operation, processing another message block, and updating the
1962 * context.
1963 */
1964 static void MD5_Update(MD5_CTX *context, unsigned char *input,
1965 size_t inputLen) {
1966 unsigned int i, index, partLen;
1967
1968 /* Compute number of bytes mod 64 */
1969 index = (unsigned int)((context->count[0] >> 3) & 0x3F);
1970
1971 /* Update number of bits */
1972 if ((context->count[0] += ((uint32_t)inputLen << 3))
1973 < ((uint32_t)inputLen << 3))
1974 context->count[1]++;
1975 context->count[1] += ((uint32_t)inputLen >> 29);
1976
1977 partLen = 64 - index;
1978
1979 /* Transform as many times as possible */
1980 if (inputLen >= partLen) {
1981 MD5_memcpy((unsigned char *) &context->buffer[index],
1982 (unsigned char *) input, partLen);
1983 MD5Transform(context->state, context->buffer);
1984
1985 for (i = partLen; i + 63 < inputLen; i += 64)
1986 MD5Transform(context->state, &input[i]);
1987
1988 index = 0;
1989
1990 } else
1991 i = 0;
1992
1993 /* Buffer remaining input */
1994 MD5_memcpy((unsigned char *) &context->buffer[index],
1995 (unsigned char *) &input[i], inputLen-i);
1996 }
1997
1998 /* MD5 finalization. Ends an MD5 message-digest operation, writing the
1999 * the message digest and zeroizing the context.
2000 */
2001 static void MD5_Final(unsigned char digest[16], MD5_CTX *context) {
2002 unsigned char bits[8];
2003 unsigned int index;
2004 size_t padLen;
2005
2006 /* Save number of bits */
2007 Encode (bits, context->count, 8);
2008
2009 /* Pad out to 56 mod 64.
2010 */
2011 index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
2012 padLen = (index < 56) ? (56 - index) : (120 - index);
2013 MD5_Update(context, PADDING, padLen);
2014
2015 /* Append length (before padding) */
2016 MD5_Update(context, bits, 8);
2017
2018 /* Store state in digest */
2019 Encode(digest, context->state, 16);
2020
2021 /* Zeroize sensitive information.
2022 */
2023 MD5_memset((unsigned char *) context, 0, sizeof(*context));
2024 }
2025
2026 /* MD5 basic transformation. Transforms state based on block.
2027 */
2028 static void MD5Transform(uint32_t state[4], unsigned char block[64]) {
2029 uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
2030
2031 Decode(x, block, 64);
2032
2033 /* Round 1 */
2034 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
2035 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
2036 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
2037 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
2038 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
2039 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
2040 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
2041 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
2042 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
2043 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
2044 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
2045 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
2046 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
2047 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
2048 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
2049 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
2050
2051 /* Round 2 */
2052 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
2053 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
2054 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
2055 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
2056 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
2057 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
2058 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
2059 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
2060 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
2061 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
2062 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
2063
2064 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
2065 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
2066 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
2067 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
2068 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
2069
2070 /* Round 3 */
2071 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
2072 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
2073 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
2074 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
2075 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
2076 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
2077 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
2078 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
2079 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
2080 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
2081 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
2082 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
2083 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
2084 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
2085 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
2086 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
2087
2088 /* Round 4 */
2089 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
2090 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
2091 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
2092 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
2093 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
2094 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
2095 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
2096 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
2097 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
2098 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
2099 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
2100 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
2101 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
2102 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
2103 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
2104 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
2105
2106 state[0] += a;
2107 state[1] += b;
2108 state[2] += c;
2109 state[3] += d;
2110
2111 /* Zeroize sensitive information.
2112 */
2113 MD5_memset((unsigned char *) x, 0, sizeof(x));
2114 }
2115
2116 /* Encodes input (unsigned long) into output (unsigned char). Assumes len is
2117 * a multiple of 4.
2118 */
2119 static void Encode(unsigned char *output, uint32_t *input, unsigned int len) {
2120 unsigned int i, j;
2121
2122 for (i = 0, j = 0; j < len; i++, j += 4) {
2123 output[j] = (unsigned char)(input[i] & 0xff);
2124 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
2125 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
2126 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
2127 }
2128 }
2129
2130 /* Decodes input (unsigned char) into output (unsigned long). Assumes len is
2131 * a multiple of 4.
2132 */
2133 static void Decode(uint32_t *output, unsigned char *input, unsigned int len) {
2134 unsigned int i, j;
2135
2136 for (i = 0, j = 0; j < len; i++, j += 4)
2137 output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
2138 (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
2139 }
2140
2141 #ifndef HAVE_MEMCPY
2142 /* Note: Replace "for loop" with standard memcpy if possible. */
2143 static void MD5_memcpy(unsigned char *output, unsigned char *input,
2144 unsigned int len) {
2145 unsigned int i;
2146
2147 for (i = 0; i < len; i++) {
2148 output[i] = input[i];
2149 }
2150 }
2151
2152 /* Note: Replace "for loop" with standard memset if possible. */
2153 static void MD5_memset(unsigned char *output, int value, unsigned int len) {
2154 unsigned int i;
2155
2156 for (i = 0; i < len; i++) {
2157 ((char *) output)[i] = (char) value;
2158 }
2159 }
2160 #endif
2161 #endif /* !PR_USE_OPENSSL */
2162
2163 static int radius_openlog(void) {
2164 int res = 0, xerrno = 0;
2165 config_rec *c;
2166 const char *path;
2167
2168 c = find_config(main_server->conf, CONF_PARAM, "RadiusLog", FALSE);
2169 if (c == NULL) {
2170 return 0;
2171 }
2172
2173 path = c->argv[0];
2174 if (strcasecmp(path, "none") == 0) {
2175 return 0;
2176 }
2177
2178 pr_signals_block();
2179 PRIVS_ROOT
2180 res = pr_log_openfile(path, &radius_logfd, PR_LOG_SYSTEM_MODE);
2181 xerrno = errno;
2182 PRIVS_RELINQUISH
2183 pr_signals_unblock();
2184
2185 errno = xerrno;
2186 return res;
2187 }
2188
2189 /* RADIUS routines */
2190
2191 /* Add an attribute to a RADIUS packet. Returns the added attribute. */
2192 static radius_attrib_t *radius_add_attrib(radius_packet_t *packet,
2193 unsigned char type, const unsigned char *data, size_t datalen) {
2194 radius_attrib_t *attrib = NULL;
2195
2196 attrib = (radius_attrib_t *) ((unsigned char *) packet +
2197 ntohs(packet->length));
2198 attrib->type = type;
2199
2200 /* Total size of the attribute. The "+ 2" takes into account the size
2201 * of the attribute identifier.
2202 */
2203 attrib->length = datalen + 2;
2204
2205 /* Increment the size of the given packet. */
2206 packet->length = htons(ntohs(packet->length) + attrib->length);
2207
2208 memcpy(attrib->data, data, datalen);
2209
2210 return attrib;
2211 }
2212
2213 /* Add a RADIUS message authenticator attribute to the packet. */
2214 static void radius_set_auth_mac(radius_packet_t *pkt,
2215 const unsigned char *secret, size_t secret_len) {
2216 #ifdef PR_USE_OPENSSL
2217 const EVP_MD *md;
2218 unsigned char digest[EVP_MAX_MD_SIZE];
2219 unsigned int digest_len = 0, mac_len = 16;
2220 radius_attrib_t *attrib = NULL;
2221
2222 /* First, add the Message-Authenticator attribute, with a value of all zeroes,
2223 * per RFC 3579, Section 3.2.
2224 */
2225 memset(digest, '\0', sizeof(digest));
2226 attrib = radius_add_attrib(pkt, RADIUS_MESSAGE_AUTHENTICATOR,
2227 (const unsigned char *) digest, mac_len);
2228
2229 /* Now, calculate the HMAC-MD5 of the packet. */
2230
2231 md = EVP_md5();
2232 if (HMAC(md, secret, secret_len, (unsigned char *) pkt, ntohs(pkt->length),
2233 digest, &digest_len) == NULL) {
2234 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2235 "error generating Message-Authenticator: %s",
2236 ERR_error_string(ERR_get_error(), NULL));
2237
2238 } else {
2239 /* Finally, overwrite the all-zeroes Message-Authenticator value with our
2240 * calculated value.
2241 */
2242 memcpy(attrib->data, digest, mac_len);
2243 }
2244 #endif /* PR_USE_OPENSSL */
2245 }
2246
2247 static int radius_verify_auth_mac(radius_packet_t *pkt, const char *pkt_type,
2248 const unsigned char *secret, size_t secret_len) {
2249 int res = 0;
2250 radius_attrib_t *attrib = NULL;
2251
2252 /* Handle any Message-Authenticator attribute, per RFC 2869, Section 5.14. */
2253 attrib = radius_get_attrib(pkt, RADIUS_MESSAGE_AUTHENTICATOR);
2254 if (attrib != NULL) {
2255 unsigned char attrib_len;
2256 unsigned int expected_len = 16;
2257
2258 attrib_len = RADIUS_ATTRIB_LEN(attrib);
2259 if (attrib_len != expected_len) {
2260 #if defined(PR_USE_OPENSSL)
2261 const EVP_MD *md;
2262 unsigned char digest[EVP_MAX_MD_SIZE], replied[EVP_MAX_MD_SIZE];
2263 unsigned int digest_len = 0;
2264
2265 /* First, make a copy of the packet's Message-Authenticator value, for
2266 * comparison with what we will calculate.
2267 */
2268 memset(replied, '\0', sizeof(replied));
2269
2270 /* Make sure that the given attribute length does not exceed our
2271 * allocated buffer size. Well-formed MAC attribute lengths should
2272 * not encounter this case, given the use of MD5; excessively long
2273 * MACs would fail verification anyway, truncation or not.
2274 */
2275 if (attrib_len > sizeof(replied)) {
2276 pr_trace_msg(trace_channel, 3,
2277 "Message-Authenticator attribute length (%u) exceeded max expected "
2278 "length (%u), truncating", (unsigned int) attrib_len,
2279 (unsigned int) sizeof(replied));
2280 attrib_len = sizeof(replied);
2281 }
2282
2283 memcpy(replied, attrib->data, attrib_len);
2284
2285 /* Next, zero out the value so that we can calculate it ourselves.
2286 *
2287 * Note that we only want to zero out the first 16 bytes, per RFC 2869.
2288 */
2289 memset(attrib->data, '\0', expected_len);
2290
2291 memset(digest, '\0', sizeof(digest));
2292 md = EVP_md5();
2293 if (HMAC(md, secret, secret_len, (unsigned char *) pkt,
2294 ntohs(pkt->length), digest, &digest_len) == NULL) {
2295 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2296 "error generating Message-Authenticator: %s",
2297 ERR_error_string(ERR_get_error(), NULL));
2298 return 0;
2299 }
2300
2301 if (memcmp(replied, digest, expected_len) != 0) {
2302 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2303 "packet Message-Authenticator verification failed: mismatched MACs");
2304 errno = EINVAL;
2305 return -1;
2306 }
2307
2308 res = 0;
2309
2310 #endif /* PR_USE_OPENSSL */
2311 } else {
2312 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2313 "%s packet has incorrect Message-Authenticator attribute length "
2314 "(%u != %u), rejecting", pkt_type, attrib_len, expected_len);
2315 errno = EINVAL;
2316 return -1;
2317 }
2318
2319 } else {
2320 pr_trace_msg(trace_channel, 6,
2321 "%s packet lacks Message-Authenticator attribute (%d)", pkt_type,
2322 RADIUS_MESSAGE_AUTHENTICATOR);
2323
2324 if (radius_opts & RADIUS_OPT_REQUIRE_MAC) {
2325 errno = EPERM;
2326 return -1;
2327 }
2328 }
2329
2330 return res;
2331 }
2332
2333 /* Add a RADIUS password attribute to the packet. */
2334 static void radius_add_passwd(radius_packet_t *packet, unsigned char type,
2335 const unsigned char *passwd, unsigned char *secret, size_t secret_len) {
2336 MD5_CTX ctx, secret_ctx;
2337 radius_attrib_t *attrib = NULL;
2338 unsigned char calculated[RADIUS_VECTOR_LEN];
2339 unsigned char pwhash[PR_TUNABLE_BUFFER_SIZE];
2340 unsigned char *digest = NULL;
2341 register unsigned int i = 0;
2342 size_t pwlen;
2343
2344 pwlen = strlen((const char *) passwd);
2345
2346 /* Clear the buffers. */
2347 memset(pwhash, '\0', sizeof(pwhash));
2348
2349 if (pwlen == 0) {
2350 pwlen = RADIUS_PASSWD_LEN;
2351
2352 } else if ((pwlen & (RADIUS_PASSWD_LEN - 1)) != 0) {
2353 /* pwlen is not a multiple of RADIUS_PASSWD_LEN, need to prepare a proper
2354 * buffer.
2355 */
2356 memcpy(pwhash, passwd, pwlen);
2357
2358 /* Round up the length. */
2359 pwlen += (RADIUS_PASSWD_LEN - 1);
2360
2361 /* Truncate the length, as necessary. */
2362 pwlen &= ~(RADIUS_PASSWD_LEN - 1);
2363
2364 } else {
2365 /* pwlen is a multiple of RADIUS_PASSWD_LEN, we can just use it. */
2366 memcpy(pwhash, passwd, pwlen);
2367 }
2368
2369 /* Find the password attribute. */
2370 attrib = radius_get_attrib(packet, RADIUS_PASSWORD);
2371
2372 if (type == RADIUS_PASSWORD) {
2373 digest = packet->digest;
2374
2375 } else {
2376 digest = attrib->data;
2377 }
2378
2379 /* Encrypt the password. Password: c[0] = p[0] ^ MD5(secret + digest) */
2380 MD5_Init(&secret_ctx);
2381 MD5_Update(&secret_ctx, secret, secret_len);
2382
2383 /* Save this hash for later. */
2384 ctx = secret_ctx;
2385
2386 MD5_Update(&ctx, digest, RADIUS_VECTOR_LEN);
2387
2388 /* Set the calculated digest. */
2389 MD5_Final(calculated, &ctx);
2390
2391 /* XOR the results. */
2392 radius_xor(pwhash, calculated, RADIUS_PASSWD_LEN);
2393
2394 /* For each step through: e[i] = p[i] ^ MD5(secret + e[i-1]) */
2395 for (i = 1; i < (pwlen >> 4); i++) {
2396
2397 /* Start with the old value of the MD5 sum. */
2398 ctx = secret_ctx;
2399
2400 MD5_Update(&ctx, &pwhash[(i-1) * RADIUS_PASSWD_LEN], RADIUS_PASSWD_LEN);
2401
2402 /* Set the calculated digest. */
2403 MD5_Final(calculated, &ctx);
2404
2405 /* XOR the results. */
2406 radius_xor(&pwhash[i * RADIUS_PASSWD_LEN], calculated, RADIUS_PASSWD_LEN);
2407 }
2408
2409 if (type == RADIUS_OLD_PASSWORD) {
2410 attrib = radius_get_attrib(packet, RADIUS_OLD_PASSWORD);
2411 }
2412
2413 if (attrib == NULL) {
2414 radius_add_attrib(packet, type, pwhash, pwlen);
2415
2416 } else {
2417 /* Overwrite the packet data. */
2418 memcpy(attrib->data, pwhash, pwlen);
2419 }
2420
2421 pr_memscrub(pwhash, sizeof(pwhash));
2422 }
2423
2424 static void radius_set_acct_digest(radius_packet_t *packet,
2425 const unsigned char *secret, size_t secret_len) {
2426 MD5_CTX ctx;
2427
2428 /* Clear the current digest (not needed yet for accounting packets) */
2429 memset(packet->digest, 0, RADIUS_VECTOR_LEN);
2430
2431 MD5_Init(&ctx);
2432
2433 /* Add the packet data to the mix. */
2434 MD5_Update(&ctx, (unsigned char *) packet, ntohs(packet->length));
2435
2436 /* Add the secret to the mix. */
2437 MD5_Update(&ctx, secret, secret_len);
2438
2439 /* Set the calculated digest in place in the packet. */
2440 MD5_Final(packet->digest, &ctx);
2441 }
2442
2443 /* Obtain a random digest. */
2444 static void radius_get_rnd_digest(radius_packet_t *packet) {
2445 MD5_CTX ctx;
2446 struct timeval tv;
2447 struct timezone tz;
2448
2449 /* Use the time of day with the best resolution the system can give us,
2450 * often close to microsecond accuracy.
2451 */
2452 gettimeofday(&tv, &tz);
2453
2454 /* Add in some (possibly) hard to guess information. */
2455 tv.tv_sec ^= (long) (getpid() * getppid());
2456
2457 /* Use MD5 to obtain (hopefully) cryptographically strong pseudo-random
2458 * numbers
2459 */
2460 MD5_Init(&ctx);
2461 MD5_Update(&ctx, (unsigned char *) &tv, sizeof(tv));
2462 MD5_Update(&ctx, (unsigned char *) &tz, sizeof(tz));
2463
2464 /* Set the calculated digest in the space provided. */
2465 MD5_Final(packet->digest, &ctx);
2466 }
2467
2468 /* RADIUS packet manipulation functions.
2469 */
2470
2471 /* For iterating through all of the attributes in a packet, callers can
2472 * provide a pointer to the previous attribute returned, or NULL.
2473 */
2474 static radius_attrib_t *radius_get_next_attrib(radius_packet_t *packet,
2475 unsigned char attrib_type, unsigned int *packet_len,
2476 radius_attrib_t *prev_attrib) {
2477 radius_attrib_t *attrib = NULL;
2478 unsigned int len;
2479
2480 if (packet_len == NULL ||
2481 *packet_len == 0) {
2482 len = ntohs(packet->length) - RADIUS_HEADER_LEN;
2483
2484 } else {
2485 len = *packet_len;
2486 }
2487
2488 if (prev_attrib == NULL) {
2489 attrib = (radius_attrib_t *) &packet->data;
2490
2491 } else {
2492 attrib = prev_attrib;
2493 }
2494
2495 while (attrib->type != attrib_type) {
2496 if (attrib->length == 0 ||
2497 (len -= attrib->length) <= 0) {
2498
2499 /* Requested attribute not found. */
2500 if (packet_len != NULL) {
2501 *packet_len = 0;
2502 }
2503
2504 return NULL;
2505 }
2506
2507 /* Examine the next attribute in the packet. */
2508 attrib = (radius_attrib_t *) ((char *) attrib + attrib->length);
2509 }
2510
2511 if (attrib == prev_attrib) {
2512 /* No next attribute of this type found. */
2513 return NULL;
2514 }
2515
2516 if (packet_len != NULL) {
2517 *packet_len = len;
2518 }
2519
2520 return attrib;
2521 }
2522
2523 static radius_attrib_t *radius_get_attrib(radius_packet_t *packet,
2524 unsigned char attrib_type) {
2525 return radius_get_next_attrib(packet, attrib_type, NULL, NULL);
2526 }
2527
2528 /* Find a Vendor-Specific Attribute (VSA) in a RADIUS packet. Note that
2529 * the packet length is always kept in network byte order.
2530 */
2531 static radius_attrib_t *radius_get_vendor_attrib(radius_packet_t *packet,
2532 unsigned char type) {
2533 radius_attrib_t *attrib = (radius_attrib_t *) &packet->data;
2534
2535 while (attrib != NULL) {
2536 unsigned int vendor_id = 0;
2537
2538 pr_signals_handle();
2539
2540 if (attrib->length == 0) {
2541 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2542 "packet includes invalid length (%u) for attribute type %u, rejecting",
2543 attrib->length, attrib->type);
2544 return NULL;
2545 }
2546
2547 if (attrib->type != RADIUS_VENDOR_SPECIFIC) {
2548 attrib = (radius_attrib_t *) ((char *) attrib + attrib->length);
2549 continue;
2550 }
2551
2552 /* The first four octets (bytes) of data will contain the Vendor-Id. */
2553 if (attrib->length >= 4) {
2554 memcpy(&vendor_id, attrib->data, 4);
2555 vendor_id = ntohl(vendor_id);
2556 }
2557
2558 if (vendor_id != radius_vendor_id) {
2559 attrib = (radius_attrib_t *) ((char *) attrib + attrib->length);
2560 continue;
2561 }
2562
2563 /* Parse the data value for this attribute into a VSA structure. */
2564 if (attrib->length > 4) {
2565 radius_attrib_t *vsa = NULL;
2566
2567 vsa = (radius_attrib_t *) ((char *) attrib->data + sizeof(int));
2568
2569 /* Does this VSA have the type requested? */
2570 if (vsa->type != type) {
2571 attrib = (radius_attrib_t *) ((char *) attrib + attrib->length);
2572 continue;
2573 }
2574
2575 return vsa;
2576 }
2577 }
2578
2579 return NULL;
2580 }
2581
2582 /* Build a RADIUS packet, initializing some of the header and adding
2583 * common attributes.
2584 */
2585 static void radius_build_packet(radius_packet_t *packet,
2586 const unsigned char *user, const unsigned char *passwd,
2587 unsigned char *secret, size_t secret_len) {
2588 unsigned int nas_port_type = htonl(RADIUS_NAS_PORT_TYPE_VIRTUAL);
2589 int nas_port = htonl(main_server->ServerPort);
2590 char *caller_id = NULL;
2591 const char *nas_identifier = NULL;
2592 size_t userlen;
2593
2594 /* Set the packet length. */
2595 packet->length = htons(RADIUS_HEADER_LEN);
2596
2597 /* Obtain a random digest. */
2598 radius_get_rnd_digest(packet);
2599
2600 /* Set the ID for the packet. */
2601 packet->id = packet->digest[0];
2602
2603 /* Add the user attribute. */
2604 userlen = strlen((const char *) user);
2605 radius_add_attrib(packet, RADIUS_USER_NAME, user, userlen);
2606
2607 /* Add the password attribute, if given. */
2608 if (passwd) {
2609 radius_add_passwd(packet, RADIUS_PASSWORD, passwd, secret, secret_len);
2610
2611 } else if (packet->code != RADIUS_ACCT_REQUEST) {
2612 /* Add a NULL password if necessary. */
2613 radius_add_passwd(packet, RADIUS_PASSWORD, (const unsigned char *) "",
2614 secret, 1);
2615 }
2616
2617 /* Add a NAS identifier attribute of the service name, e.g. 'ftp'. */
2618
2619 nas_identifier = pr_session_get_protocol(0);
2620 if (radius_nas_identifier_config != NULL) {
2621 nas_identifier = radius_nas_identifier_config;
2622 }
2623
2624 radius_add_attrib(packet, RADIUS_NAS_IDENTIFIER,
2625 (const unsigned char *) nas_identifier,
2626 strlen((const char *) nas_identifier));
2627
2628 #ifdef PR_USE_IPV6
2629 if (pr_netaddr_use_ipv6()) {
2630 const pr_netaddr_t *local_addr;
2631 int family;
2632
2633 local_addr = pr_netaddr_get_sess_local_addr();
2634 family = pr_netaddr_get_family(local_addr);
2635
2636 switch (family) {
2637 case AF_INET: {
2638 struct in_addr *inaddr;
2639
2640 inaddr = pr_netaddr_get_inaddr(local_addr);
2641
2642 /* Add a NAS-IP-Address attribute. */
2643 radius_add_attrib(packet, RADIUS_NAS_IP_ADDRESS,
2644 (unsigned char *) &(inaddr->s_addr), sizeof(inaddr->s_addr));
2645 break;
2646 }
2647
2648 case AF_INET6: {
2649 if (pr_netaddr_is_v4mappedv6(local_addr)) {
2650 pr_netaddr_t *v4_addr;
2651
2652 /* Note: in the future, switch to using a per-packet pool. */
2653 v4_addr = pr_netaddr_v6tov4(radius_pool, local_addr);
2654 if (v4_addr != NULL) {
2655 struct in_addr *inaddr;
2656
2657 inaddr = pr_netaddr_get_inaddr(v4_addr);
2658
2659 /* Add a NAS-IP-Address attribute. */
2660 radius_add_attrib(packet, RADIUS_NAS_IP_ADDRESS,
2661 (unsigned char *) &(inaddr->s_addr), sizeof(inaddr->s_addr));
2662
2663 } else {
2664 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2665 "error converting '%s' to IPv4 address: %s",
2666 pr_netaddr_get_ipstr(local_addr), strerror(errno));
2667 }
2668
2669 } else {
2670 struct in6_addr *inaddr;
2671 uint32_t ipv6_addr[4];
2672
2673 inaddr = pr_netaddr_get_inaddr(pr_netaddr_get_sess_local_addr());
2674
2675 /* Ideally we would use the inaddr->s6_addr32 to get to the 128-bit
2676 * IPv6 address. But `s6_addr32' turns out to be a macro that is not
2677 * available on all systems (FreeBSD, for example, does not provide
2678 * this macro unless you're building its kernel).
2679 *
2680 * As a workaround, try using the (hopefully) more portable s6_addr
2681 * macro.
2682 */
2683 memcpy(ipv6_addr, inaddr->s6_addr, sizeof(ipv6_addr));
2684
2685 /* Add a NAS-IPv6-Address attribute. */
2686 radius_add_attrib(packet, RADIUS_NAS_IPV6_ADDRESS,
2687 (unsigned char *) ipv6_addr, sizeof(ipv6_addr));
2688 }
2689
2690 break;
2691 }
2692 }
2693
2694 } else {
2695 #else
2696 if (TRUE) {
2697 #endif /* PR_USE_IPV6 */
2698 struct in_addr *inaddr;
2699
2700 inaddr = pr_netaddr_get_inaddr(pr_netaddr_get_sess_local_addr());
2701
2702 /* Add a NAS-IP-Address attribute. */
2703 radius_add_attrib(packet, RADIUS_NAS_IP_ADDRESS,
2704 (unsigned char *) &(inaddr->s_addr), sizeof(inaddr->s_addr));
2705 }
2706
2707 /* Add a NAS port attribute. */
2708 radius_add_attrib(packet, RADIUS_NAS_PORT, (unsigned char *) &nas_port,
2709 sizeof(int));
2710
2711 /* Add a NAS port type attribute. */
2712 radius_add_attrib(packet, RADIUS_NAS_PORT_TYPE,
2713 (unsigned char *) &nas_port_type, sizeof(int));
2714
2715 /* Add the calling station ID attribute (this is the IP of the connecting
2716 * client).
2717 */
2718 caller_id = (char *) pr_netaddr_get_ipstr(pr_netaddr_get_sess_remote_addr());
2719
2720 radius_add_attrib(packet, RADIUS_CALLING_STATION_ID,
2721 (const unsigned char *) caller_id, strlen(caller_id));
2722 }
2723
2724 static radius_server_t *radius_make_server(pool *parent_pool) {
2725 pool *server_pool = NULL;
2726 radius_server_t *server = NULL;
2727
2728 /* sanity check */
2729 if (!parent_pool)
2730 return NULL;
2731
2732 /* allocate a subpool for the new rec */
2733 server_pool = make_sub_pool(parent_pool);
2734
2735 /* allocate the rec from the subpool */
2736 server = (radius_server_t *) pcalloc(server_pool,
2737 sizeof(radius_server_t));
2738
2739 /* set the members to sane default values */
2740 server->pool = server_pool;
2741 server->addr = NULL;
2742 server->port = RADIUS_AUTH_PORT;
2743 server->secret = NULL;
2744 server->secret_len = 0;
2745 server->timeout = DEFAULT_RADIUS_TIMEOUT;
2746 server->next = NULL;
2747
2748 return server;
2749 }
2750
2751 static int radius_open_socket(void) {
2752 int sockfd = -1;
2753 struct sockaddr_in *radius_sockaddr_in = NULL;
2754 unsigned short local_port = 0;
2755
2756 /* Obtain a socket descriptor. */
2757 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
2758 if (sockfd < 0) {
2759 int xerrno = errno;
2760
2761 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2762 "notice: unable to open socket for communication: %s",
2763 strerror(xerrno));
2764
2765 errno = xerrno;
2766 return -1;
2767 }
2768
2769 /* Set the members appropriately to bind to the descriptor. */
2770 memset((void *) &radius_local_sock, 0, sizeof(radius_local_sock));
2771 radius_sockaddr_in = (struct sockaddr_in *) &radius_local_sock;
2772 radius_sockaddr_in->sin_family = AF_INET;
2773 radius_sockaddr_in->sin_addr.s_addr = INADDR_ANY;
2774
2775 /* Use our process ID as a local port for RADIUS.
2776 */
2777 local_port = (getpid() & 0x7fff) + 1024;
2778 do {
2779 pr_signals_handle();
2780
2781 local_port++;
2782 radius_sockaddr_in->sin_port = htons(local_port);
2783
2784 } while ((bind(sockfd, &radius_local_sock, sizeof(radius_local_sock)) < 0) &&
2785 (local_port < USHRT_MAX));
2786
2787 if (local_port >= USHRT_MAX) {
2788 (void) close(sockfd);
2789 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2790 "notice: unable to bind to socket: no open local ports");
2791 errno = EPERM;
2792 return -1;
2793 }
2794
2795 /* Done */
2796 return sockfd;
2797 }
2798
2799 static radius_packet_t *radius_recv_packet(int sockfd, unsigned int timeout) {
2800 static unsigned char recvbuf[RADIUS_PACKET_LEN];
2801 radius_packet_t *packet = NULL;
2802 int res = 0, recvlen = -1;
2803 socklen_t sockaddrlen = sizeof(struct sockaddr);
2804 struct timeval tv;
2805 fd_set rset;
2806
2807 /* receive the response, waiting as necessary */
2808 memset(recvbuf, '\0', sizeof(recvbuf));
2809
2810 tv.tv_sec = timeout;
2811 tv.tv_usec = 0;
2812
2813 FD_ZERO(&rset);
2814 FD_SET(sockfd, &rset);
2815
2816 res = select(sockfd + 1, &rset, NULL, NULL, &tv);
2817 if (res == 0) {
2818 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2819 "server failed to respond in %u seconds", timeout);
2820 return NULL;
2821
2822 } else if (res < 0) {
2823 int xerrno = errno;
2824
2825 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2826 "error: unable to receive response: %s", strerror(xerrno));
2827
2828 errno = xerrno;
2829 return NULL;
2830 }
2831
2832 recvlen = recvfrom(sockfd, (char *) recvbuf, RADIUS_PACKET_LEN, 0,
2833 &radius_remote_sock, &sockaddrlen);
2834 if (recvlen < 0) {
2835 int xerrno = errno;
2836
2837 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2838 "error reading packet: %s", strerror(xerrno));
2839
2840 errno = xerrno;
2841 return NULL;
2842 }
2843
2844 packet = (radius_packet_t *) recvbuf;
2845
2846 /* Make sure the packet is of valid length. */
2847 if (ntohs(packet->length) != recvlen ||
2848 ntohs(packet->length) > RADIUS_PACKET_LEN) {
2849 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2850 "received corrupted packet");
2851 return NULL;
2852 }
2853
2854 return packet;
2855 }
2856
2857 static int radius_send_packet(int sockfd, radius_packet_t *packet,
2858 radius_server_t *server) {
2859 int res;
2860 struct sockaddr_in *radius_sockaddr_in =
2861 (struct sockaddr_in *) &radius_remote_sock;
2862
2863 /* Set the members appropriately to send to the descriptor. */
2864 memset((void *) &radius_remote_sock, '\0', sizeof(radius_remote_sock));
2865 radius_sockaddr_in->sin_family = AF_INET;
2866 radius_sockaddr_in->sin_addr.s_addr = pr_netaddr_get_addrno(server->addr);
2867 radius_sockaddr_in->sin_port = htons(server->port);
2868
2869 res = sendto(sockfd, (char *) packet, ntohs(packet->length), 0,
2870 &radius_remote_sock, sizeof(struct sockaddr_in));
2871 if (res < 0) {
2872 int xerrno = errno;
2873
2874 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2875 "error: unable to send packet: %s", strerror(xerrno));
2876
2877 errno = xerrno;
2878 return -1;
2879 }
2880
2881 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2882 "sending packet to %s:%u", inet_ntoa(radius_sockaddr_in->sin_addr),
2883 ntohs(radius_sockaddr_in->sin_port));
2884
2885 return 0;
2886 }
2887
2888 static int radius_start_accting(void) {
2889 int sockfd = -1, acct_status = 0, acct_authentic = 0, now = 0, pid_len = 0;
2890 radius_packet_t *request = NULL, *response = NULL;
2891 radius_server_t *acct_server = NULL;
2892 unsigned char recvd_response = FALSE, *authenticated = NULL;
2893 char pid_str[16];
2894
2895 /* Check to see if RADIUS accounting should be done. */
2896 if (radius_engine == FALSE ||
2897 radius_acct_server == NULL) {
2898 return 0;
2899 }
2900
2901 /* Only do accounting for authenticated users. */
2902 authenticated = get_param_ptr(main_server->conf, "authenticated", FALSE);
2903 if (authenticated == NULL ||
2904 *authenticated == FALSE) {
2905 return 0;
2906 }
2907
2908 /* Open a RADIUS socket */
2909 sockfd = radius_open_socket();
2910 if (sockfd < 0) {
2911 int xerrno = errno;
2912
2913 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2914 "socket open failed: %s", strerror(xerrno));
2915
2916 errno = xerrno;
2917 return -1;
2918 }
2919
2920 /* Allocate a packet. */
2921 request = (radius_packet_t *) pcalloc(radius_pool, sizeof(radius_packet_t));
2922
2923 now = htonl(time(NULL));
2924
2925 memset(pid_str, '\0', sizeof(pid_str));
2926 pid_len = pr_snprintf(pid_str, sizeof(pid_str), "%08u",
2927 (unsigned int) session.pid);
2928
2929 /* Loop through the list of servers, trying each one until the packet is
2930 * successfully sent.
2931 */
2932 acct_server = radius_acct_server;
2933
2934 while (acct_server) {
2935 pr_signals_handle();
2936
2937 /* Clear the packet. */
2938 memset(request, '\0', sizeof(radius_packet_t));
2939
2940 /* Build the packet. */
2941 request->code = RADIUS_ACCT_REQUEST;
2942 radius_build_packet(request,
2943 radius_realm ?
2944 (const unsigned char *) pstrcat(radius_pool, session.user,
2945 radius_realm, NULL) :
2946 (const unsigned char *) session.user, NULL, acct_server->secret,
2947 acct_server->secret_len);
2948
2949 radius_last_acct_pkt_id = request->id;
2950
2951 /* Add accounting attributes. */
2952 acct_status = htonl(RADIUS_ACCT_STATUS_START);
2953 radius_add_attrib(request, RADIUS_ACCT_STATUS_TYPE,
2954 (unsigned char *) &acct_status, sizeof(int));
2955
2956 radius_add_attrib(request, RADIUS_ACCT_SESSION_ID,
2957 (const unsigned char *) pid_str, pid_len);
2958
2959 acct_authentic = htonl(RADIUS_AUTH_LOCAL);
2960 radius_add_attrib(request, RADIUS_ACCT_AUTHENTIC,
2961 (unsigned char *) &acct_authentic, sizeof(int));
2962
2963 radius_add_attrib(request, RADIUS_ACCT_EVENT_TS, (unsigned char *) &now,
2964 sizeof(int));
2965
2966 if (radius_acct_user != NULL) {
2967 /* See RFC 2865, Section 5.1. */
2968 radius_add_attrib(request, RADIUS_USER_NAME,
2969 (const unsigned char *) radius_acct_user, radius_acct_userlen);
2970 }
2971
2972 if (radius_acct_class != NULL) {
2973 radius_add_attrib(request, RADIUS_CLASS,
2974 (const unsigned char *) radius_acct_class, radius_acct_classlen);
2975 }
2976
2977 /* Calculate the signature. */
2978 radius_set_acct_digest(request, acct_server->secret,
2979 acct_server->secret_len);
2980
2981 /* Send the request. */
2982 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2983 "sending start acct request packet");
2984 if (radius_send_packet(sockfd, request, acct_server) < 0) {
2985 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2986 "packet send failed");
2987 acct_server = acct_server->next;
2988 continue;
2989 }
2990
2991 /* Receive the response. */
2992 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2993 "receiving acct response packet");
2994 response = radius_recv_packet(sockfd, acct_server->timeout);
2995 if (response == NULL) {
2996 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
2997 "packet receive failed");
2998 acct_server = acct_server->next;
2999 continue;
3000 }
3001
3002 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3003 "packet receive succeeded");
3004 recvd_response = TRUE;
3005 break;
3006 }
3007
3008 /* Close the socket. */
3009 (void) close(sockfd);
3010
3011 if (recvd_response) {
3012
3013 /* Verify the response. */
3014 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3015 "verifying packet");
3016 if (radius_verify_packet(request, response, acct_server->secret,
3017 acct_server->secret_len) < 0) {
3018 return -1;
3019 }
3020
3021 /* Handle the response. */
3022 switch (response->code) {
3023 case RADIUS_ACCT_RESPONSE:
3024 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3025 "accounting started for user '%s'", session.user);
3026 return 0;
3027
3028 default:
3029 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3030 "notice: server returned unknown response code: %02x",
3031 response->code);
3032 return -1;
3033 }
3034
3035 } else {
3036 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3037 "error: no acct servers responded");
3038 }
3039
3040 /* Default return value. */
3041 return -1;
3042 }
3043
3044 /* Maps the ProFTPD disconnect reason code to the RADIUS Acct-Terminate-Cause
3045 * attribute values.
3046 */
3047 static unsigned int radius_get_terminate_cause(void) {
3048 unsigned int cause = RADIUS_ACCT_TERMINATE_CAUSE_SERVICE_UNAVAIL;
3049
3050 switch (session.disconnect_reason) {
3051 case PR_SESS_DISCONNECT_CLIENT_QUIT:
3052 cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
3053 break;
3054
3055 case PR_SESS_DISCONNECT_CLIENT_EOF:
3056 cause = RADIUS_ACCT_TERMINATE_CAUSE_LOST_SERVICE;
3057 break;
3058
3059 case PR_SESS_DISCONNECT_SIGNAL:
3060 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_RESET;
3061 break;
3062
3063 case PR_SESS_DISCONNECT_SERVER_SHUTDOWN:
3064 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
3065 break;
3066
3067 case PR_SESS_DISCONNECT_TIMEOUT: {
3068 const char *details = NULL;
3069
3070 pr_session_get_disconnect_reason(&details);
3071 if (details != NULL) {
3072 if (strcasecmp(details, "TimeoutIdle") == 0) {
3073 cause = RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
3074
3075 } else if (strcasecmp(details, "TimeoutSession") == 0) {
3076 cause = RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
3077 }
3078 }
3079
3080 break;
3081 }
3082 }
3083
3084 return cause;
3085 }
3086
3087 static int radius_stop_accting(void) {
3088 int sockfd = -1, acct_status = 0, acct_authentic = 0, event_ts = 0,
3089 now = 0, pid_len = 0, session_duration = 0;
3090 unsigned int terminate_cause = 0;
3091 radius_packet_t *request = NULL, *response = NULL;
3092 radius_server_t *acct_server = NULL;
3093 unsigned char recvd_response = FALSE, *authenticated = NULL;
3094 off_t radius_session_bytes_in = 0;
3095 off_t radius_session_bytes_out = 0;
3096 char pid_str[16];
3097
3098 /* Check to see if RADIUS accounting should be done. */
3099 if (radius_engine == FALSE ||
3100 radius_acct_server == NULL) {
3101 return 0;
3102 }
3103
3104 /* Only do accounting for authenticated users. */
3105 authenticated = get_param_ptr(main_server->conf, "authenticated", FALSE);
3106 if (authenticated == NULL ||
3107 *authenticated == FALSE) {
3108 return 0;
3109 }
3110
3111 /* Open a RADIUS socket */
3112 sockfd = radius_open_socket();
3113 if (sockfd < 0) {
3114 int xerrno = errno;
3115
3116 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3117 "socket open failed: %s", strerror(xerrno));
3118
3119 errno = xerrno;
3120 return -1;
3121 }
3122
3123 /* Allocate a packet. */
3124 request = (radius_packet_t *) pcalloc(radius_pool, sizeof(radius_packet_t));
3125
3126 now = time(NULL);
3127 event_ts = htonl(now);
3128 session_duration = htonl(now - radius_session_start);
3129 terminate_cause = htonl(radius_get_terminate_cause());
3130
3131 memset(pid_str, '\0', sizeof(pid_str));
3132 pid_len = pr_snprintf(pid_str, sizeof(pid_str)-1, "%08u",
3133 (unsigned int) session.pid);
3134
3135 /* Loop through the list of servers, trying each one until the packet is
3136 * successfully sent.
3137 */
3138 acct_server = radius_acct_server;
3139
3140 while (acct_server) {
3141 const char *ip_str;
3142
3143 pr_signals_handle();
3144
3145 /* Clear the packet. */
3146 memset(request, '\0', sizeof(radius_packet_t));
3147
3148 /* Build the packet. */
3149 request->code = RADIUS_ACCT_REQUEST;
3150 radius_build_packet(request,
3151 radius_realm ?
3152 (const unsigned char *) pstrcat(radius_pool, session.user,
3153 radius_realm, NULL) :
3154 (const unsigned char *) session.user, NULL, acct_server->secret,
3155 acct_server->secret_len);
3156
3157 /* Use the ID of the last accounting packet sent, plus one. Be sure
3158 * to handle the datatype overflow case.
3159 */
3160 request->id = radius_last_acct_pkt_id + 1;
3161 if (request->id == 0) {
3162 request->id = 1;
3163 }
3164
3165 /* Add accounting attributes. */
3166 acct_status = htonl(RADIUS_ACCT_STATUS_STOP);
3167 radius_add_attrib(request, RADIUS_ACCT_STATUS_TYPE,
3168 (unsigned char *) &acct_status, sizeof(int));
3169
3170 radius_add_attrib(request, RADIUS_ACCT_SESSION_ID,
3171 (const unsigned char *) pid_str, pid_len);
3172
3173 acct_authentic = htonl(RADIUS_AUTH_LOCAL);
3174 radius_add_attrib(request, RADIUS_ACCT_AUTHENTIC,
3175 (unsigned char *) &acct_authentic, sizeof(int));
3176
3177 radius_add_attrib(request, RADIUS_ACCT_SESSION_TIME,
3178 (unsigned char *) &session_duration, sizeof(int));
3179
3180 radius_session_bytes_in = htonl(session.total_bytes_in);
3181 radius_add_attrib(request, RADIUS_ACCT_INPUT_OCTETS,
3182 (unsigned char *) &radius_session_bytes_in, sizeof(int));
3183
3184 radius_session_bytes_out = htonl(session.total_bytes_out);
3185 radius_add_attrib(request, RADIUS_ACCT_OUTPUT_OCTETS,
3186 (unsigned char *) &radius_session_bytes_out, sizeof(int));
3187
3188 radius_add_attrib(request, RADIUS_ACCT_TERMINATE_CAUSE,
3189 (unsigned char *) &terminate_cause, sizeof(int));
3190
3191 radius_add_attrib(request, RADIUS_ACCT_EVENT_TS,
3192 (unsigned char *) &event_ts, sizeof(int));
3193
3194 if (radius_acct_user != NULL) {
3195 /* See RFC 2865, Section 5.1. */
3196 radius_add_attrib(request, RADIUS_USER_NAME,
3197 (const unsigned char *) radius_acct_user, radius_acct_userlen);
3198 }
3199
3200 if (radius_acct_class != NULL) {
3201 radius_add_attrib(request, RADIUS_CLASS,
3202 (const unsigned char *) radius_acct_class, radius_acct_classlen);
3203 }
3204
3205 /* Calculate the signature. */
3206 radius_set_acct_digest(request, acct_server->secret,
3207 acct_server->secret_len);
3208
3209 /* Send the request. */
3210 ip_str = pr_netaddr_get_ipstr(acct_server->addr);
3211 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3212 "sending stop acct request packet to %s#%u", ip_str, acct_server->port);
3213 if (radius_send_packet(sockfd, request, acct_server) < 0) {
3214 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3215 "packet send failed to %s#%u", ip_str, acct_server->port);
3216 acct_server = acct_server->next;
3217 continue;
3218 }
3219
3220 /* Receive the response. */
3221 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3222 "receiving acct response packet");
3223 response = radius_recv_packet(sockfd, acct_server->timeout);
3224 if (response == NULL) {
3225 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3226 "packet receive failed from %s#%u", ip_str, acct_server->port);
3227 acct_server = acct_server->next;
3228 continue;
3229 }
3230
3231 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3232 "packet receive succeeded succeeded from %s#%u", ip_str,
3233 acct_server->port);
3234 recvd_response = TRUE;
3235 break;
3236 }
3237
3238 /* Close the socket. */
3239 (void) close(sockfd);
3240
3241 if (recvd_response) {
3242
3243 /* Verify the response. */
3244 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3245 "verifying packet");
3246 if (radius_verify_packet(request, response, acct_server->secret,
3247 acct_server->secret_len) < 0) {
3248 return -1;
3249 }
3250
3251 /* Handle the response. */
3252 switch (response->code) {
3253 case RADIUS_ACCT_RESPONSE:
3254 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3255 "accounting ended for user '%s'", session.user);
3256 return 0;
3257
3258 default:
3259 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3260 "notice: server returned unknown response code: %02x",
3261 response->code);
3262 return -1;
3263 }
3264
3265 } else {
3266 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3267 "error: no accounting servers responded");
3268 }
3269
3270 /* Default return value. */
3271 return -1;
3272 }
3273
3274 /* Verify the response packet from the server. */
3275 static int radius_verify_packet(radius_packet_t *req_packet,
3276 radius_packet_t *resp_packet, const unsigned char *secret,
3277 size_t secret_len) {
3278 MD5_CTX ctx;
3279 unsigned char calculated[RADIUS_VECTOR_LEN], replied[RADIUS_VECTOR_LEN];
3280
3281 /* sanity check */
3282 if (req_packet == NULL ||
3283 resp_packet == NULL ||
3284 secret == NULL) {
3285 errno = EINVAL;
3286 return -1;
3287 }
3288
3289 /* NOTE: add checks for too-big, too-small packets, invalid packet->length
3290 * values, etc.
3291 */
3292
3293 /* Check that the packet IDs match. */
3294 if (resp_packet->id != req_packet->id) {
3295 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3296 "packet verification failed: response packet ID %d does not "
3297 "match the request packet ID %d", resp_packet->id, req_packet->id);
3298 return -1;
3299 }
3300
3301 /* Make sure the buffers are void of junk values. */
3302 memset(calculated, '\0', sizeof(calculated));
3303 memset(replied, '\0', sizeof(replied));
3304
3305 /* Save a copy of the response's digest. */
3306 memcpy(replied, resp_packet->digest, RADIUS_VECTOR_LEN);
3307
3308 /* Copy in the digest sent in the request. */
3309 memcpy(resp_packet->digest, req_packet->digest, RADIUS_VECTOR_LEN);
3310
3311 /* Re-calculate a digest from the given packet, and compare it against
3312 * the provided response digest:
3313 * MD5(response packet header + digest + response packet data + secret)
3314 */
3315 MD5_Init(&ctx);
3316 MD5_Update(&ctx, (unsigned char *) resp_packet, ntohs(resp_packet->length));
3317
3318 if (*secret) {
3319 MD5_Update(&ctx, secret, secret_len);
3320 }
3321
3322 /* Set the calculated digest. */
3323 MD5_Final(calculated, &ctx);
3324
3325 /* Do the digests match properly? */
3326 if (memcmp(calculated, replied, RADIUS_VECTOR_LEN) != 0) {
3327 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3328 "packet verification failed: mismatched digests");
3329 errno = EINVAL;
3330 return -1;
3331 }
3332
3333 return 0;
3334 }
3335
3336 /* Authentication handlers
3337 */
3338
3339 MODRET radius_auth(cmd_rec *cmd) {
3340
3341 /* This authentication check has already been performed; I just need
3342 * to report the results of that check now.
3343 */
3344 if (radius_auth_ok == TRUE) {
3345 session.auth_mech = "mod_radius.c";
3346 return PR_HANDLED(cmd);
3347
3348 } else if (radius_auth_reject == TRUE) {
3349 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
3350 }
3351
3352 /* Default return value. */
3353 return PR_DECLINED(cmd);
3354 }
3355
3356 MODRET radius_check(cmd_rec *cmd) {
3357 return PR_DECLINED(cmd);
3358 }
3359
3360 MODRET radius_name2uid(cmd_rec *cmd) {
3361 return PR_DECLINED(cmd);
3362 }
3363
3364 MODRET radius_name2gid(cmd_rec *cmd) {
3365 return PR_DECLINED(cmd);
3366 }
3367
3368 MODRET radius_uid2name(cmd_rec *cmd) {
3369 return PR_DECLINED(cmd);
3370 }
3371
3372 MODRET radius_gid2name(cmd_rec *cmd) {
3373 return PR_DECLINED(cmd);
3374 }
3375
3376 MODRET radius_endgrent(cmd_rec *cmd) {
3377 return PR_DECLINED(cmd);
3378 }
3379
3380 MODRET radius_getgrnam(cmd_rec *cmd) {
3381 return PR_DECLINED(cmd);
3382 }
3383
3384 MODRET radius_getgrent(cmd_rec *cmd) {
3385 return PR_DECLINED(cmd);
3386 }
3387
3388 MODRET radius_getgrgid(cmd_rec *cmd) {
3389 return PR_DECLINED(cmd);
3390 }
3391
3392 MODRET radius_getgroups(cmd_rec *cmd) {
3393
3394 if (radius_have_group_info) {
3395 array_header *gids = NULL, *groups = NULL;
3396 register unsigned int i = 0;
3397
3398 /* Return the faked group information. */
3399
3400 /* Don't forget to include the primary GID (with accompanying name!)
3401 * in the returned info -- if provided. Otherwise, well...the user
3402 * is out of luck.
3403 */
3404
3405 /* Check for NULL values */
3406 if (cmd->argv[1]) {
3407 gids = (array_header *) cmd->argv[1];
3408
3409 if (radius_have_user_info) {
3410 *((gid_t *) push_array(gids)) = radius_passwd.pw_gid;
3411 }
3412
3413 for (i = 0; i < radius_addl_group_count; i++) {
3414 *((gid_t *) push_array(gids)) = radius_addl_group_ids[i];
3415 }
3416 }
3417
3418 if (cmd->argv[2]) {
3419 groups = (array_header *) cmd->argv[2];
3420
3421 if (radius_have_user_info) {
3422 *((char **) push_array(groups)) = radius_prime_group_name;
3423 }
3424
3425 for (i = 0; i < radius_addl_group_count; i++) {
3426 *((char **) push_array(groups)) = radius_addl_group_names[i];
3427 }
3428 }
3429
3430 /* Increment this count, for the sake of proper reporting back to the
3431 * getgroups() caller.
3432 */
3433 if (radius_have_user_info) {
3434 radius_getgroups_count = radius_addl_group_count + 1;
3435 }
3436
3437 return mod_create_data(cmd, (void *) &radius_getgroups_count);
3438 }
3439
3440 return PR_DECLINED(cmd);
3441 }
3442
3443 MODRET radius_setgrent(cmd_rec *cmd) {
3444 return PR_DECLINED(cmd);
3445 }
3446
3447 MODRET radius_endpwent(cmd_rec *cmd) {
3448 return PR_DECLINED(cmd);
3449 }
3450
3451 MODRET radius_getpwnam(cmd_rec *cmd) {
3452
3453 if (radius_have_user_info) {
3454
3455 if (radius_passwd.pw_name == NULL) {
3456 radius_passwd.pw_name = pstrdup(radius_pool, cmd->argv[0]);
3457 }
3458
3459 if (strcmp(cmd->argv[0], radius_passwd.pw_name) == 0) {
3460
3461 /* Return the faked user information. */
3462 return mod_create_data(cmd, &radius_passwd);
3463 }
3464 }
3465
3466 /* Default response */
3467 return PR_DECLINED(cmd);
3468 }
3469
3470 MODRET radius_getpwent(cmd_rec *cmd) {
3471
3472 if (radius_have_user_info) {
3473
3474 /* Return the faked user information. */
3475 return mod_create_data(cmd, &radius_passwd);
3476 }
3477
3478 /* Default response */
3479 return PR_DECLINED(cmd);
3480 }
3481
3482 MODRET radius_getpwuid(cmd_rec *cmd) {
3483
3484 if (radius_have_user_info) {
3485
3486 /* Check that given UID matches faked UID before returning. */
3487 if (*((uid_t *) cmd->argv[0]) == radius_passwd.pw_uid) {
3488
3489 /* Return the faked user information. */
3490 return mod_create_data(cmd, &radius_passwd);
3491 }
3492 }
3493
3494 /* Default response */
3495 return PR_DECLINED(cmd);
3496 }
3497
3498 MODRET radius_setpwent(cmd_rec *cmd) {
3499 return PR_DECLINED(cmd);
3500 }
3501
3502 /* Command handlers
3503 */
3504
3505 /* Handle retrieval of quota-related VSAs from response packets.
3506 */
3507 MODRET radius_quota_lookup(cmd_rec *cmd) {
3508
3509 if (radius_have_quota_info) {
3510 array_header *quota = make_array(session.pool, 9, sizeof(char *));
3511 *((char **) push_array(quota)) = cmd->argv[0];
3512 *((char **) push_array(quota)) = radius_quota_per_sess;
3513 *((char **) push_array(quota)) = radius_quota_limit_type;
3514 *((char **) push_array(quota)) = radius_quota_bytes_in;
3515 *((char **) push_array(quota)) = radius_quota_bytes_out;
3516 *((char **) push_array(quota)) = radius_quota_bytes_xfer;
3517 *((char **) push_array(quota)) = radius_quota_files_in;
3518 *((char **) push_array(quota)) = radius_quota_files_out;
3519 *((char **) push_array(quota)) = radius_quota_files_xfer;
3520
3521 return mod_create_data(cmd, quota);
3522 }
3523
3524 return PR_DECLINED(cmd);
3525 }
3526
3527 /* Perform the check with the RADIUS auth server(s) now, prior to the
3528 * actual handling of the PASS command by mod_auth, so that any of the
3529 * RadiusUserInfo parameters can be supplied by the RADIUS server.
3530 *
3531 * NOTE: first draft, does not honor UserAlias'd names (eg it uses the
3532 * username as supplied by the client.
3533 */
3534 MODRET radius_pre_pass(cmd_rec *cmd) {
3535 int pid_len = 0, sockfd = -1;
3536 radius_packet_t *request = NULL, *response = NULL;
3537 radius_server_t *auth_server = NULL;
3538 unsigned char recvd_response = FALSE;
3539 unsigned int service;
3540 const char *user;
3541 char pid_str[16];
3542
3543 /* Check to see whether RADIUS authentication should even be done. */
3544 if (radius_engine == FALSE ||
3545 radius_auth_server == NULL) {
3546 return PR_DECLINED(cmd);
3547 }
3548
3549 user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
3550 if (user == NULL) {
3551 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3552 "missing prerequisite USER command, declining to handle PASS");
3553 pr_response_add_err(R_503, _("Login with USER first"));
3554 return PR_ERROR(cmd);
3555 }
3556
3557 /* Open a RADIUS socket */
3558 sockfd = radius_open_socket();
3559 if (sockfd < 0) {
3560 int xerrno = errno;
3561 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3562 "socket open failed: %s", strerror(xerrno));
3563 errno = xerrno;
3564 return PR_DECLINED(cmd);
3565 }
3566
3567 /* Allocate a packet. */
3568 request = (radius_packet_t *) pcalloc(cmd->tmp_pool,
3569 sizeof(radius_packet_t));
3570
3571 /* Clear the OK flag. */
3572 radius_auth_ok = FALSE;
3573
3574 memset(pid_str, '\0', sizeof(pid_str));
3575 pid_len = pr_snprintf(pid_str, sizeof(pid_str)-1, "%08u",
3576 (unsigned int) session.pid);
3577
3578 /* If mod_radius expects to find VSAs in the returned packet, it needs
3579 * to send a service type of Login, otherwise, use the Authenticate-Only
3580 * service type.
3581 */
3582 if (radius_have_user_info == TRUE ||
3583 radius_have_group_info == TRUE ||
3584 radius_have_quota_info == TRUE ||
3585 radius_have_other_info == TRUE) {
3586 service = (unsigned int) htonl(RADIUS_SVC_LOGIN);
3587
3588 } else {
3589 service = (unsigned int) htonl(RADIUS_SVC_AUTHENTICATE_ONLY);
3590 }
3591
3592 /* Loop through the list of servers, trying each one until the packet is
3593 * successfully sent.
3594 */
3595 auth_server = radius_auth_server;
3596 while (auth_server != NULL) {
3597 const char *ip_str;
3598
3599 pr_signals_handle();
3600
3601 /* Clear the packet. */
3602 memset(request, '\0', sizeof(radius_packet_t));
3603
3604 /* Build the packet. */
3605 request->code = RADIUS_AUTH_REQUEST;
3606 radius_build_packet(request, radius_realm ?
3607 (const unsigned char *) pstrcat(radius_pool, user, radius_realm, NULL) :
3608 (const unsigned char *) user, (const unsigned char *) cmd->arg,
3609 auth_server->secret, auth_server->secret_len);
3610
3611 radius_add_attrib(request, RADIUS_SERVICE_TYPE, (unsigned char *) &service,
3612 sizeof(service));
3613
3614 radius_add_attrib(request, RADIUS_ACCT_SESSION_ID,
3615 (const unsigned char *) pid_str, pid_len);
3616
3617 /* Calculate the signature. */
3618 radius_set_auth_mac(request, auth_server->secret, auth_server->secret_len);
3619
3620 /* Send the request. */
3621 ip_str = pr_netaddr_get_ipstr(auth_server->addr);
3622 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3623 "sending auth request packet to %s#%d", ip_str, auth_server->port);
3624 if (radius_send_packet(sockfd, request, auth_server) < 0) {
3625 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3626 "packet send failed to %s#%d", ip_str, auth_server->port);
3627 auth_server = auth_server->next;
3628 continue;
3629 }
3630
3631 /* Receive the response. */
3632 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3633 "receiving auth response packet from %s#%d", ip_str, auth_server->port);
3634 response = radius_recv_packet(sockfd, auth_server->timeout);
3635 if (response == NULL) {
3636 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3637 "packet receive failed from %s#%d", ip_str, auth_server->port);
3638 auth_server = auth_server->next;
3639 continue;
3640 }
3641
3642 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3643 "packet receive succeeded from %s#%d", ip_str, auth_server->port);
3644 recvd_response = TRUE;
3645 break;
3646 }
3647
3648 /* Close the socket. */
3649 (void) close(sockfd);
3650
3651 if (recvd_response) {
3652 int res;
3653
3654 /* Verify the response. */
3655 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3656 "verifying packet");
3657 res = radius_verify_packet(request, response, auth_server->secret,
3658 auth_server->secret_len);
3659 if (res < 0) {
3660 return PR_DECLINED(cmd);
3661 }
3662
3663 /* Handle the response */
3664 switch (response->code) {
3665 case RADIUS_AUTH_ACCEPT:
3666 /* Process the packet for custom attributes */
3667 res = radius_process_accept_packet(response, auth_server->secret,
3668 auth_server->secret_len);
3669 if (res < 0) {
3670 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3671 "DISCARDING Access-Accept packet for user '%s' due to failed "
3672 "Message-Authenticator check; is the shared secret correct?",
3673 user);
3674 pr_log_pri(PR_LOG_NOTICE, MOD_RADIUS_VERSION
3675 ": DISCARDING Access-Accept packet for user '%s' due to failed "
3676 "Message-Authenticator check; is the shared secret correct?", user);
3677
3678 } else {
3679 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3680 "authentication successful for user '%s'", user);
3681 pr_trace_msg(trace_channel, 9,
3682 "processed %d %s in Access-Accept packet", res,
3683 res != 1 ? "attributes" : "attribute");
3684
3685 radius_auth_ok = TRUE;
3686 radius_session_authtype = htonl(RADIUS_AUTH_RADIUS);
3687 }
3688 break;
3689
3690 case RADIUS_AUTH_REJECT:
3691 /* Process the packet for custom attributes */
3692 res = radius_process_reject_packet(response, auth_server->secret,
3693 auth_server->secret_len);
3694 if (res < 0) {
3695 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3696 "DISCARDING Access-Reject packet for user '%s' due to failed "
3697 "Message-Authenticator check; is the shared secret correct?",
3698 user);
3699 pr_log_pri(PR_LOG_NOTICE, MOD_RADIUS_VERSION
3700 ": DISCARDING Access-Reject packet for user '%s' due to failed "
3701 "Message-Authenticator check; is the shared secret correct?", user);
3702
3703 } else {
3704 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3705 "authentication failed for user '%s'", user);
3706 pr_trace_msg(trace_channel, 9,
3707 "processed %d %s in Access-Reject packet", res,
3708 res != 1 ? "attributes" : "attribute");
3709
3710 radius_auth_ok = FALSE;
3711 radius_auth_reject = TRUE;
3712 radius_reset();
3713 }
3714 break;
3715
3716 case RADIUS_AUTH_CHALLENGE:
3717 /* Just log this case for now. */
3718 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3719 "authentication challenged for user '%s'", user);
3720 radius_reset();
3721 break;
3722
3723 default:
3724 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3725 "notice: server returned unknown response code: %02x",
3726 response->code);
3727 radius_reset();
3728 break;
3729 }
3730
3731 } else {
3732 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3733 "error: no auth servers responded");
3734 }
3735
3736 return PR_DECLINED(cmd);
3737 }
3738
3739 MODRET radius_post_pass(cmd_rec *cmd) {
3740
3741 /* Check to see if RADIUS accounting should be done. */
3742 if (!radius_engine || !radius_acct_server) {
3743 return PR_DECLINED(cmd);
3744 }
3745
3746 /* Fill in the username in the faked user info, if need be. */
3747 if (radius_have_user_info) {
3748 radius_passwd.pw_name = (char *) session.user;
3749 }
3750
3751 if (radius_start_accting() < 0) {
3752 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
3753 "error: unable to start accounting: %s", strerror(errno));
3754 }
3755
3756 return PR_DECLINED(cmd);
3757 }
3758
3759 MODRET radius_post_pass_err(cmd_rec *cmd) {
3760 radius_reset();
3761 return PR_DECLINED(cmd);
3762 }
3763
3764 /* Configuration handlers
3765 */
3766
3767 /* usage: RadiusAcctServer server[:port] shared-secret [timeout] */
3768 MODRET set_radiusacctserver(cmd_rec *cmd) {
3769 config_rec *c = NULL;
3770 radius_server_t *radius_server = NULL;
3771 unsigned short server_port = 0;
3772 char *port = NULL;
3773
3774 if (cmd->argc-1 < 2 ||
3775 cmd->argc-1 > 3) {
3776 CONF_ERROR(cmd, "missing parameters");
3777 }
3778
3779 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3780
3781 /* Check to see if there's a port specified in the server name */
3782 port = strchr(cmd->argv[1], ':');
3783 if (port != NULL) {
3784
3785 /* Separate the server name from the port */
3786 *(port++) = '\0';
3787
3788 server_port = (unsigned short) atoi(port);
3789 if (server_port < 1024) {
3790 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "port number must be greater "
3791 "than 1023", NULL));
3792 }
3793 }
3794
3795 if (pr_netaddr_get_addr(cmd->tmp_pool, cmd->argv[1], NULL) == NULL) {
3796 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to resolve server address: ",
3797 cmd->argv[1], NULL));
3798 }
3799
3800 /* Allocate a RADIUS server rec and populate the members */
3801 radius_server = radius_make_server(radius_pool);
3802
3803 radius_server->addr = pr_netaddr_get_addr(radius_server->pool, cmd->argv[1],
3804 NULL);
3805 radius_server->port = (server_port ? server_port : RADIUS_ACCT_PORT);
3806 radius_server->secret = (unsigned char *) pstrdup(radius_server->pool,
3807 cmd->argv[2]);
3808 radius_server->secret_len = strlen((char *) radius_server->secret);
3809
3810 if (cmd->argc-1 == 3) {
3811 int timeout = -1;
3812
3813 if (pr_str_get_duration(cmd->argv[3], &timeout) < 0) {
3814 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing timeout value '",
3815 cmd->argv[1], "': ", strerror(errno), NULL));
3816 }
3817
3818 radius_server->timeout = timeout;
3819 }
3820
3821 c = add_config_param(cmd->argv[0], 1, NULL);
3822 c->argv[0] = pcalloc(c->pool, sizeof(radius_server_t *));
3823 *((radius_server_t **) c->argv[0]) = radius_server;
3824
3825 return PR_HANDLED(cmd);
3826 }
3827
3828 /* usage: RadiusAuthServer server[:port] <shared-secret> [timeout] */
3829 MODRET set_radiusauthserver(cmd_rec *cmd) {
3830 config_rec *c = NULL;
3831 radius_server_t *radius_server = NULL;
3832 unsigned short server_port = 0;
3833 char *port = NULL;
3834
3835 if (cmd->argc-1 < 2 ||
3836 cmd->argc-1 > 3) {
3837 CONF_ERROR(cmd, "missing parameters");
3838 }
3839
3840 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3841
3842 /* Check to see if there's a port specified in the server name */
3843 if ((port = strchr(cmd->argv[1], ':')) != NULL) {
3844
3845 /* Separate the server name from the port */
3846 *(port++) = '\0';
3847
3848 server_port = (unsigned short) atoi(port);
3849 if (server_port < 1024) {
3850 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "port number must be greater "
3851 "than 1023", NULL));
3852 }
3853 }
3854
3855 if (pr_netaddr_get_addr(cmd->tmp_pool, cmd->argv[1], NULL) == NULL) {
3856 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable resolve server address: ",
3857 cmd->argv[1], NULL));
3858 }
3859
3860 /* OK, allocate a RADIUS server rec and populate the members */
3861 radius_server = radius_make_server(radius_pool);
3862
3863 radius_server->addr = pr_netaddr_get_addr(radius_server->pool, cmd->argv[1],
3864 NULL);
3865 radius_server->port = (server_port ? server_port : RADIUS_AUTH_PORT);
3866 radius_server->secret = (unsigned char *) pstrdup(radius_server->pool,
3867 cmd->argv[2]);
3868 radius_server->secret_len = strlen((char *) radius_server->secret);
3869
3870 if (cmd->argc-1 == 3) {
3871 int timeout = -1;
3872
3873 if (pr_str_get_duration(cmd->argv[3], &timeout) < 0) {
3874 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing timeout value '",
3875 cmd->argv[1], "': ", strerror(errno), NULL));
3876 }
3877
3878 radius_server->timeout = timeout;
3879 }
3880
3881 c = add_config_param(cmd->argv[0], 1, NULL);
3882 c->argv[0] = pcalloc(c->pool, sizeof(radius_server_t *));
3883 *((radius_server_t **) c->argv[0]) = radius_server;
3884
3885 return PR_HANDLED(cmd);
3886 }
3887
3888 /* usage: RadiusEngine on|off */
3889 MODRET set_radiusengine(cmd_rec *cmd) {
3890 int engine = -1;
3891 config_rec *c = NULL;
3892
3893 CHECK_ARGS(cmd, 1);
3894 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3895
3896 engine = get_boolean(cmd, 1);
3897 if (engine == -1) {
3898 CONF_ERROR(cmd, "expected Boolean parameter");
3899 }
3900
3901 c = add_config_param(cmd->argv[0], 1, NULL);
3902 c->argv[0] = pcalloc(c->pool, sizeof(int));
3903 *((int *) c->argv[0]) = engine;
3904
3905 return PR_HANDLED(cmd);
3906 }
3907
3908 /* usage: RadiusGroupInfo primary-name addl-names add-ids */
3909 MODRET set_radiusgroupinfo(cmd_rec *cmd) {
3910 config_rec *c = NULL;
3911 unsigned char group_names_vsa = FALSE;
3912 unsigned char group_ids_vsa = FALSE;
3913 unsigned int ngroups = 0, ngids = 0;
3914
3915 CHECK_ARGS(cmd, 3);
3916 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3917
3918 group_names_vsa = radius_have_var(cmd->argv[2]);
3919 group_ids_vsa = radius_have_var(cmd->argv[3]);
3920
3921 /* There will be five parameters to this config_rec:
3922 *
3923 * primary-group-name
3924 * addl-group-name-count
3925 * addl-group-names
3926 * addl-group-id-count
3927 * addl-group-ids
3928 */
3929
3930 c = add_config_param(cmd->argv[0], 5, NULL, NULL, NULL, NULL, NULL);
3931 c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
3932 c->argv[1] = pcalloc(c->pool, sizeof(unsigned int));
3933 c->argv[3] = pcalloc(c->pool, sizeof(unsigned int));
3934
3935 if (group_names_vsa) {
3936 /* As VSA variables, the group names won't be resolved until session time,
3937 * so just store the variable strings as is.
3938 */
3939 c->argv[2] = pstrdup(c->pool, cmd->argv[2]);
3940
3941 } else {
3942 char **groups = NULL;
3943
3944 if (!radius_parse_groups_str(c->pool, cmd->argv[2], &groups, &ngroups))
3945 CONF_ERROR(cmd, "badly formatted group names");
3946
3947 *((unsigned int *) c->argv[1]) = ngroups;
3948 c->argv[2] = (void *) groups;
3949 }
3950
3951 if (group_ids_vsa) {
3952 /* As VSA variables, the group IDs won't be resolved until session time,
3953 * so just store the variable strings as is.
3954 */
3955 c->argv[4] = pstrdup(c->pool, cmd->argv[3]);
3956
3957 } else {
3958 gid_t *gids = NULL;
3959
3960 if (!radius_parse_gids_str(c->pool, cmd->argv[3], &gids, &ngids))
3961 CONF_ERROR(cmd, "badly formatted group IDs");
3962
3963 *((unsigned int *) c->argv[3]) = ngids;
3964 c->argv[4] = (void *) gids;
3965 }
3966
3967 if (ngroups > 0 &&
3968 ngids > 0 &&
3969 ngroups != ngids) {
3970 char ngroups_str[32], ngids_str[32];
3971
3972 memset(ngroups_str, '\0', sizeof(ngroups_str));
3973 pr_snprintf(ngroups_str, sizeof(ngroups_str)-1, "%u", ngroups);
3974
3975 memset(ngids_str, '\0', sizeof(ngids_str));
3976 pr_snprintf(ngids_str, sizeof(ngids_str)-1, "%u", ngids);
3977
3978 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "mismatched number of group names (",
3979 ngroups_str, ") and group IDs (", ngids_str, ")", NULL));
3980 }
3981
3982 return PR_HANDLED(cmd);
3983 }
3984
3985 /* usage: RadiusLog file|"none" */
3986 MODRET set_radiuslog(cmd_rec *cmd) {
3987 if (cmd->argc-1 != 1)
3988 CONF_ERROR(cmd, "wrong number of parameters");
3989 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3990
3991 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
3992 return PR_HANDLED(cmd);
3993 }
3994
3995 /* usage: RadiusNASIdentifier string */
3996 MODRET set_radiusnasidentifier(cmd_rec *cmd) {
3997 CHECK_ARGS(cmd, 1);
3998 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3999
4000 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
4001 return PR_HANDLED(cmd);
4002 }
4003
4004 /* usage: RadiusOptions opt1 ... */
4005 MODRET set_radiusoptions(cmd_rec *cmd) {
4006 config_rec *c = NULL;
4007 register unsigned int i = 0;
4008 unsigned long opts = 0UL;
4009
4010 if (cmd->argc-1 == 0) {
4011 CONF_ERROR(cmd, "wrong number of parameters");
4012 }
4013
4014 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
4015
4016 c = add_config_param(cmd->argv[0], 1, NULL);
4017
4018 for (i = 1; i < cmd->argc; i++) {
4019 if (strcmp(cmd->argv[i], "IgnoreReplyMessage") == 0) {
4020 opts |= RADIUS_OPT_IGNORE_REPLY_MESSAGE_ATTR;
4021
4022 } else if (strcmp(cmd->argv[i], "IgnoreClass") == 0) {
4023 opts |= RADIUS_OPT_IGNORE_CLASS_ATTR;
4024
4025 } else if (strcmp(cmd->argv[i], "IgnoreIdleTimeout") == 0) {
4026 opts |= RADIUS_OPT_IGNORE_IDLE_TIMEOUT_ATTR;
4027
4028 } else if (strcmp(cmd->argv[i], "IgnoreSessionTimeout") == 0) {
4029 opts |= RADIUS_OPT_IGNORE_SESSION_TIMEOUT_ATTR;
4030
4031 } else if (strcmp(cmd->argv[i], "RequireMAC") == 0) {
4032 opts |= RADIUS_OPT_REQUIRE_MAC;
4033
4034 } else {
4035 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown TLSOption '",
4036 cmd->argv[i], "'", NULL));
4037 }
4038 }
4039
4040 c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
4041 *((unsigned long *) c->argv[0]) = opts;
4042
4043 if (pr_module_exists("mod_ifsession.c")) {
4044 /* These are needed in case this directive is used with mod_ifsession
4045 * configuration.
4046 */
4047 c->flags |= CF_MULTI;
4048 }
4049
4050 return PR_HANDLED(cmd);
4051 }
4052
4053 /* usage: RadiusQuotaInfo per-sess limit-type bytes-in bytes-out bytes-xfer
4054 * files-in files-out files-xfer
4055 */
4056 MODRET set_radiusquotainfo(cmd_rec *cmd) {
4057 CHECK_ARGS(cmd, 8);
4058 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
4059
4060 if (!radius_have_var(cmd->argv[1])) {
4061 if (strcasecmp(cmd->argv[1], "false") != 0 &&
4062 strcasecmp(cmd->argv[1], "true") != 0)
4063 CONF_ERROR(cmd, "invalid per-session value");
4064 }
4065
4066 if (!radius_have_var(cmd->argv[2])) {
4067 if (strcasecmp(cmd->argv[2], "hard") != 0 &&
4068 strcasecmp(cmd->argv[2], "soft") != 0) {
4069 CONF_ERROR(cmd, "invalid limit type value");
4070 }
4071 }
4072
4073 if (!radius_have_var(cmd->argv[3])) {
4074 char *endp = NULL;
4075
4076 /* Make sure it's a number, at least. */
4077 if (strtod(cmd->argv[3], &endp) < 0) {
4078 CONF_ERROR(cmd, "negative bytes value not allowed");
4079 }
4080
4081 if (endp && *endp) {
4082 CONF_ERROR(cmd, "invalid bytes parameter: not a number");
4083 }
4084 }
4085
4086 if (!radius_have_var(cmd->argv[4])) {
4087 char *endp = NULL;
4088
4089 /* Make sure it's a number, at least. */
4090 if (strtod(cmd->argv[4], &endp) < 0) {
4091 CONF_ERROR(cmd, "negative bytes value not allowed");
4092 }
4093
4094 if (endp && *endp) {
4095 CONF_ERROR(cmd, "invalid bytes parameter: not a number");
4096 }
4097 }
4098
4099 if (!radius_have_var(cmd->argv[5])) {
4100 char *endp = NULL;
4101
4102 /* Make sure it's a number, at least. */
4103 if (strtod(cmd->argv[5], &endp) < 0) {
4104 CONF_ERROR(cmd, "negative bytes value not allowed");
4105 }
4106
4107 if (endp && *endp) {
4108 CONF_ERROR(cmd, "invalid bytes parameter: not a number");
4109 }
4110 }
4111
4112 if (!radius_have_var(cmd->argv[6])) {
4113 char *endp = NULL;
4114
4115 /* Make sure it's a number, at least. */
4116 (void) strtoul(cmd->argv[6], &endp, 10);
4117 if (endp && *endp) {
4118 CONF_ERROR(cmd, "invalid files parameter: not a number");
4119 }
4120 }
4121
4122 if (!radius_have_var(cmd->argv[7])) {
4123 char *endp = NULL;
4124
4125 /* Make sure it's a number, at least. */
4126 (void) strtoul(cmd->argv[7], &endp, 10);
4127 if (endp && *endp) {
4128 CONF_ERROR(cmd, "invalid files parameter: not a number");
4129 }
4130 }
4131
4132 if (!radius_have_var(cmd->argv[8])) {
4133 char *endp = NULL;
4134
4135 /* Make sure it's a number, at least. */
4136 (void) strtoul(cmd->argv[8], &endp, 10);
4137 if (endp && *endp) {
4138 CONF_ERROR(cmd, "invalid files parameter: not a number");
4139 }
4140 }
4141
4142 add_config_param_str(cmd->argv[0], 8, cmd->argv[1], cmd->argv[2],
4143 cmd->argv[3], cmd->argv[4], cmd->argv[5], cmd->argv[6],
4144 cmd->argv[7], cmd->argv[8]);
4145
4146 return PR_HANDLED(cmd);
4147 }
4148
4149 /* usage: RadiusRealm string */
4150 MODRET set_radiusrealm(cmd_rec *cmd) {
4151 CHECK_ARGS(cmd, 1);
4152 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
4153
4154 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
4155 return PR_HANDLED(cmd);
4156 }
4157
4158 /* usage: RadiusUserInfo uid gid home shell */
4159 MODRET set_radiususerinfo(cmd_rec *cmd) {
4160 CHECK_ARGS(cmd, 4);
4161 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
4162
4163 if (!radius_have_var(cmd->argv[1])) {
4164 char *endp = NULL;
4165
4166 /* Make sure it's a number, at least. */
4167 (void) strtoul(cmd->argv[1], &endp, 10);
4168 if (endp && *endp) {
4169 CONF_ERROR(cmd, "invalid UID parameter: not a number");
4170 }
4171 }
4172
4173 if (!radius_have_var(cmd->argv[2])) {
4174 char *endp = NULL;
4175
4176 /* Make sure it's a number, at least. */
4177 (void) strtoul(cmd->argv[2], &endp, 10);
4178 if (endp && *endp) {
4179 CONF_ERROR(cmd, "invalid GID parameter: not a number");
4180 }
4181 }
4182
4183 if (!radius_have_var(cmd->argv[3])) {
4184 char *path;
4185
4186 path = cmd->argv[3];
4187 /* Make sure the path is absolute, at least. */
4188 if (*path != '/') {
4189 CONF_ERROR(cmd, "home relative path not allowed");
4190 }
4191 }
4192
4193 if (!radius_have_var(cmd->argv[4])) {
4194 char *path;
4195
4196 path = cmd->argv[4];
4197 /* Make sure the path is absolute, at least. */
4198 if (*path != '/') {
4199 CONF_ERROR(cmd, "shell relative path not allowed");
4200 }
4201 }
4202
4203 add_config_param_str(cmd->argv[0], 4, cmd->argv[1], cmd->argv[2],
4204 cmd->argv[3], cmd->argv[4]);
4205 return PR_HANDLED(cmd);
4206 }
4207
4208 /* usage: RadiusVendor name id */
4209 MODRET set_radiusvendor(cmd_rec *cmd) {
4210 config_rec *c = NULL;
4211 long id = 0;
4212 char *tmp = NULL;
4213
4214 CHECK_ARGS(cmd, 2);
4215 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
4216
4217 /* Make sure that the given vendor ID number is valid. */
4218 id = strtol(cmd->argv[2], &tmp, 10);
4219
4220 if (tmp && *tmp) {
4221 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": vendor id '", cmd->argv[2],
4222 "' is not a valid number", NULL));
4223 }
4224
4225 if (id < 0) {
4226 CONF_ERROR(cmd, "vendor id must be a positive number");
4227 }
4228
4229 c = add_config_param(cmd->argv[0], 2, NULL, NULL);
4230 c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
4231 c->argv[1] = pcalloc(c->pool, sizeof(unsigned int));
4232 *((unsigned int *) c->argv[1]) = id;
4233
4234 return PR_HANDLED(cmd);
4235 }
4236
4237 /* Event handlers
4238 */
4239
4240 static void radius_exit_ev(const void *event_data, void *user_data) {
4241 if (radius_stop_accting() < 0) {
4242 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
4243 "error: unable to stop accounting: %s", strerror(errno));
4244 }
4245
4246 (void) close(radius_logfd);
4247 radius_logfd = -1;
4248 }
4249
4250 #if defined(PR_SHARED_MODULE)
4251 static void radius_mod_unload_ev(const void *event_data, void *user_data) {
4252 if (strcmp("mod_radius.c", (const char *) event_data) == 0) {
4253 pr_event_unregister(&radius_module, NULL, NULL);
4254
4255 if (radius_pool) {
4256 destroy_pool(radius_pool);
4257 radius_pool = NULL;
4258 }
4259
4260 close(radius_logfd);
4261 radius_logfd = -1;
4262 }
4263 }
4264 #endif /* PR_SHARED_MODULE */
4265
4266 static void radius_restart_ev(const void *event_data, void *user_data) {
4267
4268 /* Re-allocate the pool used by this module. */
4269 if (radius_pool) {
4270 destroy_pool(radius_pool);
4271 }
4272
4273 radius_pool = make_sub_pool(permanent_pool);
4274 pr_pool_tag(radius_pool, MOD_RADIUS_VERSION);
4275
4276 return;
4277 }
4278
4279 static void radius_sess_reinit_ev(const void *event_data, void *user_data) {
4280 int res;
4281
4282 /* A HOST command changed the main_server pointer; reinitialize ourselves. */
4283
4284 pr_event_unregister(&radius_module, "core.exit", radius_exit_ev);
4285 pr_event_unregister(&radius_module, "core.session-reinit",
4286 radius_sess_reinit_ev);
4287
4288 /* Reset defaults. */
4289 radius_engine = FALSE;
4290 radius_acct_server = NULL;
4291 radius_auth_server = NULL;
4292 (void) close(radius_logfd);
4293 radius_logfd = -1;
4294 radius_opts = 0UL;
4295 radius_nas_identifier_config = NULL;
4296 radius_vendor_name = "Unix";
4297 radius_vendor_id = 4;
4298 radius_realm = NULL;
4299
4300 radius_have_user_info = FALSE;
4301 radius_uid_attr_id = 0;
4302 radius_gid_attr_id = 0;
4303 radius_home_attr_id = 0;
4304 radius_shell_attr_id = 0;
4305
4306 radius_have_group_info = FALSE;
4307 radius_prime_group_name_attr_id = 0;
4308 radius_addl_group_names_attr_id = 0;
4309 radius_addl_group_ids_attr_id = 0;
4310 radius_prime_group_name = NULL;
4311 radius_addl_group_count = 0;
4312 radius_addl_group_names = 0;
4313 radius_addl_group_names_str = NULL;
4314 radius_addl_group_ids = NULL;
4315 radius_addl_group_ids_str = NULL;
4316
4317 radius_have_quota_info = FALSE;
4318 radius_quota_per_sess_attr_id = 0;
4319 radius_quota_limit_type_attr_id = 0;
4320 radius_quota_bytes_in_attr_id = 0;
4321 radius_quota_bytes_out_attr_id = 0;
4322 radius_quota_bytes_xfer_attr_id = 0;
4323 radius_quota_files_in_attr_id = 0;
4324 radius_quota_files_out_attr_id = 0;
4325 radius_quota_files_xfer_attr_id = 0;
4326 radius_quota_per_sess = NULL;
4327 radius_quota_limit_type = NULL;
4328 radius_quota_bytes_in = NULL;
4329 radius_quota_bytes_out = NULL;
4330 radius_quota_bytes_xfer = NULL;
4331 radius_quota_files_in = NULL;
4332 radius_quota_files_out = NULL;
4333 radius_quota_files_xfer = NULL;
4334
4335 radius_have_other_info = FALSE;
4336
4337 /* Note that we deliberately leave the radius_session_start time_t alone;
4338 * it is initialized at the start of the session, regardless of vhost.
4339 */
4340
4341 res = radius_sess_init();
4342 if (res < 0) {
4343 pr_session_disconnect(&radius_module,
4344 PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
4345 }
4346 }
4347
4348 /* Initialization routines
4349 */
4350
4351 static int radius_sess_init(void) {
4352 int res = 0;
4353 config_rec *c = NULL;
4354 radius_server_t **current_server = NULL;
4355
4356 pr_event_register(&radius_module, "core.session-reinit",
4357 radius_sess_reinit_ev, NULL);
4358
4359 /* Is RadiusEngine on? */
4360 c = find_config(main_server->conf, CONF_PARAM, "RadiusEngine", FALSE);
4361 if (c != NULL) {
4362 radius_engine = *((int *) c->argv[0]);
4363 }
4364
4365 if (radius_engine == FALSE) {
4366 return 0;
4367 }
4368
4369 res = radius_openlog();
4370 if (res < 0) {
4371 if (res == -1) {
4372 pr_log_pri(PR_LOG_NOTICE, MOD_RADIUS_VERSION
4373 ": notice: unable to open RadiusLog: %s", strerror(errno));
4374
4375 } else if (res == PR_LOG_WRITABLE_DIR) {
4376 pr_log_pri(PR_LOG_WARNING, MOD_RADIUS_VERSION
4377 ": notice: unable to open RadiusLog: parent directory is "
4378 "world-writable");
4379
4380 } else if (res == PR_LOG_SYMLINK) {
4381 pr_log_pri(PR_LOG_WARNING, MOD_RADIUS_VERSION
4382 ": notice: unable to open RadiusLog: cannot log to a symbolic link");
4383 }
4384 }
4385
4386 /* Initialize session variables */
4387 time(&radius_session_start);
4388
4389 c = find_config(main_server->conf, CONF_PARAM, "RadiusOptions", FALSE);
4390 while (c != NULL) {
4391 unsigned long opts = 0;
4392
4393 pr_signals_handle();
4394
4395 opts = *((unsigned long *) c->argv[0]);
4396 radius_opts |= opts;
4397
4398 c = find_config_next(c, c->next, CONF_PARAM, "RadiusOptions", FALSE);
4399 }
4400
4401 c = find_config(main_server->conf, CONF_PARAM, "RadiusNASIdentifier", FALSE);
4402 if (c != NULL) {
4403 radius_nas_identifier_config = c->argv[0];
4404
4405 pr_trace_msg(trace_channel, 3,
4406 "RadiusNASIdentifier '%s' configured", radius_nas_identifier_config);
4407 }
4408
4409 c = find_config(main_server->conf, CONF_PARAM, "RadiusVendor", FALSE);
4410 if (c != NULL) {
4411 radius_vendor_name = c->argv[0];
4412 radius_vendor_id = *((unsigned int *) c->argv[1]);
4413
4414 pr_trace_msg(trace_channel, 3,
4415 "RadiusVendor '%s' (Vendor-Id %u) configured", radius_vendor_name,
4416 radius_vendor_id);
4417 }
4418
4419 /* Find any configured RADIUS servers for this session */
4420 c = find_config(main_server->conf, CONF_PARAM, "RadiusAcctServer", FALSE);
4421
4422 /* Point to the start of the accounting server list. */
4423 current_server = &radius_acct_server;
4424
4425 while (c != NULL) {
4426 pr_signals_handle();
4427
4428 *current_server = *((radius_server_t **) c->argv[0]);
4429 current_server = &(*current_server)->next;
4430
4431 c = find_config_next(c, c->next, CONF_PARAM, "RadiusAcctServer", FALSE);
4432 }
4433
4434 if (radius_acct_server == NULL) {
4435 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
4436 "notice: no configured RadiusAcctServers, no accounting");
4437 }
4438
4439 c = find_config(main_server->conf, CONF_PARAM, "RadiusAuthServer", FALSE);
4440
4441 /* Point to the start of the authentication server list. */
4442 current_server = &radius_auth_server;
4443
4444 while (c != NULL) {
4445 pr_signals_handle();
4446
4447 *current_server = *((radius_server_t **) c->argv[0]);
4448 current_server = &(*current_server)->next;
4449
4450 c = find_config_next(c, c->next, CONF_PARAM, "RadiusAuthServer", FALSE);
4451 }
4452
4453 if (radius_auth_server == NULL) {
4454 (void) pr_log_writefile(radius_logfd, MOD_RADIUS_VERSION,
4455 "notice: no configured RadiusAuthServers, no authentication");
4456 }
4457
4458 /* Prepare any configured fake user information. */
4459 c = find_config(main_server->conf, CONF_PARAM, "RadiusUserInfo", FALSE);
4460 if (c != NULL) {
4461
4462 /* Process the parameter string stored in the found config_rec. */
4463 radius_process_user_info(c);
4464
4465 /* Only use the faked information if authentication via RADIUS is
4466 * possible. The radius_have_user_info flag will be set to
4467 * TRUE by radius_process_user_info(), unless there was some
4468 * illegal value.
4469 */
4470 if (radius_auth_server == NULL) {
4471 radius_have_user_info = FALSE;
4472 }
4473 }
4474
4475 /* If the RadiusUserInfo directive has not been set (or if it has been
4476 * set, but it was not well-formed), then we will be acting in a
4477 * "yes/no" style of authentication, similar to PAM.
4478 *
4479 * The Auth API tries to use the same module for authenticating a user
4480 * as the one which provided information for that user. If we are not
4481 * providing user information, then we won't get a chance to authenticate
4482 * the user -- unless we disable that Auth API behavior.
4483 */
4484 if (radius_have_user_info == FALSE) {
4485 if (pr_auth_add_auth_only_module("mod_radius.c") < 0) {
4486 pr_log_debug(DEBUG2, "error adding 'mod_radius.c' to auth-only module "
4487 "list: %s", strerror(errno));
4488 }
4489 }
4490
4491 /* Prepare any configured fake group information. */
4492 c = find_config(main_server->conf, CONF_PARAM, "RadiusGroupInfo", FALSE);
4493 if (c != NULL) {
4494
4495 /* Process the parameter string stored in the found config_rec. */
4496 radius_process_group_info(c);
4497
4498 /* Only use the faked information if authentication via RADIUS is
4499 * possible. The radius_have_group_info flag will be set to
4500 * TRUE by radius_process_group_info(), unless there was some
4501 * illegal value.
4502 */
4503 if (radius_auth_server == NULL) {
4504 radius_have_group_info = FALSE;
4505 }
4506 }
4507
4508 /* Prepare any configure quota information. */
4509 c = find_config(main_server->conf, CONF_PARAM, "RadiusQuotaInfo", FALSE);
4510 if (c != NULL) {
4511 radius_process_quota_info(c);
4512
4513 if (radius_auth_server == NULL) {
4514 radius_have_quota_info = FALSE;
4515 }
4516 }
4517
4518 /* Check for a configured RadiusRealm. If present, use username + realm
4519 * in RADIUS packets as the user name, else just use the username.
4520 */
4521 radius_realm = get_param_ptr(main_server->conf, "RadiusRealm", FALSE);
4522 if (radius_realm) {
4523 pr_trace_msg(trace_channel, 3,
4524 "using RadiusRealm '%s'", radius_realm);
4525 }
4526
4527 pr_event_register(&radius_module, "core.exit", radius_exit_ev, NULL);
4528 return 0;
4529 }
4530
4531 static int radius_init(void) {
4532
4533 /* Allocate a pool for this module's use. */
4534 radius_pool = make_sub_pool(permanent_pool);
4535 pr_pool_tag(radius_pool, MOD_RADIUS_VERSION);
4536
4537 #if defined(PR_SHARED_MODULE)
4538 pr_event_register(&radius_module, "core.module-unload",
4539 radius_mod_unload_ev, NULL);
4540 #endif /* PR_SHARED_MODULE */
4541
4542 /* Register a restart handler, to cleanup the pool. */
4543 pr_event_register(&radius_module, "core.restart", radius_restart_ev, NULL);
4544
4545 return 0;
4546 }
4547
4548 /* Module API tables
4549 */
4550
4551 static conftable radius_conftab[] = {
4552 { "RadiusAcctServer", set_radiusacctserver, NULL },
4553 { "RadiusAuthServer", set_radiusauthserver, NULL },
4554 { "RadiusEngine", set_radiusengine, NULL },
4555 { "RadiusGroupInfo", set_radiusgroupinfo, NULL },
4556 { "RadiusLog", set_radiuslog, NULL },
4557 { "RadiusNASIdentifier", set_radiusnasidentifier,NULL },
4558 { "RadiusOptions", set_radiusoptions, NULL },
4559 { "RadiusQuotaInfo", set_radiusquotainfo, NULL },
4560 { "RadiusRealm", set_radiusrealm, NULL },
4561 { "RadiusUserInfo", set_radiususerinfo, NULL },
4562 { "RadiusVendor", set_radiusvendor, NULL },
4563 { NULL }
4564 };
4565
4566 static cmdtable radius_cmdtab[] = {
4567 { HOOK, "radius_quota_lookup", G_NONE,
4568 radius_quota_lookup, FALSE, FALSE },
4569
4570 { PRE_CMD, C_PASS, G_NONE, radius_pre_pass, FALSE, FALSE, CL_AUTH },
4571 { POST_CMD, C_PASS, G_NONE, radius_post_pass, FALSE, FALSE, CL_AUTH },
4572 { POST_CMD_ERR, C_PASS, G_NONE, radius_post_pass_err, FALSE, FALSE, CL_AUTH },
4573 { 0, NULL }
4574 };
4575
4576 static authtable radius_authtab[] = {
4577 { 0, "setpwent", radius_setpwent },
4578 { 0, "setgrent", radius_setgrent },
4579 { 0, "endpwent", radius_endpwent },
4580 { 0, "endgrent", radius_endgrent },
4581 { 0, "getpwent", radius_getpwent },
4582 { 0, "getgrent", radius_getgrent },
4583 { 0, "getpwnam", radius_getpwnam },
4584 { 0, "getgrnam", radius_getgrnam },
4585 { 0, "getpwuid", radius_getpwuid },
4586 { 0, "getgrgid", radius_getgrgid },
4587 { 0, "getgroups", radius_getgroups },
4588 { 0, "auth", radius_auth },
4589 { 0, "check", radius_check },
4590 { 0, "uid2name", radius_uid2name },
4591 { 0, "gid2name", radius_gid2name },
4592 { 0, "name2uid", radius_name2uid },
4593 { 0, "name2gid", radius_name2gid },
4594 { 0, NULL }
4595 };
4596
4597 module radius_module = {
4598
4599 /* Always NULL */
4600 NULL, NULL,
4601
4602 /* Module API version 2.0 */
4603 0x20,
4604
4605 /* Module name */
4606 "radius",
4607
4608 /* Module configuration handler table */
4609 radius_conftab,
4610
4611 /* Module command handler table */
4612 radius_cmdtab,
4613
4614 /* Module authentication handler table */
4615 radius_authtab,
4616
4617 /* Module initialization function */
4618 radius_init,
4619
4620 /* Module session initialization function */
4621 radius_sess_init,
4622
4623 /* Module version */
4624 MOD_RADIUS_VERSION
4625 };