LCOV - code coverage report
Current view: top level - src/ap - eap_user_db.c (source / functions) Hit Total Coverage
Test: hostapd hwsim test run 1388338050 Lines: 17 152 11.2 %
Date: 2013-12-29 Functions: 1 5 20.0 %
Branches: 9 146 6.2 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * hostapd / EAP user database
       3                 :            :  * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
       4                 :            :  *
       5                 :            :  * This software may be distributed under the terms of the BSD license.
       6                 :            :  * See README for more details.
       7                 :            :  */
       8                 :            : 
       9                 :            : #include "includes.h"
      10                 :            : #ifdef CONFIG_SQLITE
      11                 :            : #include <sqlite3.h>
      12                 :            : #endif /* CONFIG_SQLITE */
      13                 :            : 
      14                 :            : #include "common.h"
      15                 :            : #include "eap_common/eap_wsc_common.h"
      16                 :            : #include "eap_server/eap_methods.h"
      17                 :            : #include "eap_server/eap.h"
      18                 :            : #include "ap_config.h"
      19                 :            : #include "hostapd.h"
      20                 :            : 
      21                 :            : #ifdef CONFIG_SQLITE
      22                 :            : 
      23                 :          0 : static void set_user_methods(struct hostapd_eap_user *user, const char *methods)
      24                 :            : {
      25                 :            :         char *buf, *start;
      26                 :            :         int num_methods;
      27                 :            : 
      28                 :          0 :         buf = os_strdup(methods);
      29         [ #  # ]:          0 :         if (buf == NULL)
      30                 :          0 :                 return;
      31                 :            : 
      32                 :          0 :         os_memset(&user->methods, 0, sizeof(user->methods));
      33                 :          0 :         num_methods = 0;
      34                 :          0 :         start = buf;
      35         [ #  # ]:          0 :         while (*start) {
      36                 :          0 :                 char *pos3 = os_strchr(start, ',');
      37         [ #  # ]:          0 :                 if (pos3)
      38                 :          0 :                         *pos3++ = '\0';
      39                 :          0 :                 user->methods[num_methods].method =
      40                 :          0 :                         eap_server_get_type(start,
      41                 :            :                                             &user->methods[num_methods].vendor);
      42 [ #  # ][ #  # ]:          0 :                 if (user->methods[num_methods].vendor == EAP_VENDOR_IETF &&
      43                 :          0 :                     user->methods[num_methods].method == EAP_TYPE_NONE) {
      44         [ #  # ]:          0 :                         if (os_strcmp(start, "TTLS-PAP") == 0) {
      45                 :          0 :                                 user->ttls_auth |= EAP_TTLS_AUTH_PAP;
      46                 :          0 :                                 goto skip_eap;
      47                 :            :                         }
      48         [ #  # ]:          0 :                         if (os_strcmp(start, "TTLS-CHAP") == 0) {
      49                 :          0 :                                 user->ttls_auth |= EAP_TTLS_AUTH_CHAP;
      50                 :          0 :                                 goto skip_eap;
      51                 :            :                         }
      52         [ #  # ]:          0 :                         if (os_strcmp(start, "TTLS-MSCHAP") == 0) {
      53                 :          0 :                                 user->ttls_auth |= EAP_TTLS_AUTH_MSCHAP;
      54                 :          0 :                                 goto skip_eap;
      55                 :            :                         }
      56         [ #  # ]:          0 :                         if (os_strcmp(start, "TTLS-MSCHAPV2") == 0) {
      57                 :          0 :                                 user->ttls_auth |= EAP_TTLS_AUTH_MSCHAPV2;
      58                 :          0 :                                 goto skip_eap;
      59                 :            :                         }
      60                 :          0 :                         wpa_printf(MSG_INFO, "DB: Unsupported EAP type '%s'",
      61                 :            :                                    start);
      62                 :          0 :                         os_free(buf);
      63                 :          0 :                         return;
      64                 :            :                 }
      65                 :            : 
      66                 :          0 :                 num_methods++;
      67         [ #  # ]:          0 :                 if (num_methods >= EAP_MAX_METHODS)
      68                 :          0 :                         break;
      69                 :            :         skip_eap:
      70         [ #  # ]:          0 :                 if (pos3 == NULL)
      71                 :          0 :                         break;
      72                 :          0 :                 start = pos3;
      73                 :            :         }
      74                 :            : 
      75                 :          0 :         os_free(buf);
      76                 :            : }
      77                 :            : 
      78                 :            : 
      79                 :          0 : static int get_user_cb(void *ctx, int argc, char *argv[], char *col[])
      80                 :            : {
      81                 :          0 :         struct hostapd_eap_user *user = ctx;
      82                 :            :         int i;
      83                 :            : 
      84         [ #  # ]:          0 :         for (i = 0; i < argc; i++) {
      85 [ #  # ][ #  # ]:          0 :                 if (os_strcmp(col[i], "password") == 0 && argv[i]) {
      86                 :          0 :                         os_free(user->password);
      87                 :          0 :                         user->password_len = os_strlen(argv[i]);
      88                 :          0 :                         user->password = (u8 *) os_strdup(argv[i]);
      89                 :          0 :                         user->next = (void *) 1;
      90 [ #  # ][ #  # ]:          0 :                 } else if (os_strcmp(col[i], "methods") == 0 && argv[i]) {
      91                 :          0 :                         set_user_methods(user, argv[i]);
      92                 :            :                 }
      93                 :            :         }
      94                 :            : 
      95                 :          0 :         return 0;
      96                 :            : }
      97                 :            : 
      98                 :            : 
      99                 :          0 : static int get_wildcard_cb(void *ctx, int argc, char *argv[], char *col[])
     100                 :            : {
     101                 :          0 :         struct hostapd_eap_user *user = ctx;
     102                 :          0 :         int i, id = -1, methods = -1;
     103                 :            :         size_t len;
     104                 :            : 
     105         [ #  # ]:          0 :         for (i = 0; i < argc; i++) {
     106 [ #  # ][ #  # ]:          0 :                 if (os_strcmp(col[i], "identity") == 0 && argv[i])
     107                 :          0 :                         id = i;
     108 [ #  # ][ #  # ]:          0 :                 else if (os_strcmp(col[i], "methods") == 0 && argv[i])
     109                 :          0 :                         methods = i;
     110                 :            :         }
     111                 :            : 
     112 [ #  # ][ #  # ]:          0 :         if (id < 0 || methods < 0)
     113                 :          0 :                 return 0;
     114                 :            : 
     115                 :          0 :         len = os_strlen(argv[id]);
     116 [ #  # ][ #  # ]:          0 :         if (len <= user->identity_len &&
     117         [ #  # ]:          0 :             os_memcmp(argv[id], user->identity, len) == 0 &&
     118         [ #  # ]:          0 :             (user->password == NULL || len > user->password_len)) {
     119                 :          0 :                 os_free(user->password);
     120                 :          0 :                 user->password_len = os_strlen(argv[id]);
     121                 :          0 :                 user->password = (u8 *) os_strdup(argv[id]);
     122                 :          0 :                 user->next = (void *) 1;
     123                 :          0 :                 set_user_methods(user, argv[methods]);
     124                 :            :         }
     125                 :            : 
     126                 :          0 :         return 0;
     127                 :            : }
     128                 :            : 
     129                 :            : 
     130                 :            : static const struct hostapd_eap_user *
     131                 :          0 : eap_user_sqlite_get(struct hostapd_data *hapd, const u8 *identity,
     132                 :            :                     size_t identity_len, int phase2)
     133                 :            : {
     134                 :            :         sqlite3 *db;
     135                 :          0 :         struct hostapd_eap_user *user = NULL;
     136                 :            :         char id_str[256], cmd[300];
     137                 :            :         size_t i;
     138                 :            : 
     139         [ #  # ]:          0 :         if (identity_len >= sizeof(id_str))
     140                 :          0 :                 return NULL;
     141                 :          0 :         os_memcpy(id_str, identity, identity_len);
     142                 :          0 :         id_str[identity_len] = '\0';
     143         [ #  # ]:          0 :         for (i = 0; i < identity_len; i++) {
     144 [ #  # ][ #  # ]:          0 :                 if (id_str[i] >= 'a' && id_str[i] <= 'z')
     145                 :          0 :                         continue;
     146 [ #  # ][ #  # ]:          0 :                 if (id_str[i] >= 'A' && id_str[i] <= 'Z')
     147                 :          0 :                         continue;
     148 [ #  # ][ #  # ]:          0 :                 if (id_str[i] >= '0' && id_str[i] <= '9')
     149                 :          0 :                         continue;
     150 [ #  # ][ #  # ]:          0 :                 if (id_str[i] == '-' || id_str[i] == '_' || id_str[i] == '.' ||
         [ #  # ][ #  # ]
     151 [ #  # ][ #  # ]:          0 :                     id_str[i] == ',' || id_str[i] == '@' || id_str[i] == '\\' ||
                 [ #  # ]
     152 [ #  # ][ #  # ]:          0 :                     id_str[i] == '!' || id_str[i] == '#' || id_str[i] == '%' ||
                 [ #  # ]
     153         [ #  # ]:          0 :                     id_str[i] == '=' || id_str[i] == ' ')
     154                 :          0 :                         continue;
     155                 :          0 :                 wpa_printf(MSG_INFO, "DB: Unsupported character in identity");
     156                 :          0 :                 return NULL;
     157                 :            :         }
     158                 :            : 
     159                 :          0 :         os_free(hapd->tmp_eap_user.identity);
     160                 :          0 :         os_free(hapd->tmp_eap_user.password);
     161                 :          0 :         os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
     162                 :          0 :         hapd->tmp_eap_user.phase2 = phase2;
     163                 :          0 :         hapd->tmp_eap_user.identity = os_zalloc(identity_len + 1);
     164         [ #  # ]:          0 :         if (hapd->tmp_eap_user.identity == NULL)
     165                 :          0 :                 return NULL;
     166                 :          0 :         os_memcpy(hapd->tmp_eap_user.identity, identity, identity_len);
     167                 :            : 
     168         [ #  # ]:          0 :         if (sqlite3_open(hapd->conf->eap_user_sqlite, &db)) {
     169                 :          0 :                 wpa_printf(MSG_INFO, "DB: Failed to open database %s: %s",
     170                 :          0 :                            hapd->conf->eap_user_sqlite, sqlite3_errmsg(db));
     171                 :          0 :                 sqlite3_close(db);
     172                 :          0 :                 return NULL;
     173                 :            :         }
     174                 :            : 
     175                 :          0 :         os_snprintf(cmd, sizeof(cmd),
     176                 :            :                     "SELECT password,methods FROM users WHERE "
     177                 :            :                     "identity='%s' AND phase2=%d;", id_str, phase2);
     178                 :          0 :         wpa_printf(MSG_DEBUG, "DB: %s", cmd);
     179         [ #  # ]:          0 :         if (sqlite3_exec(db, cmd, get_user_cb, &hapd->tmp_eap_user, NULL) !=
     180                 :            :             SQLITE_OK) {
     181                 :          0 :                 wpa_printf(MSG_DEBUG, "DB: Failed to complete SQL operation");
     182         [ #  # ]:          0 :         } else if (hapd->tmp_eap_user.next)
     183                 :          0 :                 user = &hapd->tmp_eap_user;
     184                 :            : 
     185 [ #  # ][ #  # ]:          0 :         if (user == NULL && !phase2) {
     186                 :          0 :                 os_snprintf(cmd, sizeof(cmd),
     187                 :            :                             "SELECT identity,methods FROM wildcards;");
     188                 :          0 :                 wpa_printf(MSG_DEBUG, "DB: %s", cmd);
     189         [ #  # ]:          0 :                 if (sqlite3_exec(db, cmd, get_wildcard_cb, &hapd->tmp_eap_user,
     190                 :            :                                  NULL) != SQLITE_OK) {
     191                 :          0 :                         wpa_printf(MSG_DEBUG, "DB: Failed to complete SQL "
     192                 :            :                                    "operation");
     193         [ #  # ]:          0 :                 } else if (hapd->tmp_eap_user.next) {
     194                 :          0 :                         user = &hapd->tmp_eap_user;
     195                 :          0 :                         os_free(user->identity);
     196                 :          0 :                         user->identity = user->password;
     197                 :          0 :                         user->identity_len = user->password_len;
     198                 :          0 :                         user->password = NULL;
     199                 :          0 :                         user->password_len = 0;
     200                 :            :                 }
     201                 :            :         }
     202                 :            : 
     203                 :          0 :         sqlite3_close(db);
     204                 :            : 
     205                 :          0 :         return user;
     206                 :            : }
     207                 :            : 
     208                 :            : #endif /* CONFIG_SQLITE */
     209                 :            : 
     210                 :            : 
     211                 :            : const struct hostapd_eap_user *
     212                 :         35 : hostapd_get_eap_user(struct hostapd_data *hapd, const u8 *identity,
     213                 :            :                      size_t identity_len, int phase2)
     214                 :            : {
     215                 :         35 :         const struct hostapd_bss_config *conf = hapd->conf;
     216                 :         35 :         struct hostapd_eap_user *user = conf->eap_user;
     217                 :            : 
     218                 :            : #ifdef CONFIG_WPS
     219 [ +  - ][ +  + ]:         35 :         if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
                 [ +  - ]
     220                 :         22 :             os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
     221                 :            :                 static struct hostapd_eap_user wsc_enrollee;
     222                 :         22 :                 os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
     223                 :         22 :                 wsc_enrollee.methods[0].method = eap_server_get_type(
     224                 :            :                         "WSC", &wsc_enrollee.methods[0].vendor);
     225                 :         22 :                 return &wsc_enrollee;
     226                 :            :         }
     227                 :            : 
     228 [ +  - ][ +  - ]:         13 :         if (conf->wps_state && identity_len == WSC_ID_REGISTRAR_LEN &&
                 [ +  - ]
     229                 :         13 :             os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
     230                 :            :                 static struct hostapd_eap_user wsc_registrar;
     231                 :         13 :                 os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
     232                 :         13 :                 wsc_registrar.methods[0].method = eap_server_get_type(
     233                 :            :                         "WSC", &wsc_registrar.methods[0].vendor);
     234                 :         13 :                 wsc_registrar.password = (u8 *) conf->ap_pin;
     235                 :         26 :                 wsc_registrar.password_len = conf->ap_pin ?
     236         [ +  + ]:         13 :                         os_strlen(conf->ap_pin) : 0;
     237                 :         13 :                 return &wsc_registrar;
     238                 :            :         }
     239                 :            : #endif /* CONFIG_WPS */
     240                 :            : 
     241         [ #  # ]:          0 :         while (user) {
     242 [ #  # ][ #  # ]:          0 :                 if (!phase2 && user->identity == NULL) {
     243                 :            :                         /* Wildcard match */
     244                 :          0 :                         break;
     245                 :            :                 }
     246                 :            : 
     247 [ #  # ][ #  # ]:          0 :                 if (user->phase2 == !!phase2 && user->wildcard_prefix &&
                 [ #  # ]
     248         [ #  # ]:          0 :                     identity_len >= user->identity_len &&
     249                 :          0 :                     os_memcmp(user->identity, identity, user->identity_len) ==
     250                 :            :                     0) {
     251                 :            :                         /* Wildcard prefix match */
     252                 :          0 :                         break;
     253                 :            :                 }
     254                 :            : 
     255 [ #  # ][ #  # ]:          0 :                 if (user->phase2 == !!phase2 &&
     256         [ #  # ]:          0 :                     user->identity_len == identity_len &&
     257                 :          0 :                     os_memcmp(user->identity, identity, identity_len) == 0)
     258                 :          0 :                         break;
     259                 :          0 :                 user = user->next;
     260                 :            :         }
     261                 :            : 
     262                 :            : #ifdef CONFIG_SQLITE
     263 [ #  # ][ #  # ]:          0 :         if (user == NULL && conf->eap_user_sqlite) {
     264                 :          0 :                 return eap_user_sqlite_get(hapd, identity, identity_len,
     265                 :            :                                            phase2);
     266                 :            :         }
     267                 :            : #endif /* CONFIG_SQLITE */
     268                 :            : 
     269                 :         35 :         return user;
     270                 :            : }

Generated by: LCOV version 1.9