+static void mag_set_req_data(request_rec *req,
+ struct mag_config *cfg,
+ struct mag_conn *mc)
+{
+ apr_table_set(req->subprocess_env, "GSS_NAME", mc->gss_name);
+ apr_table_set(req->subprocess_env, "GSS_SESSION_EXPIRATION",
+ apr_psprintf(req->pool,
+ "%ld", (long)mc->expiration));
+ req->ap_auth_type = apr_pstrdup(req->pool,
+ auth_types[mc->auth_type]);
+ req->user = apr_pstrdup(req->pool, mc->user_name);
+ if (cfg->deleg_ccache_dir && mc->delegated) {
+ char *ccname;
+ ccname = mag_gss_name_to_ccache_name(req,
+ cfg->deleg_ccache_dir,
+ mc->gss_name);
+ if (ccname) {
+ mag_set_KRB5CCANME(req, ccname);
+ }
+ }
+}
+
+gss_OID_set mag_filter_unwanted_mechs(gss_OID_set src)
+{
+ gss_const_OID unwanted_mechs[] = {
+ &gss_mech_spnego,
+ gss_mech_krb5_old,
+ gss_mech_krb5_wrong,
+ gss_mech_iakerb,
+ GSS_C_NO_OID
+ };
+ gss_OID_set dst;
+ uint32_t maj, min;
+ int present = 0;
+
+ for (int i = 0; unwanted_mechs[i] != GSS_C_NO_OID; i++) {
+ maj = gss_test_oid_set_member(&min,
+ discard_const(unwanted_mechs[i]),
+ src, &present);
+ if (present) break;
+ }
+ if (present) {
+ maj = gss_create_empty_oid_set(&min, &dst);
+ if (maj != GSS_S_COMPLETE) {
+ return GSS_C_NO_OID_SET;
+ }
+ for (int i = 0; i < src->count; i++) {
+ present = 0;
+ for (int j = 0; unwanted_mechs[j] != GSS_C_NO_OID; j++) {
+ if (gss_oid_equal(&src->elements[i], unwanted_mechs[j])) {
+ present = 1;
+ break;
+ }
+ }
+ if (present) continue;
+ maj = gss_add_oid_set_member(&min, &src->elements[i], &dst);
+ if (maj != GSS_S_COMPLETE) {
+ gss_release_oid_set(&min, &dst);
+ return GSS_C_NO_OID_SET;
+ }
+ }
+ return dst;
+ }
+ return src;
+}
+
+static bool mag_auth_basic(request_rec *req,
+ struct mag_config *cfg,
+ gss_buffer_desc ba_user,
+ gss_buffer_desc ba_pwd,
+ gss_cred_usage_t cred_usage,
+ gss_name_t *client,
+ gss_OID *mech_type,
+ gss_cred_id_t *delegated_cred,
+ uint32_t *vtime)
+{
+#ifdef HAVE_GSS_KRB5_CCACHE_NAME
+ const char *user_ccache = NULL;
+ const char *orig_ccache = NULL;
+ long long unsigned int rndname;
+ apr_status_t rs;
+#endif
+ gss_name_t user = GSS_C_NO_NAME;
+ gss_cred_id_t user_cred = GSS_C_NO_CREDENTIAL;
+ gss_ctx_id_t user_ctx = GSS_C_NO_CONTEXT;
+ gss_name_t server = GSS_C_NO_NAME;
+ gss_cred_id_t server_cred = GSS_C_NO_CREDENTIAL;
+ gss_ctx_id_t server_ctx = GSS_C_NO_CONTEXT;
+ gss_buffer_desc input = GSS_C_EMPTY_BUFFER;
+ gss_buffer_desc output = GSS_C_EMPTY_BUFFER;
+ gss_OID_set indicated_mechs = GSS_C_NO_OID_SET;
+ gss_OID_set allowed_mechs;
+ gss_OID_set filtered_mechs;
+ gss_OID_set_desc all_mechs_desc;
+ gss_OID_set actual_mechs = GSS_C_NO_OID_SET;
+ uint32_t init_flags = 0;
+ uint32_t maj, min;
+ bool ret = false;
+
+#ifdef HAVE_GSS_KRB5_CCACHE_NAME
+ rs = apr_generate_random_bytes((unsigned char *)(&rndname),
+ sizeof(long long unsigned int));
+ if (rs != APR_SUCCESS) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req,
+ "Failed to generate random ccache name");
+ goto done;
+ }
+ user_ccache = apr_psprintf(req->pool, "MEMORY:user_%qu", rndname);
+ maj = gss_krb5_ccache_name(&min, user_ccache, &orig_ccache);
+ if (GSS_ERROR(maj)) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req,
+ "In Basic Auth, %s",
+ mag_error(req, "gss_krb5_ccache_name() "
+ "failed", maj, min));
+ goto done;
+ }
+#endif
+
+ maj = gss_import_name(&min, &ba_user, GSS_C_NT_USER_NAME, &user);
+ if (GSS_ERROR(maj)) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req,
+ "In Basic Auth, %s",
+ mag_error(req, "gss_import_name() failed",
+ maj, min));
+ goto done;
+ }
+
+ if (cfg->basic_mechs) {
+ allowed_mechs = cfg->basic_mechs;
+ } else if (cfg->allowed_mechs) {
+ allowed_mechs = cfg->allowed_mechs;
+ } else {
+ /* Try to fetch the default set if not explicitly configured,
+ * We need to do this because gss_acquire_cred_with_password()
+ * is currently limited to acquire creds for a single "default"
+ * mechanism if no desired mechanisms are passed in. This causes
+ * authentication to fail for secondary mechanisms as no user
+ * credentials are generated for those. */
+ maj = gss_indicate_mechs(&min, &indicated_mechs);
+ if (maj != GSS_S_COMPLETE) {
+ ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, req, "%s",
+ mag_error(req, "gss_indicate_mechs() failed",
+ maj, min));
+ /* if indicated _mechs failed, set GSS_C_NO_OID_SET. This
+ * generally causes only the krb5 mechanism to be tried due
+ * to implementation constraints, but may change in future. */
+ allowed_mechs = GSS_C_NO_OID_SET;
+ } else {
+ allowed_mechs = indicated_mechs;
+ }
+ }
+
+ /* Remove Spnego if present, or we'd repeat failed authentiations
+ * multiple times, one within Spnego and then again with an explicit
+ * mechanism. We would normally just force Spnego and use
+ * gss_set_neg_mechs, but due to the way we source the server name
+ * and the fact MIT up to 1.14 at least does no handle union names,
+ * we can't provide spnego with a server name that can be used by
+ * multiple mechanisms, causing any but the first mechanism to fail.
+ * Also remove unwanted krb mechs, or AS requests will be repeated
+ * multiple times uselessly.
+ */
+ filtered_mechs = mag_filter_unwanted_mechs(allowed_mechs);
+ if (filtered_mechs == GSS_C_NO_OID_SET) {
+ ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, req, "Fatal "
+ "failure while filtering mechs, aborting");
+ goto done;
+ } else if (filtered_mechs != allowed_mechs) {
+ /* if indicated_mechs where sourced then free them here before
+ * reusing the pointer */
+ gss_release_oid_set(&min, &indicated_mechs);
+
+ /* mark the list of mechs needs to be freed */
+ indicated_mechs = filtered_mechs;
+
+ /* use the filtered list */
+ allowed_mechs = filtered_mechs;
+ }
+
+ maj = gss_acquire_cred_with_password(&min, user, &ba_pwd,
+ GSS_C_INDEFINITE,
+ allowed_mechs,
+ GSS_C_INITIATE,
+ &user_cred, &actual_mechs, NULL);
+ if (GSS_ERROR(maj)) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req,
+ "In Basic Auth, %s",
+ mag_error(req, "gss_acquire_cred_with_password() "
+ "failed", maj, min));
+ goto done;
+ }
+
+#ifdef HAVE_CRED_STORE
+ if (cfg->deleg_ccache_dir) {
+ /* delegate ourselves credentials so we store them as requested */
+ init_flags |= GSS_C_DELEG_FLAG;
+ }
+#endif
+
+ for (int i = 0; i < actual_mechs->count; i++) {
+
+ /* skip spnego if present */
+ if (gss_oid_equal(&actual_mechs->elements[i],
+ &gss_mech_spnego)) {
+ continue;
+ }
+
+ /* free these if looping */
+ gss_release_buffer(&min, &output);
+ gss_release_buffer(&min, &input);
+ gss_release_name(&min, &server);
+ gss_release_cred(&min, &server_cred);
+
+ all_mechs_desc.count = 1;
+ all_mechs_desc.elements = &actual_mechs->elements[i];
+ allowed_mechs = &all_mechs_desc;
+
+ /* must acquire with GSS_C_ACCEPT to get the server name */
+ if (!mag_acquire_creds(req, cfg, allowed_mechs,
+ GSS_C_ACCEPT, &server_cred, NULL)) {
+ continue;
+ }
+ maj = gss_inquire_cred(&min, server_cred, &server,
+ NULL, NULL, NULL);
+ if (GSS_ERROR(maj)) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req,
+ "%s", mag_error(req, "gss_inquired_cred_() "
+ "failed", maj, min));
+ continue;
+ }
+
+ if (cred_usage == GSS_C_BOTH) {
+ /* reacquire server creds in order to allow delegation */
+ gss_release_cred(&min, &server_cred);
+ if (!mag_acquire_creds(req, cfg, allowed_mechs,
+ GSS_C_BOTH, &server_cred, NULL)) {
+ continue;
+ }
+ }
+
+ do {
+ /* output and input are inverted here, this is intentional */
+ maj = gss_init_sec_context(&min, user_cred, &user_ctx, server,
+ &actual_mechs->elements[i], init_flags,
+ 300, GSS_C_NO_CHANNEL_BINDINGS, &output,
+ NULL, &input, NULL, NULL);
+ if (GSS_ERROR(maj)) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req,
+ "%s", mag_error(req, "gss_init_sec_context() "
+ "failed", maj, min));
+ break;
+ }
+ gss_release_buffer(&min, &output);
+ maj = gss_accept_sec_context(&min, &server_ctx, server_cred,
+ &input, GSS_C_NO_CHANNEL_BINDINGS,
+ client, mech_type, &output, NULL,
+ vtime, delegated_cred);
+ if (GSS_ERROR(maj)) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req,
+ "%s", mag_error(req, "gss_accept_sec_context()"
+ " failed", maj, min));
+ break;
+ }
+ gss_release_buffer(&min, &input);
+ } while (maj == GSS_S_CONTINUE_NEEDED);
+
+ if (maj == GSS_S_COMPLETE) {
+ ret = true;
+ break;
+ }
+ }
+
+done:
+ gss_release_buffer(&min, &output);
+ gss_release_buffer(&min, &input);
+ gss_release_name(&min, &server);
+ gss_release_cred(&min, &server_cred);
+ gss_delete_sec_context(&min, &server_ctx, GSS_C_NO_BUFFER);
+ gss_release_name(&min, &user);
+ gss_release_cred(&min, &user_cred);
+ gss_delete_sec_context(&min, &user_ctx, GSS_C_NO_BUFFER);
+ gss_release_oid_set(&min, &actual_mechs);
+ gss_release_oid_set(&min, &indicated_mechs);
+#ifdef HAVE_GSS_KRB5_CCACHE_NAME
+ if (user_ccache != NULL) {
+ maj = gss_krb5_ccache_name(&min, orig_ccache, NULL);
+ if (maj != GSS_S_COMPLETE) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req,
+ "Failed to restore per-thread ccache, %s",
+ mag_error(req, "gss_krb5_ccache_name() "
+ "failed", maj, min));
+ }
+ }
+#endif
+ return ret;
+}
+
+