/* ====================================================================
* Copyright (c) 1995-1999 The Apache Group. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* 4. The names "Apache Server" and "Apache Group" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Group and was originally based
* on public domain software written at the National Center for
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
* For more information on the Apache Group and the Apache HTTP server
* project, please see .
*
*/
/*
* http_auth: authentication
*
* Rob McCool
*
* Adapted to Apache by rst.
*
* dirkx - Added Authoritative control to allow passing on to lower
* modules if and only if the user-id is not known to this
* module. A known user with a faulty or absent password still
* causes an AuthRequired. The default is 'Authoritative', i.e.
* no control is passed along.
*/
/*
* mod_auth_ntsec.c - Apache module for Cygwin 1.x ntsec based authentication
*
* $Id: mod_auth_ntsec.c,v 1.4 2001/12/02 17:24:30 tolj Exp tolj $
*
* Based upon mod_auth_nt.c by Kamalendu Biswas .
* Stipe Tolj
*
* Usage:
* Include this module if you want to check userid and password
* credentials against your local NT accounts.
*
* Install:
* To compile the module using the apxs tool please use the following
* command within your shell
*
* $ /usr/local/apache/bin/apxs -DCYGWIN_W32API \
* -c mod_auth_ntsec.c -o mod_auth_ntsec.dll
*
* Defining CYGWIN_W32API is needed due to the fact that we need to exclude
* W32API declarations in os/cygwin/os.h if we use them inside this module.
*
* Configuration Directives:
*
* Auth_ntsec [On|Off] - enable or disable ntsec based authentication
* Auth_ntsec_Authorative [Yes|No] - allow access control to be passed
* to lower authentication modules
*
* Supported require arguments:
*
* require user ...
* require valid-user
* require file-owner
*
*/
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
/*
* Cygwin needs some additional headers
*/
#ifndef __CYGWIN__
#error "mod_auth_ntsec: this module is specific to the Cygwin 1.x platform"
#else
#include
#include
#include
#include
#include
#include
#define cygwin_is_winnt (GetVersion() < 0x80000000)
static struct passwd *pw;
BOOL authenticate_ntsec(request_rec *r, char* user, char* passwd);
#endif
typedef struct auth_config_struct {
unsigned char enable_ntsec_auth;
int auth_authoritative;
} ntsec_auth_config_rec;
static void *create_auth_dir_config(pool *p, char *d)
{
ntsec_auth_config_rec *sec =
(ntsec_auth_config_rec *) ap_pcalloc(p, sizeof(ntsec_auth_config_rec));
sec->enable_ntsec_auth = 1;
sec->auth_authoritative = 1; /* keep the fortress secure by default */
return sec;
}
static const char *set_ntsec_auth_flag(cmd_parms *cmd, ntsec_auth_config_rec *sec, int arg)
{
sec->enable_ntsec_auth = (unsigned char) arg;
return NULL;
}
static const command_rec auth_cmds[] =
{
{"Auth_ntsec", set_ntsec_auth_flag,
NULL, OR_AUTHCFG, FLAG,
"Enable (on) or disable (off) Cygwin's ntsec based authentication." },
{"Auth_ntsec_Authoritative", ap_set_flag_slot,
(void *) XtOffsetOf(ntsec_auth_config_rec, auth_authoritative),
OR_AUTHCFG, FLAG,
"Set to 'no' to allow access control to be passed along to lower modules if the UserID is not known to this module"},
{NULL}
};
module MODULE_VAR_EXPORT ntsec_auth_module;
/* These functions return 0 if client is OK, and proper error status
* if not... either AUTH_REQUIRED, if we made a check, and it failed, or
* SERVER_ERROR, if things are so totally confused that we couldn't
* figure out how to tell if the client is authorized or not.
*
* If they return DECLINED, and all other modules also decline, that's
* treated by the server core as a configuration error, logged and
* reported as such.
*/
/* Determine user ID, and check if it really is that user, for HTTP
* basic authentication...
*/
static int ntsec_authenticate_basic_user(request_rec *r)
{
ntsec_auth_config_rec *sec =
(ntsec_auth_config_rec *) ap_get_module_config(r->per_dir_config, &ntsec_auth_module);
conn_rec *c = r->connection;
const char *sent_pw;
int res;
if ((res = ap_get_basic_auth_pw(r, &sent_pw)))
return res;
if (!sec->enable_ntsec_auth)
return DECLINED;
/* check the given password against the ntsec local accounts */
if (!authenticate_ntsec(r, c->user, (char*)sent_pw)) {
ap_note_basic_auth_failure(r);
return AUTH_REQUIRED;
}
return OK;
}
/* Checking ID */
static int ntsec_check_auth(request_rec *r)
{
ntsec_auth_config_rec *sec =
(ntsec_auth_config_rec *) ap_get_module_config(r->per_dir_config, &ntsec_auth_module);
char *user = r->connection->user;
int m = r->method_number;
int method_restricted = 0;
register int x;
const char *t, *w;
const array_header *reqs_arr = ap_requires(r);
require_line *reqs;
/* BUG FIX: tadc, 11-Nov-1995. If there is no "requires" directive,
* then any user will do.
*/
if (reqs_arr == NULL) {
return (OK);
}
reqs = (require_line *) reqs_arr->elts;
for (x = 0; x < reqs_arr->nelts; x++) {
if (!(reqs[x].method_mask & (1 << m)))
continue;
method_restricted = 1;
t = reqs[x].requirement;
w = ap_getword_white(r->pool, &t);
if (!strcmp(w, "valid-user"))
return OK;
if (!strcmp(w, "user")) {
while (t[0]) {
w = ap_getword_conf(r->pool, &t);
if (!strcmp(user, w))
return OK;
}
}
if (strcmp(w, "file-owner") == 0) {
struct passwd *pwent;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r,
"checking for 'owner' access for file '%s'",
r->filename);
if (r->finfo.st_ino == 0) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r,
"no stat info for '%s'", r->filename);
continue;
}
pwent = getpwuid(r->finfo.st_uid);
if (pwent == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r,
"no username for UID %d (owner of '%s')",
r->finfo.st_uid, r->filename);
} else {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r,
"checking authenticated user '%s' "
"against owner '%s' of '%s'",
user, pwent->pw_name, r->filename);
if (strcmp(user, pwent->pw_name) == 0) {
return OK;
} else {
continue;
}
}
/*
}
if (strcmp(w, "file-group") == 0) {
struct group *grent;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r,
"checking for 'group' access for file '%s'",
r->filename);
if (r->finfo.st_ino == 0) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r,
"no stat info for '%s'", r->filename);
continue;
}
grent = getgrgid(r->finfo.st_gid);
if (grent == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r,
"no group name for GID %d (owner of '%s')",
r->finfo.st_gid, r->filename);
} else {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r,
"checking groups of authenticated user '%s' "
"against owner group '%s' of '%s'",
user, grent->gr_name, r->filename);
if (ap_table_get(grpstatus, grent->gr_name) != NULL) {
return OK;
} else {
continue;
}
}
}
*/
} else if (sec->auth_authoritative) {
/* if we aren't authoritative, any require directive could be
* valid even if we don't grok it. However, if we are
* authoritative, we can warn the user they did something wrong.
* That something could be a missing "AuthAuthoritative off", but
* more likely is a typo in the require directive.
*/
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
"access to %s failed, reason: unknown require directive:"
"\"%s\"", r->uri, reqs[x].requirement);
}
}
if (!method_restricted)
return OK;
if (!(sec->auth_authoritative))
return DECLINED;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
"access to %s failed, reason: user %s not allowed access",
r->uri, user);
ap_note_basic_auth_failure(r);
return AUTH_REQUIRED;
}
#if defined(__CYGWIN__)
/* Enables the necessary NT privileges to query an other user's
* password from the system in order to compare it against the
* password the user agent has been delivered.
* Returns true if the enabling of the privilige was successfull,
* false otherwise.
*/
BOOL ntsec_enable_privilege(LPCSTR name, BOOL enable)
{
HANDLE token;
TOKEN_PRIVILEGES priv;
LUID luid;
BOOL res = FALSE;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
return FALSE;
if (LookupPrivilegeValue(0, name, &luid)) {
priv.PrivilegeCount = 1;
priv.Privileges[0].Luid = luid;
if (enable)
priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
priv.Privileges[0].Attributes = 0;
if (AdjustTokenPrivileges(token, FALSE, &priv, 0, 0, 0))
res = TRUE;
}
CloseHandle(token);
return res;
}
/* Check if the given userid and password match on the local NT
* system. Returns true if it matches, false otherwise.
*/
BOOL authenticate_ntsec(request_rec *r, char* user, char* passwd)
{
//BOOL isValidUser = FALSE;
HANDLE pHandle;
/* Enable the necessary privilege for the httpd user */
BOOL prv = ntsec_enable_privilege(SE_TCB_NAME, TRUE);
if (!prv)
{
ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
"Could not enable SE_TCB_NAME privilege for httpd user!");
return FALSE;
}
/* We are only having the chance to authenticate if we are
* running Cygwin 1.x on a NT based system.
*/
if (cygwin_is_winnt) {
pw = getpwnam(user);
pHandle = cygwin_logon_user(pw, passwd);
if (pHandle != INVALID_HANDLE_VALUE) {
cygwin_set_impersonation_token(pHandle);
CloseHandle(pHandle);
return TRUE;
} else {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
"user %s: authentication failure for \"%s\"",
user, r->uri);
return FALSE;
}
} else {
ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
"Can not use ntsec based authentication on Win9x systems!");
return FALSE;
}
}
#endif /* __CYGWIN__ */
module MODULE_VAR_EXPORT ntsec_auth_module =
{
STANDARD_MODULE_STUFF,
NULL, /* initializer */
create_auth_dir_config, /* dir config creater */
NULL, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server config */
auth_cmds, /* command table */
NULL, /* handlers */
NULL, /* filename translation */
ntsec_authenticate_basic_user,/* check_user_id */
ntsec_check_auth, /* check auth */
NULL, /* check access */
NULL, /* type_checker */
NULL, /* fixups */
NULL, /* logger */
NULL, /* header parser */
NULL, /* child_init */
NULL, /* child_exit */
NULL /* post read-request */
};