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