LCOV - code coverage report
Current view: top level - src/utils - ext_password.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1475438200 Lines: 33 41 80.5 %
Date: 2016-10-02 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * External password backend
       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             : 
      11             : #ifdef __linux__
      12             : #include <sys/mman.h>
      13             : #endif /* __linux__ */
      14             : 
      15             : #include "common.h"
      16             : #include "ext_password_i.h"
      17             : 
      18             : 
      19             : static const struct ext_password_backend *backends[] = {
      20             : #ifdef CONFIG_EXT_PASSWORD_TEST
      21             :         &ext_password_test,
      22             : #endif /* CONFIG_EXT_PASSWORD_TEST */
      23             :         NULL
      24             : };
      25             : 
      26             : struct ext_password_data {
      27             :         const struct ext_password_backend *backend;
      28             :         void *priv;
      29             : };
      30             : 
      31             : 
      32           9 : struct ext_password_data * ext_password_init(const char *backend,
      33             :                                              const char *params)
      34             : {
      35             :         struct ext_password_data *data;
      36             :         int i;
      37             : 
      38           9 :         data = os_zalloc(sizeof(*data));
      39           9 :         if (data == NULL)
      40           0 :                 return NULL;
      41             : 
      42          10 :         for (i = 0; backends[i]; i++) {
      43           9 :                 if (os_strcmp(backends[i]->name, backend) == 0) {
      44           8 :                         data->backend = backends[i];
      45           8 :                         break;
      46             :                 }
      47             :         }
      48             : 
      49           9 :         if (!data->backend) {
      50           1 :                 os_free(data);
      51           1 :                 return NULL;
      52             :         }
      53             : 
      54           8 :         data->priv = data->backend->init(params);
      55           8 :         if (data->priv == NULL) {
      56           0 :                 os_free(data);
      57           0 :                 return NULL;
      58             :         }
      59             : 
      60           8 :         return data;
      61             : }
      62             : 
      63             : 
      64        1437 : void ext_password_deinit(struct ext_password_data *data)
      65             : {
      66        1437 :         if (data && data->backend && data->priv)
      67           8 :                 data->backend->deinit(data->priv);
      68        1437 :         os_free(data);
      69        1437 : }
      70             : 
      71             : 
      72          13 : struct wpabuf * ext_password_get(struct ext_password_data *data,
      73             :                                  const char *name)
      74             : {
      75          13 :         if (data == NULL)
      76           1 :                 return NULL;
      77          12 :         return data->backend->get(data->priv, name);
      78             : }
      79             : 
      80             : 
      81          10 : struct wpabuf * ext_password_alloc(size_t len)
      82             : {
      83             :         struct wpabuf *buf;
      84             : 
      85          10 :         buf = wpabuf_alloc(len);
      86          10 :         if (buf == NULL)
      87           0 :                 return NULL;
      88             : 
      89             : #ifdef __linux__
      90          10 :         if (mlock(wpabuf_head(buf), wpabuf_len(buf)) < 0) {
      91           0 :                 wpa_printf(MSG_ERROR, "EXT PW: mlock failed: %s",
      92           0 :                            strerror(errno));
      93             :         }
      94             : #endif /* __linux__ */
      95             : 
      96          10 :         return buf;
      97             : }
      98             : 
      99             : 
     100       43974 : void ext_password_free(struct wpabuf *pw)
     101             : {
     102       43974 :         if (pw == NULL)
     103       87938 :                 return;
     104          10 :         os_memset(wpabuf_mhead(pw), 0, wpabuf_len(pw));
     105             : #ifdef __linux__
     106          10 :         if (munlock(wpabuf_head(pw), wpabuf_len(pw)) < 0) {
     107           0 :                 wpa_printf(MSG_ERROR, "EXT PW: munlock failed: %s",
     108           0 :                            strerror(errno));
     109             :         }
     110             : #endif /* __linux__ */
     111          10 :         wpabuf_free(pw);
     112             : }

Generated by: LCOV version 1.10