Skip to content

Commit 472d605

Browse files
committed
Move setting request data to a separate file
In preparation for the next commit. Signed-off-by: Simo Sorce <simo@redhat.com>
1 parent 855341b commit 472d605

File tree

6 files changed

+94
-90
lines changed

6 files changed

+94
-90
lines changed

src/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ mag_LTLIBRARIES = \
55
mod_auth_gssapi.la
66

77
dist_noinst_HEADERS = \
8-
mod_auth_gssapi.h crypto.h sessions.h
8+
mod_auth_gssapi.h crypto.h sessions.h environ.h
99

1010
mod_auth_gssapi_la_SOURCES = \
11-
mod_auth_gssapi.c crypto.c sessions.c
11+
mod_auth_gssapi.c crypto.c sessions.c environ.c
1212
mod_auth_gssapi_la_CFLAGS = \
1313
$(MAG_CFLAGS)
1414
mod_auth_gssapi_la_LIBADD = \

src/environ.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright (C) 2015 mod_auth_gssapi authors - See COPYING for (C) terms */
2+
3+
#include "mod_auth_gssapi.h"
4+
#include "environ.h"
5+
6+
static void mag_set_KRB5CCANME(request_rec *req, char *ccname)
7+
{
8+
apr_status_t status;
9+
apr_finfo_t finfo;
10+
char *value;
11+
12+
status = apr_stat(&finfo, ccname, APR_FINFO_MIN, req->pool);
13+
if (status != APR_SUCCESS && status != APR_INCOMPLETE) {
14+
/* set the file cache anyway, but warn */
15+
ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
16+
"KRB5CCNAME file (%s) lookup failed!", ccname);
17+
}
18+
19+
value = apr_psprintf(req->pool, "FILE:%s", ccname);
20+
apr_table_set(req->subprocess_env, "KRB5CCNAME", value);
21+
}
22+
23+
void mag_set_req_data(request_rec *req,
24+
struct mag_config *cfg,
25+
struct mag_conn *mc)
26+
{
27+
apr_table_set(req->subprocess_env, "GSS_NAME", mc->gss_name);
28+
apr_table_set(req->subprocess_env, "GSS_SESSION_EXPIRATION",
29+
apr_psprintf(req->pool,
30+
"%ld", (long)mc->expiration));
31+
req->ap_auth_type = apr_pstrdup(req->pool,
32+
mag_str_auth_type(mc->auth_type));
33+
req->user = apr_pstrdup(req->pool, mc->user_name);
34+
if (cfg->deleg_ccache_dir && mc->delegated) {
35+
char *ccname;
36+
ccname = mag_gss_name_to_ccache_name(req,
37+
cfg->deleg_ccache_dir,
38+
mc->gss_name);
39+
if (ccname) {
40+
mag_set_KRB5CCANME(req, ccname);
41+
}
42+
}
43+
}

src/environ.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Copyright (C) 2015 mod_auth_gssapi authors - See COPYING for (C) terms */
2+
3+
struct mag_config;
4+
struct mag_conn;
5+
6+
void mag_set_req_data(request_rec *req,
7+
struct mag_config *cfg,
8+
struct mag_conn *mc);

src/mod_auth_gssapi.c

Lines changed: 35 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
260243
static 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

353316
gss_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

929892
complete:
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)
996943
done:
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);

src/mod_auth_gssapi.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "crypto.h"
3838
#include "sessions.h"
39+
#include "environ.h"
3940

4041
#define MIN_SESS_EXP_TIME 300 /* 5 minutes validity minimum */
4142

@@ -90,8 +91,12 @@ struct mag_conn {
9091
int auth_type;
9192
bool delegated;
9293
struct databuf basic_hash;
94+
bool is_preserved;
9395
};
9496

9597
#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
9698

9799
struct mag_conn *mag_new_conn_ctx(apr_pool_t *pool);
100+
const char *mag_str_auth_type(int auth_type);
101+
char *mag_gss_name_to_ccache_name(request_rec *req,
102+
char *dir, const char *gss_name);

src/sessions.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ void mag_check_session(struct mag_req_cfg *cfg, struct mag_conn **conn)
109109
mc = *conn;
110110
if (!mc) {
111111
*conn = mc = mag_new_conn_ctx(req->pool);
112+
mc->is_preserved = true;
112113
}
113114

114115
rc = mag_session_get(req, sess, MAG_BEARER_KEY, &sessval);

0 commit comments

Comments
 (0)