Use GSS_C_NT_HOSTBASED_SERVICE instead of GSS_C_NT_USER_NAME in the gss_import_name()
[mod_auth_kerb.git] / src / mod_auth_kerb.c
1 /*
2  * Daniel Kouril <kouril@users.sourceforge.net>
3  *
4  * Source and Documentation can be found at:
5  * http://modauthkerb.sourceforge.net/
6  *
7  * Based on work by
8  *   James E. Robinson, III <james@ncstate.net>
9  *   Daniel Henninger <daniel@ncsu.edu>
10  *   Ludek Sulak <xsulak@fi.muni.cz>
11  */
12
13 /* ====================================================================
14  * The Apache Software License, Version 1.1
15  *
16  * Copyright (c) 2000-2004 The Apache Software Foundation.  All rights
17  * reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  *
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  *
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *
31  * 3. The end-user documentation included with the redistribution,
32  *    if any, must include the following acknowledgment:
33  *       "This product includes software developed by the
34  *        Apache Software Foundation (http://www.apache.org/)."
35  *    Alternately, this acknowledgment may appear in the software itself,
36  *    if and wherever such third-party acknowledgments normally appear.
37  *
38  * 4. The names "Apache" and "Apache Software Foundation" must
39  *    not be used to endorse or promote products derived from this
40  *    software without prior written permission. For written
41  *    permission, please contact apache@apache.org.
42  *
43  * 5. Products derived from this software may not be called "Apache",
44  *    nor may "Apache" appear in their name, without prior written
45  *    permission of the Apache Software Foundation.
46  *
47  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
51  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  * ====================================================================
60  *
61  * This software consists of voluntary contributions made by many
62  * individuals on behalf of the Apache Software Foundation.  For more
63  * information on the Apache Software Foundation, please see
64  * <http://www.apache.org/>.
65  *
66  * Portions of this software are based upon public domain software
67  * originally written at the National Center for Supercomputing Applications,
68  * University of Illinois, Urbana-Champaign.
69  */
70
71 #ident "$Id$"
72
73 #include "config.h"
74
75 #define MODAUTHKERB_VERSION "5.0-rc4"
76
77 #ifndef APXS1
78 #include "ap_compat.h"
79 #include "apr_strings.h"
80 #endif
81 #include "httpd.h"
82 #include "http_config.h"
83 #include "http_core.h"
84 #include "http_log.h"
85 #include "http_protocol.h"
86 #include "http_request.h"
87
88 #ifdef KRB5
89 #include <krb5.h>
90 #ifdef HEIMDAL
91 #  include <gssapi.h>
92 #else
93 #  include <gssapi/gssapi.h>
94 #  include <gssapi/gssapi_generic.h>
95 #  define GSS_C_NT_USER_NAME gss_nt_user_name
96 #  define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
97 #  define krb5_get_err_text(context,code) error_message(code)
98 #endif
99 #include "spnegokrb5.h"
100 #endif /* KRB5 */
101
102 #ifdef KRB4
103 /*Prevent warning about closesocket redefinition (Apache's ap_config.h and 
104  * MIT Kerberos' port-sockets.h both define it as close) */
105 #ifdef closesocket
106 #  undef closesocket
107 #endif
108 #include <krb.h>
109 #include <netdb.h> /* gethostbyname() */
110 #endif /* KRB4 */
111
112 #ifdef APXS1
113 module auth_kerb_module;
114 #else
115 module AP_MODULE_DECLARE_DATA auth_kerb_module;
116 #endif
117
118 /*************************************************************************** 
119  Macros To Ease Compatibility
120  ***************************************************************************/
121 #ifdef APXS1
122 #define MK_POOL pool
123 #define MK_TABLE_GET ap_table_get
124 #define MK_USER r->connection->user
125 #define MK_AUTH_TYPE r->connection->ap_auth_type
126 #define PROXYREQ_PROXY STD_PROXY
127 #else
128 #define MK_POOL apr_pool_t
129 #define MK_TABLE_GET apr_table_get
130 #define MK_USER r->user
131 #define MK_AUTH_TYPE r->ap_auth_type
132 #endif /* APXS1 */
133
134
135 /*************************************************************************** 
136  Auth Configuration Structure
137  ***************************************************************************/
138 typedef struct {
139         char *krb_auth_realms;
140         int krb_save_credentials;
141         int krb_verify_kdc;
142         char *krb_service_name;
143         int krb_authoritative;
144 #ifdef KRB5
145         char *krb_5_keytab;
146         int krb_method_gssapi;
147         int krb_method_k5pass;
148 #endif
149 #ifdef KRB4
150         char *krb_4_srvtab;
151         int krb_method_k4pass;
152 #endif
153 } kerb_auth_config;
154
155 static const char*
156 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
157
158 #ifdef APXS1
159 #define command(name, func, var, type, usage)           \
160   { name, func,                                         \
161     (void*)XtOffsetOf(kerb_auth_config, var),           \
162     OR_AUTHCFG, type, usage }
163 #else
164 #define command(name, func, var, type, usage)           \
165   AP_INIT_ ## type (name, func,                         \
166         (void*)APR_XtOffsetOf(kerb_auth_config, var),   \
167         OR_AUTHCFG, usage)
168 #endif
169
170 static const command_rec kerb_auth_cmds[] = {
171    command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
172      RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
173
174    command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
175      RAW_ARGS, "Alias for KrbAuthRealms."),
176
177    command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
178      FLAG, "Save and store credentials/tickets retrieved during auth."),
179
180    command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
181      FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
182
183    command("KrbServiceName", ap_set_string_slot, krb_service_name,
184      TAKE1, "Service name to be used by Apache for authentication."),
185
186    command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
187      FLAG, "Set to 'off' to allow access control to be passed along to lower modules if the UserID is not known to this module."),
188
189 #ifdef KRB5
190    command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
191      TAKE1, "Location of Kerberos V5 keytab file."),
192
193    command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
194      FLAG, "Enable Negotiate authentication method."),
195
196    command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
197      FLAG, "Enable Kerberos V5 password authentication."),
198 #endif 
199
200 #ifdef KRB4
201    command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
202      TAKE1, "Location of Kerberos V4 srvtab file."),
203
204    command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
205      FLAG, "Enable Kerberos V4 password authentication."),
206 #endif
207
208    { NULL }
209 };
210
211 #ifdef KRB5
212 typedef struct {
213    gss_ctx_id_t context;
214    gss_cred_id_t server_creds;
215 } gss_connection_t;
216
217 static gss_connection_t *gss_connection = NULL;
218
219 static const char *EMPTY_STRING = "\0";
220 #endif
221
222
223 /*************************************************************************** 
224  Auth Configuration Initialization
225  ***************************************************************************/
226 static void *kerb_dir_create_config(MK_POOL *p, char *d)
227 {
228         kerb_auth_config *rec;
229
230         rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
231         ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
232         ((kerb_auth_config *)rec)->krb_service_name = "HTTP";
233         ((kerb_auth_config *)rec)->krb_authoritative = 1;
234 #ifdef KRB5
235         ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
236         ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
237 #endif
238 #ifdef KRB4
239         ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
240 #endif
241         return rec;
242 }
243
244 static const char*
245 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
246 {
247    sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
248    return NULL;
249 }
250
251 void log_rerror(const char *file, int line, int level, int status,
252                 const request_rec *r, const char *fmt, ...)
253 {
254    char errstr[1024];
255    va_list ap;
256
257    va_start(ap, fmt);
258    vsnprintf(errstr, sizeof(errstr), fmt, ap);
259    va_end(ap);
260
261    
262 #ifdef APXS1
263    ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
264 #else
265    ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
266 #endif
267 }
268
269 #ifdef KRB4
270 /*************************************************************************** 
271  Username/Password Validation for Krb4
272  ***************************************************************************/
273 static int
274 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
275                  char *password, char *linstance, char *srvtab, int krb_verify_kdc)
276 {
277    int ret;
278    char *phost;
279    unsigned long addr;
280    struct hostent *hp;
281    const char *hostname;
282    KTEXT_ST ticket;
283    AUTH_DAT authdata;
284    char lrealm[REALM_SZ];
285
286    ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm, 
287                            DEFAULT_TKT_LIFE, password);
288    if (ret) {
289       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
290                  "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
291                  krb_get_err_text(ret));
292       return ret;
293    }
294
295    if (!krb_verify_kdc)
296       return ret;
297
298    hostname = ap_get_server_name(r);
299
300    hp = gethostbyname(hostname);
301    if (hp == NULL) {
302       dest_tkt();
303       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
304                  "Cannot verify krb4 ticket: gethostbyname() failed: %s",
305                  hstrerror(h_errno));
306       return h_errno;
307    }
308    memcpy(&addr, hp->h_addr, sizeof(addr));
309
310    phost = krb_get_phost((char *)hostname);
311
312    krb_get_lrealm(lrealm, 1);
313
314    ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
315    if (ret) {
316       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
317                  "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
318                  krb_get_err_text(ret));
319       dest_tkt();
320       return ret;
321    }
322
323    ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
324    if (ret) {
325       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
326                  "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
327                  krb_get_err_text(ret));
328       dest_tkt();
329    }
330
331    return ret;
332 }
333
334 static int
335 krb4_cache_cleanup(void *data)
336 {
337    char *tkt_file = (char *) data;
338    
339    krb_set_tkt_string(tkt_file);
340    dest_tkt();
341    return OK;
342 }
343
344 static int 
345 authenticate_user_krb4pwd(request_rec *r,
346                           kerb_auth_config *conf,
347                           const char *auth_line)
348 {
349    int ret;
350    const char *sent_pw;
351    const char *sent_name;
352    char *sent_instance;
353    char tkt_file[32];
354    char *tkt_file_p = NULL;
355    int fd;
356    const char *realms;
357    const char *realm;
358    char *user;
359    char lrealm[REALM_SZ];
360    int all_principals_unkown;
361
362    sent_pw = ap_pbase64decode(r->pool, auth_line);
363    sent_name = ap_getword (r->pool, &sent_pw, ':');
364
365    /* do not allow user to override realm setting of server */
366    if (strchr(sent_name, '@')) {
367       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
368                  "specifying realm in user name is prohibited");
369       return HTTP_UNAUTHORIZED;
370    }
371
372    sent_instance = strchr(sent_name, '.');
373    if (sent_instance)
374       *sent_instance++ = '\0'; 
375
376    snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
377    fd = mkstemp(tkt_file);
378    if (fd < 0) {
379       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
380                  "Cannot create krb4 ccache: mkstemp() failed: %s",
381                  strerror(errno));
382       return HTTP_INTERNAL_SERVER_ERROR;
383    }
384
385    tkt_file_p = ap_pstrdup(r->pool, tkt_file);
386    ap_register_cleanup(r->pool, tkt_file_p,
387                        krb4_cache_cleanup, ap_null_cleanup);
388
389    krb_set_tkt_string(tkt_file);
390
391    all_principals_unkown = 1;
392    realms = conf->krb_auth_realms;
393    do {
394       memset(lrealm, 0, sizeof(lrealm));
395       realm = NULL;
396       if (realms)
397          realm = ap_getword_white(r->pool, &realms);
398
399       if (realm == NULL) {
400          ret = krb_get_lrealm(lrealm, 1);
401          if (ret)
402             break;
403          realm = lrealm;
404       }
405
406       ret = verify_krb4_user(r, (char *)sent_name, 
407                              (sent_instance) ? sent_instance : "",
408                              (char *)realm, (char *)sent_pw,
409                              conf->krb_service_name,
410                              conf->krb_4_srvtab, conf->krb_verify_kdc);
411       if (!conf->krb_authoritative && ret) {
412          /* if we're not authoritative, we allow authentication to pass on
413           * to another modules if (and only if) the user is not known to us */
414          if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
415             all_principals_unkown = 0;
416       }
417
418       if (ret == 0)
419          break;
420    } while (realms && *realms);
421
422    if (ret) {
423       /* XXX log only in the verify_krb4_user() call */
424       log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
425       ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
426                  DECLINED : HTTP_UNAUTHORIZED;
427       goto end;
428    }
429
430    user = ap_pstrdup(r->pool, sent_name);
431    if (sent_instance)
432       user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
433    user = ap_pstrcat(r->pool, user, "@", realm, NULL);
434
435    MK_USER = user;
436    MK_AUTH_TYPE = "Basic";
437    ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
438
439    if (!conf->krb_save_credentials)
440       krb4_cache_cleanup(tkt_file);
441
442 end:
443    if (ret)
444       krb4_cache_cleanup(tkt_file);
445    close(fd);
446    tf_close();
447
448    return ret;
449 }
450 #endif /* KRB4 */
451
452 #ifdef KRB5
453 /*************************************************************************** 
454  Username/Password Validation for Krb5
455  ***************************************************************************/
456 /* Inspired by krb5_verify_user from Heimdal */
457 static krb5_error_code
458 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
459                  krb5_ccache ccache, const char *password, const char *service,
460                  krb5_keytab keytab, int krb_verify_kdc)
461 {
462    krb5_creds creds;
463    krb5_principal server = NULL;
464    krb5_error_code ret;
465    krb5_verify_init_creds_opt opt;
466
467    /* XXX error messages shouldn't be logged here (and in the while() loop in
468     * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
469     * entries when using multiple realms */
470
471    memset(&creds, 0, sizeof(creds));
472
473    ret = krb5_get_init_creds_password(context, &creds, principal, 
474                                       (char *)password, krb5_prompter_posix,
475                                       NULL, 0, NULL, NULL);
476    if (ret) {
477       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
478                  "krb5_get_init_creds_password() failed: %s",
479                  krb5_get_err_text(context, ret));
480       return ret;
481    }
482
483    ret = krb5_sname_to_principal(context, ap_get_server_name(r), service, 
484                                  KRB5_NT_UNKNOWN, &server);
485    if (ret) {
486       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
487                  "krb5_sname_to_principal() failed: %s",
488                  krb5_get_err_text(context, ret));
489       goto end;
490    }
491
492    krb5_verify_init_creds_opt_init(&opt);
493    krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, krb_verify_kdc);
494
495    ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
496    if (ret) {
497       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
498                  "krb5_verify_init_creds() failed: %s",
499                  krb5_get_err_text(context, ret));
500       goto end;
501    }
502                  
503    if (ccache) {
504       ret = krb5_cc_initialize(context, ccache, principal);
505       if (ret) {
506          log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
507                     "krb5_cc_initialize() failed: %s",
508                     krb5_get_err_text(context, ret));
509          goto end;
510       }
511
512       ret = krb5_cc_store_cred(context, ccache, &creds);
513       if (ret) {
514          log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
515                     "krb5_cc_store_cred() failed: %s",
516                     krb5_get_err_text(context, ret));
517          goto end;
518       }
519    }
520
521 end:
522    krb5_free_cred_contents(context, &creds);
523    if (server)
524       krb5_free_principal(context, server);
525    return ret;
526 }
527
528 static int
529 krb5_cache_cleanup(void *data)
530 {
531    krb5_context context;
532    krb5_ccache  cache;
533    krb5_error_code problem;
534    char *cache_name = (char *) data;
535
536    problem = krb5_init_context(&context);
537    if (problem) {
538       /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
539       return HTTP_INTERNAL_SERVER_ERROR;
540    }
541
542    problem = krb5_cc_resolve(context, cache_name, &cache);
543    if (problem) {
544       /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL, 
545                 "krb5_cc_resolve() failed (%s: %s)",
546                 cache_name, krb5_get_err_text(context, problem)); */
547       return HTTP_INTERNAL_SERVER_ERROR;
548    }
549
550    krb5_cc_destroy(context, cache);
551    krb5_free_context(context);
552    return OK;
553 }
554
555 static int
556 create_krb5_ccache(krb5_context kcontext,
557                    request_rec *r,
558                    kerb_auth_config *conf,
559                    krb5_principal princ,
560                    krb5_ccache *ccache)
561 {
562    char *ccname;
563    int fd;
564    krb5_error_code problem;
565    int ret;
566    krb5_ccache tmp_ccache = NULL;
567
568    ccname = ap_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
569    fd = mkstemp(ccname + strlen("FILE:"));
570    if (fd < 0) {
571       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
572                  "mkstemp() failed: %s", strerror(errno));
573       ret = HTTP_INTERNAL_SERVER_ERROR;
574       goto end;
575    }
576    close(fd);
577
578    problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
579    if (problem) {
580       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
581                  "krb5_cc_resolve() failed: %s",
582                  krb5_get_err_text(kcontext, problem));
583       ret = HTTP_INTERNAL_SERVER_ERROR;
584       unlink(ccname);
585       goto end;
586    }
587
588    problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
589    if (problem) {
590       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
591                  "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
592                  ccname, krb5_get_err_text(kcontext, problem));
593       ret = HTTP_INTERNAL_SERVER_ERROR;
594       goto end;
595    }
596
597    ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
598    ap_register_cleanup(r->pool, ccname,
599                        krb5_cache_cleanup, ap_null_cleanup);
600
601    *ccache = tmp_ccache;
602    tmp_ccache = NULL;
603
604    ret = OK;
605
606 end:
607    if (tmp_ccache)
608       krb5_cc_destroy(kcontext, tmp_ccache);
609
610    return ret;
611 }
612
613 static int
614 store_krb5_creds(krb5_context kcontext,
615                  request_rec *r,
616                  kerb_auth_config *conf,
617                  krb5_ccache delegated_cred)
618 {
619    char errstr[1024];
620    krb5_error_code problem;
621    krb5_principal princ;
622    krb5_ccache ccache;
623    int ret;
624
625    problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
626    if (problem) {
627       snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
628                krb5_get_err_text(kcontext, problem));
629       return HTTP_INTERNAL_SERVER_ERROR;
630    }
631
632    ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
633    if (ret) {
634       krb5_free_principal(kcontext, princ);
635       return ret;
636    }
637
638 #ifdef HEIMDAL
639    problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
640 #else
641    problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
642 #endif
643    krb5_free_principal(kcontext, princ);
644    if (problem) {
645       snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
646                krb5_get_err_text(kcontext, problem));
647       krb5_cc_destroy(kcontext, ccache);
648       return HTTP_INTERNAL_SERVER_ERROR;
649    }
650
651    krb5_cc_close(kcontext, ccache);
652    return OK;
653 }
654
655
656 int authenticate_user_krb5pwd(request_rec *r,
657                               kerb_auth_config *conf,
658                               const char *auth_line)
659 {
660    const char      *sent_pw = NULL; 
661    const char      *sent_name = NULL;
662    const char      *realms = NULL;
663    krb5_context    kcontext = NULL;
664    krb5_error_code code;
665    krb5_principal  client = NULL;
666    krb5_ccache     ccache = NULL;
667    krb5_keytab     keytab = NULL;
668    int             ret;
669    char            *name = NULL;
670    int             all_principals_unkown;
671    char            *ccname = NULL;
672    int             fd;
673
674    code = krb5_init_context(&kcontext);
675    if (code) {
676       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
677                  "Cannot initialize Kerberos5 context (%d)", code);
678       return HTTP_INTERNAL_SERVER_ERROR;
679    }
680
681    sent_pw = ap_pbase64decode(r->pool, auth_line);
682    sent_name = ap_getword (r->pool, &sent_pw, ':');
683    /* do not allow user to override realm setting of server */
684    if (strchr(sent_name, '@')) {
685       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
686                  "specifying realm in user name is prohibited");
687       ret = HTTP_UNAUTHORIZED;
688       goto end;
689    }
690
691    if (sent_pw == NULL || *sent_pw == '\0') {
692       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
693                  "empty passwords are not accepted");
694       ret = HTTP_UNAUTHORIZED;
695       goto end;
696    }
697
698 #ifdef HEIMDAL
699    code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
700 #else
701    code = krb5_cc_resolve(kcontext, "MEMORY:", &ccache);
702 #endif
703    if (code) {
704       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
705                  "generating new memory ccache failed: %s",
706                  krb5_get_err_text(kcontext, code));
707       ret = HTTP_INTERNAL_SERVER_ERROR;
708       unlink(ccname);
709       goto end;
710    }
711
712    if (conf->krb_5_keytab)
713       krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
714
715    all_principals_unkown = 1;
716    realms = conf->krb_auth_realms;
717    do {
718       if (realms && (code = krb5_set_default_realm(kcontext,
719                                            ap_getword_white(r->pool, &realms)))){
720          log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
721                     "krb5_set_default_realm() failed: %s",
722                     krb5_get_err_text(kcontext, code));
723          continue;
724       }
725
726       if (client) {
727          krb5_free_principal(kcontext, client);
728          client = NULL;
729       }
730       code = krb5_parse_name(kcontext, sent_name, &client);
731       if (code) {
732          log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
733                     "krb5_parse_name() failed: %s",
734                     krb5_get_err_text(kcontext, code));
735          continue;
736       }
737
738       code = verify_krb5_user(r, kcontext, client, ccache, sent_pw, 
739                               conf->krb_service_name, 
740                               keytab, conf->krb_verify_kdc);
741       if (!conf->krb_authoritative && code) {
742          /* if we're not authoritative, we allow authentication to pass on
743           * to another modules if (and only if) the user is not known to us */
744          if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
745             all_principals_unkown = 0;
746       }
747
748       if (code == 0)
749          break;
750
751       /* ap_getword_white() used above shifts the parameter, so it's not
752          needed to touch the realms variable */
753    } while (realms && *realms);
754
755    memset((char *)sent_pw, 0, strlen(sent_pw));
756
757    if (code) {
758       if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
759          ret = DECLINED;
760       else
761          ret = HTTP_UNAUTHORIZED;
762
763       goto end;
764    }
765
766    code = krb5_unparse_name(kcontext, client, &name);
767    if (code) {
768       log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
769                  krb5_get_err_text(kcontext, code));
770       ret = HTTP_UNAUTHORIZED;
771       goto end;
772    }
773    MK_USER = ap_pstrdup (r->pool, name);
774    MK_AUTH_TYPE = "Basic";
775    free(name);
776
777    if (conf->krb_save_credentials)
778       store_krb5_creds(kcontext, r, conf, ccache);
779
780    ret = OK;
781
782 end:
783    if (client)
784       krb5_free_principal(kcontext, client);
785    if (ccache)
786       krb5_cc_destroy(kcontext, ccache);
787    if (keytab)
788       krb5_kt_close(kcontext, keytab);
789    krb5_free_context(kcontext);
790
791    return ret;
792 }
793
794 /*********************************************************************
795  * GSSAPI Authentication
796  ********************************************************************/
797
798 static const char *
799 get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
800 {
801    OM_uint32 maj_stat, min_stat; 
802    OM_uint32 msg_ctx = 0;
803    gss_buffer_desc status_string;
804    char *err_msg;
805    size_t len;
806
807    err_msg = ap_pstrdup(p, prefix);
808    do {
809       maj_stat = gss_display_status (&min_stat,
810                                      err_maj,
811                                      GSS_C_GSS_CODE,
812                                      GSS_C_NO_OID,
813                                      &msg_ctx,
814                                      &status_string);
815       if (GSS_ERROR(maj_stat))
816          break;
817       err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
818       gss_release_buffer(&min_stat, &status_string);
819       
820       maj_stat = gss_display_status (&min_stat,
821                                      err_min,
822                                      GSS_C_MECH_CODE,
823                                      GSS_C_NULL_OID,
824                                      &msg_ctx,
825                                      &status_string);
826       if (!GSS_ERROR(maj_stat)) {
827          err_msg = ap_pstrcat(p, err_msg,
828                               " (", (char*) status_string.value, ")", NULL);
829          gss_release_buffer(&min_stat, &status_string);
830       }
831    } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
832
833    return err_msg;
834 }
835
836 static int
837 cleanup_gss_connection(void *data)
838 {
839    OM_uint32 minor_status;
840    gss_connection_t *gss_conn = (gss_connection_t *)data;
841
842    if (data == NULL)
843       return OK;
844    if (gss_conn->context != GSS_C_NO_CONTEXT)
845       gss_delete_sec_context(&minor_status, &gss_conn->context,
846                              GSS_C_NO_BUFFER);
847    if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
848       gss_release_cred(&minor_status, &gss_conn->server_creds);
849
850    gss_connection = NULL;
851
852    return OK;
853 }
854
855 static int
856 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
857                 gss_cred_id_t delegated_cred)
858 {
859    OM_uint32 maj_stat, min_stat;
860    krb5_principal princ = NULL;
861    krb5_ccache ccache = NULL;
862    krb5_error_code problem;
863    krb5_context context;
864    int ret = HTTP_INTERNAL_SERVER_ERROR;
865
866    problem = krb5_init_context(&context);
867    if (problem) {
868       log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
869       return HTTP_INTERNAL_SERVER_ERROR;
870    }
871
872    problem = krb5_parse_name(context, princ_name, &princ);
873    if (problem) {
874       log_rerror(APLOG_MARK, APLOG_ERR, 0, r, 
875          "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
876       goto end;
877    }
878
879    problem = create_krb5_ccache(context, r, conf, princ, &ccache);
880    if (problem) {
881       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
882          "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
883       goto end;
884    }
885
886    maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
887    if (GSS_ERROR(maj_stat)) {
888       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
889          "Cannot store delegated credential (%s)", 
890          get_gss_error(r->pool, maj_stat, min_stat, "gss_krb5_copy_ccache"));
891       goto end;
892    }
893
894    krb5_cc_close(context, ccache);
895    ccache = NULL;
896    ret = 0;
897
898 end:
899    if (princ)
900       krb5_free_principal(context, princ);
901    if (ccache)
902       krb5_cc_destroy(context, ccache);
903    krb5_free_context(context);
904    return ret;
905 }
906
907 static int
908 get_gss_creds(request_rec *r,
909               kerb_auth_config *conf,
910               gss_cred_id_t *server_creds)
911 {
912    gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
913    OM_uint32 major_status, minor_status, minor_status2;
914    gss_name_t server_name = GSS_C_NO_NAME;
915    char buf[1024];
916
917 #if 0
918    /* Don't specify service name. This makes MIT 1.3 not to use replay caches,
919     * which causes large problems with the Microsoft krb5 implementation. MS
920     * obviously uses a format of the krb5 authenticator that is considered by
921     * the MIT as replay (Two valid MS authenticators may contain the same time
922     * and utime fields and only differ in the sequential numbers).
923     */
924    snprintf(buf, sizeof(buf), "%s@%s", conf->krb_service_name,
925          ap_get_server_name(r));
926
927    input_token.value = buf;
928    input_token.length = strlen(buf) + 1;
929
930    major_status = gss_import_name(&minor_status, &input_token,
931                                   GSS_C_NT_HOSTBASED_SERVICE,
932                                   &server_name);
933    if (GSS_ERROR(major_status)) {
934       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
935                  "%s", get_gss_error(r->pool, major_status, minor_status,
936                  "gss_import_name() failed"));
937       return HTTP_INTERNAL_SERVER_ERROR;
938    }
939 #endif
940    
941    major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
942                                    GSS_C_NO_OID_SET, GSS_C_ACCEPT,
943                                    server_creds, NULL, NULL);
944    gss_release_name(&minor_status2, &server_name);
945    if (GSS_ERROR(major_status)) {
946       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
947                  "%s", get_gss_error(r->pool, major_status, minor_status,
948                                      "gss_acquire_cred() failed"));
949       return HTTP_INTERNAL_SERVER_ERROR;
950    }
951    
952    return 0;
953 }
954
955 static int
956 cmp_gss_type(gss_buffer_t token, gss_OID oid)
957 {
958    unsigned char *p;
959    size_t len;
960
961    if (token->length == 0)
962       return GSS_S_DEFECTIVE_TOKEN;
963
964    p = token->value;
965    if (*p++ != 0x60)
966       return GSS_S_DEFECTIVE_TOKEN;
967    len = *p++;
968    if (len & 0x80) {
969       if ((len & 0x7f) > 4)
970          return GSS_S_DEFECTIVE_TOKEN;
971       p += len & 0x7f;
972    }
973    if (*p++ != 0x06)
974       return GSS_S_DEFECTIVE_TOKEN;
975
976    if (((OM_uint32) *p++) != oid->length)
977       return GSS_S_DEFECTIVE_TOKEN;
978
979    return memcmp(p, oid->elements, oid->length);
980 }
981
982 static int
983 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
984                       const char *auth_line, char **negotiate_ret_value)
985 {
986   OM_uint32 major_status, minor_status, minor_status2;
987   gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
988   gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
989   const char *auth_param = NULL;
990   int ret;
991   gss_name_t client_name = GSS_C_NO_NAME;
992   gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
993   OM_uint32 (*accept_sec_token)();
994   gss_OID_desc spnego_oid;
995
996   *negotiate_ret_value = (char *)EMPTY_STRING;
997
998   spnego_oid.length = 6;
999   spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
1000
1001   if (gss_connection == NULL) {
1002      gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
1003      if (gss_connection == NULL) {
1004         log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1005                    "ap_pcalloc() failed (not enough memory)");
1006         ret = HTTP_INTERNAL_SERVER_ERROR;
1007         goto end;
1008      }
1009      memset(gss_connection, 0, sizeof(*gss_connection));
1010      ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
1011   }
1012
1013   if (conf->krb_5_keytab) {
1014      char *ktname;
1015      /* we don't use the ap_* calls here, since the string passed to putenv()
1016       * will become part of the enviroment and shouldn't be free()ed by apache
1017       */
1018      ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
1019      if (ktname == NULL) {
1020         log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
1021         ret = HTTP_INTERNAL_SERVER_ERROR;
1022         goto end;
1023      }
1024      sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1025      putenv(ktname);
1026   }
1027
1028   if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
1029      ret = get_gss_creds(r, conf, &gss_connection->server_creds);
1030      if (ret)
1031         goto end;
1032   }
1033
1034   /* ap_getword() shifts parameter */
1035   auth_param = ap_getword_white(r->pool, &auth_line);
1036   if (auth_param == NULL) {
1037      log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1038                 "No Authorization parameter in request from client");
1039      ret = HTTP_UNAUTHORIZED;
1040      goto end;
1041   }
1042
1043   input_token.length = ap_base64decode_len(auth_param) + 1;
1044   input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
1045   if (input_token.value == NULL) {
1046      log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1047                 "ap_pcalloc() failed (not enough memory)");
1048      ret = HTTP_INTERNAL_SERVER_ERROR;
1049      goto end;
1050   }
1051   input_token.length = ap_base64decode(input_token.value, auth_param);
1052
1053   accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1054                         gss_accept_sec_context_spnego : gss_accept_sec_context;
1055
1056   major_status = accept_sec_token(&minor_status,
1057                                   &gss_connection->context,
1058                                   gss_connection->server_creds,
1059                                   &input_token,
1060                                   GSS_C_NO_CHANNEL_BINDINGS,
1061                                   &client_name,
1062                                   NULL,
1063                                   &output_token,
1064                                   NULL,
1065                                   NULL,
1066                                   &delegated_cred);
1067   if (output_token.length) {
1068      char *token = NULL;
1069      size_t len;
1070      
1071      len = ap_base64encode_len(output_token.length) + 1;
1072      token = ap_pcalloc(r->connection->pool, len + 1);
1073      if (token == NULL) {
1074         log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1075                    "ap_pcalloc() failed (not enough memory)");
1076         ret = HTTP_INTERNAL_SERVER_ERROR;
1077         gss_release_buffer(&minor_status2, &output_token);
1078         goto end;
1079      }
1080      ap_base64encode(token, output_token.value, output_token.length);
1081      token[len] = '\0';
1082      *negotiate_ret_value = token;
1083      gss_release_buffer(&minor_status2, &output_token);
1084   }
1085
1086   if (GSS_ERROR(major_status)) {
1087      log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1088                 "%s", get_gss_error(r->pool, major_status, minor_status,
1089                                     "gss_accept_sec_context() failed"));
1090      /* Don't offer the Negotiate method again if call to GSS layer failed */
1091      *negotiate_ret_value = NULL;
1092      ret = HTTP_UNAUTHORIZED;
1093      goto end;
1094   }
1095
1096   if (major_status & GSS_S_CONTINUE_NEEDED) {
1097      /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple 
1098       * iterations to establish authentication */
1099      ret = HTTP_UNAUTHORIZED;
1100      goto end;
1101   }
1102
1103   major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1104   gss_release_name(&minor_status, &client_name); 
1105   if (GSS_ERROR(major_status)) {
1106     log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1107                "%s", get_gss_error(r->pool, major_status, minor_status,
1108                                    "gss_export_name() failed"));
1109     ret = HTTP_INTERNAL_SERVER_ERROR;
1110     goto end;
1111   }
1112
1113   MK_AUTH_TYPE = "Negotiate";
1114   MK_USER = ap_pstrdup(r->pool, output_token.value);
1115
1116   if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1117      store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1118
1119   gss_release_buffer(&minor_status, &output_token);
1120
1121   ret = OK;
1122
1123 end:
1124   if (delegated_cred)
1125      gss_release_cred(&minor_status, &delegated_cred);
1126
1127   if (output_token.length) 
1128      gss_release_buffer(&minor_status, &output_token);
1129
1130   if (client_name != GSS_C_NO_NAME)
1131      gss_release_name(&minor_status, &client_name);
1132
1133   if (! major_status & GSS_S_CONTINUE_NEEDED)
1134      cleanup_gss_connection(gss_connection);
1135
1136   return ret;
1137 }
1138 #endif /* KRB5 */
1139
1140 static int
1141 already_succeeded(request_rec *r)
1142 {
1143    if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1144       return 0;
1145    if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1146        (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1147       return 1;
1148    return 0;
1149 }
1150
1151 static void
1152 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
1153                        int use_krb4, int use_krb5, char *negotiate_ret_value)
1154 {
1155    const char *auth_name = NULL;
1156    int set_basic = 0;
1157    char *negoauth_param;
1158    const char *header_name = 
1159       (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1160
1161    /* get the user realm specified in .htaccess */
1162    auth_name = ap_auth_name(r);
1163
1164    /* XXX should the WWW-Authenticate header be cleared first? */
1165 #ifdef KRB5
1166    if (use_krb5 && conf->krb_method_gssapi && negotiate_ret_value != NULL) {
1167       negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
1168                   ap_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
1169       ap_table_add(r->err_headers_out, header_name, negoauth_param);
1170    }
1171    if (use_krb5 && conf->krb_method_k5pass) {
1172       ap_table_add(r->err_headers_out, header_name,
1173                    ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1174       set_basic = 1;
1175    }
1176 #endif
1177
1178 #ifdef KRB4
1179    if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1180       ap_table_add(r->err_headers_out, header_name,
1181                   ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1182 #endif
1183 }
1184
1185 int kerb_authenticate_user(request_rec *r)
1186 {
1187    kerb_auth_config *conf = 
1188       (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1189                                                 &auth_kerb_module);
1190    const char *auth_type = NULL;
1191    const char *auth_line = NULL;
1192    const char *type = NULL;
1193    int use_krb5 = 0, use_krb4 = 0;
1194    int ret;
1195    static int last_return = HTTP_UNAUTHORIZED;
1196    char *negotiate_ret_value;
1197
1198    /* get the type specified in .htaccess */
1199    type = ap_auth_type(r);
1200
1201    if (type && strcasecmp(type, "Kerberos") == 0)
1202       use_krb5 = use_krb4 = 1;
1203    else if(type && strcasecmp(type, "KerberosV5") == 0)
1204       use_krb4 = 0;
1205    else if(type && strcasecmp(type, "KerberosV4") == 0)
1206       use_krb5 = 0;
1207    else
1208       return DECLINED;
1209
1210    /* get what the user sent us in the HTTP header */
1211    auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1212    if (!auth_line) {
1213        auth_line = MK_TABLE_GET(r->headers_in, "Proxy-Authorization");
1214        if (!auth_line) {
1215                note_kerb_auth_failure(r, conf, use_krb4, use_krb5, "\0");
1216                return HTTP_UNAUTHORIZED;
1217        }
1218    }
1219    auth_type = ap_getword_white(r->pool, &auth_line);
1220
1221    if (already_succeeded(r))
1222       return last_return;
1223
1224    ret = HTTP_UNAUTHORIZED;
1225
1226 #ifdef KRB5
1227    if (use_krb5 && conf->krb_method_gssapi &&
1228        strcasecmp(auth_type, "Negotiate") == 0) {
1229       ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1230    } else if (use_krb5 && conf->krb_method_k5pass &&
1231               strcasecmp(auth_type, "Basic") == 0) {
1232        ret = authenticate_user_krb5pwd(r, conf, auth_line);
1233    }
1234 #endif
1235
1236 #ifdef KRB4
1237    if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1238        strcasecmp(auth_type, "Basic") == 0)
1239       ret = authenticate_user_krb4pwd(r, conf, auth_line);
1240 #endif
1241
1242    if (ret == HTTP_UNAUTHORIZED)
1243       note_kerb_auth_failure(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1244
1245    last_return = ret;
1246    return ret;
1247 }
1248
1249
1250 /*************************************************************************** 
1251  Module Setup/Configuration
1252  ***************************************************************************/
1253 #ifdef APXS1
1254 module MODULE_VAR_EXPORT auth_kerb_module = {
1255         STANDARD_MODULE_STUFF,
1256         NULL,                           /*      module initializer            */
1257         kerb_dir_create_config,         /*      per-directory config creator  */
1258         NULL,                           /*      per-directory config merger   */
1259         NULL,                           /*      per-server    config creator  */
1260         NULL,                           /*      per-server    config merger   */
1261         kerb_auth_cmds,                 /*      command table                 */
1262         NULL,                           /* [ 9] content handlers              */
1263         NULL,                           /* [ 2] URI-to-filename translation   */
1264         kerb_authenticate_user,         /* [ 5] check/validate user_id        */
1265         NULL,                           /* [ 6] check user_id is valid *here* */
1266         NULL,                           /* [ 4] check access by host address  */
1267         NULL,                           /* [ 7] MIME type checker/setter      */
1268         NULL,                           /* [ 8] fixups                        */
1269         NULL,                           /* [10] logger                        */
1270         NULL,                           /* [ 3] header parser                 */
1271         NULL,                           /*      process initialization        */
1272         NULL,                           /*      process exit/cleanup          */
1273         NULL                            /* [ 1] post read_request handling    */
1274 };
1275 #else
1276 static int
1277 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1278                   apr_pool_t *ptemp, server_rec *s)
1279 {
1280    ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1281    return OK;
1282 }
1283
1284 void kerb_register_hooks(apr_pool_t *p)
1285 {
1286    ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1287    ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1288 }
1289
1290 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1291 {
1292    STANDARD20_MODULE_STUFF,
1293    kerb_dir_create_config,      /* create per-dir    conf structures  */
1294    NULL,                        /* merge  per-dir    conf structures  */
1295    NULL,                        /* create per-server conf structures  */
1296    NULL,                        /* merge  per-server conf structures  */
1297    kerb_auth_cmds,              /* table of configuration directives  */
1298    kerb_register_hooks          /* register hooks                     */
1299 };
1300 #endif