@@ -109,6 +109,7 @@ static int mag_pre_connection(conn_rec *c, void *csd)
109109 struct mag_conn * mc ;
110110
111111 mc = mag_new_conn_ctx (c -> pool );
112+ mc -> is_preserved = true;
112113 ap_set_module_config (c -> conn_config , & auth_gssapi_module , (void * )mc );
113114 return OK ;
114115}
@@ -134,7 +135,6 @@ struct mag_conn *mag_new_conn_ctx(apr_pool_t *pool)
134135 * when the connection/request is terminated */
135136 apr_pool_cleanup_register (mc -> pool , (void * )mc ,
136137 mag_conn_destroy , apr_pool_cleanup_null );
137-
138138 return mc ;
139139}
140140
@@ -225,8 +225,8 @@ static char *escape(apr_pool_t *pool, const char *name,
225225 return escaped ;
226226}
227227
228- static char * mag_gss_name_to_ccache_name (request_rec * req ,
229- char * dir , const char * gss_name )
228+ char * mag_gss_name_to_ccache_name (request_rec * req ,
229+ char * dir , const char * gss_name )
230230{
231231 char * escaped ;
232232
@@ -240,27 +240,9 @@ static char *mag_gss_name_to_ccache_name(request_rec *req,
240240 return apr_psprintf (req -> pool , "%s/%s" , dir , escaped );
241241}
242242
243- static void mag_set_KRB5CCANME (request_rec * req , char * ccname )
244- {
245- apr_status_t status ;
246- apr_finfo_t finfo ;
247- char * value ;
248-
249- status = apr_stat (& finfo , ccname , APR_FINFO_MIN , req -> pool );
250- if (status != APR_SUCCESS && status != APR_INCOMPLETE ) {
251- /* set the file cache anyway, but warn */
252- ap_log_rerror (APLOG_MARK , APLOG_ERR |APLOG_NOERRNO , 0 , req ,
253- "KRB5CCNAME file (%s) lookup failed!" , ccname );
254- }
255-
256- value = apr_psprintf (req -> pool , "FILE:%s" , ccname );
257- apr_table_set (req -> subprocess_env , "KRB5CCNAME" , value );
258- }
259-
260243static void mag_store_deleg_creds (request_rec * req ,
261- char * dir , char * clientname ,
262- gss_cred_id_t delegated_cred ,
263- char * * ccachefile )
244+ char * dir , const char * gss_name ,
245+ gss_cred_id_t delegated_cred )
264246{
265247 gss_key_value_element_desc element ;
266248 gss_key_value_set_desc store ;
@@ -270,7 +252,7 @@ static void mag_store_deleg_creds(request_rec *req,
270252 store .elements = & element ;
271253 store .count = 1 ;
272254
273- ccname = mag_gss_name_to_ccache_name (req , dir , clientname );
255+ ccname = mag_gss_name_to_ccache_name (req , dir , gss_name );
274256 element .value = apr_psprintf (req -> pool , "FILE:%s" , ccname );
275257
276258 maj = gss_store_cred_into (& min , delegated_cred , GSS_C_INITIATE ,
@@ -280,8 +262,6 @@ static void mag_store_deleg_creds(request_rec *req,
280262 mag_error (req , "failed to store delegated creds" ,
281263 maj , min ));
282264 }
283-
284- * ccachefile = ccname ;
285265}
286266#endif
287267
@@ -328,26 +308,9 @@ const char *auth_types[] = {
328308 NULL
329309};
330310
331- static void mag_set_req_data (request_rec * req ,
332- struct mag_config * cfg ,
333- struct mag_conn * mc )
311+ const char * mag_str_auth_type (int auth_type )
334312{
335- apr_table_set (req -> subprocess_env , "GSS_NAME" , mc -> gss_name );
336- apr_table_set (req -> subprocess_env , "GSS_SESSION_EXPIRATION" ,
337- apr_psprintf (req -> pool ,
338- "%ld" , (long )mc -> expiration ));
339- req -> ap_auth_type = apr_pstrdup (req -> pool ,
340- auth_types [mc -> auth_type ]);
341- req -> user = apr_pstrdup (req -> pool , mc -> user_name );
342- if (cfg -> deleg_ccache_dir && mc -> delegated ) {
343- char * ccname ;
344- ccname = mag_gss_name_to_ccache_name (req ,
345- cfg -> deleg_ccache_dir ,
346- mc -> gss_name );
347- if (ccname ) {
348- mag_set_KRB5CCANME (req , ccname );
349- }
350- }
313+ return auth_types [auth_type ];
351314}
352315
353316gss_OID_set mag_filter_unwanted_mechs (gss_OID_set src )
@@ -705,12 +668,10 @@ static int mag_auth(request_rec *req)
705668 uint32_t maj , min ;
706669 char * reply ;
707670 size_t replen ;
708- char * clientname ;
709671 gss_OID mech_type = GSS_C_NO_OID ;
710672 gss_OID_set desired_mechs = GSS_C_NO_OID_SET ;
711673 gss_buffer_desc lname = GSS_C_EMPTY_BUFFER ;
712674 struct mag_conn * mc = NULL ;
713- time_t expiration ;
714675 int i ;
715676
716677 type = ap_auth_type (req );
@@ -792,6 +753,8 @@ static int mag_auth(request_rec *req)
792753 }
793754 pctx = & mc -> ctx ;
794755 } else {
756+ /* no preserved mc, create one just for this request */
757+ mc = mag_new_conn_ctx (req -> pool );
795758 pctx = & ctx ;
796759 }
797760
@@ -834,7 +797,7 @@ static int mag_auth(request_rec *req)
834797 ba_user .length = strlen (ba_user .value );
835798 ba_pwd .length = strlen (ba_pwd .value );
836799
837- if (mc && mc -> established &&
800+ if (mc -> is_preserved && mc -> established &&
838801 mag_basic_check (req_cfg , mc , ba_user , ba_pwd )) {
839802 ap_log_rerror (APLOG_MARK , APLOG_DEBUG , 0 , req ,
840803 "Already established BASIC AUTH context found!" );
@@ -864,14 +827,14 @@ static int mag_auth(request_rec *req)
864827 goto done ;
865828 }
866829
867- if (mc && mc -> established ) {
830+ if (mc -> established ) {
868831 /* if we are re-authenticating make sure the conn context
869832 * is cleaned up so we do not accidentally reuse an existing
870833 * established context */
871834 mag_conn_clear (mc );
872835 }
873836
874- req -> ap_auth_type = apr_pstrdup ( req -> pool , auth_types [ auth_type ]) ;
837+ mc -> auth_type = auth_type ;
875838
876839#ifdef HAVE_CRED_STORE
877840 if (use_s4u2proxy (req_cfg )) {
@@ -914,7 +877,7 @@ static int mag_auth(request_rec *req)
914877 maj , min ));
915878 goto done ;
916879 } else if (maj == GSS_S_CONTINUE_NEEDED ) {
917- if (!mc ) {
880+ if (!mc -> is_preserved ) {
918881 ap_log_rerror (APLOG_MARK , APLOG_ERR , 0 , req ,
919882 "Mechanism needs continuation but neither "
920883 "GssapiConnectionBound nor "
@@ -927,34 +890,24 @@ static int mag_auth(request_rec *req)
927890 }
928891
929892complete :
930- /* Always set the GSS name in an env var */
931893 maj = gss_display_name (& min , client , & name , NULL );
932894 if (GSS_ERROR (maj )) {
933895 ap_log_rerror (APLOG_MARK , APLOG_ERR , 0 , req , "%s" ,
934896 mag_error (req , "gss_display_name() failed" ,
935897 maj , min ));
936898 goto done ;
937899 }
938- clientname = apr_pstrndup (req -> pool , name .value , name .length );
939- apr_table_set ( req -> subprocess_env , "GSS_NAME" , clientname );
940- expiration = time ( NULL ) + vtime ;
941- apr_table_set ( req -> subprocess_env , "GSS_SESSION_EXPIRATION" ,
942- apr_psprintf ( req -> pool , "%ld" , ( long ) expiration )) ;
900+ mc -> gss_name = apr_pstrndup (req -> pool , name .value , name .length );
901+ if ( vtime == GSS_C_INDEFINITE || vtime < MIN_SESS_EXP_TIME ) {
902+ vtime = MIN_SESS_EXP_TIME ;
903+ }
904+ mc -> expiration = time ( NULL ) + vtime ;
943905
944906#ifdef HAVE_CRED_STORE
945907 if (cfg -> deleg_ccache_dir && delegated_cred != GSS_C_NO_CREDENTIAL ) {
946- char * ccachefile = NULL ;
947-
948- mag_store_deleg_creds (req , cfg -> deleg_ccache_dir , clientname ,
949- delegated_cred , & ccachefile );
950-
951- if (ccachefile ) {
952- mag_set_KRB5CCANME (req , ccachefile );
953- }
954-
955- if (mc ) {
956- mc -> delegated = true;
957- }
908+ mag_store_deleg_creds (req , cfg -> deleg_ccache_dir , mc -> gss_name ,
909+ delegated_cred );
910+ mc -> delegated = true;
958911 }
959912#endif
960913
@@ -965,27 +918,21 @@ static int mag_auth(request_rec *req)
965918 mag_error (req , "gss_localname() failed" , maj , min ));
966919 goto done ;
967920 }
968- req -> user = apr_pstrndup (req -> pool , lname .value , lname .length );
921+ mc -> user_name = apr_pstrndup (req -> pool , lname .value , lname .length );
969922 } else {
970- req -> user = clientname ;
923+ mc -> user_name = apr_pstrdup ( mc -> pool , mc -> gss_name ) ;
971924 }
972925
973- if (mc ) {
974- mc -> user_name = apr_pstrdup (mc -> pool , req -> user );
975- mc -> gss_name = apr_pstrdup (mc -> pool , clientname );
976- mc -> established = true;
977- if (vtime == GSS_C_INDEFINITE || vtime < MIN_SESS_EXP_TIME ) {
978- vtime = MIN_SESS_EXP_TIME ;
979- }
980- mc -> expiration = expiration ;
981- mc -> auth_type = auth_type ;
982- if (auth_type == AUTH_TYPE_BASIC ) {
983- mag_basic_cache (req_cfg , mc , ba_user , ba_pwd );
984- }
985- if (req_cfg -> use_sessions ) {
986- mag_attempt_session (req_cfg , mc );
987- }
926+ mc -> established = true;
927+ if (auth_type == AUTH_TYPE_BASIC ) {
928+ mag_basic_cache (req_cfg , mc , ba_user , ba_pwd );
988929 }
930+ if (req_cfg -> use_sessions ) {
931+ mag_attempt_session (req_cfg , mc );
932+ }
933+
934+ /* Now set request data and env vars */
935+ mag_set_req_data (req , cfg , mc );
989936
990937 if (req_cfg -> send_persist )
991938 apr_table_set (req -> headers_out , "Persistent-Auth" ,
@@ -996,11 +943,11 @@ static int mag_auth(request_rec *req)
996943done :
997944
998945 if ((auth_type != AUTH_TYPE_BASIC ) && (output .length != 0 )) {
999- int prefixlen = strlen (auth_types [ auth_type ] ) + 1 ;
946+ int prefixlen = strlen (mag_str_auth_type ( auth_type ) ) + 1 ;
1000947 replen = apr_base64_encode_len (output .length ) + 1 ;
1001948 reply = apr_pcalloc (req -> pool , prefixlen + replen );
1002949 if (reply ) {
1003- memcpy (reply , auth_types [ auth_type ] , prefixlen - 1 );
950+ memcpy (reply , mag_str_auth_type ( auth_type ) , prefixlen - 1 );
1004951 reply [prefixlen - 1 ] = ' ' ;
1005952 apr_base64_encode (& reply [prefixlen ], output .value , output .length );
1006953 apr_table_add (req -> err_headers_out , req_cfg -> rep_proto , reply );
0 commit comments