import from HEAD
[freeradius.git] / src / modules / rlm_sim_files / rlm_sim_files.c
1 /*
2  * rlm_sim_files.c      authorization: Find a SIM user in the "simtriplets"
3  *                                     file.
4  *
5  * Version:     $Id$
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * Copyright 2004  Michael Richardson <mcr@sandelman.ottawa.on.ca>
22  *
23  * (Adapted from rlm_files/rlm_files.c )
24  */
25
26 /*
27  * this is an authorization-only module that walks the file every time.
28  *
29  * this is an example of getting data for rlm_eap_sim from an external
30  * place.
31  *
32  * in a real system, this would be replaced with a lookup to the SS7
33  * network, but those interfaces are distinctly non-standard, and might
34  * even be totally proprietary
35  *
36  */
37
38 /* FILE FORMAT
39  *
40  *
41  * The triplets file contains records of the form:
42  *
43  * IMSI            RAND                             SRES     Kc
44  * 232420100000015,30000000000000000000000000000000,30112233,445566778899AABB
45  *
46  * there must be *three* entries for every IMSI for it to be considered valid.
47  *
48  * Lines starting with # are ignored.
49  *
50  * Conveniently, this file format is produced by XXXX.
51
52  *
53  */
54
55
56 static const char rcsid[] = "$Id$";
57
58 #include        "autoconf.h"
59 #include        "libradius.h"
60
61 #include        <sys/stat.h>
62
63 #include        <stdlib.h>
64 #include        <string.h>
65 #include        <netdb.h>
66 #include        <ctype.h>
67 #include        <fcntl.h>
68 #include        <limits.h>
69
70 #include        "radiusd.h"
71 #include        "modules.h"
72 #include        "../rlm_eap/libeap/eap_sim.h"
73
74 struct sim_file_instance {
75         /* autz */
76         char *file;
77 };
78
79 static CONF_PARSER module_config[] = {
80         { "simtriplets",        PW_TYPE_STRING_PTR,
81           offsetof(struct sim_file_instance, file),
82           NULL, "${raddbdir}/simtriplets.dat" },
83
84         { NULL, -1, 0, NULL, NULL }
85 };
86
87 /*
88  *      (Re-)read the "users" file into memory.
89  */
90 static int sim_file_instantiate(CONF_SECTION *conf, void **instance)
91 {
92         struct sim_file_instance *inst;
93
94         inst = rad_malloc(sizeof *inst);
95         if (!inst) {
96                 return -1;
97         }
98         memset(inst, 0, sizeof(*inst));
99
100         if (cf_section_parse(conf, inst, module_config) < 0) {
101                 free(inst);
102                 return -1;
103         }
104
105         *instance = inst;
106         return 0;
107 }
108
109 /*
110  *      Find the named user in the database.  Create the
111  *      set of attribute-value pairs to check and reply with
112  *      for this user from the database. The main code only
113  *      needs to check the password, the rest is done here.
114  */
115 static int sim_file_authorize(void *instance, REQUEST *request)
116 {
117         VALUE_PAIR      *namepair;
118         VALUE_PAIR      *reply_tmp;
119         const char      *name;
120         struct sim_file_instance *inst = instance;
121         VALUE_PAIR     **reply_pairs;
122         VALUE_PAIR     **config_pairs;
123         FILE            *triplets;
124         char             tripbuf[sizeof("232420100000015,30000000000000000000000000000000,30112233,445566778899AABB")*2];
125         char             imsi[128], chal[256], kc[128], sres[128];
126         int              imsicount;
127         int              fieldcount;
128         int lineno;
129
130         reply_pairs = &request->reply->vps;
131         config_pairs = &request->config_items;
132
133         /*
134          *      Grab the canonical user name.
135          */
136         namepair = request->username;
137         name = namepair ? (char *) namepair->strvalue : "NONE";
138
139         triplets = fopen(inst->file, "r");
140
141         if(triplets == NULL) {
142                 radlog(L_ERR, "can not open %s: %s",
143                        inst->file, strerror(errno));
144                 return RLM_MODULE_NOTFOUND;
145         }
146
147         imsicount = 0;
148         lineno = 0;
149
150         while(fgets(tripbuf, sizeof(tripbuf), triplets) == tripbuf
151               && imsicount < 3)
152         {
153                 char *f;
154                 char *l;
155                 VALUE_PAIR *r, *k, *s;
156
157                 lineno++;
158                 if(tripbuf[0]=='#') continue;
159
160                 l = tripbuf;
161                 fieldcount = 0;
162                 chal[0]='0'; chal[1]='x';
163                 kc[0]='0';   kc[1]='x';
164                 sres[0]='0'; sres[1]='x';
165
166                 f = strsep(&l, ",");
167                 if(f)
168                 {
169                         imsi[0]='\0';
170                         strncat(imsi, f, sizeof(imsi));
171                         fieldcount++;
172                 }
173
174                 if(strcmp(imsi, name) != 0)
175                 {
176                         continue;
177                 }
178
179                 /* we found one */
180                 f = strsep(&l, ",");
181                 if(f)
182                 {
183                         chal[2]='\0';
184                         strncat(chal+2, f, sizeof(chal)-2);
185                         fieldcount++;
186                 }
187
188                 f = strsep(&l, ",");
189                 if(f)
190                 {
191                         sres[2]='\0';
192                         strncat(sres+2, f, sizeof(sres)-2);
193                         fieldcount++;
194                 }
195
196                 f = strsep(&l, ",\n");
197                 if(f)
198                 {
199                         kc[2]='\0';
200                         strncat(kc+2, f, sizeof(kc)-2);
201                         fieldcount++;
202                 }
203
204                 if(fieldcount != 4)
205                 {
206                         radlog(L_ERR, "invalid number of fields %d at line %d",
207                                fieldcount, lineno);
208                         /* complain about malformed line */
209                         continue;
210                 }
211
212
213                 r = paircreate(ATTRIBUTE_EAP_SIM_RAND1 + imsicount, PW_TYPE_OCTETS);
214                 r = pairparsevalue(r, chal);
215                 pairadd(reply_pairs, r);
216
217                 k = paircreate(ATTRIBUTE_EAP_SIM_KC1 + imsicount, PW_TYPE_OCTETS);
218                 k = pairparsevalue(k, kc);
219                 rad_assert(k != NULL);
220                 pairadd(reply_pairs, k);
221
222                 s = paircreate(ATTRIBUTE_EAP_SIM_SRES1 + imsicount, PW_TYPE_OCTETS);
223                 s = pairparsevalue(s, sres);
224                 pairadd(reply_pairs, s);
225
226                 imsicount++;
227         }
228         fclose(triplets);
229
230         if (imsicount < 3)
231         {
232                 DEBUG("rlm_sim_files: "
233                       "insufficient number of challenges for imsi %s: %d\n",
234                       name, imsicount);
235                 return RLM_MODULE_NOTFOUND;
236         }
237
238         DEBUG("rlm_sim_files: "
239               "authorized user/imsi %s\n", name);
240
241         /*
242          * EAP module will also grab based upon presence of EAP packet
243          * and it will add the Autz-Type entry.
244          */
245
246         if((reply_tmp = pairmake ("EAP-Type", "SIM", T_OP_EQ)))
247         {
248                 radlog(L_INFO, "rlm_sim_files: Adding EAP-Type: eap-sim");
249                 pairadd (config_pairs, reply_tmp);
250         }
251
252 #if 0
253         DEBUG("rlm_sim_files: saw config");
254         vp_printlist(stdout, *config_pairs);
255
256         DEBUG("rlm_sim_files: saw reply");
257         vp_printlist(stdout, *reply_pairs);
258 #endif
259
260         return RLM_MODULE_OK;
261 }
262
263
264 /*
265  *      Clean up.
266  */
267 static int sim_file_detach(void *instance)
268 {
269         struct sim_file_instance *inst = instance;
270
271         free(inst);
272         return 0;
273 }
274
275
276 /* globally exported name */
277 module_t rlm_sim_files = {
278         "sim_files",
279         0,                              /* type: reserved */
280         NULL,                           /* initialization */
281         sim_file_instantiate,           /* instantiation */
282         {
283                 NULL,                   /* authentication */
284                 sim_file_authorize,     /* authorization */
285                 NULL,                   /* preaccounting */
286                 NULL,                   /* accounting */
287                 NULL,                   /* checksimul */
288                 NULL,                   /* pre-proxy */
289                 NULL,                   /* post-proxy */
290                 NULL                    /* post-auth */
291         },
292         sim_file_detach,                /* detach */
293         NULL                            /* destroy */
294 };
295