config.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * $Id: config.c,v 1.1 2004/11/14 07:26:26 paulus Exp $
  3. *
  4. * Copyright (C) 1995,1996,1997 Lars Fenneberg
  5. *
  6. * Copyright 1992 Livingston Enterprises, Inc.
  7. *
  8. * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
  9. * and Merit Network, Inc. All Rights Reserved
  10. *
  11. * See the file COPYRIGHT for the respective terms and conditions.
  12. * If the file is missing contact me at lf@elemental.net
  13. * and I'll send you a copy.
  14. *
  15. */
  16. #include <includes.h>
  17. #include <radiusclient.h>
  18. #include <options.h>
  19. static int test_config(char *);
  20. /*
  21. * Function: find_option
  22. *
  23. * Purpose: find an option in the option list
  24. *
  25. * Returns: pointer to option on success, NULL otherwise
  26. */
  27. static OPTION *find_option(char *optname, unsigned int type)
  28. {
  29. int i;
  30. /* there're so few options that a binary search seems not necessary */
  31. for (i = 0; i < num_options; i++) {
  32. if (!strcmp(config_options[i].name, optname) &&
  33. (config_options[i].type & type))
  34. return &config_options[i];
  35. }
  36. return NULL;
  37. }
  38. /*
  39. * Function: set_option_...
  40. *
  41. * Purpose: set a specific option doing type conversions
  42. *
  43. * Returns: 0 on success, -1 on failure
  44. */
  45. static int set_option_str(char *filename, int line, OPTION *option, char *p)
  46. {
  47. if (p)
  48. option->val = (void *) strdup(p);
  49. else
  50. option->val = NULL;
  51. return 0;
  52. }
  53. static int set_option_int(char *filename, int line, OPTION *option, char *p)
  54. {
  55. int *iptr;
  56. if (p == NULL) {
  57. error("%s: line %d: bogus option value", filename, line);
  58. return (-1);
  59. }
  60. if ((iptr = (int *) malloc(sizeof(iptr))) == NULL) {
  61. novm("read_config");
  62. return (-1);
  63. }
  64. *iptr = atoi(p);
  65. option->val = (void *) iptr;
  66. return 0;
  67. }
  68. static int set_option_srv(char *filename, int line, OPTION *option, char *p)
  69. {
  70. SERVER *serv;
  71. char *q;
  72. struct servent *svp;
  73. int i;
  74. if (p == NULL) {
  75. error("%s: line %d: bogus option value", filename, line);
  76. return (-1);
  77. }
  78. serv = (SERVER *) option->val;
  79. for (i = 0; i < serv->max; i++) {
  80. free(serv->name[i]);
  81. }
  82. serv->max = 0;
  83. while ((p = strtok(p, ", \t")) != NULL) {
  84. if ((q = strchr(p,':')) != NULL) {
  85. *q = '\0';
  86. q++;
  87. serv->port[serv->max] = atoi(q);
  88. } else {
  89. if (!strcmp(option->name,"authserver"))
  90. if ((svp = getservbyname ("radius", "udp")) == NULL)
  91. serv->port[serv->max] = PW_AUTH_UDP_PORT;
  92. else
  93. serv->port[serv->max] = ntohs ((unsigned int) svp->s_port);
  94. else if (!strcmp(option->name, "acctserver"))
  95. if ((svp = getservbyname ("radacct", "udp")) == NULL)
  96. serv->port[serv->max] = PW_ACCT_UDP_PORT;
  97. else
  98. serv->port[serv->max] = ntohs ((unsigned int) svp->s_port);
  99. else {
  100. error("%s: line %d: no default port for %s", filename, line, option->name);
  101. return (-1);
  102. }
  103. }
  104. serv->name[serv->max++] = strdup(p);
  105. p = NULL;
  106. }
  107. return 0;
  108. }
  109. static int set_option_auo(char *filename, int line, OPTION *option, char *p)
  110. {
  111. int *iptr;
  112. if (p == NULL) {
  113. warn("%s: line %d: bogus option value", filename, line);
  114. return (-1);
  115. }
  116. if ((iptr = (int *) malloc(sizeof(iptr))) == NULL) {
  117. novm("read_config");
  118. return (-1);
  119. }
  120. *iptr = 0;
  121. p = strtok(p, ", \t");
  122. if (!strncmp(p, "local", 5))
  123. *iptr = AUTH_LOCAL_FST;
  124. else if (!strncmp(p, "radius", 6))
  125. *iptr = AUTH_RADIUS_FST;
  126. else {
  127. error("%s: auth_order: unknown keyword: %s", filename, p);
  128. return (-1);
  129. }
  130. p = strtok(NULL, ", \t");
  131. if (p && (*p != '\0')) {
  132. if ((*iptr & AUTH_RADIUS_FST) && !strcmp(p, "local"))
  133. *iptr = (*iptr) | AUTH_LOCAL_SND;
  134. else if ((*iptr & AUTH_LOCAL_FST) && !strcmp(p, "radius"))
  135. *iptr = (*iptr) | AUTH_RADIUS_SND;
  136. else {
  137. error("%s: auth_order: unknown or unexpected keyword: %s", filename, p);
  138. return (-1);
  139. }
  140. }
  141. option->val = (void *) iptr;
  142. return 0;
  143. }
  144. /*
  145. * Function: rc_read_config
  146. *
  147. * Purpose: read the global config file
  148. *
  149. * Returns: 0 on success, -1 when failure
  150. */
  151. int rc_read_config(char *filename)
  152. {
  153. FILE *configfd;
  154. char buffer[512], *p;
  155. OPTION *option;
  156. int line, pos;
  157. if ((configfd = fopen(filename,"r")) == NULL)
  158. {
  159. error("rc_read_config: can't open %s: %m", filename);
  160. return (-1);
  161. }
  162. line = 0;
  163. while ((fgets(buffer, sizeof(buffer), configfd) != NULL))
  164. {
  165. line++;
  166. p = buffer;
  167. if ((*p == '\n') || (*p == '#') || (*p == '\0'))
  168. continue;
  169. p[strlen(p)-1] = '\0';
  170. if ((pos = strcspn(p, "\t ")) == 0) {
  171. error("%s: line %d: bogus format: %s", filename, line, p);
  172. return (-1);
  173. }
  174. p[pos] = '\0';
  175. if ((option = find_option(p, OT_ANY)) == NULL) {
  176. warn("%s: line %d: unrecognized keyword: %s", filename, line, p);
  177. continue;
  178. }
  179. if (option->status != ST_UNDEF) {
  180. error("%s: line %d: duplicate option line: %s", filename, line, p);
  181. return (-1);
  182. }
  183. p += pos+1;
  184. while (isspace(*p))
  185. p++;
  186. switch (option->type) {
  187. case OT_STR:
  188. if (set_option_str(filename, line, option, p) < 0)
  189. return (-1);
  190. break;
  191. case OT_INT:
  192. if (set_option_int(filename, line, option, p) < 0)
  193. return (-1);
  194. break;
  195. case OT_SRV:
  196. if (set_option_srv(filename, line, option, p) < 0)
  197. return (-1);
  198. break;
  199. case OT_AUO:
  200. if (set_option_auo(filename, line, option, p) < 0)
  201. return (-1);
  202. break;
  203. default:
  204. fatal("rc_read_config: impossible case branch!");
  205. abort();
  206. }
  207. }
  208. fclose(configfd);
  209. return test_config(filename);
  210. }
  211. /*
  212. * Function: rc_conf_str, rc_conf_int, rc_conf_src
  213. *
  214. * Purpose: get the value of a config option
  215. *
  216. * Returns: config option value
  217. */
  218. char *rc_conf_str(char *optname)
  219. {
  220. OPTION *option;
  221. option = find_option(optname, OT_STR);
  222. if (option == NULL)
  223. fatal("rc_conf_str: unkown config option requested: %s", optname);
  224. return (char *)option->val;
  225. }
  226. int rc_conf_int(char *optname)
  227. {
  228. OPTION *option;
  229. option = find_option(optname, OT_INT|OT_AUO);
  230. if (option == NULL)
  231. fatal("rc_conf_int: unkown config option requested: %s", optname);
  232. return *((int *)option->val);
  233. }
  234. SERVER *rc_conf_srv(char *optname)
  235. {
  236. OPTION *option;
  237. option = find_option(optname, OT_SRV);
  238. if (option == NULL)
  239. fatal("rc_conf_srv: unkown config option requested: %s", optname);
  240. return (SERVER *)option->val;
  241. }
  242. /*
  243. * Function: test_config
  244. *
  245. * Purpose: test the configuration the user supplied
  246. *
  247. * Returns: 0 on success, -1 when failure
  248. */
  249. static int test_config(char *filename)
  250. {
  251. #if 0
  252. struct stat st;
  253. char *file;
  254. #endif
  255. if (!(rc_conf_srv("authserver")->max))
  256. {
  257. error("%s: no authserver specified", filename);
  258. return (-1);
  259. }
  260. if (!(rc_conf_srv("acctserver")->max))
  261. {
  262. error("%s: no acctserver specified", filename);
  263. return (-1);
  264. }
  265. if (!rc_conf_str("servers"))
  266. {
  267. error("%s: no servers file specified", filename);
  268. return (-1);
  269. }
  270. if (!rc_conf_str("dictionary"))
  271. {
  272. error("%s: no dictionary specified", filename);
  273. return (-1);
  274. }
  275. if (rc_conf_int("radius_timeout") <= 0)
  276. {
  277. error("%s: radius_timeout <= 0 is illegal", filename);
  278. return (-1);
  279. }
  280. if (rc_conf_int("radius_retries") <= 0)
  281. {
  282. error("%s: radius_retries <= 0 is illegal", filename);
  283. return (-1);
  284. }
  285. #if 0
  286. file = rc_conf_str("login_local");
  287. if (stat(file, &st) == 0)
  288. {
  289. if (!S_ISREG(st.st_mode)) {
  290. error("%s: not a regular file: %s", filename, file);
  291. return (-1);
  292. }
  293. } else {
  294. error("%s: file not found: %s", filename, file);
  295. return (-1);
  296. }
  297. file = rc_conf_str("login_radius");
  298. if (stat(file, &st) == 0)
  299. {
  300. if (!S_ISREG(st.st_mode)) {
  301. error("%s: not a regular file: %s", filename, file);
  302. return (-1);
  303. }
  304. } else {
  305. error("%s: file not found: %s", filename, file);
  306. return (-1);
  307. }
  308. #endif
  309. if (rc_conf_int("login_tries") <= 0)
  310. {
  311. error("%s: login_tries <= 0 is illegal", filename);
  312. return (-1);
  313. }
  314. if (rc_conf_str("seqfile") == NULL)
  315. {
  316. error("%s: seqfile not specified", filename);
  317. return (-1);
  318. }
  319. if (rc_conf_int("login_timeout") <= 0)
  320. {
  321. error("%s: login_timeout <= 0 is illegal", filename);
  322. return (-1);
  323. }
  324. if (rc_conf_str("mapfile") == NULL)
  325. {
  326. error("%s: mapfile not specified", filename);
  327. return (-1);
  328. }
  329. if (rc_conf_str("nologin") == NULL)
  330. {
  331. error("%s: nologin not specified", filename);
  332. return (-1);
  333. }
  334. return 0;
  335. }
  336. /*
  337. * Function: rc_find_match
  338. *
  339. * Purpose: see if ip_addr is one of the ip addresses of hostname
  340. *
  341. * Returns: 0 on success, -1 when failure
  342. *
  343. */
  344. static int find_match (UINT4 *ip_addr, char *hostname)
  345. {
  346. UINT4 addr;
  347. char **paddr;
  348. struct hostent *hp;
  349. if (rc_good_ipaddr (hostname) == 0)
  350. {
  351. if (*ip_addr == ntohl(inet_addr (hostname)))
  352. {
  353. return (0);
  354. }
  355. }
  356. else
  357. {
  358. if ((hp = gethostbyname (hostname)) == (struct hostent *) NULL)
  359. {
  360. return (-1);
  361. }
  362. for (paddr = hp->h_addr_list; *paddr; paddr++)
  363. {
  364. addr = ** (UINT4 **) paddr;
  365. if (ntohl(addr) == *ip_addr)
  366. {
  367. return (0);
  368. }
  369. }
  370. }
  371. return (-1);
  372. }
  373. /*
  374. * Function: rc_find_server
  375. *
  376. * Purpose: search a server in the servers file
  377. *
  378. * Returns: 0 on success, -1 on failure
  379. *
  380. */
  381. int rc_find_server (char *server_name, UINT4 *ip_addr, char *secret)
  382. {
  383. UINT4 myipaddr = 0;
  384. int len;
  385. int result;
  386. FILE *clientfd;
  387. char *h;
  388. char *s;
  389. char *host2;
  390. char buffer[128];
  391. char hostnm[AUTH_ID_LEN + 1];
  392. /* Get the IP address of the authentication server */
  393. if ((*ip_addr = rc_get_ipaddr (server_name)) == (UINT4) 0)
  394. return (-1);
  395. if ((clientfd = fopen (rc_conf_str("servers"), "r")) == (FILE *) NULL)
  396. {
  397. error("rc_find_server: couldn't open file: %m: %s", rc_conf_str("servers"));
  398. return (-1);
  399. }
  400. myipaddr = rc_own_ipaddress();
  401. result = 0;
  402. while (fgets (buffer, sizeof (buffer), clientfd) != (char *) NULL)
  403. {
  404. if (*buffer == '#')
  405. continue;
  406. if ((h = strtok (buffer, " \t\n")) == NULL) /* first hostname */
  407. continue;
  408. memset (hostnm, '\0', AUTH_ID_LEN);
  409. len = strlen (h);
  410. if (len > AUTH_ID_LEN)
  411. {
  412. len = AUTH_ID_LEN;
  413. }
  414. strncpy (hostnm, h, (size_t) len);
  415. hostnm[AUTH_ID_LEN] = '\0';
  416. if ((s = strtok (NULL, " \t\n")) == NULL) /* and secret field */
  417. continue;
  418. memset (secret, '\0', MAX_SECRET_LENGTH);
  419. len = strlen (s);
  420. if (len > MAX_SECRET_LENGTH)
  421. {
  422. len = MAX_SECRET_LENGTH;
  423. }
  424. strncpy (secret, s, (size_t) len);
  425. secret[MAX_SECRET_LENGTH] = '\0';
  426. if (!strchr (hostnm, '/')) /* If single name form */
  427. {
  428. if (find_match (ip_addr, hostnm) == 0)
  429. {
  430. result++;
  431. break;
  432. }
  433. }
  434. else /* <name1>/<name2> "paired" form */
  435. {
  436. strtok (hostnm, "/");
  437. if (find_match (&myipaddr, hostnm) == 0)
  438. { /* If we're the 1st name, target is 2nd */
  439. host2 = strtok (NULL, " ");
  440. if (find_match (ip_addr, host2) == 0)
  441. {
  442. result++;
  443. break;
  444. }
  445. }
  446. else /* If we were 2nd name, target is 1st name */
  447. {
  448. if (find_match (ip_addr, hostnm) == 0)
  449. {
  450. result++;
  451. break;
  452. }
  453. }
  454. }
  455. }
  456. fclose (clientfd);
  457. if (result == 0)
  458. {
  459. memset (buffer, '\0', sizeof (buffer));
  460. memset (secret, '\0', sizeof (secret));
  461. error("rc_find_server: couldn't find RADIUS server %s in %s",
  462. server_name, rc_conf_str("servers"));
  463. return (-1);
  464. }
  465. return 0;
  466. }