]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/config.c
CI: Updated the 'checkout' actions that were using Node.js 16 to Node.js 20. (#489)
[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                         fclose(configfd);
216                         return (-1);
217                 }
218
219                 p[pos] = '\0';
220
221                 if ((option = find_option(p, OT_ANY)) == NULL) {
222                         warn("%s: line %d: unrecognized keyword: %s", filename, line, p);
223                         continue;
224                 }
225
226                 if (option->status != ST_UNDEF) {
227                         error("%s: line %d: duplicate option line: %s", filename, line, p);
228                         fclose(configfd);
229                         return (-1);
230                 }
231
232                 p += pos+1;
233                 while (isspace(*p))
234                         p++;
235
236                 switch (option->type) {
237                         case OT_STR:
238                                 if (set_option_str(filename, line, option, p) < 0) {
239                                         fclose(configfd);
240                                         return (-1);
241                                 }
242                                 break;
243                         case OT_INT:
244                                 if (set_option_int(filename, line, option, p) < 0) {
245                                         fclose(configfd);
246                                         return (-1);
247                                 }
248                                 break;
249                         case OT_SRV:
250                                 if (set_option_srv(filename, line, option, p) < 0) {
251                                         fclose(configfd);
252                                         return (-1);
253                                 }
254                                 break;
255                         case OT_AUO:
256                                 if (set_option_auo(filename, line, option, p) < 0) {
257                                         fclose(configfd);
258                                         return (-1);
259                                 }
260                                 break;
261                         default:
262                                 fatal("rc_read_config: impossible case branch!");
263                                 abort();
264                 }
265         }
266         fclose(configfd);
267
268         return test_config(filename);
269 }
270
271 /*
272  * Function: rc_conf_str, rc_conf_int, rc_conf_src
273  *
274  * Purpose: get the value of a config option
275  *
276  * Returns: config option value
277  */
278
279 char *rc_conf_str(char *optname)
280 {
281         OPTION *option;
282
283         option = find_option(optname, OT_STR);
284
285         if (option == NULL)
286                 fatal("rc_conf_str: unknown config option requested: %s", optname);
287         return (char *)option->val;
288 }
289
290 int rc_conf_int(char *optname)
291 {
292         OPTION *option;
293
294         option = find_option(optname, OT_INT|OT_AUO);
295
296         if (option == NULL)
297                 fatal("rc_conf_int: unknown config option requested: %s", optname);
298         return *((int *)option->val);
299 }
300
301 SERVER *rc_conf_srv(char *optname)
302 {
303         OPTION *option;
304
305         option = find_option(optname, OT_SRV);
306
307         if (option == NULL)
308                 fatal("rc_conf_srv: unknown config option requested: %s", optname);
309         return (SERVER *)option->val;
310 }
311
312 /*
313  * Function: test_config
314  *
315  * Purpose: test the configuration the user supplied
316  *
317  * Returns: 0 on success, -1 when failure
318  */
319
320 static int test_config(char *filename)
321 {
322 #if 0
323         struct stat st;
324         char        *file;
325 #endif
326
327         if (!(rc_conf_srv("authserver")->max))
328         {
329                 error("%s: no authserver specified", filename);
330                 return (-1);
331         }
332         if (!(rc_conf_srv("acctserver")->max))
333         {
334                 error("%s: no acctserver specified", filename);
335                 return (-1);
336         }
337         if (!rc_conf_str("servers"))
338         {
339                 error("%s: no servers file specified", filename);
340                 return (-1);
341         }
342         if (!rc_conf_str("dictionary"))
343         {
344                 error("%s: no dictionary specified", filename);
345                 return (-1);
346         }
347
348         if (rc_conf_int("radius_timeout") <= 0)
349         {
350                 error("%s: radius_timeout <= 0 is illegal", filename);
351                 return (-1);
352         }
353         if (rc_conf_int("radius_retries") <= 0)
354         {
355                 error("%s: radius_retries <= 0 is illegal", filename);
356                 return (-1);
357         }
358
359 #if 0
360         file = rc_conf_str("login_local");
361         if (stat(file, &st) == 0)
362         {
363                 if (!S_ISREG(st.st_mode)) {
364                         error("%s: not a regular file: %s", filename, file);
365                         return (-1);
366                 }
367         } else {
368                 error("%s: file not found: %s", filename, file);
369                 return (-1);
370         }
371         file = rc_conf_str("login_radius");
372         if (stat(file, &st) == 0)
373         {
374                 if (!S_ISREG(st.st_mode)) {
375                         error("%s: not a regular file: %s", filename, file);
376                         return (-1);
377                 }
378         } else {
379                 error("%s: file not found: %s", filename, file);
380                 return (-1);
381         }
382 #endif
383
384         if (rc_conf_int("login_tries") <= 0)
385         {
386                 error("%s: login_tries <= 0 is illegal", filename);
387                 return (-1);
388         }
389         if (rc_conf_str("seqfile") == NULL)
390         {
391                 error("%s: seqfile not specified", filename);
392                 return (-1);
393         }
394         if (rc_conf_int("login_timeout") <= 0)
395         {
396                 error("%s: login_timeout <= 0 is illegal", filename);
397                 return (-1);
398         }
399         if (rc_conf_str("mapfile") == NULL)
400         {
401                 error("%s: mapfile not specified", filename);
402                 return (-1);
403         }
404         if (rc_conf_str("nologin") == NULL)
405         {
406                 error("%s: nologin not specified", filename);
407                 return (-1);
408         }
409
410         return 0;
411 }
412
413 /*
414  * Function: rc_find_match
415  *
416  * Purpose: see if ip_addr is one of the ip addresses of hostname
417  *
418  * Returns: 0 on success, -1 when failure
419  *
420  */
421
422 static int find_match (UINT4 *ip_addr, char *hostname)
423 {
424         UINT4           addr;
425         char          **paddr;
426         struct hostent *hp;
427
428         if (rc_good_ipaddr (hostname) == 0)
429         {
430                 if (*ip_addr == ntohl(inet_addr (hostname)))
431                 {
432                         return (0);
433                 }
434         }
435         else
436         {
437                 if ((hp = gethostbyname (hostname)) == (struct hostent *) NULL)
438                 {
439                         return (-1);
440                 }
441                 for (paddr = hp->h_addr_list; *paddr; paddr++)
442                 {
443                         addr = ** (UINT4 **) paddr;
444                         if (ntohl(addr) == *ip_addr)
445                         {
446                                 return (0);
447                         }
448                 }
449         }
450         return (-1);
451 }
452
453 /*
454  * Function: rc_find_server
455  *
456  * Purpose: search a server in the servers file
457  *
458  * Returns: 0 on success, -1 on failure
459  *
460  */
461
462 int rc_find_server (char *server_name, UINT4 *ip_addr, char *secret)
463 {
464         UINT4   myipaddr = 0;
465         int             len;
466         int             result;
467         FILE           *clientfd;
468         char           *h;
469         char           *s;
470         char           *host2;
471         char            buffer[128];
472         char            hostnm[AUTH_ID_LEN + 1];
473
474         /* Get the IP address of the authentication server */
475         if ((*ip_addr = rc_get_ipaddr (server_name)) == (UINT4) 0)
476                 return (-1);
477
478         if ((clientfd = fopen (rc_conf_str("servers"), "r")) == (FILE *) NULL)
479         {
480                 error("rc_find_server: couldn't open file: %m: %s", rc_conf_str("servers"));
481                 return (-1);
482         }
483
484         myipaddr = rc_own_ipaddress();
485
486         result = 0;
487         while (fgets (buffer, sizeof (buffer), clientfd) != (char *) NULL)
488         {
489                 if (*buffer == '#')
490                         continue;
491
492                 if ((h = strtok (buffer, " \t\n")) == NULL) /* first hostname */
493                         continue;
494
495                 memset (hostnm, '\0', AUTH_ID_LEN + 1);
496                 strlcpy (hostnm, h, AUTH_ID_LEN + 1);
497
498                 if ((s = strtok (NULL, " \t\n")) == NULL) /* and secret field */
499                         continue;
500
501                 memset (secret, '\0', MAX_SECRET_LENGTH + 1);
502                 strlcpy (secret, s, MAX_SECRET_LENGTH + 1);
503
504                 if (!strchr (hostnm, '/')) /* If single name form */
505                 {
506                         if (find_match (ip_addr, hostnm) == 0)
507                         {
508                                 result++;
509                                 break;
510                         }
511                 }
512                 else /* <name1>/<name2> "paired" form */
513                 {
514                         strtok (hostnm, "/");
515                         if (find_match (&myipaddr, hostnm) == 0)
516                         {            /* If we're the 1st name, target is 2nd */
517                                 host2 = strtok (NULL, " ");
518                                 if (find_match (ip_addr, host2) == 0)
519                                 {
520                                         result++;
521                                         break;
522                                 }
523                         }
524                         else    /* If we were 2nd name, target is 1st name */
525                         {
526                                 if (find_match (ip_addr, hostnm) == 0)
527                                 {
528                                         result++;
529                                         break;
530                                 }
531                         }
532                 }
533         }
534         fclose (clientfd);
535         if (result == 0)
536         {
537                 memset (buffer, '\0', sizeof (buffer));
538                 error("rc_find_server: couldn't find RADIUS server %s in %s",
539                       server_name, rc_conf_str("servers"));
540                 return (-1);
541         }
542         return 0;
543 }