Apache HTTPD
mod_authn_file.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "apr_strings.h"
18#include "apr_md5.h" /* for apr_password_validate */
19
20#include "ap_config.h"
21#include "ap_provider.h"
22#include "httpd.h"
23#include "http_config.h"
24#include "http_core.h"
25#include "http_log.h"
26#include "http_protocol.h"
27#include "http_request.h"
28
29#include "mod_auth.h"
30
31typedef struct {
32 char *pwfile;
34
36#define AUTHN_CACHE_STORE(r,user,realm,data) \
37 if (authn_cache_store != NULL) \
38 authn_cache_store((r), "file", (user), (realm), (data))
39
41{
42 authn_file_config_rec *conf = apr_palloc(p, sizeof(*conf));
43
44 conf->pwfile = NULL; /* just to illustrate the default really */
45 return conf;
46}
47
49{
50 AP_INIT_TAKE1("AuthUserFile", ap_set_file_slot,
51 (void *)APR_OFFSETOF(authn_file_config_rec, pwfile),
52 OR_AUTHCFG, "text file containing user IDs and passwords"),
53 {NULL}
54};
55
56module AP_MODULE_DECLARE_DATA authn_file_module;
57
58static authn_status check_password(request_rec *r, const char *user,
59 const char *password)
60{
62 &authn_file_module);
64 char l[MAX_STRING_LEN];
66 char *file_password = NULL;
67
68 if (!conf->pwfile) {
70 "AuthUserFile not specified in the configuration");
71 return AUTH_GENERAL_ERROR;
72 }
73
74 status = ap_pcfg_openfile(&f, r->pool, conf->pwfile);
75
76 if (status != APR_SUCCESS) {
78 "Could not open password file: %s", conf->pwfile);
79 return AUTH_GENERAL_ERROR;
80 }
81
82 while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
83 const char *rpw, *w;
84
85 /* Skip # or blank lines. */
86 if ((l[0] == '#') || (!l[0])) {
87 continue;
88 }
89
90 rpw = l;
91 w = ap_getword(r->pool, &rpw, ':');
92
93 if (!strcmp(user, w)) {
95 break;
96 }
97 }
99
100 if (!file_password) {
101 return AUTH_USER_NOT_FOUND;
102 }
104
106 if (status != APR_SUCCESS) {
107 return AUTH_DENIED;
108 }
109
110 return AUTH_GRANTED;
111}
112
113static authn_status get_realm_hash(request_rec *r, const char *user,
114 const char *realm, char **rethash)
115{
117 &authn_file_module);
119 char l[MAX_STRING_LEN];
121 char *file_hash = NULL;
122
123 if (!conf->pwfile) {
125 "AuthUserFile not specified in the configuration");
126 return AUTH_GENERAL_ERROR;
127 }
128
129 status = ap_pcfg_openfile(&f, r->pool, conf->pwfile);
130
131 if (status != APR_SUCCESS) {
133 "Could not open password file: %s", conf->pwfile);
134 return AUTH_GENERAL_ERROR;
135 }
136
137 while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
138 const char *rpw, *w, *x;
139
140 /* Skip # or blank lines. */
141 if ((l[0] == '#') || (!l[0])) {
142 continue;
143 }
144
145 rpw = l;
146 w = ap_getword(r->pool, &rpw, ':');
147 x = ap_getword(r->pool, &rpw, ':');
148
149 if (x && w && !strcmp(user, w) && !strcmp(realm, x)) {
150 /* Remember that this is a md5 hash of user:realm:password. */
151 file_hash = ap_getword(r->pool, &rpw, ':');
152 break;
153 }
154 }
156
157 if (!file_hash) {
158 return AUTH_USER_NOT_FOUND;
159 }
160
162 AUTHN_CACHE_STORE(r, user, realm, file_hash);
163
164 return AUTH_USER_FOUND;
165}
166
172
184
186{
188 create_authn_file_dir_config, /* dir config creater */
189 NULL, /* dir merger --- default is to override */
190 NULL, /* server config */
191 NULL, /* merge server config */
192 authn_file_cmds, /* command apr_table_t */
193 register_hooks /* register hooks */
194};
Symbol export macros and hook functions.
Apache Provider API.
APR MD5 Routines.
apr_size_t const unsigned char unsigned int unsigned int d
Definition apr_siphash.h:72
APR Strings library.
#define AP_INIT_TAKE1(directive, func, mconfig, where, help)
#define ap_get_module_config(v, m)
int ap_cfg_closefile(ap_configfile_t *cfp)
Definition util.c:931
apr_status_t ap_pcfg_openfile(ap_configfile_t **ret_cfg, apr_pool_t *p, const char *name)
Definition util.c:957
#define AP_DECLARE_MODULE(foo)
request_rec * r
void ap_hook_optional_fn_retrieve(ap_HOOK_optional_fn_retrieve_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition config.c:195
const char * ap_set_file_slot(cmd_parms *cmd, void *struct_ptr, const char *arg)
Definition config.c:1535
apr_status_t ap_cfg_getline(char *buf, apr_size_t bufsize, ap_configfile_t *cfp)
Definition util.c:1198
#define MAX_STRING_LEN
Definition httpd.h:300
#define APLOGNO(n)
Definition http_log.h:117
#define ap_log_rerror
Definition http_log.h:454
#define APLOG_ERR
Definition http_log.h:67
#define APLOG_MARK
Definition http_log.h:283
apr_status_t ap_register_auth_provider(apr_pool_t *pool, const char *provider_group, const char *provider_name, const char *provider_version, const void *provider, int type)
Definition request.c:2179
#define AP_AUTH_INTERNAL_PER_CONF
apr_file_t * f
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define APR_RETRIEVE_OPTIONAL_FN(name)
#define APR_OPTIONAL_FN_TYPE(name)
#define OR_AUTHCFG
#define STANDARD20_MODULE_STUFF
char * ap_getword(apr_pool_t *p, const char **line, char stop)
Definition util.c:723
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char const char * password
int int status
Apache Configuration.
CORE HTTP Daemon.
Apache Logging library.
HTTP protocol handling.
Apache Request library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
Authentication and Authorization Extension for Apache.
#define AUTHN_PROVIDER_VERSION
Definition mod_auth.h:41
#define AUTHN_PROVIDER_GROUP
Definition mod_auth.h:39
authn_status
Definition mod_auth.h:64
@ AUTH_GRANTED
Definition mod_auth.h:66
@ AUTH_DENIED
Definition mod_auth.h:65
@ AUTH_USER_FOUND
Definition mod_auth.h:67
@ AUTH_GENERAL_ERROR
Definition mod_auth.h:69
@ AUTH_USER_NOT_FOUND
Definition mod_auth.h:68
static const command_rec authn_file_cmds[]
static authn_status get_realm_hash(request_rec *r, const char *user, const char *realm, char **rethash)
static const authn_provider authn_file_provider
static authn_status check_password(request_rec *r, const char *user, const char *password)
static void opt_retr(void)
static void register_hooks(apr_pool_t *p)
#define AUTHN_CACHE_STORE(r, user, realm, data)
static apr_OFN_ap_authn_cache_store_t * authn_cache_store
static void * create_authn_file_dir_config(apr_pool_t *p, char *d)
static void ap_authn_cache_store(request_rec *r, const char *module, const char *user, const char *realm, const char *data)
return NULL
Definition mod_so.c:359
A structure that represents the current request.
Definition httpd.h:845
apr_pool_t * pool
Definition httpd.h:847
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047