Fawkes API  Fawkes Development Version
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 HAVE_CRYPT
31 # ifdef __USE_GNU
32 # include <crypt.h>
33 # else
34 # include <unistd.h>
35 # endif
36 #endif
37 #ifdef HAVE_APR_UTIL
38 # include <apr_md5.h>
39 #endif
40 using namespace fawkes;
41 
42 /** @class WebviewUserVerifier "user_verifier.h"
43  * Webview user verification.
44  * Verifies users against entries in the configuration database.
45  * @author Tim Niemueller
46  */
47 
48 /** Constructor.
49  * @param config configuration to read users from
50  * @param logger logger for log output
51  */
53  : config(config)
54 {
55 }
56 
57 
58 /** Destructor. */
60 {
61 }
62 
63 
64 bool
65 WebviewUserVerifier::verify_user(const char *user, const char *password) throw()
66 {
67  try {
68  std::string userpath = std::string("/webview/users/") + user;
69  std::string confpass = config->get_string(userpath.c_str());
70 
71  if (confpass.find("!cleartext!") == 0) {
72  return (confpass.substr(11) == password);
73  }
74 
75 #ifdef HAVE_APR_UTIL
76  return
77  (apr_password_validate(password, confpass.c_str()) == APR_SUCCESS);
78 
79 #elif defined(HAVE_CRYPT)
80 # ifdef __USE_GNU
81  struct crypt_data cd;
82  cd.initialized = 0;
83 
84  char *crypted = crypt_r(password, confpass.c_str(), &cd);
85 # else
86  char *crypted = crypt(password, confpass.c_str());
87 # endif
88 
89  if (confpass == crypted) {
90  return true;
91  } else {
92  //logger->log_warn("WebviewUserVerifier", "Access denied for user %s, "
93  // "invalid clear hashed password", user);
94  return false;
95  }
96 #else
97  return (confpass == password);
98 #endif
99 
100  } catch (Exception &e) {
101  //logger->log_warn("WebviewUserVerifier", "Access denied for unknown user %s",
102  // user);
103  return false;
104  }
105 
106  // should not actually happen, just in case...
107  return false;
108 }
Fawkes library namespace.
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:67
WebviewUserVerifier(fawkes::Configuration *config, fawkes::Logger *logger)
Constructor.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
Interface for logging.
Definition: logger.h:34