Removed most #if 0's. Removed k4 code. Changed waklog_aklog so it doesn't segfault...
[hcoop/zz_old/modwaklog.git] / mod_waklog.c
index 32a1266..0b6eae2 100644 (file)
 #include <sys/ioccom.h>
 #endif /* sun */
 #include <stropts.h>
-#if 0
-#include <kerberosIV/krb.h>
-#include <kerberosIV/des.h>
-#endif /* 0 */
 #include <afs/venus.h>
 #include <afs/auth.h>
 #include <rx/rxkad.h>
 
-#if 0
-#include <asm/bitops.h>
-#include <sys/shm.h>
-#endif /* 0 */
-
-#define KEYTAB                 "/home/drh/keytab.umweb.drhtest"
-#define KEYTAB_PRINCIPAL       "umweb/drhtest"
+#define KEYTAB                  "/etc/keytab.default"
+#define KEYTAB_PRINCIPAL        "defaultprinc"
+#define AFS_CELL       "someplace.edu" /* NB: lower case */
 
 #define TKT_LIFE       10*60*60
-#define        SLEEP_TIME      5*60 /* should be TKT_LIFE */
+#define        SLEEP_TIME      TKT_LIFE - 5*60
 
-#define AFS_CELL       "umich.edu" /* NB: lower case */
 
 #define K5PATH         "FILE:/tmp/waklog.creds.k5"
-#define K4PATH         "/tmp/waklog.creds.k4"
 
 module waklog_module;
 
-struct ClearToken {
-    long AuthHandle;
-    char HandShakeKey[ 8 ];
-    long ViceId;
-    long BeginTimestamp;
-    long EndTimestamp;
-};
-
 typedef struct {
     int                configured;
     int                protect;
@@ -59,7 +41,7 @@ typedef struct {
 } waklog_child_config;
 waklog_child_config    child;
 
-
+#if 0
     static void *
 waklog_create_dir_config( pool *p, char *path )
 {
@@ -74,6 +56,7 @@ waklog_create_dir_config( pool *p, char *path )
 
     return( cfg );
 }
+#endif /* 0 */
 
 
     static void *
@@ -88,6 +71,9 @@ waklog_create_server_config( pool *p, server_rec *s )
     cfg->keytab_principal = KEYTAB_PRINCIPAL;
     cfg->afs_cell = AFS_CELL;
 
+       
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, s, "mod_waklog: server config created" );
+
     return( cfg );
 }
 
@@ -97,35 +83,28 @@ set_waklog_protect( cmd_parms *params, void *mconfig, int flag )
 {
     waklog_host_config          *cfg;
 
-    if ( params->path == NULL ) {
-        cfg = (waklog_host_config *) ap_get_module_config(
-                params->server->module_config, &waklog_module );
-    } else {
-        cfg = (waklog_host_config *)mconfig;
-    }
+    cfg = (waklog_host_config *) ap_get_module_config(
+            params->server->module_config, &waklog_module );
 
     cfg->protect = flag;
     cfg->configured = 1;
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, params->server, "mod_waklog: waklog_protect set" );
     return( NULL );
 }
 
 
     static const char *
-set_waklog_use_keytab( cmd_parms *params, void *mconfig, char *file  )
+set_waklog_keytab( cmd_parms *params, void *mconfig, char *file  )
 {
     waklog_host_config          *cfg;
 
-    if ( params->path == NULL ) {
-        cfg = (waklog_host_config *) ap_get_module_config(
+    cfg = (waklog_host_config *) ap_get_module_config(
                 params->server->module_config, &waklog_module );
-    } else {
-        cfg = (waklog_host_config *)mconfig;
-    }
 
     ap_log_error( APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, params->server,
-           "mod_waklog: using keytab: %s", file );
+           "mod_waklog: will use keytab: %s", file );
 
-    cfg->keytab = file;
+    cfg->keytab = ap_pstrdup ( params->pool, file );
     cfg->configured = 1;
     return( NULL );
 }
@@ -136,17 +115,13 @@ set_waklog_use_keytab_principal( cmd_parms *params, void *mconfig, char *file  )
 {
     waklog_host_config          *cfg;
 
-    if ( params->path == NULL ) {
-        cfg = (waklog_host_config *) ap_get_module_config(
+    cfg = (waklog_host_config *) ap_get_module_config(
                 params->server->module_config, &waklog_module );
-    } else {
-        cfg = (waklog_host_config *)mconfig;
-    }
 
     ap_log_error( APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, params->server,
-           "mod_waklog: using keytab_principal: %s", file );
+           "mod_waklog: will use keytab_principal: %s", file );
 
-    cfg->keytab_principal = file;
+    cfg->keytab_principal = ap_pstrdup ( params->pool, file );
     cfg->configured = 1;
     return( NULL );
 }
@@ -157,17 +132,13 @@ set_waklog_use_afs_cell( cmd_parms *params, void *mconfig, char *file  )
 {
     waklog_host_config          *cfg;
 
-    if ( params->path == NULL ) {
-        cfg = (waklog_host_config *) ap_get_module_config(
+    cfg = (waklog_host_config *) ap_get_module_config(
                 params->server->module_config, &waklog_module );
-    } else {
-        cfg = (waklog_host_config *)mconfig;
-    }
 
     ap_log_error( APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, params->server,
-           "mod_waklog: using afs_cell: %s", file );
+           "mod_waklog: will use afs_cell: %s", file );
 
-    cfg->afs_cell = file;
+    cfg->afs_cell = ap_pstrdup( params->pool, file );
     cfg->configured = 1;
     return( NULL );
 }
@@ -191,16 +162,16 @@ command_rec waklog_cmds[ ] =
     NULL, RSRC_CONF | ACCESS_CONF, FLAG,
     "enable waklog on a location or directory basis" },
 
-    { "WaklogUseKeytabPath", set_waklog_use_keytab,
-    NULL, RSRC_CONF, TAKE1,
+    { "WaklogKeytab", set_waklog_keytab,
+    NULL, RSRC_CONF | ACCESS_CONF, TAKE1,
     "Use the supplied keytab rather than the default" },
 
     { "WaklogUseKeytabPrincipal", set_waklog_use_keytab_principal,
-    NULL, RSRC_CONF, TAKE1,
+    NULL, RSRC_CONF | ACCESS_CONF, TAKE1,
     "Use the supplied keytab principal rather than the default" },
 
     { "WaklogUseAFSCell", set_waklog_use_afs_cell,
-    NULL, RSRC_CONF, TAKE1,
+    NULL, RSRC_CONF | ACCESS_CONF, TAKE1,
     "Use the supplied AFS cell rather than the default" },
 
     { NULL }
@@ -217,7 +188,7 @@ token_cleanup( void *data )
 
        ktc_ForgetAllTokens();
 
-       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
            "mod_waklog: ktc_ForgetAllTokens succeeded: pid: %d", getpid() );
     }
     return;
@@ -236,16 +207,17 @@ waklog_kinit( server_rec *s )
     krb5_keytab                        keytab = NULL;
     char                       ktbuf[ MAX_KEYTAB_NAME_LEN + 1 ];
     waklog_host_config         *cfg;
+    int                                i;
 
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, s,
-       "mod_waklog: waklog_kinit called" );
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, s,
+       "mod_waklog: waklog_kinit called: pid: %d", getpid() );
 
     cfg = (waklog_host_config *) ap_get_module_config( s->module_config,
            &waklog_module );
 
     if (( kerror = krb5_init_context( &kcontext ))) {
        ap_log_error( APLOG_MARK, APLOG_ERR, s,
-               (char *)error_message( kerror ));
+               "mod_waklog: %s", (char *)error_message( kerror ));
 
         goto cleanup;
     }
@@ -253,14 +225,17 @@ waklog_kinit( server_rec *s )
     /* use the path */
     if (( kerror = krb5_cc_resolve( kcontext, K5PATH, &kccache )) != 0 ) {
        ap_log_error( APLOG_MARK, APLOG_ERR, s,
-               (char *)error_message( kerror ));
+               "mod_waklog: %s", (char *)error_message( kerror ));
 
        goto cleanup;
     }
 
-   if (( kerror = krb5_parse_name( kcontext, cfg->keytab_principal, &kprinc ))) {
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, s,
+       "mod_waklog: keytab_principal: %s", cfg->keytab_principal );
+
+    if (( kerror = krb5_parse_name( kcontext, cfg->keytab_principal, &kprinc ))) {
        ap_log_error( APLOG_MARK, APLOG_ERR, s,
-               (char *)error_message( kerror ));
+               "mod_waklog: %s", (char *)error_message( kerror ));
 
                goto cleanup;
     }
@@ -274,38 +249,33 @@ waklog_kinit( server_rec *s )
     /* keytab from config */
     strncpy( ktbuf, cfg->keytab, sizeof( ktbuf ) - 1 );
 
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, s,
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, s,
            "mod_waklog: waklog_kinit using: %s", ktbuf );
 
     if (( kerror = krb5_kt_resolve( kcontext, ktbuf, &keytab )) != 0 ) {
        ap_log_error( APLOG_MARK, APLOG_ERR, s,
-               (char *)error_message( kerror ));
+               "mod_waklog:krb5_kt_resolve %s", (char *)error_message( kerror ));
 
        goto cleanup;
     }
 
+    memset( (char *)&v5creds, 0, sizeof(v5creds));
+
     /* get the krbtgt */
     if (( kerror = krb5_get_init_creds_keytab( kcontext, &v5creds, 
                kprinc, keytab, 0, NULL, &kopts ))) {
 
        ap_log_error( APLOG_MARK, APLOG_ERR, s,
-               (char *)error_message( kerror ));
+               "mod_waklog:krb5_get_init_creds_keytab %s", (char *)error_message( kerror ));
 
        goto cleanup;
     }
 
-   if (( kerror = krb5_verify_init_creds( kcontext, &v5creds,
-           kprinc, keytab, NULL, NULL )) != 0 ) {
 
-       ap_log_error( APLOG_MARK, APLOG_ERR, s,
-               (char *)error_message( kerror ));
-
-       goto cleanup;
-    }
 
     if (( kerror = krb5_cc_initialize( kcontext, kccache, kprinc )) != 0 ) {
        ap_log_error( APLOG_MARK, APLOG_ERR, s,
-               (char *)error_message( kerror ));
+               "mod_waklog:krb5_cc_initialize %s", (char *)error_message( kerror ));
 
        goto cleanup;
     }
@@ -314,12 +284,12 @@ waklog_kinit( server_rec *s )
     krb5_free_cred_contents( kcontext, &v5creds );
     if ( kerror != 0 ) {
        ap_log_error( APLOG_MARK, APLOG_ERR, s,
-               (char *)error_message( kerror ));
+               "mod_waklog: %s", (char *)error_message( kerror ));
 
        goto cleanup;
     }
 
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, s,
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, s,
        "mod_waklog: waklog_kinit success" );
 
 cleanup:
@@ -332,7 +302,7 @@ cleanup:
     if ( kcontext )
        krb5_free_context( kcontext );
 
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, s,
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, s,
        "mod_waklog: waklog_kinit: exiting" );
 
     return( 0 );
@@ -343,8 +313,7 @@ cleanup:
 waklog_aklog( request_rec *r )
 {
     int                                rc;
-    char                       buf[ 2048 ];
-    const char                 *k4path = NULL;
+    char                       buf[ MAXKTCTICKETLEN ];
     const char                 *k5path = NULL;
     krb5_error_code            kerror;
     krb5_context               kcontext = NULL;
@@ -358,13 +327,12 @@ waklog_aklog( request_rec *r )
     int                                buflen;
 
     k5path = ap_table_get( r->subprocess_env, "KRB5CCNAME" );
-    k4path = ap_table_get( r->subprocess_env, "KRBTKFILE" );
 
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
-       "mod_waklog: waklog_aklog called: k5path: %s, k4path: %s", k5path, k4path );
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, r->server,
+       "mod_waklog: waklog_aklog called: k5path: %s", k5path );
 
-    if ( !k5path || !k4path ) {   
-       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+    if ( k5path == NULL ) {
+       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
                "mod_waklog: waklog_aklog giving up" );
        goto cleanup;
     }
@@ -423,10 +391,10 @@ waklog_aklog( request_rec *r )
        goto cleanup;
     }
 
-    /* don't overflor */
-    if ( v5credsp->ticket.length >= 344 ) {    /* from krb524d.c */
+    /* don't overflow */
+    if ( v5credsp->ticket.length >= MAXKTCTICKETLEN ) {        /* from krb524d.c */
        ap_log_error( APLOG_MARK, APLOG_ERR, r->server,
-           "mod_waklog: ticket size (%d) to big to fake", v5credsp->ticket.length );
+           "mod_waklog: ticket size (%d) too big to fake", v5credsp->ticket.length );
        goto cleanup;
     }
 
@@ -447,39 +415,41 @@ waklog_aklog( request_rec *r )
                    sizeof( token.sessionKey ) )) ||
            (memcmp( child.token.ticket, token.ticket, token.ticketLen )) ) {
 
-       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
                "mod_waklog: client: %s", buf );
 
        /* build the name */
-       memmove( buf, v5credsp->client->data[0].data, v5credsp->client->data[0].length );
+       memmove( buf, v5credsp->client->data[0].data,
+               min( v5credsp->client->data[0].length, MAXKTCNAMELEN - 1 ) );
        buf[ v5credsp->client->data[0].length ] = '\0';
        if ( v5credsp->client->length > 1 ) {
                strncat( buf, ".", sizeof( buf ) - strlen( buf ) - 1 );
                buflen = strlen( buf );
-               memmove( buf + buflen, v5credsp->client->data[1].data, v5credsp->client->data[1].length );
+               memmove( buf + buflen, v5credsp->client->data[1].data,
+                       min( v5credsp->client->data[1].length, MAXKTCNAMELEN - strlen( buf ) - 1 ) );
                buf[ buflen + v5credsp->client->data[1].length ] = '\0';
        }
 
        /* assemble the client */
        strncpy( client.name, buf,              sizeof( client.name ) - 1 );
        strncpy( client.instance, "",           sizeof( client.instance) - 1 );
-       memmove( buf, v5credsp->client->realm.data, v5credsp->client->realm.length );
+       memmove( buf, v5credsp->client->realm.data, 
+               min( v5credsp->client->realm.length, MAXKTCNAMELEN - 1 ) );
        buf[ v5credsp->client->realm.length ] = '\0';
        strncpy( client.cell, buf,              sizeof( client.cell ) - 1 );
 
        /* assemble the server's cell */
        strncpy( server.cell, cfg->afs_cell ,   sizeof( server.cell ) - 1 );
 
-       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
                "mod_waklog: server: name=%s, instance=%s, cell=%s",
                server.name, server.instance, server.cell );
 
-       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
                "mod_waklog: client: name=%s, instance=%s, cell=%s",
                 client.name, client.instance, client.cell );
 
        /* use the path */
-       krb_set_tkt_string( (char *)k4path );
 
        /* rumor: we have to do this for AIX 4.1.4 with AFS 3.4+ */
        write( 2, "", 0 );
@@ -509,7 +479,7 @@ cleanup:
     if ( kcontext )
        krb5_free_context( kcontext );
 
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
                "mod_waklog: finished with waklog_aklog" );
 
     return;
@@ -520,7 +490,7 @@ cleanup:
 waklog_child_routine( void *s, child_info *pinfo )
 {
     if ( !getuid() ) {
-       ap_log_error( APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, s,
+       ap_log_error( APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, s,
                "mod_waklog: waklog_child_routine called as root" );
 
        /* this was causing the credential file to get owned by root */
@@ -548,7 +518,7 @@ waklog_init( server_rec *s, pool *p )
     pid = ap_bspawn_child( p, waklog_child_routine, s, kill_always,
            NULL, NULL, NULL );
 
-    ap_log_error( APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, s,
+    ap_log_error( APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, s,
            "mod_waklog: ap_bspawn_child: %d.", pid );
 }
 
@@ -558,37 +528,29 @@ waklog_phase0( request_rec *r )
 {
     waklog_host_config  *cfg;
 
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
        "mod_waklog: phase0 called" );
 
-    /* directory config? */
-    cfg = (waklog_host_config *)ap_get_module_config(
-            r->per_dir_config, &waklog_module);
-
-    /* server config? */
-    if ( !cfg->configured ) {
        cfg = (waklog_host_config *)ap_get_module_config(
            r->server->module_config, &waklog_module);
-    }
 
     if ( !cfg->protect ) {
-       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
            "mod_waklog: phase0 declining" );
         return( DECLINED );
     }
 
+    /* set our environment variables */
+    ap_table_set( r->subprocess_env, "KRB5CCNAME", K5PATH );
+
     /* do this only if we are still unauthenticated */
     if ( !child.token.ticketLen ) {
 
-       /* set our environment variables */
-       ap_table_set( r->subprocess_env, "KRB5CCNAME", K5PATH );
-       ap_table_set( r->subprocess_env, "KRBTKFILE", K4PATH );
-
        /* stuff the credentials into the kernel */
        waklog_aklog( r );
     }
     
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
        "mod_waklog: phase0 returning" );
     return DECLINED;
 }
@@ -599,18 +561,11 @@ waklog_phase7( request_rec *r )
 {
     waklog_host_config *cfg;
 
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
        "mod_waklog: phase7 called" );
 
-    /* directory config? */
-    cfg = (waklog_host_config *)ap_get_module_config(
-            r->per_dir_config, &waklog_module);
-
-    /* server config? */
-    if ( !cfg->configured ) {
        cfg = (waklog_host_config *)ap_get_module_config(
            r->server->module_config, &waklog_module);
-    }
 
     if ( !cfg->protect ) {
         return( DECLINED );
@@ -619,7 +574,7 @@ waklog_phase7( request_rec *r )
     /* stuff the credentials into the kernel */
     waklog_aklog( r );
 
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
        "mod_waklog: phase7 returning" );
 
     return DECLINED;
@@ -627,15 +582,51 @@ waklog_phase7( request_rec *r )
 
     static void
 waklog_new_connection( conn_rec *c ) {
-    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, c->server,
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, c->server,
        "mod_waklog: new_connection called: pid: %d", getpid() );
     return;
 }
 
+
+/*
+**  Here's a quick explaination for phase0 and phase2:
+**  Apache does a stat() on the path between phase0 and
+**  phase2, and must by ACLed rl to succeed.  So, at
+**  phase0 we acquire credentials for umweb:servers from
+**  a keytab, and at phase2 we must ensure we remove them.
+**
+**  Failure to "unlog" would be a security risk.
+*/
+    static int
+waklog_phase2( request_rec *r )
+{
+
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
+           "mod_waklog: phase2 called" );
+
+    if ( child.token.ticketLen ) {
+       memset( &child.token, 0, sizeof( struct ktc_token ) );
+
+       ktc_ForgetAllTokens();
+
+       ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
+           "mod_waklog: ktc_ForgetAllTokens succeeded: pid: %d", getpid() );
+    }
+
+    ap_log_error( APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, r->server,
+           "mod_waklog: phase2 returning" );
+
+    return DECLINED;
+}
+
 module MODULE_VAR_EXPORT waklog_module = {
     STANDARD_MODULE_STUFF, 
-    waklog_init,              /* module initializer                  */
+    waklog_init,           /* module initializer                  */
+#if 0
     waklog_create_dir_config, /* create per-dir    config structures */
+#else /* 0 */
+    NULL,                  /* create per-dir    config structures */
+#endif /* 0 */
     NULL,                  /* merge  per-dir    config structures */
     waklog_create_server_config, /* create per-server config structures */
     NULL,                  /* merge  per-server config structures */
@@ -648,7 +639,7 @@ module MODULE_VAR_EXPORT waklog_module = {
     NULL,                  /* [#6] determine MIME type            */
     waklog_phase7,         /* [#7] pre-run fixups                 */
     NULL,                  /* [#9] log a transaction              */
-    NULL,                  /* [#2] header parser                  */
+    waklog_phase2,         /* [#2] header parser                  */
     waklog_child_init,     /* child_init                          */
     NULL,                  /* child_exit                          */
     waklog_phase0          /* [#0] post read-request              */
@@ -659,3 +650,4 @@ module MODULE_VAR_EXPORT waklog_module = {
     waklog_new_connection  /* EAPI: new_connection                */
 #endif
 };
+