1 /* ====================================================================
2 * Copyright (c) 1995-2000 The Apache Software Foundation. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Software Foundation
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Software Foundation" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Software Foundation.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the Apache Software Foundation
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
35 * THIS SOFTWARE IS PROVIDED BY THE Apache Software Foundation ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE Apache Software Foundation OR
39 * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Software Foundation and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
59 * http_auth: authentication
61 * Rob McCool & Brian Behlendorf.
63 * Adapted to Apache by rst.
65 * Version 0.5 May 1996
67 * Modified by Dirk.vanGulik@jrc.it to
69 * Adapted to allow anonymous logins, just like with Anon-FTP, when
70 * one gives the magic user name 'anonymous' and ones email address
73 * Just add the following tokes to your <directory> setup:
75 * Anonymous magic-userid [magic-userid]...
77 * Anonymous_MustGiveEmail [ on | off ] default = off
78 * Anonymous_LogEmail [ on | off ] default = on
79 * Anonymous_VerifyEmail [ on | off ] default = off
80 * Anonymous_NoUserId [ on | off ] default = off
81 * Anonymous_Authoritative [ on | off ] default = off
83 * The magic user id is something like 'anonymous', it is NOT case sensitive.
85 * The MustGiveEmail flag can be used to force users to enter something
86 * in the password field (like an email address). Default is off.
88 * Furthermore the 'NoUserID' flag can be set to allow completely empty
89 * usernames in as well; this can be is convenient as a single return
90 * in broken GUIs like W95 is often given by the user. The Default is off.
92 * Dirk.vanGulik@jrc.it; http://ewse.ceo.org; http://me-www.jrc.it/~dirkx
97 #include "http_config.h"
98 #include "http_core.h"
100 #include "http_protocol.h"
101 #include "http_request.h"
103 typedef struct auth_anon {
105 struct auth_anon *next;
110 auth_anon *auth_anon_passwords;
111 int auth_anon_nouserid;
112 int auth_anon_logemail;
113 int auth_anon_verifyemail;
114 int auth_anon_mustemail;
115 int auth_anon_authoritative;
117 } anon_auth_config_rec;
119 static void *create_anon_auth_dir_config(ap_context_t *p, char *d)
121 anon_auth_config_rec *sec = (anon_auth_config_rec *)
122 ap_pcalloc(p, sizeof(anon_auth_config_rec));
125 return NULL; /* no memory... */
127 /* just to illustrate the defaults really. */
128 sec->auth_anon_passwords = NULL;
130 sec->auth_anon_nouserid = 0;
131 sec->auth_anon_logemail = 1;
132 sec->auth_anon_verifyemail = 0;
133 sec->auth_anon_mustemail = 1;
134 sec->auth_anon_authoritative = 0;
138 static const char *anon_set_passwd_flag(cmd_parms *cmd,
139 anon_auth_config_rec * sec, int arg)
141 sec->auth_anon_mustemail = arg;
145 static const char *anon_set_userid_flag(cmd_parms *cmd,
146 anon_auth_config_rec * sec, int arg)
148 sec->auth_anon_nouserid = arg;
151 static const char *anon_set_logemail_flag(cmd_parms *cmd,
152 anon_auth_config_rec * sec, int arg)
154 sec->auth_anon_logemail = arg;
157 static const char *anon_set_verifyemail_flag(cmd_parms *cmd,
158 anon_auth_config_rec * sec, int arg)
160 sec->auth_anon_verifyemail = arg;
163 static const char *anon_set_authoritative_flag(cmd_parms *cmd,
164 anon_auth_config_rec * sec, int arg)
166 sec->auth_anon_authoritative = arg;
170 static const char *anon_set_string_slots(cmd_parms *cmd,
171 anon_auth_config_rec * sec, char *arg)
177 return "Anonymous string cannot be empty, use Anonymous_NoUserId instead";
179 /* squeeze in a record */
180 first = sec->auth_anon_passwords;
183 (!(sec->auth_anon_passwords = (auth_anon *) ap_palloc(cmd->pool, sizeof(auth_anon)))) ||
184 (!(sec->auth_anon_passwords->password = arg))
186 return "Failed to claim memory for an anonymous password...";
188 /* and repair the next */
189 sec->auth_anon_passwords->next = first;
194 static const command_rec anon_auth_cmds[] =
196 {"Anonymous", anon_set_string_slots, NULL, OR_AUTHCFG, ITERATE,
197 "a space-separated list of user IDs"},
198 {"Anonymous_MustGiveEmail", anon_set_passwd_flag, NULL, OR_AUTHCFG, FLAG,
199 "Limited to 'on' or 'off'"},
200 {"Anonymous_NoUserId", anon_set_userid_flag, NULL, OR_AUTHCFG, FLAG,
201 "Limited to 'on' or 'off'"},
202 {"Anonymous_VerifyEmail", anon_set_verifyemail_flag, NULL, OR_AUTHCFG, FLAG,
203 "Limited to 'on' or 'off'"},
204 {"Anonymous_LogEmail", anon_set_logemail_flag, NULL, OR_AUTHCFG, FLAG,
205 "Limited to 'on' or 'off'"},
206 {"Anonymous_Authoritative", anon_set_authoritative_flag, NULL, OR_AUTHCFG, FLAG,
207 "Limited to 'on' or 'off'"},
212 module MODULE_VAR_EXPORT anon_auth_module;
214 static int anon_authenticate_basic_user(request_rec *r)
216 anon_auth_config_rec *sec =
217 (anon_auth_config_rec *) ap_get_module_config(r->per_dir_config,
222 if ((res = ap_get_basic_auth_pw(r, &sent_pw)))
225 /* Ignore if we are not configured */
226 if (!sec->auth_anon_passwords)
229 /* Do we allow an empty userID and/or is it the magic one
232 if ((!(r->user[0])) && (sec->auth_anon_nouserid)) {
236 auth_anon *p = sec->auth_anon_passwords;
238 while ((res == DECLINED) && (p != NULL)) {
239 if (!(strcasecmp(r->user, p->password)))
247 /* password been filled out ? */
248 && ((!sec->auth_anon_mustemail) || strlen(sent_pw))
249 /* does the password look like an email address ? */
250 && ((!sec->auth_anon_verifyemail)
251 || ((strpbrk("@", sent_pw) != NULL)
252 && (strpbrk(".", sent_pw) != NULL)))) {
253 if (sec->auth_anon_logemail && ap_is_initial_req(r)) {
254 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, r,
255 "Anonymous: Passwd <%s> Accepted",
256 sent_pw ? sent_pw : "\'none\'");
261 if (sec->auth_anon_authoritative) {
262 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, APR_SUCCESS, r,
263 "Anonymous: Authoritative, Passwd <%s> not accepted",
264 sent_pw ? sent_pw : "\'none\'");
265 return AUTH_REQUIRED;
267 /* Drop out the bottom to return DECLINED */
273 static int check_anon_access(request_rec *r)
276 conn_rec *c = r->connection;
277 anon_auth_config_rec *sec =
278 (anon_auth_config_rec *) ap_get_module_config(r->per_dir_config,
284 if (strcasecmp(r->connection->user, sec->auth_anon))
291 static void register_hooks(void)
293 ap_hook_check_user_id(anon_authenticate_basic_user,NULL,NULL,HOOK_MIDDLE);
294 ap_hook_auth_checker(check_anon_access,NULL,NULL,HOOK_MIDDLE);
297 module MODULE_VAR_EXPORT anon_auth_module =
299 STANDARD20_MODULE_STUFF,
300 create_anon_auth_dir_config,/* dir config creater */
301 NULL, /* dir merger ensure strictness */
302 NULL, /* server config */
303 NULL, /* merge server config */
304 anon_auth_cmds, /* command ap_table_t */
306 register_hooks /* register hooks */