Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
user_verifier.cpp
1 
2 /***************************************************************************
3  * user_verifier.cpp - Webview user verifier
4  *
5  * Created: Mon Jan 24 18:43:47 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "user_verifier.h"
24 
25 #include <core/exception.h>
26 #include <config/config.h>
27 #include <logging/logger.h>
28 
29 #include <string>
30 #ifdef __USE_GNU
31 # include <crypt.h>
32 #else
33 # include <unistd.h>
34 #endif
35 
36 #define HASH_REGEX "^(\\$([[:alnum:]]+)\\$([a-zA-Z0-9/.]+)\\$)[a-zA-Z0-9/.]+$"
37 
38 using namespace fawkes;
39 
40 /** @class WebviewUserVerifier "user_verifier.h"
41  * Webview user verification.
42  * Verifies users against entries in the configuration database.
43  * @author Tim Niemueller
44  */
45 
46 /** Constructor.
47  * @param config configuration to read users from
48  * @param logger logger for log output
49  */
51  : config(config), logger(logger)
52 {
53  int regerr;
54  if ((regerr = regcomp(&__hash_regex, HASH_REGEX, REG_EXTENDED)) != 0) {
55  char errtmp[1024];
56  regerror(regerr, &__hash_regex, errtmp, sizeof(errtmp));
57  regfree(&__hash_regex);
58  throw Exception("Failed to compile hash regex: %s", errtmp);
59  }
60 }
61 
62 
63 /** Destructor. */
65 {
66  regfree(&__hash_regex);
67 }
68 
69 
70 bool
71 WebviewUserVerifier::verify_user(const char *user, const char *password) throw()
72 {
73  try {
74  std::string userpath = std::string("/webview/users/") + user;
75  std::string confpass = config->get_string(userpath.c_str());
76 
77  regmatch_t m[4];
78  if (regexec(&__hash_regex, confpass.c_str(), 4, m, 0) == REG_NOMATCH) {
79  // assume clear text
80  //logger->log_warn("WebviewUserVerifier", "Access denied for user %s, "
81  // "invalid clear text password", user);
82  return (confpass == password);
83  }
84 
85 #ifdef __USE_GNU
86  struct crypt_data cd;
87  cd.initialized = 0;
88 
89  char *crypted = crypt_r(password, confpass.c_str(), &cd);
90 #else
91  char *crypted = crypt(password, confpass.c_str());
92 #endif
93 
94  if (confpass == crypted) {
95  return true;
96  } else {
97  //logger->log_warn("WebviewUserVerifier", "Access denied for user %s, "
98  // "invalid clear hashed password", user);
99  return false;
100  }
101 
102  } catch (Exception &e) {
103  //logger->log_warn("WebviewUserVerifier", "Access denied for unknown user %s",
104  // user);
105  return false;
106  }
107 
108  // should not actually happen, just in case...
109  return false;
110 }
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
Definition: sqlite.cpp:1164
virtual ~WebviewUserVerifier()
Destructor.
virtual bool verify_user(const char *user, const char *password)
Verify a user.
Base class for exceptions in Fawkes.
Definition: exception.h:36
Interface for configuration handling.
Definition: config.h:63
WebviewUserVerifier(fawkes::Configuration *config, fawkes::Logger *logger)
Constructor.
Interface for logging.
Definition: logger.h:34