Fix error message copy&paste error
[mod_auth_gssapi.git] / src / sessions.c
1 /* Copyright (C) 2014 mod_auth_gssapi authors - See COPYING for (C) terms */
2
3 #include "mod_auth_gssapi.h"
4
5 APLOG_USE_MODULE(auth_gssapi);
6
7 static APR_OPTIONAL_FN_TYPE(ap_session_load) *mag_sess_load_fn = NULL;
8 static APR_OPTIONAL_FN_TYPE(ap_session_get) *mag_sess_get_fn = NULL;
9 static APR_OPTIONAL_FN_TYPE(ap_session_set) *mag_sess_set_fn = NULL;
10
11 void mag_post_config_session(void)
12 {
13     mag_sess_load_fn = APR_RETRIEVE_OPTIONAL_FN(ap_session_load);
14     mag_sess_get_fn = APR_RETRIEVE_OPTIONAL_FN(ap_session_get);
15     mag_sess_set_fn = APR_RETRIEVE_OPTIONAL_FN(ap_session_set);
16 }
17
18 static apr_status_t mag_session_load(request_rec *req, session_rec **sess)
19 {
20     if (mag_sess_load_fn) {
21         return mag_sess_load_fn(req, sess);
22     }
23     return DECLINED;
24 }
25
26 static apr_status_t mag_session_get(request_rec *req, session_rec *sess,
27                                     const char *key, const char **value)
28 {
29     if (mag_sess_get_fn) {
30         return mag_sess_get_fn(req, sess, key, value);
31     }
32     return DECLINED;
33 }
34
35 static apr_status_t mag_session_set(request_rec *req, session_rec *sess,
36                                     const char *key, const char *value)
37 {
38     if (mag_sess_set_fn) {
39         return mag_sess_set_fn(req, sess, key, value);
40     }
41     return DECLINED;
42 }
43
44 #define MAG_BEARER_KEY "MagBearerToken"
45
46 void mag_check_session(request_rec *req,
47                        struct mag_config *cfg, struct mag_conn **conn)
48 {
49     struct mag_conn *mc;
50     apr_status_t rc;
51     session_rec *sess = NULL;
52     const char *sessval = NULL;
53     int declen;
54     struct databuf ctxbuf = { 0 };
55     struct databuf cipherbuf = { 0 };
56     char *next, *last;
57     time_t expiration;
58
59     rc = mag_session_load(req, &sess);
60     if (rc != OK || sess == NULL) {
61         ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, req,
62                       "Sessions not available, no cookies!");
63         return;
64     }
65
66     mc = *conn;
67     if (!mc) {
68         mc = apr_pcalloc(req->pool, sizeof(struct mag_conn));
69         if (!mc) return;
70
71         mc->parent = req->pool;
72         *conn = mc;
73     }
74
75     rc = mag_session_get(req, sess, MAG_BEARER_KEY, &sessval);
76     if (rc != OK) {
77         ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
78                       "Failed to get session data!");
79         return;
80     }
81     if (!sessval) {
82         /* no session established, just return */
83         return;
84     }
85
86     if (!cfg->mag_skey) {
87         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, req,
88                       "Session key not available, no cookies!");
89         /* we do not have a key, just return */
90         return;
91     }
92
93     /* decode it */
94     declen = apr_base64_decode_len(sessval);
95     cipherbuf.value = apr_palloc(req->pool, declen);
96     if (!cipherbuf.value) return;
97     cipherbuf.length = (int)apr_base64_decode((char *)cipherbuf.value, sessval);
98
99     rc = UNSEAL_BUFFER(req->pool, cfg->mag_skey, &cipherbuf, &ctxbuf);
100     if (rc != OK) {
101         ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
102                       "Failed to unseal session data!");
103         return;
104     }
105
106     /* get time */
107     next = apr_strtok((char *)ctxbuf.value, ":", &last);
108     expiration = (time_t)apr_atoi64(next);
109     if (expiration < time(NULL)) {
110         /* credentials fully expired, return nothing */
111         return;
112     }
113
114     /* user name is next */
115     next = apr_strtok(NULL, ":", &last);
116     mc->user_name = apr_pstrdup(mc->parent, next);
117     if (!mc->user_name) return;
118
119     /* gssapi name (often a principal) is last.
120      * (because it may contain the separator as a valid char we
121      * just read last as is, without further tokenizing */
122     mc->gss_name = apr_pstrdup(mc->parent, last);
123     if (!mc->gss_name) return;
124
125     /* OK we have a valid token */
126     mc->established = true;
127 }
128
129 void mag_attempt_session(request_rec *req,
130                          struct mag_config *cfg, struct mag_conn *mc)
131 {
132     session_rec *sess = NULL;
133     char *sessval = NULL;
134     struct databuf plainbuf = { 0 };
135     struct databuf cipherbuf = { 0 };
136     struct databuf ctxbuf = { 0 };
137     apr_status_t rc;
138
139     /* we save the session only if the authentication is established */
140
141     if (!mc->established) return;
142     rc = mag_session_load(req, &sess);
143     if (rc != OK || sess == NULL) {
144         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, req,
145                       "Sessions not available, can't send cookies!");
146         return;
147     }
148
149     if (!cfg->mag_skey) {
150         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, req,
151                       "Session key not available, generating new one.");
152         rc = SEAL_KEY_CREATE(cfg->pool, &cfg->mag_skey, NULL);
153         if (rc != OK) {
154             ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
155                           "Failed to create sealing key!");
156             return;
157         }
158     }
159
160     sessval = apr_psprintf(req->pool, "%ld:%s:%s",
161                            (long)mc->expiration, mc->user_name, mc->gss_name);
162     if (!sessval) return;
163
164     plainbuf.length = strlen(sessval) + 1;
165     plainbuf.value = (unsigned char *)sessval;
166
167     rc = SEAL_BUFFER(req->pool, cfg->mag_skey, &plainbuf, &cipherbuf);
168     if (rc != OK) {
169         ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
170                       "Failed to seal session data!");
171         return;
172     }
173
174     ctxbuf.length = apr_base64_encode_len(cipherbuf.length);
175     ctxbuf.value = apr_pcalloc(req->pool, ctxbuf.length);
176     if (!ctxbuf.value) return;
177
178     ctxbuf.length = apr_base64_encode((char *)ctxbuf.value,
179                                       (char *)cipherbuf.value,
180                                       cipherbuf.length);
181
182     rc = mag_session_set(req, sess, MAG_BEARER_KEY, (char *)ctxbuf.value);
183     if (rc != OK) {
184         ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
185                       "Failed to set session data!");
186         return;
187     }
188 }
189