|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
* RADIUS -- Remote Authentication Dial In User Service |
| 3 |
* |
| 4 |
* |
| 5 |
* Livingston Enterprises, Inc. 6920 Koll Center Parkway Pleasanton, CA 94566 |
| 6 |
* |
| 7 |
* Copyright 1992 Livingston Enterprises, Inc. |
| 8 |
* |
| 9 |
* Permission to use, copy, modify, and distribute this software for any |
| 10 |
* purpose and without fee is hereby granted, provided that this copyright |
| 11 |
* and permission notice appear on all copies and supporting documentation, |
| 12 |
* the name of Livingston Enterprises, Inc. not be used in advertising or |
| 13 |
* publicity pertaining to distribution of the program without specific |
| 14 |
* prior permission, and notice be given in supporting documentation that |
| 15 |
* copying and distribution is by permission of Livingston Enterprises, Inc. |
| 16 |
* |
| 17 |
* Livingston Enterprises, Inc. makes no representations about the suitability |
| 18 |
* of this software for any purpose. It is provided "as is" without express |
| 19 |
* or implied warranty. |
| 20 |
* |
| 21 |
* [C] The Regents of the University of Michigan and Merit Network, Inc. 1992, |
| 22 |
* 1993, 1994, 1995, 1996 All Rights Reserved |
| 23 |
* |
| 24 |
* Permission to use, copy, modify, and distribute this software and its |
| 25 |
* documentation for any purpose and without fee is hereby granted, provided |
| 26 |
* that the above copyright notice and this permission notice appear in all |
| 27 |
* copies of the software and derivative works or modified versions thereof, |
| 28 |
* and that both the copyright notice and this permission and disclaimer |
| 29 |
* notice appear in supporting documentation. |
| 30 |
* |
| 31 |
* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
| 32 |
* EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF |
| 33 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE REGENTS OF THE |
| 34 |
* UNIVERSITY OF MICHIGAN AND MERIT NETWORK, INC. DO NOT WARRANT THAT THE |
| 35 |
* FUNCTIONS CONTAINED IN THE SOFTWARE WILL MEET LICENSEE'S REQUIREMENTS OR |
| 36 |
* THAT OPERATION WILL BE UNINTERRUPTED OR ERROR FREE. The Regents of the |
| 37 |
* University of Michigan and Merit Network, Inc. shall not be liable for |
| 38 |
* any special, indirect, incidental or consequential damages with respect |
| 39 |
* to any claim by Licensee or any third party arising from use of the |
| 40 |
* software. |
| 41 |
* |
| 42 |
* Public entry points in this file: |
| 43 |
* |
| 44 |
* add_file_list |
| 45 |
* config_init |
| 46 |
* config_files |
| 47 |
* config_fini |
| 48 |
* dns_recv |
| 49 |
* find_auth_type |
| 50 |
* find_auth_ent |
| 51 |
* find_client |
| 52 |
* find_client_by_name |
| 53 |
* find_host_by_name |
| 54 |
* free_user_ent |
| 55 |
* get_our_addr |
| 56 |
* ip_hostname |
| 57 |
* list_cat |
| 58 |
* list_copy |
| 59 |
* pair_parse |
| 60 |
* return_file_list |
| 61 |
* user_find |
| 62 |
* user_gettime |
| 63 |
* user_update |
| 64 |
* |
| 65 |
*/ |
| 66 |
|
| 67 |
static char sccsid[] = |
| 68 |
"@(#)users.c 1.3 Copyright 1992 Livingston Enterprises Inc"; |
| 69 |
|
| 70 |
static char rcsid[] = "$Id: users.c,v 1.89 1996/06/11 21:10:39 web Exp $"; |
| 71 |
|
| 72 |
#include <sys/types.h> |
| 73 |
#include <sys/param.h> |
| 74 |
#include <sys/socket.h> |
| 75 |
#include <sys/time.h> |
| 76 |
#include <netinet/in.h> |
| 77 |
#include <arpa/inet.h> |
| 78 |
|
| 79 |
#include <stdio.h> |
| 80 |
#include <stdlib.h> |
| 81 |
#include <netdb.h> |
| 82 |
#include <time.h> |
| 83 |
#include <ctype.h> |
| 84 |
#include <dirent.h> |
| 85 |
#include <syslog.h> |
| 86 |
|
| 87 |
#include "radius.h" |
| 88 |
|
| 89 |
static void fieldcpy PROTO((char *, char **)); |
| 90 |
static FILE_LIST *find_file_ent PROTO((char *)); |
| 91 |
static void free_clients PROTO((CLIENT_ENTRY *)); |
| 92 |
static void free_file_lists PROTO((void)); |
| 93 |
static int host_is_us PROTO((char *)); |
| 94 |
static int insert_client PROTO((char *, char *, char *)); |
| 95 |
static int read_auth PROTO((FILE_LIST *, int)); |
| 96 |
static int read_users PROTO((FILE_LIST *, int)); |
| 97 |
|
| 98 |
#if defined(ultrix) || defined(__hpux) || defined(__bsdi__) || defined(linux) || defined(SCO) |
| 99 |
extern int h_errno; |
| 100 |
#endif /* ultrix */ |
| 101 |
|
| 102 |
extern char send_buffer[4096]; |
| 103 |
extern char recv_buffer[4096]; |
| 104 |
extern char ourhostname[MAXHOSTNAMELEN]; |
| 105 |
extern AATV *authtype_tv[]; /* AATVs by authentication types */ |
| 106 |
extern int debug_flag; |
| 107 |
extern int dumpcore; |
| 108 |
extern char *radius_dir; |
| 109 |
extern FILE *ddt; |
| 110 |
extern int authfile_cnt; |
| 111 |
extern int clients_cnt; |
| 112 |
extern int users_cnt; |
| 113 |
extern int file_logging; /* 0 => syslog, 1 => logfile, 2 => stderr */ |
| 114 |
extern AATV *rad_ipc_aatv; |
| 115 |
extern FILE *msgfd; |
| 116 |
|
| 117 |
int spawn_flag = 1; |
| 118 |
int dnspid = 0; /* PID of current DNS resolver process */ |
| 119 |
char default_radius_server[128] = DEFAULT_RADIUS_SERVER; |
| 120 |
char default_tacacs_server[128] = DEFAULT_TACACS_SERVER; |
| 121 |
int rad_ipc_port = 0; |
| 122 |
|
| 123 |
static FILE_LIST *file_list = (FILE_LIST *) NULL; |
| 124 |
static CLIENT_ENTRY *client_list = (CLIENT_ENTRY *) NULL; |
| 125 |
|
| 126 |
static UINT4 self_ip[11]; /* Used with multi-homed servers */ |
| 127 |
static int is_engine = 0; /* rlmadmin will not change this */ |
| 128 |
static CLIENT_ENTRY *old_clients; |
| 129 |
|
| 130 |
/************************************************************************* |
| 131 |
* |
| 132 |
* Function: add_file_list |
| 133 |
* |
| 134 |
* Purpose: Find an existing FILE_LIST entry on file_list with the |
| 135 |
* specified prefix or add and init a new one if the |
| 136 |
* entry doesn't already exist. |
| 137 |
* |
| 138 |
*************************************************************************/ |
| 139 |
|
| 140 |
int |
| 141 |
add_file_list (prefix) |
| 142 |
|
| 143 |
char *prefix; |
| 144 |
|
| 145 |
{ |
| 146 |
FILE_LIST *file_ent; |
| 147 |
FILE_LIST **fl_prev; |
| 148 |
static char *func = "add_file_list"; |
| 149 |
|
| 150 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 151 |
|
| 152 |
for (fl_prev = &file_list, file_ent = file_list; |
| 153 |
file_ent; |
| 154 |
fl_prev = &file_ent->next, file_ent = *fl_prev) |
| 155 |
{ |
| 156 |
if (strcmp (file_ent->prefix, prefix) == 0) |
| 157 |
{ |
| 158 |
return 0; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if ((file_ent = (FILE_LIST *) malloc (sizeof (FILE_LIST))) |
| 163 |
== (FILE_LIST *) NULL) |
| 164 |
{ |
| 165 |
logit (LOG_DAEMON, LOG_ALERT, |
| 166 |
"%s: Couldn't allocate FILE_ENTRY storage", func); |
| 167 |
return (-1); |
| 168 |
} |
| 169 |
file_ent->prefix = add_string (prefix, ASIS); |
| 170 |
file_ent->user_list = (USER_ENTRY *) NULL; |
| 171 |
file_ent->auth_list = (AUTH_ENTRY *) NULL; |
| 172 |
file_ent->next = (FILE_LIST *) NULL; |
| 173 |
*fl_prev = file_ent; |
| 174 |
|
| 175 |
return 0; |
| 176 |
} /* end of add_file_list () */ |
| 177 |
|
| 178 |
/************************************************************************* |
| 179 |
* |
| 180 |
* Function: config_init |
| 181 |
* |
| 182 |
* Purpose: Setup environment for config_files() to run in. |
| 183 |
* |
| 184 |
*************************************************************************/ |
| 185 |
|
| 186 |
void |
| 187 |
config_init () |
| 188 |
|
| 189 |
{ |
| 190 |
is_engine = 1; /* flag set when engine calls us */ |
| 191 |
|
| 192 |
/* |
| 193 |
* Set dnspid to defer the call to update_clients() |
| 194 |
* until the end of config_files(). |
| 195 |
*/ |
| 196 |
if (dnspid == 0) |
| 197 |
{ |
| 198 |
dnspid = -1; |
| 199 |
} |
| 200 |
|
| 201 |
/* |
| 202 |
* Save the old clients list so we can pick up |
| 203 |
* the DNS addresses for the new list. |
| 204 |
*/ |
| 205 |
old_clients = client_list; |
| 206 |
client_list = (CLIENT_ENTRY *) NULL; |
| 207 |
return; |
| 208 |
} /* end of config_init () */ |
| 209 |
|
| 210 |
/************************************************************************* |
| 211 |
* |
| 212 |
* Function: config_files |
| 213 |
* |
| 214 |
* Purpose: Read database files into memory data structures. Reads |
| 215 |
* "RADIUS_CLIENTS" and "RADIUS_AUTH" files unconditionally |
| 216 |
* and "RADIUS_USERS" file if users_flag is not zero. Will |
| 217 |
* read multiple users and authfiles if the "file_pfx" is |
| 218 |
* specified in a client entry (allowing use of different |
| 219 |
* files for different client NASes). |
| 220 |
* |
| 221 |
* If clear_flag is greater than zero, remove existing entries. |
| 222 |
* |
| 223 |
* A new CLIENT_ENTRY is added to the client_list for each |
| 224 |
* client appearing in the "RADIUS_CLIENTS" file. A new |
| 225 |
* FILE_LIST entry is added for each unique file_pfx found |
| 226 |
* in the "RADIUS_CLIENTS" file. Each FILE_LIST entry points |
| 227 |
* to a list of USER_ENTRYs containing the information read |
| 228 |
* from the "RADIUS_USERS" file with the corresponding |
| 229 |
* file_pfx. Also each FILE_LIST entry contains a pointer to |
| 230 |
* the list of AUTH_ENTRYs containing realm information read |
| 231 |
* from the "RADIUS_AUTH" file with the corresponding file_pfx. |
| 232 |
* If either the "RADIUS_USERS" file or the "RADIUS_AUTH" file |
| 233 |
* with the given file_pfx did not exist, the default |
| 234 |
* (non-prefixed) file name entries are used instead. |
| 235 |
* |
| 236 |
*************************************************************************/ |
| 237 |
|
| 238 |
int |
| 239 |
config_files (users_flag, clear_flag, dolog) |
| 240 |
|
| 241 |
int users_flag; |
| 242 |
int clear_flag; |
| 243 |
int dolog; |
| 244 |
|
| 245 |
{ |
| 246 |
int i; |
| 247 |
int ent_cnt = 0; |
| 248 |
int line_nbr = 0; |
| 249 |
int result; |
| 250 |
FILE_LIST *file_ent; |
| 251 |
FILE *clientfd; |
| 252 |
struct hostent *hp; |
| 253 |
char *file_pfx; |
| 254 |
char *host; |
| 255 |
char *hostnm; |
| 256 |
char *secret; |
| 257 |
char **paddr; |
| 258 |
char buffer[128]; |
| 259 |
char fname[MAXPATHLEN]; |
| 260 |
static char *func = "config_files"; |
| 261 |
|
| 262 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 263 |
|
| 264 |
if (clear_flag > 0) /* Free current list, if already set up */ |
| 265 |
{ |
| 266 |
free_file_lists (); |
| 267 |
} |
| 268 |
|
| 269 |
/* |
| 270 |
* Add default file_list entry - the entry for the "users" and |
| 271 |
* "authfile" with no optional prefix |
| 272 |
*/ |
| 273 |
add_file_list (""); |
| 274 |
|
| 275 |
/* |
| 276 |
* Determine the IP address(es) of this machine |
| 277 |
*/ |
| 278 |
if ((hp = gethostbyname (ourhostname)) == (struct hostent *) NULL) |
| 279 |
{ |
| 280 |
logit (LOG_DAEMON, LOG_CRIT, |
| 281 |
"%s: Couldn't get our own IP address(es)", func); |
| 282 |
return (-1); |
| 283 |
} |
| 284 |
|
| 285 |
/* |
| 286 |
* First clear, then save our IP address(es) |
| 287 |
* leaving a zero entry at the end. |
| 288 |
*/ |
| 289 |
memset ((char *) self_ip, '\0', sizeof (self_ip)); |
| 290 |
|
| 291 |
if (hp->h_addr_list != (char **) NULL) |
| 292 |
{ |
| 293 |
for (i = 0, paddr = hp->h_addr_list; |
| 294 |
(*paddr != (char *) NULL) && |
| 295 |
(i < sizeof (self_ip) / 4 - 1); |
| 296 |
i++, paddr++) |
| 297 |
{ |
| 298 |
memcpy ((char *) &self_ip[i], (char *) *paddr, |
| 299 |
hp->h_length); |
| 300 |
self_ip[i] = ntohl(self_ip[i]); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
/* |
| 305 |
* Now read in all client file entries, adding a new "CLIENT_ENTRY" |
| 306 |
* to client_list for each valid file entry found. If there is more |
| 307 |
* than one IP address for a given client DNS name, add an entry for |
| 308 |
* each address. |
| 309 |
*/ |
| 310 |
sprintf (fname, "%s/%s", radius_dir, RADIUS_CLIENTS); |
| 311 |
if ((clientfd = fopen (fname, "r")) == (FILE *) NULL) |
| 312 |
{ |
| 313 |
logit (LOG_DAEMON, LOG_ERR, "%s: Couldn't open %s for reading", |
| 314 |
func, fname); |
| 315 |
return (-1); |
| 316 |
} |
| 317 |
|
| 318 |
clients_cnt = 0; |
| 319 |
result = -1; /* Init result code */ |
| 320 |
|
| 321 |
while (fgets (buffer, sizeof (buffer), clientfd) != (char *) NULL) |
| 322 |
{ |
| 323 |
line_nbr++; |
| 324 |
|
| 325 |
if (*buffer == COMMENT) |
| 326 |
{ |
| 327 |
continue; |
| 328 |
} |
| 329 |
|
| 330 |
/* first hostname */ |
| 331 |
if ((hostnm = strtok (buffer, " \t\n\r")) == (char *) NULL) |
| 332 |
{ |
| 333 |
continue; |
| 334 |
} |
| 335 |
|
| 336 |
/* and secret field */ |
| 337 |
if ((secret = strtok (NULL, " \t\n\r")) == (char *) NULL) |
| 338 |
{ |
| 339 |
logit (LOG_DAEMON, LOG_ERR, |
| 340 |
"%s: missing shared secret: %s line %d", |
| 341 |
func, fname, line_nbr); |
| 342 |
continue; |
| 343 |
} |
| 344 |
|
| 345 |
file_pfx = strtok (NULL, " \t\n\r"); /* optional prefix */ |
| 346 |
|
| 347 |
/* |
| 348 |
* Look for "paired" <name1>/<name2> entries. This type of |
| 349 |
* entry allows core RADIUS servers to share a common "clients" |
| 350 |
* file. If we're name1, name2 is the target name; if we are |
| 351 |
* name2, then name1 is the target. If we're neither name, |
| 352 |
* then this entry isn't of interest to us and we ignore it. |
| 353 |
*/ |
| 354 |
host = hostnm; /* Assume just one name */ |
| 355 |
|
| 356 |
if (strchr (hostnm, '/')) /* If <name1>/<name2> form */ |
| 357 |
{ |
| 358 |
strtok (hostnm, "/"); |
| 359 |
host = strtok (NULL, " "); |
| 360 |
if (host_is_us (hostnm)) |
| 361 |
{ |
| 362 |
/* We are 1st name - client is 2nd */ |
| 363 |
} |
| 364 |
else |
| 365 |
{ |
| 366 |
if (host_is_us (host)) |
| 367 |
{ |
| 368 |
/* We are 2nd - client is 1st */ |
| 369 |
host = hostnm; |
| 370 |
} |
| 371 |
else /* We are neither - no match */ |
| 372 |
{ |
| 373 |
continue; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
if ((result = insert_client (host, secret, file_pfx)) < 0) |
| 379 |
{ |
| 380 |
break; |
| 381 |
} |
| 382 |
ent_cnt++; |
| 383 |
} /* end of while () */ |
| 384 |
fclose (clientfd); |
| 385 |
if (dolog) |
| 386 |
{ |
| 387 |
logit (LOG_DAEMON, LOG_INFO, |
| 388 |
"%s: %s (%u entries) read to memory", |
| 389 |
func, fname, ent_cnt); |
| 390 |
} |
| 391 |
clients_cnt = ent_cnt; |
| 392 |
|
| 393 |
/* |
| 394 |
* Finally, go through all the file_list entries just added, reading |
| 395 |
* in the "users" and "authfile" for each prefix found. |
| 396 |
*/ |
| 397 |
for (file_ent = file_list; file_ent; file_ent = file_ent->next) |
| 398 |
{ |
| 399 |
|
| 400 |
#if !(defined(USE_DBM) || defined(USE_NDBM)) |
| 401 |
if (users_flag) |
| 402 |
if (read_users (file_ent, dolog) != 0) |
| 403 |
{ |
| 404 |
return (-1); |
| 405 |
} |
| 406 |
#endif /* USE_DBM || USE_NDBM */ |
| 407 |
|
| 408 |
if (read_auth (file_ent, dolog) != 0) |
| 409 |
{ |
| 410 |
return (-1); |
| 411 |
} |
| 412 |
|
| 413 |
} |
| 414 |
|
| 415 |
if (result < 0) |
| 416 |
{ |
| 417 |
return (-1); |
| 418 |
} |
| 419 |
|
| 420 |
return 0; |
| 421 |
} /* end of config_files () */ |
| 422 |
|
| 423 |
/************************************************************************* |
| 424 |
* |
| 425 |
* Function: config_fini |
| 426 |
* |
| 427 |
* Purpose: Cleanup environment after config_files() has run. |
| 428 |
* |
| 429 |
*************************************************************************/ |
| 430 |
|
| 431 |
void |
| 432 |
config_fini () |
| 433 |
|
| 434 |
{ |
| 435 |
free_clients (old_clients); |
| 436 |
old_clients = (CLIENT_ENTRY *) NULL; |
| 437 |
|
| 438 |
if ((file_logging == 1) && (msgfd != (FILE *) NULL)) |
| 439 |
{ |
| 440 |
fflush (msgfd); |
| 441 |
} |
| 442 |
|
| 443 |
if (dnspid == -1) |
| 444 |
{ |
| 445 |
dnspid = 0; |
| 446 |
} |
| 447 |
|
| 448 |
update_clients (); /* Start up the DNS resolver. */ |
| 449 |
|
| 450 |
return; |
| 451 |
} /* end of config_fini () */ |
| 452 |
|
| 453 |
/************************************************************************* |
| 454 |
* |
| 455 |
* Function: dns_recv |
| 456 |
* |
| 457 |
* Purpose: Process received DNS updates for clients database. |
| 458 |
* |
| 459 |
*************************************************************************/ |
| 460 |
|
| 461 |
void |
| 462 |
dns_recv (sin, from_ipaddr, rcvlen) |
| 463 |
|
| 464 |
struct sockaddr_in *sin; |
| 465 |
UINT4 from_ipaddr; |
| 466 |
int rcvlen; |
| 467 |
|
| 468 |
{ |
| 469 |
int cnt; |
| 470 |
u_char aliascnt; |
| 471 |
UINT4 temp; |
| 472 |
UINT4 *ourad; |
| 473 |
char *ptr; |
| 474 |
IP_ADDRESS *an_address; |
| 475 |
DNS_NAME *a_name; |
| 476 |
CLIENT_ENTRY *client_ent; |
| 477 |
static int notify_count = 0; |
| 478 |
static char *func = "dns_recv"; |
| 479 |
|
| 480 |
notify_count ++; |
| 481 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered (%d)", |
| 482 |
func, notify_count)); |
| 483 |
|
| 484 |
ptr = recv_buffer + 1; |
| 485 |
|
| 486 |
for (ourad = self_ip; |
| 487 |
(*ourad != (UINT4) 0) && (*ourad != from_ipaddr); |
| 488 |
ourad++) |
| 489 |
{ |
| 490 |
continue; |
| 491 |
} |
| 492 |
|
| 493 |
if (*ourad == (UINT4) 0) |
| 494 |
{ |
| 495 |
logit (LOG_DAEMON, LOG_INFO, "%s: from %s - Security Breach", |
| 496 |
func, ip_hostname (from_ipaddr)); |
| 497 |
return; |
| 498 |
} |
| 499 |
|
| 500 |
for (client_ent = client_list; |
| 501 |
client_ent != (CLIENT_ENTRY *) NULL; |
| 502 |
client_ent = client_ent->next) |
| 503 |
{ |
| 504 |
if (strcmp (ptr, client_ent->hostname) == 0) |
| 505 |
{ |
| 506 |
break; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
if (client_ent == (CLIENT_ENTRY *) NULL) |
| 511 |
{ |
| 512 |
return; |
| 513 |
} |
| 514 |
|
| 515 |
ptr += strlen (ptr) + 1; |
| 516 |
aliascnt = *ptr++; |
| 517 |
|
| 518 |
if (*ptr != '\0') /* If alias or IP address present, clear old ones */ |
| 519 |
{ |
| 520 |
/* |
| 521 |
* Reset expire_time with some randomness (0 - 60 minutes) |
| 522 |
* to avoid a burst of calls to gethostbyname(). |
| 523 |
*/ |
| 524 |
client_ent->expire_time = (time (0) + ADDRESS_AGING + |
| 525 |
(rand () & 3) * 60 * 20) & ~3; |
| 526 |
for (an_address = client_ent->addrs; |
| 527 |
an_address != (IP_ADDRESS *) NULL; |
| 528 |
an_address = client_ent->addrs) |
| 529 |
{ |
| 530 |
client_ent->addrs = an_address->next; |
| 531 |
free (an_address); |
| 532 |
} |
| 533 |
|
| 534 |
for (a_name = client_ent->names; |
| 535 |
a_name != (DNS_NAME *) NULL; |
| 536 |
a_name = client_ent->names) |
| 537 |
{ |
| 538 |
client_ent->names = a_name->next; |
| 539 |
free (a_name); |
| 540 |
} |
| 541 |
} |
| 542 |
else /* no alias or IP address present */ |
| 543 |
{ |
| 544 |
memcpy ((char *) &temp, ptr, sizeof (struct in_addr)); |
| 545 |
if (temp == TRY_AGAIN) /* TRY_AGAIN is in netdb.h */ |
| 546 |
{ |
| 547 |
client_ent->expire_time = (time (0) + ADDRESS_AGING) |
| 548 |
& ~3; |
| 549 |
logit (LOG_DAEMON, LOG_ALERT, |
| 550 |
"%s: DNS timeout on client or host '%s'", |
| 551 |
func, client_ent->hostname); |
| 552 |
return; |
| 553 |
} |
| 554 |
else |
| 555 |
{ |
| 556 |
/* |
| 557 |
* Name couldn't be resolved -- log it and retry |
| 558 |
* shortly. Cleverly (or foolishly) use the low |
| 559 |
* two bits of expire_time to control the logging |
| 560 |
* frequency. |
| 561 |
*/ |
| 562 |
if ((cnt = client_ent->expire_time & 3) == 0) |
| 563 |
{ |
| 564 |
/* Log every fourth time (or once per hour) */ |
| 565 |
logit (LOG_DAEMON, LOG_ALERT, |
| 566 |
"%s: DNS couldn't resolve name '%s' (%ld)", |
| 567 |
func, client_ent->hostname, temp); |
| 568 |
} |
| 569 |
|
| 570 |
client_ent->expire_time = ((time (0) + ADDRESS_AGING/4) |
| 571 |
& ~3) | (++cnt & 3); |
| 572 |
if (client_ent->addrs != (IP_ADDRESS *) NULL) |
| 573 |
{ |
| 574 |
return; |
| 575 |
} |
| 576 |
|
| 577 |
/* Add invalid address to client_ent */ |
| 578 |
memset (ptr, 255, sizeof (struct in_addr)); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
/* Add alias names to client_ent structure */ |
| 583 |
while (aliascnt-- > 0) |
| 584 |
{ |
| 585 |
/* Note that DNS_NAME structure reserves one extra character. */ |
| 586 |
if ((a_name = |
| 587 |
(DNS_NAME *) malloc (sizeof (DNS_NAME) + strlen (ptr))) |
| 588 |
== (DNS_NAME *) NULL) |
| 589 |
{ |
| 590 |
logit (LOG_DAEMON, LOG_ALERT, |
| 591 |
"%s: FATAL Couldn't allocate DNS_NAME storage", |
| 592 |
func); |
| 593 |
abort (); |
| 594 |
} |
| 595 |
|
| 596 |
/* Note that type zero will always be last one. */ |
| 597 |
strcpy (a_name->name, ptr); |
| 598 |
ptr += strlen (ptr) + 1; |
| 599 |
a_name->type = (u_char) *ptr++; |
| 600 |
a_name->next = client_ent->names; |
| 601 |
client_ent->names = a_name; |
| 602 |
} |
| 603 |
|
| 604 |
/* |
| 605 |
* For each address in the list, add the address to the client_ent. |
| 606 |
*/ |
| 607 |
while (*ptr != '\0') |
| 608 |
{ |
| 609 |
if ((an_address = (IP_ADDRESS *) malloc (sizeof (IP_ADDRESS))) |
| 610 |
== (IP_ADDRESS *) NULL) |
| 611 |
{ |
| 612 |
logit (LOG_DAEMON, LOG_ALERT, |
| 613 |
"%s: FATAL Couldn't allocate IP_ADDRESS storage", |
| 614 |
func); |
| 615 |
abort (); |
| 616 |
} |
| 617 |
|
| 618 |
an_address->next = client_ent->addrs; |
| 619 |
memcpy ((char *) &temp, ptr, sizeof (struct in_addr)); |
| 620 |
an_address->ipaddr.s_addr = ntohl(temp); |
| 621 |
client_ent->addrs = an_address; |
| 622 |
ptr += sizeof (struct in_addr); |
| 623 |
} |
| 624 |
|
| 625 |
if (notify_count % DNS_SLEEP == DNS_SLEEP - 1) |
| 626 |
{ |
| 627 |
logit (LOG_DAEMON, LOG_INFO, "%s: Notified of (%d) DNS changes", |
| 628 |
func, notify_count); |
| 629 |
} |
| 630 |
|
| 631 |
return; |
| 632 |
} /* end of dns_recv () */ |
| 633 |
|
| 634 |
/************************************************************************* |
| 635 |
* |
| 636 |
* Function: fieldcpy |
| 637 |
* |
| 638 |
* Purpose: Copy a data field from the buffer. Advance the buffer |
| 639 |
* past the data field. |
| 640 |
* |
| 641 |
*************************************************************************/ |
| 642 |
|
| 643 |
static void |
| 644 |
fieldcpy (string, uptr) |
| 645 |
|
| 646 |
char *string; |
| 647 |
char **uptr; |
| 648 |
|
| 649 |
{ |
| 650 |
char *ptr; |
| 651 |
|
| 652 |
ptr = *uptr; |
| 653 |
if (*ptr == '"') |
| 654 |
{ |
| 655 |
ptr++; |
| 656 |
while (*ptr != '"' && *ptr != '\0' && *ptr != '\n') |
| 657 |
{ |
| 658 |
*string++ = *ptr++; |
| 659 |
} |
| 660 |
*string = '\0'; |
| 661 |
if (*ptr == '"') |
| 662 |
{ |
| 663 |
ptr++; |
| 664 |
} |
| 665 |
*uptr = ptr; |
| 666 |
return; |
| 667 |
} |
| 668 |
|
| 669 |
while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' && |
| 670 |
*ptr != '=' && *ptr != ',') |
| 671 |
{ |
| 672 |
*string++ = *ptr++; |
| 673 |
} |
| 674 |
*string = '\0'; |
| 675 |
*uptr = ptr; |
| 676 |
return; |
| 677 |
} /* end of fieldcpy () */ |
| 678 |
|
| 679 |
/************************************************************************* |
| 680 |
* |
| 681 |
* Function: find_auth_ent |
| 682 |
* |
| 683 |
* Purpose: Gives access to the private AUTH_ENT for the given realm. |
| 684 |
* |
| 685 |
* Returns: pointer to the AUTH_ENT for the given realm, |
| 686 |
* or, NULL, if error. |
| 687 |
* |
| 688 |
*************************************************************************/ |
| 689 |
|
| 690 |
AUTH_ENTRY * |
| 691 |
find_auth_ent (u_realm, prot, pfx) |
| 692 |
|
| 693 |
char *u_realm; |
| 694 |
int prot; |
| 695 |
char *pfx; |
| 696 |
|
| 697 |
{ |
| 698 |
int head; |
| 699 |
int pat_len; |
| 700 |
FILE_LIST *file_ent; |
| 701 |
AUTH_ENTRY *auth_ent; |
| 702 |
AUTH_ENTRY *entry; |
| 703 |
char *p; |
| 704 |
char *realm_name; |
| 705 |
static char temp[AUTH_ID_LEN + 1]; |
| 706 |
static char *func = "find_auth_ent"; |
| 707 |
|
| 708 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 709 |
|
| 710 |
if (u_realm[0] == 0) /* A null realm would match every line. */ |
| 711 |
{ |
| 712 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: NULL realm", func)); |
| 713 |
return (AUTH_ENTRY *) NULL; |
| 714 |
} |
| 715 |
|
| 716 |
if ((file_ent = find_file_ent (pfx)) == (FILE_LIST *) NULL) |
| 717 |
{ |
| 718 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: NULL file_ent", func)); |
| 719 |
return (AUTH_ENTRY *) NULL; |
| 720 |
} |
| 721 |
|
| 722 |
if ((auth_ent = file_ent->auth_list) == (AUTH_ENTRY *) NULL) |
| 723 |
{ |
| 724 |
/* If no auth_list for this prefix */ |
| 725 |
file_ent = file_list; |
| 726 |
/* Default file_ent is first in file_list */ |
| 727 |
if ((auth_ent = file_ent->auth_list) == (AUTH_ENTRY *) NULL) |
| 728 |
{ |
| 729 |
logit (LOG_DAEMON, LOG_ERR, |
| 730 |
"%s: no default authfile data structure", |
| 731 |
func); |
| 732 |
return (AUTH_ENTRY *) NULL; |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
/* |
| 737 |
* Match realm name (either exact match or substring match |
| 738 |
* based on *.realm syntax) with user supplied string. |
| 739 |
*/ |
| 740 |
for ( ; auth_ent ; auth_ent = auth_ent->next ) |
| 741 |
{ |
| 742 |
realm_name = (char *) NULL; |
| 743 |
if (auth_ent->parent == (AUTH_ENTRY *) NULL) /* parent realm */ |
| 744 |
{ |
| 745 |
entry = auth_ent; |
| 746 |
/* Look for name match. */ |
| 747 |
if (entry->name[0] == '*') /* this is wildcard realm */ |
| 748 |
{ |
| 749 |
p = &entry->name[1]; |
| 750 |
pat_len = strlen (p); |
| 751 |
head = strlen (u_realm) - pat_len; |
| 752 |
if (strncmp ((char *) &u_realm[head], |
| 753 |
(char *) &entry->name[1], pat_len) == 0) |
| 754 |
{ |
| 755 |
realm_name = u_realm; |
| 756 |
} |
| 757 |
else |
| 758 |
{ |
| 759 |
realm_name = (char *) NULL; |
| 760 |
} |
| 761 |
} |
| 762 |
else /* not a wildcard realm */ |
| 763 |
{ |
| 764 |
if (strcasecmp (entry->name, u_realm) == 0) |
| 765 |
{ |
| 766 |
realm_name = entry->name; |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
else /* this entry is an alias name for some real realm */ |
| 771 |
{ |
| 772 |
entry = auth_ent->parent; |
| 773 |
/* Look for name match. */ |
| 774 |
if (entry->name[0] == '*') /* alias in wildcard realm */ |
| 775 |
{ |
| 776 |
p = &entry->name[1]; |
| 777 |
pat_len = strlen (p); |
| 778 |
head = strlen (u_realm) - pat_len; |
| 779 |
if (strncmp ((char *) &u_realm[head], |
| 780 |
(char *) &entry->name[1], pat_len) == 0) |
| 781 |
{ |
| 782 |
/* combine real prefix, parent suffix */ |
| 783 |
strcpy (temp, u_realm); |
| 784 |
if (strtok (temp, ".") != (char *) NULL) |
| 785 |
{ |
| 786 |
realm_name = strcat (temp, |
| 787 |
&entry->name[1]); |
| 788 |
} |
| 789 |
} |
| 790 |
else |
| 791 |
{ |
| 792 |
realm_name = (char *) NULL; |
| 793 |
} |
| 794 |
} |
| 795 |
else /* regular alias */ |
| 796 |
{ |
| 797 |
if (strcasecmp (auth_ent->name, u_realm) == 0) |
| 798 |
{ |
| 799 |
realm_name = entry->name; |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
if (realm_name != (char *) NULL) /* then we have a name match */ |
| 805 |
{ |
| 806 |
if (!entry->prot || (entry->prot == prot)) |
| 807 |
{ |
| 808 |
break; |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
if (auth_ent == (AUTH_ENTRY *) NULL) |
| 814 |
{ |
| 815 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: realm not found", func)); |
| 816 |
return (AUTH_ENTRY *) NULL; |
| 817 |
} |
| 818 |
|
| 819 |
return entry; |
| 820 |
} /* end of find_auth_ent () */ |
| 821 |
|
| 822 |
/************************************************************************* |
| 823 |
* |
| 824 |
* Function: find_auth_type |
| 825 |
* |
| 826 |
* Purpose: Find the proper AUTH_ENTRY to use for the given authentication |
| 827 |
* realm name from the FILE_LIST entry with the given file_pfx. |
| 828 |
* |
| 829 |
* Returns: The authentication type, name of the authentication agent to |
| 830 |
* use, the primary realm name and any optional packet filter |
| 831 |
* to be applied are returned. |
| 832 |
* |
| 833 |
* Returns: 0 = normal return, |
| 834 |
* -1 = error return |
| 835 |
* |
| 836 |
*************************************************************************/ |
| 837 |
|
| 838 |
int |
| 839 |
find_auth_type (u_realm, prot, pfx, type, agent, realm, filter) |
| 840 |
|
| 841 |
char *u_realm; |
| 842 |
int prot; |
| 843 |
char *pfx; |
| 844 |
int *type; /* receives resultant authentication type value */ |
| 845 |
char **agent; /* receives resultant authentication agent name */ |
| 846 |
char **realm; /* receives resultant primary realm name */ |
| 847 |
char **filter; /* receives resultant authentication filter name */ |
| 848 |
|
| 849 |
{ |
| 850 |
int head; |
| 851 |
int pat_len; |
| 852 |
FILE_LIST *file_ent; |
| 853 |
AUTH_ENTRY *auth_ent; |
| 854 |
AUTH_ENTRY *entry; |
| 855 |
char *p; |
| 856 |
char *realm_name; |
| 857 |
static char temp[AUTH_ID_LEN + 1]; |
| 858 |
static char *func = "find_auth_type"; |
| 859 |
|
| 860 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 861 |
|
| 862 |
if (u_realm[0] == 0) /* A null realm would match every line. */ |
| 863 |
{ |
| 864 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: NULL realm", func)); |
| 865 |
return (-1); |
| 866 |
} |
| 867 |
|
| 868 |
if ((file_ent = find_file_ent (pfx)) == (FILE_LIST *) NULL) |
| 869 |
{ |
| 870 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: NULL file_ent", func)); |
| 871 |
return (-1); |
| 872 |
} |
| 873 |
|
| 874 |
if ((auth_ent = file_ent->auth_list) == (AUTH_ENTRY *) NULL) |
| 875 |
{ |
| 876 |
/* If no auth_list for this prefix */ |
| 877 |
file_ent = file_list; |
| 878 |
/* Default file_ent is first in file_list */ |
| 879 |
if ((auth_ent = file_ent->auth_list) == (AUTH_ENTRY *) NULL) |
| 880 |
{ |
| 881 |
logit (LOG_DAEMON, LOG_ERR, |
| 882 |
"%s: no default authfile data structure", |
| 883 |
func); |
| 884 |
return (-1); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
/* |
| 889 |
* Match realm name (either exact match or substring match |
| 890 |
* based on *.realm syntax) with user supplied string. |
| 891 |
*/ |
| 892 |
for ( ; auth_ent ; auth_ent = auth_ent->next ) |
| 893 |
{ |
| 894 |
realm_name = (char *) NULL; |
| 895 |
if (auth_ent->parent == (AUTH_ENTRY *) NULL) /* parent realm */ |
| 896 |
{ |
| 897 |
entry = auth_ent; |
| 898 |
/* Look for name match. */ |
| 899 |
if (entry->name[0] == '*') /* this is wildcard realm */ |
| 900 |
{ |
| 901 |
p = &entry->name[1]; |
| 902 |
pat_len = strlen (p); |
| 903 |
head = strlen (u_realm) - pat_len; |
| 904 |
if (strncmp ((char *) &u_realm[head], |
| 905 |
(char *) &entry->name[1], pat_len) == 0) |
| 906 |
{ |
| 907 |
realm_name = u_realm; |
| 908 |
} |
| 909 |
else |
| 910 |
{ |
| 911 |
realm_name = (char *) NULL; |
| 912 |
} |
| 913 |
} |
| 914 |
else /* not a wildcard realm */ |
| 915 |
{ |
| 916 |
if (strcasecmp (entry->name, u_realm) == 0) |
| 917 |
{ |
| 918 |
realm_name = entry->name; |
| 919 |
} |
| 920 |
} |
| 921 |
} |
| 922 |
else /* this entry is an alias name for some real realm */ |
| 923 |
{ |
| 924 |
entry = auth_ent->parent; |
| 925 |
/* Look for name match. */ |
| 926 |
if (entry->name[0] == '*') /* alias in wildcard realm */ |
| 927 |
{ |
| 928 |
p = &entry->name[1]; |
| 929 |
pat_len = strlen (p); |
| 930 |
head = strlen (u_realm) - pat_len; |
| 931 |
if (strncmp ((char *) &u_realm[head], |
| 932 |
(char *) &entry->name[1], pat_len) == 0) |
| 933 |
{ |
| 934 |
/* combine real prefix, parent suffix */ |
| 935 |
strcpy (temp, u_realm); |
| 936 |
if (strtok (temp, ".") != (char *) NULL) |
| 937 |
{ |
| 938 |
realm_name = strcat (temp, |
| 939 |
&entry->name[1]); |
| 940 |
} |
| 941 |
} |
| 942 |
else |
| 943 |
{ |
| 944 |
realm_name = (char *) NULL; |
| 945 |
} |
| 946 |
} |
| 947 |
else /* regular alias */ |
| 948 |
{ |
| 949 |
if (strcasecmp (auth_ent->name, u_realm) == 0) |
| 950 |
{ |
| 951 |
realm_name = entry->name; |
| 952 |
} |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
if (realm_name != (char *) NULL) /* then we have a name match */ |
| 957 |
{ |
| 958 |
if (!entry->prot || (entry->prot == prot)) |
| 959 |
{ |
| 960 |
break; |
| 961 |
} |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
if (auth_ent == (AUTH_ENTRY *) NULL) |
| 966 |
{ |
| 967 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: realm not found", func)); |
| 968 |
return (-1); |
| 969 |
} |
| 970 |
|
| 971 |
*type = entry->type; |
| 972 |
*agent = entry->host; |
| 973 |
*realm = realm_name; |
| 974 |
*filter = entry->filter; |
| 975 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, |
| 976 |
"%s: type %d, agent '%s', realm '%s' and filter '%s'", |
| 977 |
func, entry->type, entry->host, realm_name, entry->filter)); |
| 978 |
return 0; |
| 979 |
} /* end of find_auth_type () */ |
| 980 |
|
| 981 |
/************************************************************************* |
| 982 |
* |
| 983 |
* Function: find_client |
| 984 |
* |
| 985 |
* Purpose: Find the CLIENT_ENTRY in client_list for the client with |
| 986 |
* the given IP address. If the entry is found, the secret |
| 987 |
* shared with this client, any configured file_pfx to be |
| 988 |
* used for this client and the client's name are returned. |
| 989 |
* |
| 990 |
* Returns: 0 = found client entry, |
| 991 |
* -1 = client not found. |
| 992 |
* |
| 993 |
*************************************************************************/ |
| 994 |
|
| 995 |
int |
| 996 |
find_client (ipaddr, hostname, secret, pfx) |
| 997 |
|
| 998 |
UINT4 ipaddr; |
| 999 |
char **hostname; /* Receives resultant hostname, if found */ |
| 1000 |
char **secret; /* Receives resultant secret, if found */ |
| 1001 |
char **pfx; /* Receives resultant prefix, if found */ |
| 1002 |
|
| 1003 |
{ |
| 1004 |
int ud = 0; |
| 1005 |
CLIENT_ENTRY *client_ent; |
| 1006 |
IP_ADDRESS *an_address; |
| 1007 |
time_t cur_time; |
| 1008 |
static char *func = "find_client"; |
| 1009 |
|
| 1010 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 1011 |
|
| 1012 |
cur_time = time (0); |
| 1013 |
for (client_ent = client_list; |
| 1014 |
client_ent; |
| 1015 |
client_ent = client_ent->next) |
| 1016 |
{ |
| 1017 |
if (cur_time > client_ent->expire_time) |
| 1018 |
{ |
| 1019 |
ud = 1; |
| 1020 |
} |
| 1021 |
|
| 1022 |
for (an_address = client_ent->addrs; |
| 1023 |
an_address != (IP_ADDRESS *) NULL; |
| 1024 |
an_address = an_address->next) |
| 1025 |
{ |
| 1026 |
if (an_address->ipaddr.s_addr == ipaddr) |
| 1027 |
{ |
| 1028 |
break; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
if (an_address) |
| 1033 |
{ |
| 1034 |
break; |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
if (ud > 0) |
| 1039 |
{ |
| 1040 |
update_clients (); |
| 1041 |
} |
| 1042 |
|
| 1043 |
/* Don't match host-only entries (those with a null secret) */ |
| 1044 |
if (client_ent == (CLIENT_ENTRY *) NULL || *client_ent->secret == '\0') |
| 1045 |
{ |
| 1046 |
return (-1); |
| 1047 |
} |
| 1048 |
|
| 1049 |
*hostname = client_ent->hostname; |
| 1050 |
*secret = client_ent->secret; |
| 1051 |
*pfx = client_ent->prefix; |
| 1052 |
|
| 1053 |
return (0); |
| 1054 |
} /* end of find_client () */ |
| 1055 |
|
| 1056 |
/************************************************************************* |
| 1057 |
* |
| 1058 |
* Function: find_client_by_name |
| 1059 |
* |
| 1060 |
* Purpose: Find the CLIENT_ENTRY in client_list for the client with |
| 1061 |
* the given hostname. If the entry is found, the secret |
| 1062 |
* shared with this client, any configured file_pfx to be |
| 1063 |
* used for this client and the client's name are returned. |
| 1064 |
* |
| 1065 |
* Returns: 0 = found client entry and resolved IP address, |
| 1066 |
* 1 = found client entry but no IP address, |
| 1067 |
* 2 = found host entry but IP address not obtained |
| 1068 |
* (unresolvable DNS name), |
| 1069 |
* -1 = client not found. |
| 1070 |
* |
| 1071 |
*************************************************************************/ |
| 1072 |
|
| 1073 |
int |
| 1074 |
find_client_by_name (ipaddr, hostname, secret, pfx) |
| 1075 |
|
| 1076 |
UINT4 *ipaddr; /* Receives resultant address, if found */ |
| 1077 |
char *hostname; /* Match this name */ |
| 1078 |
char **secret; /* Receives resultant secret, if found */ |
| 1079 |
char **pfx; /* Receives resultant prefix, if found */ |
| 1080 |
|
| 1081 |
{ |
| 1082 |
int ud = 0; |
| 1083 |
CLIENT_ENTRY *client_ent; |
| 1084 |
DNS_NAME *name_ent; |
| 1085 |
char *name; |
| 1086 |
time_t cur_time; |
| 1087 |
static char *func = "find_client_by_name"; |
| 1088 |
|
| 1089 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 1090 |
|
| 1091 |
if (good_ipaddr (hostname) == 0) |
| 1092 |
{ |
| 1093 |
/* name = address same as find_client() call */ |
| 1094 |
*ipaddr = ntohl(inet_addr (hostname)); |
| 1095 |
return find_client (*ipaddr, &name, secret, pfx); |
| 1096 |
} |
| 1097 |
|
| 1098 |
if (strcmp (hostname, RADIUS_LOCALSERVER) == 0) |
| 1099 |
{ |
| 1100 |
hostname = ourhostname; |
| 1101 |
} |
| 1102 |
|
| 1103 |
cur_time = time (0); |
| 1104 |
for (client_ent = client_list; |
| 1105 |
client_ent != (CLIENT_ENTRY *) NULL; |
| 1106 |
client_ent = client_ent->next) |
| 1107 |
{ |
| 1108 |
if (cur_time > client_ent->expire_time) |
| 1109 |
{ |
| 1110 |
ud = 1; |
| 1111 |
} |
| 1112 |
|
| 1113 |
if (strcmp (client_ent->hostname, hostname) == 0) |
| 1114 |
{ |
| 1115 |
break; |
| 1116 |
} |
| 1117 |
|
| 1118 |
for (name_ent = client_ent->names; |
| 1119 |
name_ent != (DNS_NAME *) NULL; |
| 1120 |
name_ent = name_ent->next) |
| 1121 |
{ |
| 1122 |
if (strcmp (name_ent->name, hostname) == 0) |
| 1123 |
{ |
| 1124 |
break; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
if (name_ent != (DNS_NAME *) NULL) |
| 1129 |
{ |
| 1130 |
break; |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (ud > 0) |
| 1135 |
{ |
| 1136 |
update_clients (); |
| 1137 |
} |
| 1138 |
|
| 1139 |
/* Don't match host-only entries (those with a null secret) */ |
| 1140 |
if (client_ent == (CLIENT_ENTRY *) NULL || *client_ent->secret == '\0') |
| 1141 |
{ |
| 1142 |
return (-1); |
| 1143 |
} |
| 1144 |
|
| 1145 |
*secret = client_ent->secret; |
| 1146 |
*pfx = client_ent->prefix; |
| 1147 |
|
| 1148 |
if (client_ent->addrs == (IP_ADDRESS *) NULL) |
| 1149 |
{ |
| 1150 |
*ipaddr = 0; |
| 1151 |
return (1); |
| 1152 |
} |
| 1153 |
|
| 1154 |
if ((*ipaddr = client_ent->addrs->ipaddr.s_addr) == -1) |
| 1155 |
{ |
| 1156 |
return (2); |
| 1157 |
} |
| 1158 |
|
| 1159 |
return (0); |
| 1160 |
} /* end of find_client_by_name () */ |
| 1161 |
|
| 1162 |
/************************************************************************* |
| 1163 |
* |
| 1164 |
* Function: find_host_by_name |
| 1165 |
* |
| 1166 |
* Purpose: Resolve the host address by looking in the client list. |
| 1167 |
* Non-clients (those with a null secret) in this list |
| 1168 |
* are matched as well as normal clients. |
| 1169 |
* |
| 1170 |
* Returns: 0 = found host entry and resolved IP address, |
| 1171 |
* 1 = found host entry but unresolved IP address, |
| 1172 |
* 2 = found host entry but IP address not obtained |
| 1173 |
* (unresolvable DNS name - uses address 255.255.255.255), |
| 1174 |
* -1 = host not found. |
| 1175 |
* |
| 1176 |
*************************************************************************/ |
| 1177 |
|
| 1178 |
int |
| 1179 |
find_host_by_name (ipaddr, hostname) |
| 1180 |
|
| 1181 |
UINT4 *ipaddr; /* receives resultant address if found */ |
| 1182 |
char *hostname; /* Match this name */ |
| 1183 |
|
| 1184 |
{ |
| 1185 |
int ud = 0; |
| 1186 |
char *p; |
| 1187 |
char *q; |
| 1188 |
CLIENT_ENTRY *client_ent; |
| 1189 |
DNS_NAME *name_ent; |
| 1190 |
time_t cur_time; |
| 1191 |
static char *func = "find_host_by_name"; |
| 1192 |
|
| 1193 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 1194 |
|
| 1195 |
if (good_ipaddr (hostname) == 0) |
| 1196 |
{ |
| 1197 |
*ipaddr = ntohl(inet_addr (hostname)); |
| 1198 |
return 0; |
| 1199 |
} |
| 1200 |
|
| 1201 |
if (strcmp (hostname, RADIUS_LOCALSERVER) == 0) |
| 1202 |
{ |
| 1203 |
*ipaddr = self_ip[0]; |
| 1204 |
return 0; |
| 1205 |
} |
| 1206 |
|
| 1207 |
/* See if it's us. Match full name or up to "." of our name */ |
| 1208 |
for (p = hostname, q = ourhostname; *p == *q; p++, q++) |
| 1209 |
{ |
| 1210 |
if (*p == '\0') |
| 1211 |
{ |
| 1212 |
break; |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
if (*p == '\0' && (*q == '\0' || *q == '.')) |
| 1217 |
{ |
| 1218 |
*ipaddr = self_ip[0]; |
| 1219 |
return 0; |
| 1220 |
} |
| 1221 |
|
| 1222 |
cur_time = time (0); |
| 1223 |
for (client_ent = client_list; |
| 1224 |
client_ent != (CLIENT_ENTRY *) NULL; |
| 1225 |
client_ent = client_ent->next) |
| 1226 |
{ |
| 1227 |
if (cur_time > client_ent->expire_time) |
| 1228 |
{ |
| 1229 |
ud = 1; |
| 1230 |
} |
| 1231 |
|
| 1232 |
if (strcmp (client_ent->hostname, hostname) == 0) |
| 1233 |
{ |
| 1234 |
break; |
| 1235 |
} |
| 1236 |
|
| 1237 |
for (name_ent = client_ent->names; |
| 1238 |
name_ent != (DNS_NAME *) NULL; |
| 1239 |
name_ent = name_ent->next) |
| 1240 |
{ |
| 1241 |
if (strcmp (name_ent->name, hostname) == 0) |
| 1242 |
{ |
| 1243 |
break; |
| 1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
if (name_ent != (DNS_NAME *) NULL) |
| 1248 |
{ |
| 1249 |
break; |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
if (ud > 0) |
| 1254 |
{ |
| 1255 |
update_clients (); |
| 1256 |
} |
| 1257 |
|
| 1258 |
if (client_ent == (CLIENT_ENTRY *) NULL) |
| 1259 |
{ |
| 1260 |
*ipaddr = 0; |
| 1261 |
return (-1); |
| 1262 |
} |
| 1263 |
|
| 1264 |
if (client_ent->addrs == (struct ip_address *) NULL) |
| 1265 |
{ |
| 1266 |
*ipaddr = 0; |
| 1267 |
return (1); |
| 1268 |
} |
| 1269 |
|
| 1270 |
if ((*ipaddr = client_ent->addrs->ipaddr.s_addr) == -1) |
| 1271 |
{ |
| 1272 |
return (2); |
| 1273 |
} |
| 1274 |
return (0); |
| 1275 |
} /* end of find_host_by_name () */ |
| 1276 |
|
| 1277 |
/************************************************************************* |
| 1278 |
* |
| 1279 |
* Function: find_file_ent |
| 1280 |
* |
| 1281 |
* Purpose: Find a FILE_LIST entry on file_list with the specified |
| 1282 |
* file_pfx. The entry should be found as find_file_ent is |
| 1283 |
* only called for file_pfx's that were found in the "clients" |
| 1284 |
* file at initialization time. |
| 1285 |
* |
| 1286 |
*************************************************************************/ |
| 1287 |
|
| 1288 |
static FILE_LIST * |
| 1289 |
find_file_ent (file_pfx) |
| 1290 |
|
| 1291 |
char *file_pfx; |
| 1292 |
|
| 1293 |
{ |
| 1294 |
FILE_LIST *file_ent; |
| 1295 |
static char *func = "find_file_ent"; |
| 1296 |
|
| 1297 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 1298 |
|
| 1299 |
if ((file_ent = file_list) == (FILE_LIST *) NULL) |
| 1300 |
{ |
| 1301 |
logit (LOG_DAEMON, LOG_ERR, |
| 1302 |
"%s: No users/authfile data structure", func); |
| 1303 |
return (FILE_LIST *) NULL; |
| 1304 |
} |
| 1305 |
if (file_pfx && file_pfx[0]) |
| 1306 |
{ |
| 1307 |
while (strcmp (file_ent->prefix, file_pfx) != 0) |
| 1308 |
{ |
| 1309 |
if ((file_ent = file_ent->next) == (FILE_LIST *) NULL) |
| 1310 |
{ |
| 1311 |
logit (LOG_DAEMON, LOG_ERR, |
| 1312 |
"%s: Couldn't match %s in FILE_LIST", |
| 1313 |
func, file_pfx); |
| 1314 |
return (FILE_LIST *) NULL; |
| 1315 |
} |
| 1316 |
} |
| 1317 |
} |
| 1318 |
return file_ent; |
| 1319 |
} /* end of find_file_ent () */ |
| 1320 |
|
| 1321 |
/************************************************************************* |
| 1322 |
* |
| 1323 |
* Function: free_clients |
| 1324 |
* |
| 1325 |
* Purpose: Toss client list entries and associated address structure. |
| 1326 |
* |
| 1327 |
* Remark: Zap storage blocks to avoid leaving any secrets around. |
| 1328 |
* |
| 1329 |
*************************************************************************/ |
| 1330 |
|
| 1331 |
static void |
| 1332 |
free_clients (client_list) |
| 1333 |
|
| 1334 |
CLIENT_ENTRY *client_list; |
| 1335 |
|
| 1336 |
{ |
| 1337 |
CLIENT_ENTRY *client_ent; |
| 1338 |
IP_ADDRESS *an_address; |
| 1339 |
DNS_NAME *a_name; |
| 1340 |
static char *func = "free_clients"; |
| 1341 |
|
| 1342 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 1343 |
|
| 1344 |
for (client_ent = client_list; |
| 1345 |
client_ent != (CLIENT_ENTRY *) NULL; |
| 1346 |
client_ent = client_list) |
| 1347 |
{ |
| 1348 |
client_list = client_ent->next; |
| 1349 |
|
| 1350 |
for (an_address = client_ent->addrs; |
| 1351 |
an_address != (IP_ADDRESS *) NULL; |
| 1352 |
an_address = client_ent->addrs) |
| 1353 |
{ |
| 1354 |
client_ent->addrs = an_address->next; |
| 1355 |
free (an_address); |
| 1356 |
} |
| 1357 |
|
| 1358 |
for (a_name = client_ent->names; |
| 1359 |
a_name != (DNS_NAME *) NULL; |
| 1360 |
a_name = client_ent->names) |
| 1361 |
{ |
| 1362 |
client_ent->names = a_name->next; |
| 1363 |
free (a_name); |
| 1364 |
} |
| 1365 |
|
| 1366 |
free (client_ent); |
| 1367 |
} |
| 1368 |
return; |
| 1369 |
} /* end of free_clients () */ |
| 1370 |
|
| 1371 |
/************************************************************************* |
| 1372 |
* |
| 1373 |
* Function: free_file_lists |
| 1374 |
* |
| 1375 |
* Purpose: Free all the storage for the "users" and "authfile" |
| 1376 |
* memory resident data structures allocated by calling |
| 1377 |
* config_files(). |
| 1378 |
* |
| 1379 |
*************************************************************************/ |
| 1380 |
|
| 1381 |
static void |
| 1382 |
free_file_lists () |
| 1383 |
|
| 1384 |
{ |
| 1385 |
FILE_LIST *file_ent; |
| 1386 |
USER_ENTRY *user_ent; |
| 1387 |
AUTH_ENTRY *auth_ent; |
| 1388 |
static char *func = "free_file_lists"; |
| 1389 |
|
| 1390 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 1391 |
|
| 1392 |
authfile_cnt = 0; |
| 1393 |
users_cnt = 0; |
| 1394 |
|
| 1395 |
for (file_ent = file_list; file_ent; file_ent = file_list) |
| 1396 |
{ |
| 1397 |
for (user_ent = file_ent->user_list; |
| 1398 |
user_ent; |
| 1399 |
user_ent = file_ent->user_list) |
| 1400 |
{ |
| 1401 |
file_ent->user_list = user_ent->next; |
| 1402 |
free_user_ent (user_ent); |
| 1403 |
} |
| 1404 |
for (auth_ent = file_ent->auth_list; |
| 1405 |
auth_ent; |
| 1406 |
auth_ent = file_ent->auth_list) |
| 1407 |
{ |
| 1408 |
file_ent->auth_list = auth_ent->next; |
| 1409 |
free (auth_ent); |
| 1410 |
} |
| 1411 |
file_list = file_ent->next; |
| 1412 |
free (file_ent); |
| 1413 |
} |
| 1414 |
|
| 1415 |
return; |
| 1416 |
} /* end of free_file_lists () */ |
| 1417 |
|
| 1418 |
/************************************************************************* |
| 1419 |
* |
| 1420 |
* Function: free_user_ent |
| 1421 |
* |
| 1422 |
* Purpose: Free all components of a USER_ENTRY structure. Zap |
| 1423 |
* the USER_ENTRY storage. |
| 1424 |
* |
| 1425 |
*************************************************************************/ |
| 1426 |
|
| 1427 |
void |
| 1428 |
free_user_ent (user_ent) |
| 1429 |
|
| 1430 |
USER_ENTRY *user_ent; |
| 1431 |
|
| 1432 |
{ |
| 1433 |
list_free (user_ent->check); |
| 1434 |
list_free (user_ent->reply); |
| 1435 |
memset ((char *) user_ent, '\0', sizeof (USER_ENTRY)); |
| 1436 |
free (user_ent); |
| 1437 |
return; |
| 1438 |
} /* end of free_user_ent () */ |
| 1439 |
|
| 1440 |
/************************************************************************* |
| 1441 |
* |
| 1442 |
* Function: get_our_addr |
| 1443 |
* |
| 1444 |
* Purpose: A global function to return a local variable (?) |
| 1445 |
* |
| 1446 |
* Returns: (an) IP address of this machine. |
| 1447 |
* |
| 1448 |
*************************************************************************/ |
| 1449 |
|
| 1450 |
UINT4 |
| 1451 |
get_our_addr () |
| 1452 |
|
| 1453 |
{ |
| 1454 |
return self_ip[0]; |
| 1455 |
} /* end of get_our_addr () */ |
| 1456 |
|
| 1457 |
/************************************************************************* |
| 1458 |
* |
| 1459 |
* Function: host_is_us |
| 1460 |
* |
| 1461 |
* Purpose: Determine if we are the given host. |
| 1462 |
* |
| 1463 |
* Returns: 1 if the given hostname is the name of this host, |
| 1464 |
* 0 otherwise. |
| 1465 |
* |
| 1466 |
*************************************************************************/ |
| 1467 |
|
| 1468 |
static int |
| 1469 |
host_is_us (hostname) |
| 1470 |
|
| 1471 |
char *hostname; |
| 1472 |
|
| 1473 |
{ |
| 1474 |
UINT4 addr; |
| 1475 |
UINT4 *adptr; |
| 1476 |
|
| 1477 |
if (find_host_by_name (&addr, hostname) == 0) |
| 1478 |
{ |
| 1479 |
for (adptr = self_ip; *adptr > 0; adptr++) |
| 1480 |
{ |
| 1481 |
if (*adptr == addr) |
| 1482 |
{ |
| 1483 |
return 1; |
| 1484 |
} |
| 1485 |
} |
| 1486 |
} |
| 1487 |
return 0; |
| 1488 |
} /* end of host_is_us () */ |
| 1489 |
|
| 1490 |
/************************************************************************* |
| 1491 |
* |
| 1492 |
* Function: insert_client |
| 1493 |
* |
| 1494 |
* Purpose: Inserts a CLIENT_ENTRY node into client_list for the |
| 1495 |
* given hostname. |
| 1496 |
* |
| 1497 |
* Returns: 0 - inserted ok |
| 1498 |
* -1 - bad news |
| 1499 |
* |
| 1500 |
*************************************************************************/ |
| 1501 |
|
| 1502 |
static int |
| 1503 |
insert_client (hostname, secret, prefix) |
| 1504 |
|
| 1505 |
char *hostname; |
| 1506 |
char *secret; |
| 1507 |
char *prefix; |
| 1508 |
|
| 1509 |
{ |
| 1510 |
CLIENT_ENTRY *client_ent = (CLIENT_ENTRY *) NULL; |
| 1511 |
CLIENT_ENTRY *oldent; |
| 1512 |
CLIENT_ENTRY **prev; |
| 1513 |
IP_ADDRESS *ip_address; |
| 1514 |
struct in_addr addr; |
| 1515 |
static char *func = "insert_client"; |
| 1516 |
|
| 1517 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 1518 |
|
| 1519 |
/* Convert generic name for us to our real name */ |
| 1520 |
if (strcmp (hostname, RADIUS_LOCALSERVER) == 0) |
| 1521 |
{ |
| 1522 |
hostname = ourhostname; |
| 1523 |
} |
| 1524 |
|
| 1525 |
/* Look for entry from previous list (before HUP) */ |
| 1526 |
|
| 1527 |
if (old_clients != (CLIENT_ENTRY *) NULL) |
| 1528 |
{ |
| 1529 |
for (prev = &old_clients; |
| 1530 |
(oldent = *prev) != (CLIENT_ENTRY *) NULL; |
| 1531 |
prev = &oldent->next) |
| 1532 |
{ |
| 1533 |
if (strcmp (hostname, oldent->hostname) == 0) |
| 1534 |
{ |
| 1535 |
/* Matched - Remove from old list */ |
| 1536 |
*prev = oldent->next; |
| 1537 |
client_ent = oldent; |
| 1538 |
break; |
| 1539 |
} |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
if (client_ent == (CLIENT_ENTRY *) NULL) |
| 1544 |
{ |
| 1545 |
if ((client_ent = |
| 1546 |
(CLIENT_ENTRY *) malloc ( sizeof (CLIENT_ENTRY))) |
| 1547 |
== (CLIENT_ENTRY *) NULL) |
| 1548 |
{ |
| 1549 |
logit (LOG_DAEMON, LOG_ALERT, |
| 1550 |
"%s: FATAL Couldn't allocate CLIENT_ENTRY storage", |
| 1551 |
func); |
| 1552 |
abort (); |
| 1553 |
} |
| 1554 |
client_ent->hostname = add_string (hostname, ASIS); |
| 1555 |
client_ent->names = (DNS_NAME *) NULL; |
| 1556 |
client_ent->addrs = (IP_ADDRESS *) NULL; |
| 1557 |
client_ent->type = CE_DNS; |
| 1558 |
|
| 1559 |
/* Set constant addrs now so we don't have to wait for DNS */ |
| 1560 |
if (good_ipaddr (hostname) == 0) |
| 1561 |
{ |
| 1562 |
client_ent->type = CE_NUMERIC; |
| 1563 |
addr.s_addr = ntohl(inet_addr (hostname)); |
| 1564 |
} |
| 1565 |
else |
| 1566 |
{ |
| 1567 |
if (strcmp (hostname, ourhostname) == 0) |
| 1568 |
{ |
| 1569 |
client_ent->type = CE_OURADDR; |
| 1570 |
addr.s_addr = self_ip[0]; |
| 1571 |
} |
| 1572 |
} |
| 1573 |
|
| 1574 |
if (client_ent->type != CE_DNS) |
| 1575 |
{ |
| 1576 |
if ((ip_address = |
| 1577 |
(IP_ADDRESS *) malloc (sizeof (CLIENT_ENTRY))) |
| 1578 |
== (IP_ADDRESS *) NULL) |
| 1579 |
{ |
| 1580 |
logit (LOG_DAEMON, LOG_ALERT, |
| 1581 |
"%s: FATAL Couldn't allocate IP_ADDRESS storage", |
| 1582 |
func); |
| 1583 |
abort (); |
| 1584 |
} |
| 1585 |
|
| 1586 |
ip_address->ipaddr = addr; |
| 1587 |
ip_address->next = (IP_ADDRESS *) NULL; |
| 1588 |
client_ent->addrs = ip_address; |
| 1589 |
} |
| 1590 |
} |
| 1591 |
|
| 1592 |
client_ent->secret = add_string (secret, ASIS); |
| 1593 |
client_ent->prefix = add_string (prefix, ASIS); |
| 1594 |
client_ent->expire_time = (time_t) 0; |
| 1595 |
client_ent->next = client_list; |
| 1596 |
client_list = client_ent; |
| 1597 |
|
| 1598 |
/* |
| 1599 |
* If the entry had an optional file prefix, add a new FILE_ENTRY |
| 1600 |
* to the file_list to handle this prefix. Add_file_list() will |
| 1601 |
* not add duplicate entries. |
| 1602 |
*/ |
| 1603 |
if (client_ent->prefix[0] != '\0') |
| 1604 |
{ |
| 1605 |
add_file_list (client_ent->prefix); |
| 1606 |
} |
| 1607 |
return 0; |
| 1608 |
} /* end of insert_client () */ |
| 1609 |
|
| 1610 |
#define MAX_HOSTNAME_BUFFERS 20 |
| 1611 |
|
| 1612 |
/************************************************************************* |
| 1613 |
* |
| 1614 |
* Function: ip_hostname |
| 1615 |
* |
| 1616 |
* Purpose: Return a printable host name (or IP address in dotted quad |
| 1617 |
* notation) for the supplied IP address. |
| 1618 |
* |
| 1619 |
*************************************************************************/ |
| 1620 |
|
| 1621 |
char * |
| 1622 |
ip_hostname (h_ipaddr) |
| 1623 |
|
| 1624 |
UINT4 h_ipaddr; |
| 1625 |
|
| 1626 |
{ |
| 1627 |
UINT4 *ourad; |
| 1628 |
CLIENT_ENTRY *client_ent; |
| 1629 |
IP_ADDRESS *an_address; |
| 1630 |
DNS_NAME *a_name; |
| 1631 |
struct hostent *hp; |
| 1632 |
struct in_addr inad; |
| 1633 |
static char buffers[MAX_HOSTNAME_BUFFERS][128]; |
| 1634 |
static int ndx = 0; |
| 1635 |
char *hstname = buffers[ndx]; |
| 1636 |
|
| 1637 |
for (client_ent = client_list; |
| 1638 |
client_ent != (CLIENT_ENTRY *) NULL; |
| 1639 |
client_ent = client_ent->next) |
| 1640 |
{ |
| 1641 |
for (an_address = client_ent->addrs; |
| 1642 |
an_address != (IP_ADDRESS *) NULL; |
| 1643 |
an_address = an_address->next) |
| 1644 |
{ |
| 1645 |
if (an_address->ipaddr.s_addr == h_ipaddr) |
| 1646 |
{ |
| 1647 |
break; |
| 1648 |
} |
| 1649 |
} |
| 1650 |
|
| 1651 |
if (an_address != (IP_ADDRESS *) NULL) |
| 1652 |
{ |
| 1653 |
break; |
| 1654 |
} |
| 1655 |
} |
| 1656 |
|
| 1657 |
if (client_ent != (CLIENT_ENTRY *) NULL) |
| 1658 |
{ |
| 1659 |
/* |
| 1660 |
* We found something in our tables. |
| 1661 |
* Return a pointer to that instead of |
| 1662 |
* copying it to our own static area. |
| 1663 |
*/ |
| 1664 |
if ((a_name = client_ent->names) == (DNS_NAME *) NULL || |
| 1665 |
(a_name->type != 0)) |
| 1666 |
{ |
| 1667 |
return (client_ent->hostname); |
| 1668 |
} |
| 1669 |
else /* return official name if it's not in the main entry */ |
| 1670 |
{ |
| 1671 |
return (a_name->name); |
| 1672 |
} |
| 1673 |
} |
| 1674 |
|
| 1675 |
/* Didn't find it in our tables. Keep looking... */ |
| 1676 |
for (ourad = self_ip; |
| 1677 |
(*ourad > (UINT4) 0) && (*ourad != h_ipaddr); |
| 1678 |
ourad++) |
| 1679 |
{ |
| 1680 |
continue; |
| 1681 |
} |
| 1682 |
|
| 1683 |
/* If it was our own address, return our hostname. */ |
| 1684 |
if (*ourad > (UINT4) 0) |
| 1685 |
{ |
| 1686 |
return (ourhostname); |
| 1687 |
} |
| 1688 |
|
| 1689 |
/* It wasn't us, so make something up. */ |
| 1690 |
inad.s_addr = htonl(h_ipaddr); |
| 1691 |
strcpy (hstname, inet_ntoa (inad)); /* xxx.yyy.zzz.qqq */ |
| 1692 |
|
| 1693 |
/* |
| 1694 |
* Special check for non-server use. |
| 1695 |
* Note: a server always will have at |
| 1696 |
* least one client. |
| 1697 |
*/ |
| 1698 |
if (client_list == (CLIENT_ENTRY *) NULL) |
| 1699 |
{ |
| 1700 |
if ((hp = gethostbyaddr ((char *) &inad.s_addr, |
| 1701 |
sizeof (struct in_addr), |
| 1702 |
AF_INET)) |
| 1703 |
!= (struct hostent *) NULL) |
| 1704 |
{ |
| 1705 |
strcpy (hstname, hp->h_name); |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
/* Circulate through the buffers... */ |
| 1710 |
ndx++; |
| 1711 |
if (ndx >= MAX_HOSTNAME_BUFFERS) |
| 1712 |
{ |
| 1713 |
ndx = 0; |
| 1714 |
} |
| 1715 |
|
| 1716 |
return (hstname); |
| 1717 |
|
| 1718 |
} /* end of ip_hostname () */ |
| 1719 |
|
| 1720 |
/************************************************************************* |
| 1721 |
* |
| 1722 |
* Function: list_cat |
| 1723 |
* |
| 1724 |
* Purpose: Given two lists, "a" and "b", place "b" at the end of "a" |
| 1725 |
* |
| 1726 |
*************************************************************************/ |
| 1727 |
|
| 1728 |
void |
| 1729 |
list_cat (a, b) |
| 1730 |
|
| 1731 |
VALUE_PAIR **a; |
| 1732 |
VALUE_PAIR *b; |
| 1733 |
|
| 1734 |
{ |
| 1735 |
VALUE_PAIR **last; |
| 1736 |
FILE *debugout = stdout; |
| 1737 |
static char *func = "list_cat"; |
| 1738 |
|
| 1739 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 1740 |
|
| 1741 |
if (debug_flag >= 4) |
| 1742 |
{ |
| 1743 |
if (ddt) |
| 1744 |
{ |
| 1745 |
debugout = ddt; |
| 1746 |
} |
| 1747 |
fprintf (debugout, "First list:\n"); |
| 1748 |
} |
| 1749 |
|
| 1750 |
for (last = a; *last != (VALUE_PAIR *) NULL; last = &((*last)->next)) |
| 1751 |
{ |
| 1752 |
if (debug_flag >= 4) |
| 1753 |
{ |
| 1754 |
debug_pair (debugout, *last); |
| 1755 |
} |
| 1756 |
} |
| 1757 |
|
| 1758 |
*last = b; |
| 1759 |
|
| 1760 |
if (debug_flag >= 4) |
| 1761 |
{ |
| 1762 |
fprintf (debugout, "and Second list:\n"); |
| 1763 |
debug_list (debugout, b); |
| 1764 |
} |
| 1765 |
|
| 1766 |
return; |
| 1767 |
} /* end of list_cat () */ |
| 1768 |
|
| 1769 |
#define LIST_COPY_LIMIT 256 /* Limit the number of items we will copy. */ |
| 1770 |
|
| 1771 |
/************************************************************************* |
| 1772 |
* |
| 1773 |
* Function: list_copy |
| 1774 |
* |
| 1775 |
* Purpose: Make a copy of the entire list of value_pairs pointed to by |
| 1776 |
* from_list. It is necessary to copy the check_items and |
| 1777 |
* reply_items from a USER_ENTRY before processing a request |
| 1778 |
* because they may be modified or freed. |
| 1779 |
* |
| 1780 |
*************************************************************************/ |
| 1781 |
|
| 1782 |
void |
| 1783 |
list_copy (to_list, from_list) |
| 1784 |
|
| 1785 |
VALUE_PAIR **to_list; |
| 1786 |
VALUE_PAIR *from_list; |
| 1787 |
|
| 1788 |
{ |
| 1789 |
int count = 0; /* Count items we copy. */ |
| 1790 |
VALUE_PAIR *copy_item; |
| 1791 |
VALUE_PAIR *new_item; |
| 1792 |
VALUE_PAIR **last; |
| 1793 |
VALUE_PAIR **old_end; |
| 1794 |
static char *func = "list_copy"; |
| 1795 |
|
| 1796 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 1797 |
|
| 1798 |
if (to_list == (VALUE_PAIR **) NULL) |
| 1799 |
{ |
| 1800 |
logit (LOG_DAEMON, LOG_ERR, "%s: NULL parameter", func); |
| 1801 |
exit (-13); |
| 1802 |
} |
| 1803 |
|
| 1804 |
copy_item = (VALUE_PAIR *) NULL; |
| 1805 |
|
| 1806 |
/* run to end of destination list */ |
| 1807 |
for (last = to_list ; |
| 1808 |
*last != (VALUE_PAIR *) NULL ; |
| 1809 |
last = &((*last)->next)) |
| 1810 |
{ |
| 1811 |
if (*last == from_list) |
| 1812 |
{ |
| 1813 |
logit (LOG_DAEMON, LOG_ALERT, |
| 1814 |
"%s: FATAL (0x%p->0x%p,0x%p) crosslinked at 0x%p->0x%p", |
| 1815 |
func, to_list, *to_list, from_list, last, *last); |
| 1816 |
dumpcore = 1; |
| 1817 |
abort (); |
| 1818 |
} |
| 1819 |
} |
| 1820 |
old_end = last; /* Save old end-ptr for sanity checking. */ |
| 1821 |
|
| 1822 |
new_item = (VALUE_PAIR *) NULL; |
| 1823 |
|
| 1824 |
dprintf(5, (LOG_AUTH, LOG_DEBUG, |
| 1825 |
"%s: copy from list 0x%p to end of list at 0x%p->0x%p", |
| 1826 |
func, from_list, to_list, *to_list)); |
| 1827 |
|
| 1828 |
for (copy_item = from_list ; |
| 1829 |
copy_item != (VALUE_PAIR *) NULL ; |
| 1830 |
copy_item = copy_item->next) |
| 1831 |
{ |
| 1832 |
if (count > LIST_COPY_LIMIT) |
| 1833 |
{ |
| 1834 |
logit (LOG_DAEMON, LOG_ALERT, |
| 1835 |
"%s: FATAL (0x%p->0x%p, 0x%p), count=%d, limit exceed", |
| 1836 |
func, to_list, *to_list, from_list, copy_item, |
| 1837 |
count); |
| 1838 |
dumpcore = 1; |
| 1839 |
abort (); |
| 1840 |
} |
| 1841 |
|
| 1842 |
if (copy_item == *old_end) |
| 1843 |
{ |
| 1844 |
logit (LOG_DAEMON, LOG_ALERT, |
| 1845 |
"%s: FATAL (0x%X->0x%X, 0x%X) list appended to itself at 0x%X", |
| 1846 |
func, to_list, *to_list, from_list, copy_item); |
| 1847 |
dumpcore = 1; |
| 1848 |
abort (); |
| 1849 |
} |
| 1850 |
|
| 1851 |
if ((new_item = (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR))) |
| 1852 |
== (VALUE_PAIR *) NULL) |
| 1853 |
{ |
| 1854 |
logit (LOG_DAEMON, LOG_ALERT, "%s: FATAL out of memory", |
| 1855 |
func); |
| 1856 |
abort (); |
| 1857 |
} |
| 1858 |
memcpy ((char *) new_item, (char *) copy_item, |
| 1859 |
sizeof (VALUE_PAIR)); |
| 1860 |
new_item->next = (VALUE_PAIR *) NULL; |
| 1861 |
|
| 1862 |
*last = new_item; /* always put copy at end of to_list */ |
| 1863 |
last = &(new_item->next); /* new end of to_list */ |
| 1864 |
count++; |
| 1865 |
} |
| 1866 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: copied %d items", func, count)); |
| 1867 |
return; |
| 1868 |
} /* end of list_copy () */ |
| 1869 |
|
| 1870 |
#define PARSE_MODE_NAME 0 |
| 1871 |
#define PARSE_MODE_EQUAL 1 |
| 1872 |
#define PARSE_MODE_VALUE 2 |
| 1873 |
#define PARSE_MODE_INVALID 3 |
| 1874 |
|
| 1875 |
/************************************************************************* |
| 1876 |
* |
| 1877 |
* Function: pair_parse |
| 1878 |
* |
| 1879 |
* Purpose: Parses the buffer to extract the attribute-value pairs. |
| 1880 |
* |
| 1881 |
* Returns: 0 = successful parse of attribute-value pair, |
| 1882 |
* -1 = syntax (or other) error detected. |
| 1883 |
* |
| 1884 |
*************************************************************************/ |
| 1885 |
|
| 1886 |
int |
| 1887 |
pair_parse (buffer, first_pair) |
| 1888 |
|
| 1889 |
char *buffer; |
| 1890 |
VALUE_PAIR **first_pair; |
| 1891 |
|
| 1892 |
{ |
| 1893 |
int mode; |
| 1894 |
int rc; |
| 1895 |
char attrstr[AUTH_ID_LEN]; |
| 1896 |
char valstr[AUTH_ID_LEN]; |
| 1897 |
DICT_ATTR *attr; |
| 1898 |
DICT_VALUE *dval; |
| 1899 |
VALUE_PAIR *pair; |
| 1900 |
VALUE_PAIR *link; |
| 1901 |
struct tm *tm; |
| 1902 |
time_t timeval; |
| 1903 |
static char *func = "pair_parse"; |
| 1904 |
|
| 1905 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 1906 |
|
| 1907 |
mode = PARSE_MODE_NAME; |
| 1908 |
while (*buffer != '\n' && *buffer != '\0') |
| 1909 |
{ |
| 1910 |
if (*buffer == ' ' || *buffer == '\t' || *buffer == ',') |
| 1911 |
{ |
| 1912 |
buffer++; |
| 1913 |
continue; |
| 1914 |
} |
| 1915 |
|
| 1916 |
switch (mode) |
| 1917 |
{ |
| 1918 |
case PARSE_MODE_NAME: /* Attribute Name */ |
| 1919 |
fieldcpy (attrstr, &buffer); |
| 1920 |
if ((attr = |
| 1921 |
dict_attrfind (attrstr)) == (DICT_ATTR *) NULL) |
| 1922 |
{ |
| 1923 |
return (-1); |
| 1924 |
} |
| 1925 |
mode = PARSE_MODE_EQUAL; |
| 1926 |
break; |
| 1927 |
|
| 1928 |
case PARSE_MODE_EQUAL: /* Equal sign */ |
| 1929 |
if (*buffer == '=') |
| 1930 |
{ |
| 1931 |
mode = PARSE_MODE_VALUE; |
| 1932 |
buffer++; |
| 1933 |
} |
| 1934 |
else |
| 1935 |
{ |
| 1936 |
return (-1); |
| 1937 |
} |
| 1938 |
break; |
| 1939 |
|
| 1940 |
case PARSE_MODE_VALUE: /* Value */ |
| 1941 |
fieldcpy (valstr, &buffer); |
| 1942 |
|
| 1943 |
if ((pair = |
| 1944 |
(VALUE_PAIR *) malloc (sizeof (VALUE_PAIR))) |
| 1945 |
== (VALUE_PAIR *) NULL) |
| 1946 |
{ |
| 1947 |
logit (LOG_DAEMON, LOG_ALERT, |
| 1948 |
"%s: FATAL out of memory", func); |
| 1949 |
abort (); |
| 1950 |
} |
| 1951 |
strcpy (pair->name, attr->name); |
| 1952 |
pair->attribute = attr->value; |
| 1953 |
pair->type = attr->type; |
| 1954 |
|
| 1955 |
switch (pair->type) |
| 1956 |
{ |
| 1957 |
|
| 1958 |
case PW_TYPE_STRING: |
| 1959 |
strcpy (pair->strvalue, valstr); |
| 1960 |
break; |
| 1961 |
|
| 1962 |
case PW_TYPE_INTEGER: |
| 1963 |
if (isdigit (*valstr)) |
| 1964 |
{ |
| 1965 |
pair->lvalue = atoi (valstr); |
| 1966 |
} |
| 1967 |
else |
| 1968 |
{ |
| 1969 |
if ((dval = dict_valfind (valstr)) |
| 1970 |
== (DICT_VALUE *) NULL) |
| 1971 |
{ |
| 1972 |
free (pair); |
| 1973 |
return (-1); |
| 1974 |
} |
| 1975 |
else |
| 1976 |
{ |
| 1977 |
pair->lvalue = dval->value; |
| 1978 |
} |
| 1979 |
} |
| 1980 |
break; |
| 1981 |
|
| 1982 |
case PW_TYPE_IPADDR: |
| 1983 |
rc = find_host_by_name (&pair->lvalue, valstr); |
| 1984 |
if (rc == -1) /* Name not in our list yet */ |
| 1985 |
{ |
| 1986 |
insert_client (valstr, "", ""); |
| 1987 |
} |
| 1988 |
else |
| 1989 |
{ |
| 1990 |
if (rc == 2) /* Unresolvable DNS name */ |
| 1991 |
{ |
| 1992 |
free (pair); |
| 1993 |
return (-1); |
| 1994 |
} |
| 1995 |
} |
| 1996 |
|
| 1997 |
/* If DNS name given, store it in strvalue */ |
| 1998 |
if (good_ipaddr (valstr) == 0) |
| 1999 |
{ |
| 2000 |
*pair->strvalue = '\0'; |
| 2001 |
} |
| 2002 |
else /* Now we can re-resolve these names */ |
| 2003 |
{ |
| 2004 |
strcpy (pair->strvalue, valstr); |
| 2005 |
} |
| 2006 |
break; |
| 2007 |
|
| 2008 |
case PW_TYPE_DATE: |
| 2009 |
timeval = time (0); |
| 2010 |
tm = localtime (&timeval); |
| 2011 |
tm->tm_hour = 0; |
| 2012 |
tm->tm_min = 0; |
| 2013 |
tm->tm_sec = 0; |
| 2014 |
user_gettime (valstr, tm); |
| 2015 |
#ifdef TIMELOCAL |
| 2016 |
pair->lvalue = (UINT4) timelocal (tm); |
| 2017 |
#else /* TIMELOCAL */ |
| 2018 |
pair->lvalue = (UINT4) mktime (tm); |
| 2019 |
#endif /* TIMELOCAL */ |
| 2020 |
break; |
| 2021 |
|
| 2022 |
default: |
| 2023 |
free (pair); |
| 2024 |
return (-1); |
| 2025 |
} |
| 2026 |
pair->next = (VALUE_PAIR *) NULL; |
| 2027 |
|
| 2028 |
if (*first_pair == (VALUE_PAIR *) NULL) |
| 2029 |
{ |
| 2030 |
*first_pair = pair; |
| 2031 |
} |
| 2032 |
else |
| 2033 |
{ |
| 2034 |
link = *first_pair; |
| 2035 |
while (link->next != (VALUE_PAIR *) NULL) |
| 2036 |
{ |
| 2037 |
link = link->next; |
| 2038 |
} |
| 2039 |
link->next = pair; |
| 2040 |
} |
| 2041 |
|
| 2042 |
mode = PARSE_MODE_NAME; |
| 2043 |
break; |
| 2044 |
|
| 2045 |
default: |
| 2046 |
mode = PARSE_MODE_NAME; |
| 2047 |
break; |
| 2048 |
} |
| 2049 |
} |
| 2050 |
return (0); |
| 2051 |
} /* end of pair_parse () */ |
| 2052 |
|
| 2053 |
/************************************************************************* |
| 2054 |
* |
| 2055 |
* Function: read_auth |
| 2056 |
* |
| 2057 |
* Purpose: Reads in the realm information from the "authfile" with |
| 2058 |
* file_pfx corresponding to the given file_ent. The information |
| 2059 |
* read is copied to a data structure that is linked to the |
| 2060 |
* given file_ent. |
| 2061 |
* |
| 2062 |
*************************************************************************/ |
| 2063 |
|
| 2064 |
static int |
| 2065 |
read_auth (file_ent, dolog) |
| 2066 |
|
| 2067 |
FILE_LIST *file_ent; |
| 2068 |
int dolog; |
| 2069 |
|
| 2070 |
{ |
| 2071 |
FILE *authfd; |
| 2072 |
char *name; |
| 2073 |
char *prot; |
| 2074 |
char *type; |
| 2075 |
char *host; |
| 2076 |
char *filter; |
| 2077 |
char *alias_list; |
| 2078 |
int line_no = 0; |
| 2079 |
int ent_cnt = 0; |
| 2080 |
int type_inx; |
| 2081 |
int prot_inx; |
| 2082 |
int error = 0; |
| 2083 |
AUTH_ENTRY *auth_ent; |
| 2084 |
AUTH_ENTRY **end_ent; |
| 2085 |
AUTH_ALIAS_ENTRY *alias_ent; |
| 2086 |
DICT_VALUE *type_val; |
| 2087 |
static char *auth_prots[] = |
| 2088 |
{ |
| 2089 |
PW_PROTTYPES_DFLT, |
| 2090 |
PW_PROTTYPES_CHAP, |
| 2091 |
PW_PROTTYPES_PW, |
| 2092 |
NULL |
| 2093 |
}; |
| 2094 |
char buffer[128]; |
| 2095 |
char fname[MAXPATHLEN]; |
| 2096 |
char *func = "read_auth"; |
| 2097 |
|
| 2098 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 2099 |
|
| 2100 |
sprintf (fname, "%s/%s%s", radius_dir, file_ent->prefix, RADIUS_AUTH); |
| 2101 |
if ((authfd = fopen (fname, "r")) == (FILE *) NULL) |
| 2102 |
{ |
| 2103 |
/* |
| 2104 |
* It's okay if a non-prefixed RADIUS_AUTH file doesn't exist, |
| 2105 |
* as long as no CI_AUTHENTICATION_TYPE = REALM entries are |
| 2106 |
* configured in the RADIUS_USERS file. If <pfx>RADIUS_AUTH |
| 2107 |
* doesn't exist, then the non-prefixed RADIUS_AUTH file will |
| 2108 |
* be used instead. |
| 2109 |
*/ |
| 2110 |
return (0); |
| 2111 |
} |
| 2112 |
end_ent = &file_ent->auth_list; |
| 2113 |
while (fgets (buffer, sizeof (buffer), authfd) != (char *) NULL) |
| 2114 |
{ |
| 2115 |
line_no++; |
| 2116 |
if (isspace (*buffer) || (*buffer == COMMENT)) |
| 2117 |
{ |
| 2118 |
continue; |
| 2119 |
} |
| 2120 |
name = strtok (buffer, " ,\t\n\r"); |
| 2121 |
|
| 2122 |
if (strcmp ("DEFAULT_RADIUS_SERVER", name) == 0) |
| 2123 |
{ |
| 2124 |
if ((host = strtok (NULL, " \"\n\r")) != (char *) NULL) |
| 2125 |
{ |
| 2126 |
strcpy (default_radius_server, host); |
| 2127 |
} |
| 2128 |
else |
| 2129 |
{ |
| 2130 |
default_radius_server[0] = '\0'; |
| 2131 |
} |
| 2132 |
continue; |
| 2133 |
} |
| 2134 |
else |
| 2135 |
{ |
| 2136 |
if (strcmp ("DEFAULT_TACACS_SERVER", name) == 0) |
| 2137 |
{ |
| 2138 |
if ((host = strtok (NULL, " \"\n\r")) |
| 2139 |
!= (char *) NULL) |
| 2140 |
{ |
| 2141 |
strcpy (default_tacacs_server, host); |
| 2142 |
} |
| 2143 |
else |
| 2144 |
{ |
| 2145 |
default_tacacs_server[0] = '\0'; |
| 2146 |
} |
| 2147 |
continue; |
| 2148 |
} |
| 2149 |
} |
| 2150 |
|
| 2151 |
/* Scan for optional alias list or protocol indicator */ |
| 2152 |
|
| 2153 |
prot_inx = 0; |
| 2154 |
alias_list = NULL; |
| 2155 |
type = NULL; |
| 2156 |
|
| 2157 |
while (type == NULL && !error) |
| 2158 |
{ |
| 2159 |
prot = strtok (NULL, " ,\t\n\r"); |
| 2160 |
if (prot == NULL) |
| 2161 |
{ |
| 2162 |
error++; |
| 2163 |
continue; |
| 2164 |
} |
| 2165 |
|
| 2166 |
|
| 2167 |
switch (*prot) |
| 2168 |
{ |
| 2169 |
case '(': /* "(<aliases>)" */ |
| 2170 |
alias_list = prot; |
| 2171 |
if (prot[strlen (prot) - 1] != ')') |
| 2172 |
{ |
| 2173 |
if (strtok (NULL, ")") == NULL) |
| 2174 |
{ |
| 2175 |
error++; |
| 2176 |
} |
| 2177 |
/* We don't want to break up alias list yet */ |
| 2178 |
prot[strlen (prot)] = ' '; |
| 2179 |
} |
| 2180 |
break; |
| 2181 |
|
| 2182 |
case ('-'): /* "-<protocol>" */ |
| 2183 |
if (*++prot == '\0') |
| 2184 |
{ |
| 2185 |
error++; |
| 2186 |
break; |
| 2187 |
} |
| 2188 |
|
| 2189 |
for (prot_inx = 0; |
| 2190 |
auth_prots[prot_inx] && |
| 2191 |
strcmp (prot, auth_prots[prot_inx]) != 0; |
| 2192 |
prot_inx++) |
| 2193 |
{ |
| 2194 |
; |
| 2195 |
} |
| 2196 |
|
| 2197 |
if (!auth_prots[prot_inx]) |
| 2198 |
{ |
| 2199 |
error++; |
| 2200 |
} |
| 2201 |
break; |
| 2202 |
|
| 2203 |
default: |
| 2204 |
type = prot; |
| 2205 |
break; |
| 2206 |
} |
| 2207 |
} |
| 2208 |
|
| 2209 |
if (error) |
| 2210 |
{ |
| 2211 |
logit (LOG_DAEMON, LOG_ERR, |
| 2212 |
"%s: Invalid entry in %s at line %d", |
| 2213 |
func, fname, line_no); |
| 2214 |
continue; |
| 2215 |
} |
| 2216 |
|
| 2217 |
type_val = dict_valfind (type); |
| 2218 |
if (type_val == (DICT_VALUE *) NULL) |
| 2219 |
{ |
| 2220 |
logit (LOG_DAEMON, LOG_ERR, |
| 2221 |
"%s: Invalid TYPE '%s' in %s at line %d", |
| 2222 |
func, type, fname, line_no); |
| 2223 |
continue; |
| 2224 |
} |
| 2225 |
type_inx = type_val->value; |
| 2226 |
|
| 2227 |
host = strtok (NULL, " \t\n\r"); |
| 2228 |
filter = strtok (NULL, " \t\n\r"); |
| 2229 |
|
| 2230 |
if (host == NULL && type_inx != AA_UNIX) |
| 2231 |
{ |
| 2232 |
logit (LOG_DAEMON, LOG_ERR, |
| 2233 |
"%s: Invalid entry in %s at line %d", |
| 2234 |
func, fname, line_no); |
| 2235 |
continue; |
| 2236 |
} |
| 2237 |
|
| 2238 |
if ((auth_ent = (AUTH_ENTRY *) malloc (sizeof (AUTH_ENTRY))) |
| 2239 |
== (AUTH_ENTRY *) NULL) |
| 2240 |
{ |
| 2241 |
logit (LOG_DAEMON, LOG_ALERT, |
| 2242 |
"%s: No memory for auth_ent", func); |
| 2243 |
fclose (authfd); |
| 2244 |
return (-1); |
| 2245 |
} |
| 2246 |
auth_ent->parent = (AUTH_ENTRY *) NULL; |
| 2247 |
auth_ent->type = type_inx; |
| 2248 |
|
| 2249 |
if (is_engine == 1) /* then we are being called by the engine */ |
| 2250 |
{ |
| 2251 |
if (authtype_tv[type_inx] == (AATV *) NULL) |
| 2252 |
{ |
| 2253 |
logit (LOG_DAEMON, LOG_ERR, |
| 2254 |
"%s: Missing AATV for entry on line %d of %s", |
| 2255 |
func, line_no, fname); |
| 2256 |
fclose (authfd); |
| 2257 |
return (-1); |
| 2258 |
} |
| 2259 |
} |
| 2260 |
|
| 2261 |
auth_ent->prot = prot_inx; |
| 2262 |
auth_ent->name = add_string (name, ASIS); |
| 2263 |
auth_ent->host = add_string (host, ASIS); |
| 2264 |
auth_ent->filter = add_string (filter, ASIS); |
| 2265 |
auth_ent->next = (AUTH_ENTRY *) NULL; |
| 2266 |
*end_ent = auth_ent; |
| 2267 |
end_ent = &auth_ent->next; |
| 2268 |
ent_cnt++; |
| 2269 |
|
| 2270 |
/* Add alias entries if aliases given */ |
| 2271 |
if (alias_list) |
| 2272 |
{ |
| 2273 |
alias_list++; |
| 2274 |
name = strtok (alias_list, " ,)"); |
| 2275 |
while (name) |
| 2276 |
{ |
| 2277 |
if ((alias_ent = |
| 2278 |
(AUTH_ALIAS_ENTRY *) malloc |
| 2279 |
(sizeof(AUTH_ALIAS_ENTRY)) ) |
| 2280 |
== (AUTH_ALIAS_ENTRY *) NULL) |
| 2281 |
{ |
| 2282 |
logit (LOG_DAEMON, LOG_ALERT, |
| 2283 |
"%s: No memory for auth_ent", |
| 2284 |
func); |
| 2285 |
fclose (authfd); |
| 2286 |
return (-1); |
| 2287 |
} |
| 2288 |
alias_ent->name = add_string (name, ASIS); |
| 2289 |
alias_ent->parent = auth_ent; |
| 2290 |
alias_ent->next = (AUTH_ENTRY *) NULL; |
| 2291 |
*end_ent = (AUTH_ENTRY *) alias_ent; |
| 2292 |
end_ent = &alias_ent->next; |
| 2293 |
name = strtok (NULL, " ,)"); |
| 2294 |
} |
| 2295 |
} |
| 2296 |
} |
| 2297 |
fclose (authfd); |
| 2298 |
if (dolog) |
| 2299 |
{ |
| 2300 |
logit (LOG_DAEMON, LOG_INFO, |
| 2301 |
"%s: %s (%u entries) read to memory", |
| 2302 |
func, fname, ent_cnt); |
| 2303 |
} |
| 2304 |
authfile_cnt += ent_cnt; |
| 2305 |
|
| 2306 |
return 0; |
| 2307 |
} /* end of read_auth () */ |
| 2308 |
|
| 2309 |
#define FIND_MODE_NAME 0 |
| 2310 |
#define FIND_MODE_REPLY 1 |
| 2311 |
#define FIND_MODE_SKIP 2 |
| 2312 |
#define FIND_MODE_FLUSH 3 |
| 2313 |
|
| 2314 |
/************************************************************************* |
| 2315 |
* |
| 2316 |
* Function: read_users |
| 2317 |
* |
| 2318 |
* Purpose: For each entry in a "users" file (with the file_pfx |
| 2319 |
* corresponding to the given file_ent), read all check_items |
| 2320 |
* and reply_items into a data structure. Each such |
| 2321 |
* data structure is linked to the given file_ent. |
| 2322 |
* |
| 2323 |
* Returns: 0 = normal (successful) return, |
| 2324 |
* -1 = error return. |
| 2325 |
* |
| 2326 |
*************************************************************************/ |
| 2327 |
|
| 2328 |
static int |
| 2329 |
read_users (file_ent, dolog) |
| 2330 |
|
| 2331 |
FILE_LIST *file_ent; |
| 2332 |
int dolog; |
| 2333 |
|
| 2334 |
{ |
| 2335 |
FILE *userfd; |
| 2336 |
char *ptr; |
| 2337 |
int mode; |
| 2338 |
int line_nbr = 0; |
| 2339 |
int count = 0; |
| 2340 |
USER_ENTRY *user_ent = (USER_ENTRY *) NULL; |
| 2341 |
USER_ENTRY **end_ent; |
| 2342 |
char buffer[256]; |
| 2343 |
char fname[MAXPATHLEN]; |
| 2344 |
static char *func = "read_users"; |
| 2345 |
|
| 2346 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 2347 |
|
| 2348 |
/* |
| 2349 |
* Open the user table |
| 2350 |
*/ |
| 2351 |
sprintf (fname, "%s/%s%s", radius_dir, file_ent->prefix, RADIUS_USERS); |
| 2352 |
if ((userfd = fopen (fname, "r")) == (FILE *) NULL) |
| 2353 |
{ |
| 2354 |
/* |
| 2355 |
* It's ok for prefixed user files to not exist, but |
| 2356 |
* non-prefixed user file has to exist. |
| 2357 |
*/ |
| 2358 |
if (file_ent->prefix[0] == 0) |
| 2359 |
{ |
| 2360 |
logit (LOG_DAEMON, LOG_ERR, |
| 2361 |
"%s: Couldn't open %s for reading", |
| 2362 |
func, fname); |
| 2363 |
return (-1); |
| 2364 |
} |
| 2365 |
return (0); |
| 2366 |
} |
| 2367 |
|
| 2368 |
end_ent = &file_ent->user_list; |
| 2369 |
|
| 2370 |
mode = FIND_MODE_NAME; |
| 2371 |
while (fgets (buffer, sizeof (buffer), userfd) != (char *) NULL) |
| 2372 |
{ |
| 2373 |
line_nbr++; |
| 2374 |
if (*buffer == COMMENT) |
| 2375 |
{ |
| 2376 |
continue; |
| 2377 |
} |
| 2378 |
|
| 2379 |
for (;;) |
| 2380 |
{ |
| 2381 |
if (mode == FIND_MODE_NAME) |
| 2382 |
{ |
| 2383 |
if (isspace (*buffer)) |
| 2384 |
{ |
| 2385 |
break; /* to read another line */ |
| 2386 |
} |
| 2387 |
if ((user_ent = (USER_ENTRY *) |
| 2388 |
malloc (sizeof (USER_ENTRY))) |
| 2389 |
== (USER_ENTRY *) NULL) |
| 2390 |
{ |
| 2391 |
logit (LOG_DAEMON, LOG_ALERT, |
| 2392 |
"%s: Couldn't allocate USER_ENTRY storage", |
| 2393 |
func); |
| 2394 |
return (-1); |
| 2395 |
} |
| 2396 |
ptr = strtok (buffer, " \t\n\r"); |
| 2397 |
user_ent->name = add_string (ptr, ASIS); |
| 2398 |
user_ent->check = (VALUE_PAIR *) NULL; |
| 2399 |
user_ent->reply = (VALUE_PAIR *) NULL; |
| 2400 |
user_ent->next = (USER_ENTRY *) NULL; |
| 2401 |
|
| 2402 |
/* |
| 2403 |
* Parse the check values |
| 2404 |
*/ |
| 2405 |
ptr += strlen (ptr) + 1; |
| 2406 |
if (pair_parse (ptr, &user_ent->check) != 0) |
| 2407 |
{ |
| 2408 |
logit (LOG_DAEMON, LOG_ERR, |
| 2409 |
"%s: %d: Parse error for user %s (check)", |
| 2410 |
func, line_nbr, user_ent->name); |
| 2411 |
free_user_ent (user_ent); |
| 2412 |
user_ent = (USER_ENTRY *) NULL; |
| 2413 |
} |
| 2414 |
else |
| 2415 |
{ |
| 2416 |
mode = FIND_MODE_REPLY; |
| 2417 |
} |
| 2418 |
break; /* to read another line */ |
| 2419 |
} |
| 2420 |
|
| 2421 |
/* Reading reply items */ |
| 2422 |
if (isspace (*buffer)) |
| 2423 |
{ |
| 2424 |
/* |
| 2425 |
* Parse the reply values |
| 2426 |
*/ |
| 2427 |
if (pair_parse (buffer, &user_ent->reply) != 0) |
| 2428 |
{ |
| 2429 |
logit (LOG_DAEMON, LOG_ERR, |
| 2430 |
"%s: %d: Parse error for user %s (reply)", |
| 2431 |
func, line_nbr, user_ent->name); |
| 2432 |
free_user_ent (user_ent); |
| 2433 |
user_ent = (USER_ENTRY *) NULL; |
| 2434 |
mode = FIND_MODE_NAME; |
| 2435 |
} |
| 2436 |
break; |
| 2437 |
} |
| 2438 |
|
| 2439 |
/* Start of next entry */ |
| 2440 |
*end_ent = user_ent; |
| 2441 |
end_ent = &user_ent->next; |
| 2442 |
user_ent = (USER_ENTRY *) NULL; |
| 2443 |
mode = FIND_MODE_NAME; |
| 2444 |
count++; |
| 2445 |
continue; /* with same input */ |
| 2446 |
} |
| 2447 |
} |
| 2448 |
fclose (userfd); |
| 2449 |
if (user_ent != (USER_ENTRY *) NULL) |
| 2450 |
{ |
| 2451 |
*end_ent = user_ent; |
| 2452 |
count++; |
| 2453 |
} |
| 2454 |
|
| 2455 |
if (dolog) |
| 2456 |
{ |
| 2457 |
logit (LOG_DAEMON, LOG_INFO, |
| 2458 |
"%s: %s (%u entries) read to memory", |
| 2459 |
func, fname, count); |
| 2460 |
} |
| 2461 |
users_cnt += count; |
| 2462 |
|
| 2463 |
return (0); |
| 2464 |
} /* end of read_users () */ |
| 2465 |
|
| 2466 |
/************************************************************************* |
| 2467 |
* |
| 2468 |
* Function: return_file_list |
| 2469 |
* |
| 2470 |
* Purpose: Returns the private file_list pointer for clients in need. |
| 2471 |
* |
| 2472 |
*************************************************************************/ |
| 2473 |
|
| 2474 |
FILE_LIST * |
| 2475 |
return_file_list () |
| 2476 |
|
| 2477 |
{ |
| 2478 |
return (file_list); |
| 2479 |
} /* end of return_file_list () */ |
| 2480 |
|
| 2481 |
/************************************************************************* |
| 2482 |
* |
| 2483 |
* Function: update_clients |
| 2484 |
* |
| 2485 |
* Purpose: Updates IP address(es) and expire_time in given |
| 2486 |
* CLIENT_ENTRY node. |
| 2487 |
* |
| 2488 |
*************************************************************************/ |
| 2489 |
|
| 2490 |
int |
| 2491 |
update_clients () |
| 2492 |
|
| 2493 |
{ |
| 2494 |
int len; |
| 2495 |
int notify_count = 0; |
| 2496 |
struct hostent *hp; |
| 2497 |
struct in_addr **addrlist; |
| 2498 |
char *aliascnt; |
| 2499 |
char *ptr; |
| 2500 |
char **name; |
| 2501 |
CLIENT_ENTRY *client_ent; |
| 2502 |
struct sockaddr_in send_sin; |
| 2503 |
time_t cur_time; |
| 2504 |
static char *func = "update_clients"; |
| 2505 |
|
| 2506 |
dprintf(4, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 2507 |
|
| 2508 |
if (dnspid != 0) /* Already resolving addrs */ |
| 2509 |
{ |
| 2510 |
return 0; |
| 2511 |
} |
| 2512 |
|
| 2513 |
if (spawn_flag > 0) |
| 2514 |
{ |
| 2515 |
if ((dnspid = (int) fork ()) < 0) |
| 2516 |
{ |
| 2517 |
dnspid = 0; |
| 2518 |
logit (LOG_DAEMON, LOG_ALERT, "%s: fork: %s", |
| 2519 |
func, get_errmsg ()); |
| 2520 |
return (-1); |
| 2521 |
} |
| 2522 |
|
| 2523 |
if (dnspid != 0) /* Parent */ |
| 2524 |
{ |
| 2525 |
return 0; |
| 2526 |
} |
| 2527 |
} |
| 2528 |
|
| 2529 |
/* ======= Child process code ======= */ |
| 2530 |
|
| 2531 |
memset ((char *) &send_sin, '\0', sizeof (send_sin)); |
| 2532 |
send_sin.sin_family = AF_INET; |
| 2533 |
send_sin.sin_addr.s_addr = htonl(self_ip[0]); |
| 2534 |
send_sin.sin_port = htons(rad_ipc_port); |
| 2535 |
|
| 2536 |
cur_time = time (0); |
| 2537 |
for (client_ent = client_list; |
| 2538 |
client_ent != (CLIENT_ENTRY *) NULL; |
| 2539 |
client_ent = client_ent->next) |
| 2540 |
{ |
| 2541 |
if (cur_time < client_ent->expire_time) |
| 2542 |
{ |
| 2543 |
continue; |
| 2544 |
} |
| 2545 |
|
| 2546 |
hp = gethostbyname (client_ent->hostname); |
| 2547 |
ptr = send_buffer; |
| 2548 |
*ptr++ = '\0'; /* Only one code for now */ |
| 2549 |
strcpy (ptr, client_ent->hostname); |
| 2550 |
ptr += strlen (ptr) + 1; |
| 2551 |
*ptr = '\0'; |
| 2552 |
aliascnt = ptr++; |
| 2553 |
|
| 2554 |
if (hp != (struct hostent *) NULL) |
| 2555 |
{ |
| 2556 |
if (hp->h_aliases != (char **) NULL) |
| 2557 |
{ |
| 2558 |
for (name = hp->h_aliases; |
| 2559 |
*name != (char *) NULL; |
| 2560 |
name++) |
| 2561 |
{ |
| 2562 |
if (strcmp (client_ent->hostname, |
| 2563 |
*name) != 0) |
| 2564 |
{ |
| 2565 |
(*aliascnt)++; |
| 2566 |
strcpy (ptr, *name); |
| 2567 |
ptr += strlen (ptr) + 1; |
| 2568 |
|
| 2569 |
/* Indicate just an alias */ |
| 2570 |
*ptr++ = 1; |
| 2571 |
} |
| 2572 |
} |
| 2573 |
} |
| 2574 |
|
| 2575 |
/* Pass official name last */ |
| 2576 |
if (strcmp (client_ent->hostname, hp->h_name) != 0) |
| 2577 |
{ |
| 2578 |
(*aliascnt)++; |
| 2579 |
strcpy (ptr, hp->h_name); |
| 2580 |
ptr += strlen (ptr) + 1; |
| 2581 |
*ptr++ = 0; /* Indicate official name */ |
| 2582 |
} |
| 2583 |
|
| 2584 |
if (hp->h_addr_list != (char **) NULL) |
| 2585 |
{ |
| 2586 |
addrlist = (struct in_addr **) hp->h_addr_list; |
| 2587 |
while (*addrlist) |
| 2588 |
{ |
| 2589 |
memcpy (ptr, *(char **) addrlist, |
| 2590 |
sizeof (struct in_addr)); |
| 2591 |
ptr += sizeof (struct in_addr); |
| 2592 |
addrlist++; |
| 2593 |
} |
| 2594 |
} |
| 2595 |
memset (ptr, '\0', sizeof (struct in_addr)); |
| 2596 |
} |
| 2597 |
else /* Extra check for brain-dead gethostbyname() calls */ |
| 2598 |
{ |
| 2599 |
if (client_ent->type == CE_NUMERIC) |
| 2600 |
{ |
| 2601 |
struct in_addr temp; |
| 2602 |
|
| 2603 |
temp.s_addr = ntohl(inet_addr (client_ent->hostname)); |
| 2604 |
memcpy (ptr, (char *) &temp, |
| 2605 |
sizeof (struct in_addr)); |
| 2606 |
ptr += sizeof (struct in_addr); |
| 2607 |
memset (ptr, '\0', sizeof (struct in_addr)); |
| 2608 |
} |
| 2609 |
else |
| 2610 |
{ |
| 2611 |
memset (ptr, '\0', sizeof (struct in_addr)); |
| 2612 |
/* Pass error code in packet */ |
| 2613 |
*(ptr + sizeof (struct in_addr) - 1) = h_errno; |
| 2614 |
} |
| 2615 |
} |
| 2616 |
|
| 2617 |
len = ptr - send_buffer + sizeof (struct in_addr); |
| 2618 |
notify_count++; |
| 2619 |
|
| 2620 |
dprintf (2, (LOG_DAEMON, LOG_DEBUG, |
| 2621 |
"%s: Sendto call number (%d) for client '%s'", |
| 2622 |
func, notify_count, client_ent->hostname)); |
| 2623 |
|
| 2624 |
if (notify_count % DNS_SLEEP == 0) |
| 2625 |
{ |
| 2626 |
sleep (1); |
| 2627 |
} |
| 2628 |
|
| 2629 |
/* Send it to main process */ |
| 2630 |
sendto (rad_ipc_aatv->sockfd, send_buffer, len, (int) 0, |
| 2631 |
(struct sockaddr *) & send_sin, sizeof (send_sin)); |
| 2632 |
|
| 2633 |
} |
| 2634 |
|
| 2635 |
if (spawn_flag > 0) |
| 2636 |
{ |
| 2637 |
_exit (0); |
| 2638 |
} |
| 2639 |
|
| 2640 |
return 0; |
| 2641 |
} /* end of update_clients () */ |
| 2642 |
|
| 2643 |
/************************************************************************* |
| 2644 |
* |
| 2645 |
* Function: user_find |
| 2646 |
* |
| 2647 |
* Purpose: Find the named user in the users database. Create the |
| 2648 |
* set of attribute-value pairs to check and reply with |
| 2649 |
* for this user from the database. |
| 2650 |
* |
| 2651 |
* Note that the users database can be either in the file |
| 2652 |
* "RADIUS_USERS" (-u option), in memory (default), or in |
| 2653 |
* a dbm(3) database. |
| 2654 |
* |
| 2655 |
*************************************************************************/ |
| 2656 |
|
| 2657 |
int |
| 2658 |
user_find (file_pfx, name, protocol, check_pairs, reply_pairs, not_user_file) |
| 2659 |
|
| 2660 |
char *file_pfx; /* Selects which "users" file to use */ |
| 2661 |
char *name; |
| 2662 |
int protocol; |
| 2663 |
VALUE_PAIR **check_pairs; |
| 2664 |
VALUE_PAIR **reply_pairs; |
| 2665 |
int not_user_file; /* Look up user in a domain specific file */ |
| 2666 |
|
| 2667 |
{ |
| 2668 |
int mode; |
| 2669 |
int namelen; |
| 2670 |
FILE *userfd; |
| 2671 |
VALUE_PAIR *check_first; |
| 2672 |
VALUE_PAIR *reply_first; |
| 2673 |
VALUE_PAIR *prot_ent; |
| 2674 |
FILE_LIST *file_ent; |
| 2675 |
USER_ENTRY *user_ent; |
| 2676 |
USER_ENTRY *dflt_ent; |
| 2677 |
char *ptr; |
| 2678 |
FILE *debugout = stdout; |
| 2679 |
char buffer[256]; |
| 2680 |
char fname[MAXPATHLEN]; |
| 2681 |
static char *func = "user_find"; |
| 2682 |
|
| 2683 |
#ifdef USE_NDBM |
| 2684 |
datum named; |
| 2685 |
datum contentd; |
| 2686 |
DBM *db; |
| 2687 |
#endif /* USE_NDBM */ |
| 2688 |
|
| 2689 |
#ifdef USE_DBM |
| 2690 |
datum named; |
| 2691 |
datum contentd; |
| 2692 |
#endif /* USE_DBM */ |
| 2693 |
|
| 2694 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered name='%s'", func, name)); |
| 2695 |
|
| 2696 |
*check_pairs = check_first = (VALUE_PAIR *) NULL; |
| 2697 |
*reply_pairs = reply_first = (VALUE_PAIR *) NULL; |
| 2698 |
|
| 2699 |
if ((namelen = strlen (name)) == 0) |
| 2700 |
{ |
| 2701 |
return 1; /* A null name would match every line. */ |
| 2702 |
} |
| 2703 |
|
| 2704 |
/* |
| 2705 |
* First check what type of lookup to do. |
| 2706 |
* See if user file(s) have been cached in memory. |
| 2707 |
*/ |
| 2708 |
|
| 2709 |
if (not_user_file == 0 && |
| 2710 |
(dflt_ent = file_list->user_list) != (USER_ENTRY *) NULL) |
| 2711 |
{ |
| 2712 |
if ((file_ent = find_file_ent (file_pfx)) == (FILE_LIST *) NULL) |
| 2713 |
{ |
| 2714 |
return (-1); |
| 2715 |
} |
| 2716 |
if ((user_ent = file_ent->user_list) == (USER_ENTRY *) NULL) |
| 2717 |
{ |
| 2718 |
/* If no user file for this prefix, ... */ |
| 2719 |
/* ... then use first entry in file_list as ... */ |
| 2720 |
/* ... default file_ent. */ |
| 2721 |
user_ent = dflt_ent; |
| 2722 |
} |
| 2723 |
for (; user_ent; user_ent = user_ent->next) |
| 2724 |
{ |
| 2725 |
/* Allow match for entry specifying framed protocol |
| 2726 |
* type specified in "protocol". An entry with a |
| 2727 |
* matching name but no framed-protocol type check |
| 2728 |
* item matches unconditionally. An entry with a |
| 2729 |
* matching name and a framed-protocol type check |
| 2730 |
* item must match value in "protocol". |
| 2731 |
*/ |
| 2732 |
|
| 2733 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: cache name='%s'", func, user_ent->name)); |
| 2734 |
|
| 2735 |
#ifndef REGNAMES |
| 2736 |
if (strcmp (user_ent->name, name) == 0) |
| 2737 |
#else /* REGNAMES */ |
| 2738 |
if (regimatch(user_ent->name, name)) |
| 2739 |
#endif /* REGNAMES */ |
| 2740 |
{ |
| 2741 |
if ((prot_ent = |
| 2742 |
get_vp (user_ent->check, |
| 2743 |
PW_FRAMED_PROTOCOL)) |
| 2744 |
== (VALUE_PAIR *) NULL) |
| 2745 |
{ |
| 2746 |
break; |
| 2747 |
} |
| 2748 |
if (prot_ent->lvalue == protocol) |
| 2749 |
{ |
| 2750 |
break; |
| 2751 |
} |
| 2752 |
} |
| 2753 |
} |
| 2754 |
if (!user_ent) /* rc 1 => User not found and no error */ |
| 2755 |
{ |
| 2756 |
return 1; |
| 2757 |
} |
| 2758 |
list_copy (&check_first, user_ent->check); |
| 2759 |
list_copy (&reply_first, user_ent->reply); |
| 2760 |
if (debug_flag >= 2) |
| 2761 |
{ |
| 2762 |
if (ddt) |
| 2763 |
{ |
| 2764 |
debugout = ddt; |
| 2765 |
} |
| 2766 |
|
| 2767 |
fprintf (debugout, "Check items:\n"); |
| 2768 |
debug_list (debugout, user_ent->check); |
| 2769 |
fprintf (debugout, "Reply items:\n"); |
| 2770 |
debug_list (debugout, user_ent->reply); |
| 2771 |
} |
| 2772 |
*check_pairs = check_first; |
| 2773 |
*reply_pairs = reply_first; |
| 2774 |
return (0); /* rc 0 => User found */ |
| 2775 |
} /* End of cached users file(s) lookup */ |
| 2776 |
|
| 2777 |
/* |
| 2778 |
* Open the user table. |
| 2779 |
* If prefixed file doesn't exist, use default (non-prefixed) file. |
| 2780 |
*/ |
| 2781 |
for (;;) |
| 2782 |
{ |
| 2783 |
sprintf (fname, "%s/%s%s", radius_dir, |
| 2784 |
file_pfx ? file_pfx : "", RADIUS_USERS); |
| 2785 |
|
| 2786 |
#if defined(USE_DBM) || defined(USE_NDBM) |
| 2787 |
#ifdef USE_NDBM |
| 2788 |
if ((db = dbm_open (fname, O_RDONLY, 0)) == 0) |
| 2789 |
{ |
| 2790 |
#endif /* USE_NDBM */ |
| 2791 |
#ifdef USE_DBM |
| 2792 |
if (dbminit (fname) != 0) |
| 2793 |
{ |
| 2794 |
#endif /* USE_DBM */ |
| 2795 |
#else /* USE_DBM || USE_NDBM */ |
| 2796 |
if ((userfd = fopen (fname, "r")) == (FILE *) NULL) |
| 2797 |
{ |
| 2798 |
#endif /* USE_DBM || USE_NDBM */ |
| 2799 |
|
| 2800 |
if (not_user_file == 0 && |
| 2801 |
file_pfx != NULL && |
| 2802 |
file_pfx[0] != 0) |
| 2803 |
{ |
| 2804 |
file_pfx = NULL; |
| 2805 |
continue; |
| 2806 |
} |
| 2807 |
|
| 2808 |
logit (LOG_DAEMON, LOG_ERR, |
| 2809 |
"%s: Couldn't open %s for reading", |
| 2810 |
func, fname); |
| 2811 |
return (-1); |
| 2812 |
} |
| 2813 |
break; |
| 2814 |
} |
| 2815 |
|
| 2816 |
#if defined(USE_DBM) || defined(USE_NDBM) |
| 2817 |
/* |
| 2818 |
* Note that the DBM feature currently does not support protocol specific user |
| 2819 |
* entry matching. It could be done by appending a protocol type to the |
| 2820 |
* name when building the database and looking for that modified name here. |
| 2821 |
* This seems like a non-trivial task and we don't use the DBM feature |
| 2822 |
* here so I'll leave this to someone else to implement. |
| 2823 |
*/ |
| 2824 |
named.dptr = name; |
| 2825 |
named.dsize = namelen; |
| 2826 |
|
| 2827 |
#ifdef USE_NDBM |
| 2828 |
contentd = dbm_fetch (db, named); |
| 2829 |
#else /* USE_NDBM */ |
| 2830 |
contentd = fetch (named); |
| 2831 |
#endif /* USE_NDBM */ |
| 2832 |
|
| 2833 |
if (contentd.dsize == 0) |
| 2834 |
{ |
| 2835 |
named.dptr = "DEFAULT"; |
| 2836 |
named.dsize = strlen ("DEFAULT"); |
| 2837 |
|
| 2838 |
#ifdef USE_NDBM |
| 2839 |
contentd = dbm_fetch (db, named); |
| 2840 |
#else /* USE_NDBM */ |
| 2841 |
contentd = fetch (named); |
| 2842 |
#endif /* USE_NDBM */ |
| 2843 |
|
| 2844 |
if (contentd.dsize == 0) |
| 2845 |
{ |
| 2846 |
#ifdef USE_NDBM |
| 2847 |
dbm_close (db); |
| 2848 |
#else /* USE_NDBM */ |
| 2849 |
dbmclose (); |
| 2850 |
#endif /* USE_NDBM */ |
| 2851 |
return (-1); |
| 2852 |
} |
| 2853 |
} |
| 2854 |
|
| 2855 |
/* |
| 2856 |
* Parse the check values |
| 2857 |
*/ |
| 2858 |
ptr = contentd.dptr; |
| 2859 |
contentd.dptr[contentd.dsize] = '\0'; |
| 2860 |
|
| 2861 |
if (pair_parse (ptr, &check_first) != 0) |
| 2862 |
{ |
| 2863 |
logit (LOG_DAEMON, LOG_ERR, |
| 2864 |
"%s: Parse error for user %s in %s (check)", |
| 2865 |
func, name, fname); |
| 2866 |
list_free (check_first); |
| 2867 |
#ifdef USE_NDBM |
| 2868 |
dbm_close (db); |
| 2869 |
#else /* USE_NDBM */ |
| 2870 |
dbmclose (); |
| 2871 |
#endif /* USE_NDBM */ |
| 2872 |
return (-1); |
| 2873 |
} |
| 2874 |
while (*ptr != '\n' && *ptr != '\0') |
| 2875 |
{ |
| 2876 |
ptr++; |
| 2877 |
} |
| 2878 |
if (*ptr != '\n') |
| 2879 |
{ |
| 2880 |
list_free (check_first); |
| 2881 |
#ifdef USE_NDBM |
| 2882 |
dbm_close (db); |
| 2883 |
#else /* USE_NDBM */ |
| 2884 |
dbmclose (); |
| 2885 |
#endif /* USE_NDBM */ |
| 2886 |
return (-1); |
| 2887 |
} |
| 2888 |
ptr++; |
| 2889 |
|
| 2890 |
/* |
| 2891 |
* Parse the reply values |
| 2892 |
*/ |
| 2893 |
if (pair_parse (ptr, &reply_first) != 0) |
| 2894 |
{ |
| 2895 |
logit (LOG_DAEMON, LOG_ERR, |
| 2896 |
"%s: Parse error for user %s in %s (reply)", |
| 2897 |
func, name, fname); |
| 2898 |
list_free (check_first); |
| 2899 |
list_free (reply_first); |
| 2900 |
#ifdef USE_NDBM |
| 2901 |
dbm_close (db); |
| 2902 |
#else /* USE_NDBM */ |
| 2903 |
dbmclose (); |
| 2904 |
#endif /* USE_NDBM */ |
| 2905 |
return (-1); |
| 2906 |
} |
| 2907 |
#ifdef USE_NDBM |
| 2908 |
dbm_close (db); |
| 2909 |
#else /* USE_NDBM */ |
| 2910 |
dbmclose (); |
| 2911 |
#endif /* USE_NDBM */ |
| 2912 |
*check_pairs = check_first; |
| 2913 |
*reply_pairs = reply_first; |
| 2914 |
return (0); |
| 2915 |
|
| 2916 |
#else /* USE_DBM || USE_NBDM */ |
| 2917 |
|
| 2918 |
mode = FIND_MODE_NAME; |
| 2919 |
|
| 2920 |
while (fgets (buffer, sizeof (buffer), userfd) != (char *) NULL) |
| 2921 |
{ |
| 2922 |
if (*buffer == COMMENT) |
| 2923 |
{ |
| 2924 |
continue; |
| 2925 |
} |
| 2926 |
|
| 2927 |
if (mode == FIND_MODE_NAME) |
| 2928 |
{ |
| 2929 |
/* |
| 2930 |
* Find the entry starting with the users name. |
| 2931 |
*/ |
| 2932 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: name='%s'", func, user_ent->name)); |
| 2933 |
|
| 2934 |
#ifndef REGNAMES |
| 2935 |
if (strncmp (buffer, name, namelen) == 0 && |
| 2936 |
(buffer[namelen] == ' ' || buffer[namelen] == '\t')) |
| 2937 |
{ |
| 2938 |
ptr = &buffer[namelen]; |
| 2939 |
#else /* REGNAMES */ |
| 2940 |
ptr = strtok(buffer, " \t\n"); |
| 2941 |
fprintf(debugout, "name='%s'", ptr); |
| 2942 |
if (regimatch(ptr, name)) |
| 2943 |
{ |
| 2944 |
ptr = &buffer[strlen(ptr)+1]; |
| 2945 |
fprintf(debugout, "name='%s'", ptr); |
| 2946 |
#endif /* REGNAMES */ |
| 2947 |
/* |
| 2948 |
* Parse the check values |
| 2949 |
*/ |
| 2950 |
if (pair_parse (ptr, &check_first) != 0) |
| 2951 |
{ |
| 2952 |
logit (LOG_DAEMON, LOG_ERR, |
| 2953 |
"%s: Parse error for user %s in %s (DBM check)", |
| 2954 |
func, name, fname); |
| 2955 |
list_free (check_first); |
| 2956 |
fclose (userfd); |
| 2957 |
return (-1); |
| 2958 |
} |
| 2959 |
/* Allow match for entry specifying framed |
| 2960 |
* protocol type specified in "protocol". An |
| 2961 |
* entry with matching name but no |
| 2962 |
* framed-protocol type check item matches |
| 2963 |
* unconditionally. An entry with matching |
| 2964 |
* name and a framed-protocol type check item |
| 2965 |
* must match value in "protocol". |
| 2966 |
*/ |
| 2967 |
if ((prot_ent = |
| 2968 |
get_vp (check_first, |
| 2969 |
PW_FRAMED_PROTOCOL)) |
| 2970 |
!= (VALUE_PAIR *) NULL) |
| 2971 |
{ |
| 2972 |
if (prot_ent->lvalue != protocol) |
| 2973 |
{ |
| 2974 |
list_free (check_first); |
| 2975 |
continue; |
| 2976 |
} |
| 2977 |
} |
| 2978 |
mode = FIND_MODE_REPLY; |
| 2979 |
} |
| 2980 |
} |
| 2981 |
else |
| 2982 |
{ |
| 2983 |
if (*buffer == ' ' || *buffer == '\t') |
| 2984 |
{ |
| 2985 |
/* |
| 2986 |
* Parse the reply values |
| 2987 |
*/ |
| 2988 |
if (pair_parse (buffer, &reply_first) != 0) |
| 2989 |
{ |
| 2990 |
logit (LOG_DAEMON, LOG_ERR, |
| 2991 |
"%s: Parse error for user %s in %s (DBM reply)", |
| 2992 |
func, name, fname); |
| 2993 |
list_free (check_first); |
| 2994 |
list_free (reply_first); |
| 2995 |
fclose (userfd); |
| 2996 |
return (-1); |
| 2997 |
} |
| 2998 |
} |
| 2999 |
else /* We are done */ |
| 3000 |
{ |
| 3001 |
break; |
| 3002 |
} |
| 3003 |
} |
| 3004 |
} |
| 3005 |
fclose (userfd); |
| 3006 |
|
| 3007 |
/* Update the callers pointers */ |
| 3008 |
if (mode == FIND_MODE_NAME) |
| 3009 |
{ |
| 3010 |
return 1; /* No error and no match */ |
| 3011 |
} |
| 3012 |
*check_pairs = check_first; |
| 3013 |
*reply_pairs = reply_first; |
| 3014 |
return (0); |
| 3015 |
#endif /* USE_DBM || USE_NBDM */ |
| 3016 |
} /* end of user_find () */ |
| 3017 |
|
| 3018 |
static char * months[] = |
| 3019 |
{ |
| 3020 |
"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 3021 |
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 3022 |
}; |
| 3023 |
|
| 3024 |
/************************************************************************* |
| 3025 |
* |
| 3026 |
* Function: user_gettime |
| 3027 |
* |
| 3028 |
* Purpose: Turns printable string into correct tm struct entries. |
| 3029 |
* |
| 3030 |
*************************************************************************/ |
| 3031 |
|
| 3032 |
void |
| 3033 |
user_gettime (valstr, tm) |
| 3034 |
|
| 3035 |
char *valstr; |
| 3036 |
struct tm *tm; |
| 3037 |
|
| 3038 |
{ |
| 3039 |
int i; |
| 3040 |
static char *func = "user_gettime"; |
| 3041 |
|
| 3042 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 3043 |
|
| 3044 |
/* Get the month */ |
| 3045 |
for (i = 0; i < 12; i++) |
| 3046 |
{ |
| 3047 |
if (strncmp (months[i], valstr, 3) == 0) |
| 3048 |
{ |
| 3049 |
tm->tm_mon = i; |
| 3050 |
i = 13; |
| 3051 |
} |
| 3052 |
} |
| 3053 |
|
| 3054 |
/* Get the Day */ |
| 3055 |
tm->tm_mday = atoi (&valstr[4]); |
| 3056 |
|
| 3057 |
/* Now the year */ |
| 3058 |
tm->tm_year = atoi (&valstr[7]) - 1900; |
| 3059 |
} /* end of user_gettime () */ |
| 3060 |
|
| 3061 |
/************************************************************************* |
| 3062 |
* |
| 3063 |
* Function: user_update |
| 3064 |
* |
| 3065 |
* Purpose: Updates a user in the database. Replaces the original |
| 3066 |
* entry with the name, the list of check items, and the |
| 3067 |
* list of reply items which are supplied. |
| 3068 |
* |
| 3069 |
*************************************************************************/ |
| 3070 |
|
| 3071 |
int |
| 3072 |
user_update (name, user_check, user_reply) |
| 3073 |
|
| 3074 |
char *name; |
| 3075 |
VALUE_PAIR *user_check; |
| 3076 |
VALUE_PAIR *user_reply; |
| 3077 |
|
| 3078 |
{ |
| 3079 |
FILE *oldfd; |
| 3080 |
FILE *userfd; |
| 3081 |
char buffer[256]; |
| 3082 |
char buffer1[256]; |
| 3083 |
int namelen; |
| 3084 |
int mode; |
| 3085 |
static char *func = "user_update"; |
| 3086 |
|
| 3087 |
dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); |
| 3088 |
|
| 3089 |
sprintf (buffer, "%s/%s", radius_dir, RADIUS_USERS); |
| 3090 |
sprintf (buffer1, "%s/%s", radius_dir, RADIUS_HOLD); |
| 3091 |
|
| 3092 |
/* Move the user table to a temporary location */ |
| 3093 |
if (rename (buffer, buffer1) != 0) |
| 3094 |
{ |
| 3095 |
logit (LOG_DAEMON, LOG_ERR, "%s: Couldn't rename %s", |
| 3096 |
func, buffer); |
| 3097 |
return (-1); |
| 3098 |
} |
| 3099 |
|
| 3100 |
/* Open the old user file (using the temporary name */ |
| 3101 |
if ((oldfd = fopen (buffer1, "r")) == (FILE *) NULL) |
| 3102 |
{ |
| 3103 |
logit (LOG_DAEMON, LOG_ERR, "%s: Couldn't open %s for reading", |
| 3104 |
func, buffer1); |
| 3105 |
exit (-9); |
| 3106 |
} |
| 3107 |
|
| 3108 |
/* Open the new user file */ |
| 3109 |
if ((userfd = fopen (buffer, "w")) == (FILE *) NULL) |
| 3110 |
{ |
| 3111 |
logit (LOG_DAEMON, LOG_ERR, "%s: Couldn't open %s for writing", |
| 3112 |
func, buffer); |
| 3113 |
exit (-9); |
| 3114 |
} |
| 3115 |
|
| 3116 |
mode = FIND_MODE_NAME; |
| 3117 |
namelen = strlen (name); |
| 3118 |
|
| 3119 |
/* Copy the old to the new, only recreating the changed user */ |
| 3120 |
while (fgets (buffer, sizeof (buffer), oldfd) != (char *) NULL) |
| 3121 |
{ |
| 3122 |
if (mode == FIND_MODE_NAME) |
| 3123 |
{ |
| 3124 |
if ((strncmp (buffer, name, namelen) == 0 && |
| 3125 |
(buffer[namelen] == ' ' || buffer[namelen] == '\t'))) |
| 3126 |
{ |
| 3127 |
|
| 3128 |
/* Write our new information */ |
| 3129 |
fprintf (userfd, "%s\t", name); |
| 3130 |
while (user_check != (VALUE_PAIR *) NULL) |
| 3131 |
{ |
| 3132 |
fprint_attr_val (userfd, user_check); |
| 3133 |
if (user_check->next != |
| 3134 |
(VALUE_PAIR *) NULL) |
| 3135 |
{ |
| 3136 |
fprintf (userfd, ", "); |
| 3137 |
} |
| 3138 |
user_check = user_check->next; |
| 3139 |
} |
| 3140 |
fprintf (userfd, "\n\t"); |
| 3141 |
while (user_reply != (VALUE_PAIR *) NULL) |
| 3142 |
{ |
| 3143 |
fprint_attr_val (userfd, user_reply); |
| 3144 |
if (user_reply->next != |
| 3145 |
(VALUE_PAIR *) NULL) |
| 3146 |
{ |
| 3147 |
fprintf (userfd, ",\n\t"); |
| 3148 |
} |
| 3149 |
user_reply = user_reply->next; |
| 3150 |
} |
| 3151 |
fprintf (userfd, "\n"); |
| 3152 |
mode = FIND_MODE_SKIP; |
| 3153 |
} |
| 3154 |
else |
| 3155 |
{ |
| 3156 |
fputs (buffer, userfd); |
| 3157 |
} |
| 3158 |
} |
| 3159 |
else if (mode == FIND_MODE_SKIP) |
| 3160 |
{ |
| 3161 |
if (*buffer != ' ' && *buffer != '\t') |
| 3162 |
{ |
| 3163 |
fputs (buffer, userfd); |
| 3164 |
mode = FIND_MODE_FLUSH; |
| 3165 |
} |
| 3166 |
} |
| 3167 |
else |
| 3168 |
{ |
| 3169 |
fputs (buffer, userfd); |
| 3170 |
} |
| 3171 |
} |
| 3172 |
fclose (oldfd); |
| 3173 |
fclose (userfd); |
| 3174 |
return (0); |
| 3175 |
} /* end of user_update () */ |