]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radrealms.c
Fix include paths for plugins to use the public API of pppd
[ppp.git] / pppd / plugins / radius / radrealms.c
1 /*
2 *
3 * radrealms.c
4 *
5 * A pppd plugin which is stacked on top of radius.so.  This plugin
6 * allows selection of alternate set of servers based on the user's realm.
7 *
8 * Author: Ben McKeegan  ben@netservers.co.uk
9 *
10 * Copyright (C) 2002 Netservers
11 *
12 * This plugin may be distributed according to the terms of the GNU
13 * General Public License, version 2 or (at your option) any later version.
14 *
15 */
16
17 static char const RCSID[] =
18     "$Id: radrealms.c,v 1.2 2004/11/14 07:26:26 paulus Exp $";
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <pppd/pppd.h>
24
25 #include "radiusclient.h"
26
27 char pppd_version[] = PPPD_VERSION;
28
29 char radrealms_config[MAXPATHLEN] = "/etc/radiusclient/realms";
30
31 static option_t Options[] = {
32     { "realms-config-file", o_string, &radrealms_config,
33       "Configuration file for RADIUS realms", OPT_STATIC, NULL, MAXPATHLEN },
34     { NULL }
35 };
36
37 extern void (*radius_pre_auth_hook)(char const *user,
38                                     SERVER **authserver,
39                                     SERVER **acctserver);
40
41 static void
42 lookup_realm(char const *user,
43              SERVER **authserver,
44              SERVER **acctserver)
45 {
46     char *realm;
47     FILE *fd;
48     SERVER *accts, *auths, *s;
49     char buffer[512], *p;
50     int line = 0;
51     
52     auths = (SERVER *) malloc(sizeof(SERVER));
53     auths->max = 0;
54     accts = (SERVER *) malloc(sizeof(SERVER));
55     accts->max = 0;
56     
57     realm = strrchr(user, '@');
58     
59     if (realm) {
60         info("Looking up servers for realm '%s'", realm);
61     } else {
62         info("Looking up servers for DEFAULT realm");
63     }
64     if (realm) {
65         if (*(++realm) == '\0') {
66             realm = NULL;
67         }
68     }
69     
70     if ((fd = fopen(radrealms_config, "r")) == NULL) {
71         option_error("cannot open %s", radrealms_config);
72         free(auths);
73         free(accts);
74         return;
75     }
76     info("Reading %s", radrealms_config);
77
78     while ((fgets(buffer, sizeof(buffer), fd) != NULL)) {
79         line++;
80
81         if ((*buffer == '\n') || (*buffer == '#') || (*buffer == '\0'))
82             continue;
83
84         buffer[strlen(buffer)-1] = '\0';
85
86         p = strtok(buffer, "\t ");
87
88         if (p == NULL || (strcmp(p, "authserver") !=0
89             && strcmp(p, "acctserver"))) {
90             fclose(fd);
91             option_error("%s: invalid line %d: %s", radrealms_config,
92                          line, buffer);
93             free(auths);
94             free(accts);
95             return;
96         }
97         info("Parsing '%s' entry:", p);
98         s = auths;
99         if (p[1] == 'c') {
100             s = accts;
101         }
102         if (s->max >= SERVER_MAX)
103             continue;
104
105         if ((p = strtok(NULL, "\t ")) == NULL) {
106             fclose(fd);
107             option_error("%s: realm name missing on line %d: %s",
108                          radrealms_config, line, buffer);
109             free(auths);
110             free(accts);
111             return;
112         }
113
114         if ((realm != NULL && strcmp(p, realm) == 0) ||
115             (realm == NULL && strcmp(p, "DEFAULT") == 0) ) {
116             info(" - Matched realm %s", p);
117             if ((p = strtok(NULL, ":")) == NULL) {
118                 fclose(fd);
119                 option_error("%s: server address missing on line %d: %s",
120                              radrealms_config, line, buffer);
121                 free(auths);
122                 free(accts);
123                 return;
124             }
125             s->name[s->max] = strdup(p);
126             info(" - Address is '%s'",p);
127             if ((p = strtok(NULL, "\t ")) == NULL) {
128                 fclose(fd);
129                 option_error("%s: server port missing on line %d:  %s",
130                              radrealms_config, line, buffer);
131                 free(auths);
132                 free(accts);
133                 return;
134             }
135             s->port[s->max] = atoi(p);
136             info(" - Port is '%d'", s->port[s->max]);
137             s->max++;
138         } else 
139             info(" - Skipping realm '%s'", p);
140     }
141     fclose(fd);
142
143     if (accts->max)
144         *acctserver = accts;
145
146     if (auths->max)
147         *authserver = auths;
148
149     return;
150 }
151
152 void
153 plugin_init(void)
154 {
155     radius_pre_auth_hook = lookup_realm;
156
157     add_options(Options);
158     info("RADIUS Realms plugin initialized.");
159 }