Delete trailing whitespace.
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_psk / userinfo.c
1 /* $Id$ */
2
3
4 /*
5  * userinfo.c
6  *
7  * Implementation of the user management
8  *
9  *
10  * Copyright (C) France Télécom R&D (DR&D/MAPS/NSS)
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25  *
26  * Copyright 2006 The FreeRADIUS server project
27  *
28  */
29
30 #include <freeradius-devel/ident.h>
31 RCSID("$Id$")
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38
39 #include "userinfo.h"
40 #include "eap_psk_ssm.h"
41 #include "eap_psk.h"  //hex2Bin()
42
43
44
45 userinfo_t*   pskGetUserInfo(char* path, char* peerID)
46 {
47     FILE*       fp;
48     char        buff[1024]; //FIXME: give the buffer a proper size
49                             //when we know more about ID length
50     userinfo_t* uinfo = NULL;
51     int         found = 0;
52     char*       AK = NULL;
53     char*       KDK = NULL;
54         int res;
55
56     fp = fopen(path, "r");
57     if (fp == NULL)
58         {
59             radlog(L_ERR, "pskGetUserInfo: failed to open PSK users file");
60             return NULL;
61         }
62
63     while (!found && fgets(buff, sizeof(buff), fp))
64         {
65           unsigned int     i = 0;
66
67             // ignore comments
68             if (buff[0] == '#')
69                 continue;
70
71             // read this login name
72             while (! isspace(buff[i]))
73                 i++;
74
75             // is it the one we looking for?
76             if ((i != strlen(peerID))
77                 || (strncmp(peerID, buff, i) != 0))
78                 continue;
79             else
80                 found = 1;
81
82             // skip spaces
83             while (isspace(buff[i]))
84                 i++;
85
86             // prepare to store user info
87             uinfo = (userinfo_t*) malloc(sizeof(userinfo_t));
88             if (uinfo == NULL)
89                 {
90                     radlog(L_ERR, "pskGetUserInfo: out of memory");
91                     return NULL;
92                 }
93
94             //get AK
95             AK = strndup(buff + i, PSK_AK_STRLEN);
96             if (AK == NULL) {
97                 radlog(L_ERR, "pskGetUserInfo: out of memory");
98                                 free(uinfo);
99                                 return NULL;
100             }
101             //FIXME: shouldnt we check the key size?
102             /*
103               else if (strlen(AK) != 32) {
104               log();
105               return NULL;
106               }
107             */
108             res=pskHex2Bin(AK, &(uinfo->AK),PSK_AK_SIZE);
109
110                 if(!res)
111                 {
112                         radlog(L_ERR, "pskGetUserInfo: the key isn't in hexadecimal format");
113                         free(uinfo);
114                         free(AK);
115                         return NULL;
116                 }
117
118             //get KDK
119             KDK = strndup(buff + i + PSK_AK_STRLEN, PSK_KDK_STRLEN);
120             if (KDK == NULL) {
121                         radlog(L_ERR, "psk_get_user_info: out of memory");
122                         free(uinfo);
123                         free(AK);
124                         return NULL;
125             }
126             //FIXME: shouldnt we check the key size?
127             /*
128               else if (strlen(KDK) != 32) {
129               log();
130               return NULL;
131               }
132             */
133             res=pskHex2Bin(KDK, &(uinfo->KDK),PSK_KDK_SIZE);
134
135                 if(!res)
136                 {
137                         radlog(L_ERR, "pskGetUserInfo: the key isn't in hexadecimal format");
138                         free(uinfo);
139                         free(AK);
140                         free(KDK);
141                         return NULL;
142                 }
143
144             free(AK);
145             free(KDK);
146         }
147
148
149     // if user was not found, NULL is returned
150     fclose(fp);
151     return uinfo;
152 }
153