2 #include "http_config.h"
3 #include "http_conf_globals.h"
5 #include "http_protocol.h"
6 #include "http_request.h"
12 #include <sys/ioccom.h>
15 #include <kerberosIV/krb.h>
16 #include <kerberosIV/des.h>
17 #include <afs/venus.h>
21 #include <asm/bitops.h>
24 #define KEYTAB_PATH "/home/drh/keytab.umweb.drhtest"
25 #define PRINCIPAL "umweb/drhtest"
27 #define IN_TKT_SERVICE "krbtgt/UMICH.EDU"
29 #define K5PATH "FILE:/tmp/waklog.creds.k5"
30 #define K4PATH "/tmp/waklog.creds.k4"
36 char HandShakeKey
[ 8 ];
46 char *keytab_principal
;
51 struct ktc_token token
;
52 } waklog_child_config
;
53 waklog_child_config
*child
= NULL
;
57 waklog_create_dir_config( pool
*p
, char *path
)
59 waklog_host_config
*cfg
;
61 cfg
= (waklog_host_config
*)ap_pcalloc( p
, sizeof( waklog_host_config
));
65 cfg
->keytab_principal
= 0;
66 cfg
->afs_instance
= 0;
73 waklog_create_server_config( pool
*p
, server_rec
*s
)
75 waklog_host_config
*cfg
;
77 cfg
= (waklog_host_config
*)ap_pcalloc( p
, sizeof( waklog_host_config
));
81 cfg
->keytab_principal
= 0;
82 cfg
->afs_instance
= 0;
89 set_waklog_protect( cmd_parms
*params
, void *mconfig
, int flag
)
91 waklog_host_config
*cfg
;
93 if ( params
->path
== NULL
) {
94 cfg
= (waklog_host_config
*) ap_get_module_config(
95 params
->server
->module_config
, &waklog_module
);
97 cfg
= (waklog_host_config
*)mconfig
;
107 set_waklog_use_keytab( cmd_parms
*params
, void *mconfig
, char *file
)
109 waklog_host_config
*cfg
;
111 if ( params
->path
== NULL
) {
112 cfg
= (waklog_host_config
*) ap_get_module_config(
113 params
->server
->module_config
, &waklog_module
);
115 cfg
= (waklog_host_config
*)mconfig
;
118 ap_log_error( APLOG_MARK
, APLOG_INFO
|APLOG_NOERRNO
, params
->server
,
119 "mod_waklog: using keytab: %s", file
);
128 waklog_child_init( server_rec
*s
, pool
*p
)
131 if ( child
== NULL
) {
132 child
= (waklog_child_config
*) ap_palloc( p
, sizeof( waklog_child_config
) );
135 memset( &child
->token
, 0, sizeof( struct ktc_token
) );
143 command_rec waklog_cmds
[ ] =
145 { "WaklogProtected", set_waklog_protect
,
146 NULL
, RSRC_CONF
| ACCESS_CONF
, FLAG
,
147 "enable waklog on a location or directory basis" },
149 { "WaklogUseKeytab", set_waklog_use_keytab
,
150 NULL
, RSRC_CONF
, TAKE1
,
151 "Use the supplied keytab file rather than the user's TGT" },
158 token_cleanup( void *data
)
160 request_rec
*r
= (request_rec
*)data
;
162 if ( child
->token
.ticketLen
) {
163 memset( &child
->token
, 0, sizeof( struct ktc_token
) );
165 ktc_ForgetAllTokens();
167 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
168 "mod_waklog: ktc_ForgetAllTokens succeeded" );
175 waklog_kinit( server_rec
*s
)
177 krb5_error_code kerror
;
178 krb5_context kcontext
= NULL
;
179 krb5_principal kprinc
= NULL
;
180 krb5_get_init_creds_opt kopts
;
183 krb5_ccache kccache
= NULL
;
184 krb5_keytab keytab
= NULL
;
185 char ktbuf
[ MAX_KEYTAB_NAME_LEN
+ 1 ];
186 waklog_host_config
*cfg
;
188 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, s
,
189 "mod_waklog: waklog_kinit called" );
191 if (( kerror
= krb5_init_context( &kcontext
))) {
192 ap_log_error( APLOG_MARK
, APLOG_ERR
, s
,
193 (char *)error_message( kerror
));
199 if (( kerror
= krb5_cc_resolve( kcontext
, K5PATH
, &kccache
)) != 0 ) {
200 ap_log_error( APLOG_MARK
, APLOG_ERR
, s
,
201 (char *)error_message( kerror
));
206 if (( kerror
= krb5_parse_name( kcontext
, PRINCIPAL
, &kprinc
))) {
207 ap_log_error( APLOG_MARK
, APLOG_ERR
, s
,
208 (char *)error_message( kerror
));
213 krb5_get_init_creds_opt_init( &kopts
);
214 krb5_get_init_creds_opt_set_tkt_life( &kopts
, 10*60*60 );
215 krb5_get_init_creds_opt_set_renew_life( &kopts
, 0 );
216 krb5_get_init_creds_opt_set_forwardable( &kopts
, 1 );
217 krb5_get_init_creds_opt_set_proxiable( &kopts
, 0 );
219 cfg
= (waklog_host_config
*) ap_get_module_config( s
->module_config
,
222 /* which keytab should we use? */
223 strcpy( ktbuf
, cfg
->keytab
? cfg
->keytab
: KEYTAB_PATH
);
225 if ( strlen( ktbuf
) > MAX_KEYTAB_NAME_LEN
) {
226 ap_log_error( APLOG_MARK
, APLOG_ERR
, s
,
227 "server configuration error" );
232 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, s
,
233 "mod_waklog: waklog_kinit using: %s", ktbuf
);
235 if (( kerror
= krb5_kt_resolve( kcontext
, ktbuf
, &keytab
)) != 0 ) {
236 ap_log_error( APLOG_MARK
, APLOG_ERR
, s
,
237 (char *)error_message( kerror
));
243 if (( kerror
= krb5_get_init_creds_keytab( kcontext
, &v5creds
,
244 kprinc
, keytab
, 0, IN_TKT_SERVICE
, &kopts
))) {
246 ap_log_error( APLOG_MARK
, APLOG_ERR
, s
,
247 (char *)error_message( kerror
));
252 if (( kerror
= krb5_verify_init_creds( kcontext
, &v5creds
,
253 kprinc
, keytab
, NULL
, NULL
)) != 0 ) {
255 ap_log_error( APLOG_MARK
, APLOG_ERR
, s
,
256 (char *)error_message( kerror
));
261 if (( kerror
= krb5_cc_initialize( kcontext
, kccache
, kprinc
)) != 0 ) {
262 ap_log_error( APLOG_MARK
, APLOG_ERR
, s
,
263 (char *)error_message( kerror
));
268 kerror
= krb5_cc_store_cred( kcontext
, kccache
, &v5creds
);
269 krb5_free_cred_contents( kcontext
, &v5creds
);
271 ap_log_error( APLOG_MARK
, APLOG_ERR
, s
,
272 (char *)error_message( kerror
));
277 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, s
,
278 "mod_waklog: waklog_kinit success" );
282 (void)krb5_kt_close( kcontext
, keytab
);
284 krb5_free_principal( kcontext
, kprinc
);
286 krb5_cc_close( kcontext
, kccache
);
288 krb5_free_context( kcontext
);
290 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, s
,
291 "mod_waklog: waklog_kinit: exiting" );
298 waklog_aklog( request_rec
*r
)
302 const char *k4path
= NULL
;
303 const char *k5path
= NULL
;
304 krb5_error_code kerror
;
305 krb5_context kcontext
= NULL
;
307 krb5_creds
*v5credsp
= NULL
;
309 krb5_ccache kccache
= NULL
;
310 struct ktc_principal server
= { "afs", "", "umich.edu" };
311 struct ktc_principal client
;
312 struct ktc_token token
;
314 k5path
= ap_table_get( r
->subprocess_env
, "KRB5CCNAME" );
315 k4path
= ap_table_get( r
->subprocess_env
, "KRBTKFILE" );
317 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
318 "mod_waklog: waklog_aklog called: k5path: %s, k4path: %s", k5path
, k4path
);
320 if ( !k5path
|| !k4path
) {
321 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
322 "mod_waklog: waklog_aklog giving up" );
327 ** Get/build creds from file/tgs, then see if we need to SetToken
330 if (( kerror
= krb5_init_context( &kcontext
))) {
331 /* Authentication Required ( kerberos error ) */
332 ap_log_error( APLOG_MARK
, APLOG_ERR
, r
->server
,
333 (char *)error_message( kerror
));
338 memset( (char *)&increds
, 0, sizeof(increds
));
340 /* set server part */
341 if (( kerror
= krb5_parse_name( kcontext
, AFS
, &increds
.server
))) {
342 ap_log_error( APLOG_MARK
, APLOG_ERR
, r
->server
,
343 (char *)error_message( kerror
));
348 if (( kerror
= krb5_cc_resolve( kcontext
, k5path
, &kccache
)) != 0 ) {
349 ap_log_error( APLOG_MARK
, APLOG_ERR
, r
->server
,
350 (char *)error_message( kerror
));
355 /* set client part */
356 krb5_cc_get_principal( kcontext
, kccache
, &increds
.client
);
358 increds
.times
.endtime
= 0;
359 /* Ask for DES since that is what V4 understands */
360 increds
.keyblock
.enctype
= ENCTYPE_DES_CBC_CRC
;
362 /* get the V5 credentials */
363 if (( kerror
= krb5_get_credentials( kcontext
, 0, kccache
,
364 &increds
, &v5credsp
) ) ) {
365 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
366 "mod_waklog: krb5_get_credentials: %s", krb_err_txt
[ kerror
] );
370 /* get the V4 credentials */
371 if (( kerror
= krb524_convert_creds_kdc( kcontext
, v5credsp
, &v4creds
) ) ) {
372 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
373 "mod_waklog: krb524_convert_creds_kdc: %s", krb_err_txt
[ kerror
] );
377 /* assemble the token */
378 token
.kvno
= v4creds
.kvno
;
379 token
.startTime
= v4creds
.issue_date
;
380 token
.endTime
= v5credsp
->times
.endtime
;
381 memmove( &token
.sessionKey
, v4creds
.session
, 8 );
382 token
.ticketLen
= v4creds
.ticket_st
.length
;
383 memmove( token
.ticket
, v4creds
.ticket_st
.dat
, token
.ticketLen
);
385 /* make sure we have to do this */
386 if ( child
->token
.kvno
!= token
.kvno
||
387 child
->token
.ticketLen
!= token
.ticketLen
||
388 memcmp( &child
->token
.sessionKey
, &token
.sessionKey
,
389 sizeof( token
.sessionKey
) ) ||
390 memcmp( child
->token
.ticket
, token
.ticket
, token
.ticketLen
) ) {
392 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
393 "mod_waklog: %s.%s@%s", v4creds
.service
, v4creds
.instance
,
395 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
396 "mod_waklog: %d %d %d", v4creds
.lifetime
, v4creds
.kvno
,
397 v4creds
.issue_date
);
398 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
399 "mod_waklog: %s %s", v4creds
.pname
, v4creds
.pinst
);
400 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
401 "mod_waklog: %d", v4creds
.ticket_st
.length
);
404 strcpy( buf
, v4creds
.pname
);
405 if ( v4creds
.pinst
[ 0 ] ) {
407 strcat( buf
, v4creds
.pinst
);
410 /* assemble the client */
411 strncpy( client
.name
, buf
, MAXKTCNAMELEN
- 1 );
412 strcpy( client
.instance
, "" );
413 strncpy( client
.cell
, v4creds
.realm
, MAXKTCNAMELEN
- 1 );
415 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
416 "mod_waklog: server: name=%s, instance=%s, cell=%s",
417 server
.name
, server
.instance
, server
.cell
);
419 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
420 "mod_waklog: client: name=%s, instance=%s, cell=%s",
421 client
.name
, client
.instance
, client
.cell
);
424 krb_set_tkt_string( (char *)k4path
);
426 /* rumor: we have to do this for AIX 4.1.4 with AFS 3.4+ */
429 if ( ( rc
= ktc_SetToken( &server
, &token
, &client
, 0 ) ) ) {
430 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
431 "mod_waklog: settoken returned %d", rc
);
435 memmove( &child
->token
, &token
, sizeof( struct ktc_token
) );
437 /* we'll need to unlog when this connection is done. */
438 ap_register_cleanup( r
->pool
, (void *)r
, token_cleanup
, ap_null_cleanup
);
443 krb5_free_cred_contents( kcontext
, v5credsp
);
444 if ( increds
.client
)
445 krb5_free_principal( kcontext
, increds
.client
);
446 if ( increds
.server
)
447 krb5_free_principal( kcontext
, increds
.server
);
449 krb5_cc_close( kcontext
, kccache
);
451 krb5_free_context( kcontext
);
453 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
454 "mod_waklog: finished with waklog_aklog" );
461 waklog_child_routine( void *s
, child_info
*pinfo
)
464 ap_log_error( APLOG_MARK
, APLOG_ERR
|APLOG_NOERRNO
, s
,
465 "mod_waklog: waklog_child_routine called as root" );
467 /* this was causing the credential file to get owned by root */
474 sleep( 300 /* 10*60*60 - 5*60 */ );
481 waklog_init( server_rec
*s
, pool
*p
)
483 extern char *version
;
486 ap_log_error( APLOG_MARK
, APLOG_INFO
|APLOG_NOERRNO
, s
,
487 "mod_waklog: version %s initialized.", version
);
489 pid
= ap_bspawn_child( p
, waklog_child_routine
, s
, kill_always
,
492 ap_log_error( APLOG_MARK
, APLOG_ERR
|APLOG_NOERRNO
, s
,
493 "mod_waklog: ap_bspawn_child: %d.", pid
);
498 waklog_phase0( request_rec
*r
)
500 waklog_host_config
*cfg
;
502 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
503 "mod_waklog: phase0 called" );
505 /* directory config? */
506 cfg
= (waklog_host_config
*)ap_get_module_config(
507 r
->per_dir_config
, &waklog_module
);
510 if ( !cfg
->configured
) {
511 cfg
= (waklog_host_config
*)ap_get_module_config(
512 r
->server
->module_config
, &waklog_module
);
515 if ( !cfg
->protect
) {
516 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
517 "mod_waklog: phase0 declining" );
521 /* do this only if we are still unauthenticated */
522 if ( !child
->token
.ticketLen
) {
524 /* set our environment variables */
525 ap_table_set( r
->subprocess_env
, "KRB5CCNAME", K5PATH
);
526 ap_table_set( r
->subprocess_env
, "KRBTKFILE", K4PATH
);
528 /* stuff the credentials into the kernel */
532 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
533 "mod_waklog: phase0 returning" );
539 waklog_phase7( request_rec
*r
)
541 waklog_host_config
*cfg
;
543 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
544 "mod_waklog: phase7 called" );
546 /* directory config? */
547 cfg
= (waklog_host_config
*)ap_get_module_config(
548 r
->per_dir_config
, &waklog_module
);
551 if ( !cfg
->configured
) {
552 cfg
= (waklog_host_config
*)ap_get_module_config(
553 r
->server
->module_config
, &waklog_module
);
556 if ( !cfg
->protect
) {
560 /* stuff the credentials into the kernel */
563 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, r
->server
,
564 "mod_waklog: phase7 returning" );
570 waklog_new_connection( conn_rec
*c
) {
571 ap_log_error( APLOG_MARK
, APLOG_NOERRNO
|APLOG_ERR
, c
->server
,
572 "mod_waklog: new_connection called: conn_rec: 0x%08x pid: %d", c
, getpid() );
576 module MODULE_VAR_EXPORT waklog_module
= {
577 STANDARD_MODULE_STUFF
,
578 waklog_init
, /* module initializer */
579 waklog_create_dir_config
, /* create per-dir config structures */
580 NULL
, /* merge per-dir config structures */
581 waklog_create_server_config
, /* create per-server config structures */
582 NULL
, /* merge per-server config structures */
583 waklog_cmds
, /* table of config file commands */
584 NULL
, /* [#8] MIME-typed-dispatched handlers */
585 NULL
, /* [#1] URI to filename translation */
586 NULL
, /* [#4] validate user id from request */
587 NULL
, /* [#5] check if the user is ok _here_ */
588 NULL
, /* [#3] check access by host address */
589 NULL
, /* [#6] determine MIME type */
590 waklog_phase7
, /* [#7] pre-run fixups */
591 NULL
, /* [#9] log a transaction */
592 NULL
, /* [#2] header parser */
593 waklog_child_init
, /* child_init */
594 NULL
, /* child_exit */
595 waklog_phase0
/* [#0] post read-request */
597 ,NULL
, /* EAPI: add_module */
598 NULL
, /* EAPI: remove_module */
599 NULL
, /* EAPI: rewrite_command */
600 waklog_new_connection
/* EAPI: new_connection */