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