+ if (strcasecmp(auth_header_type, "Negotiate") == 0) {
+ auth_type = "Negotiate";
+
+ auth_header_value = ap_getword_white(req->pool, &auth_header);
+ if (!auth_header_value) goto done;
+ input.length = apr_base64_decode_len(auth_header_value) + 1;
+ input.value = apr_pcalloc(req->pool, input.length);
+ if (!input.value) goto done;
+ input.length = apr_base64_decode(input.value, auth_header_value);
+ } else if ((strcasecmp(auth_header_type, "Basic") == 0) &&
+ (cfg->use_basic_auth == true)) {
+ auth_type = "Basic";
+ is_basic = true;
+
+ gss_buffer_desc ba_user;
+ gss_buffer_desc ba_pwd;
+
+ ba_pwd.value = ap_pbase64decode(req->pool, auth_header);
+ if (!ba_pwd.value) goto done;
+ ba_user.value = ap_getword_nulls_nc(req->pool,
+ (char **)&ba_pwd.value, ':');
+ if (!ba_user.value) goto done;
+ if (((char *)ba_user.value)[0] == '\0' ||
+ ((char *)ba_pwd.value)[0] == '\0') {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req,
+ "Invalid empty user or password for Basic Auth");
+ goto done;
+ }
+ ba_user.length = strlen(ba_user.value);
+ ba_pwd.length = strlen(ba_pwd.value);
+ maj = gss_import_name(&min, &ba_user, GSS_C_NT_USER_NAME, &client);
+ 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;
+ }
+#ifdef HAVE_GSS_KRB5_CCACHE_NAME
+ /* Set a per-thread ccache in case we are using kerberos,
+ * it is not elegant but avoids interference between threads */
+ long long unsigned int rndname;
+ apr_status_t rs;
+ 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_acquire_cred_with_password(&min, client, &ba_pwd,
+ GSS_C_INDEFINITE,
+ GSS_C_NO_OID_SET,
+ GSS_C_INITIATE,
+ &user_cred, NULL, 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;
+ }
+ gss_release_name(&min, &client);
+ } else {
+ goto done;
+ }
+
+ req->ap_auth_type = apr_pstrdup(req->pool, auth_type);
+
+#ifdef HAVE_GSS_ACQUIRE_CRED_FROM
+ if (cfg->use_s4u2proxy) {
+ cred_usage = GSS_C_BOTH;
+ }
+ if (cfg->cred_store) {
+ maj = gss_acquire_cred_from(&min, GSS_C_NO_NAME, GSS_C_INDEFINITE,
+ GSS_C_NO_OID_SET, cred_usage,
+ cfg->cred_store, &acquired_cred,
+ NULL, NULL);
+ if (GSS_ERROR(maj)) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req, "%s",
+ mag_error(req, "gss_acquire_cred_from() failed",
+ maj, min));
+ goto done;
+ }
+ }
+#endif
+
+ if (is_basic) {
+ if (!acquired_cred) {
+ /* Try to acquire default creds */
+ maj = gss_acquire_cred(&min, GSS_C_NO_NAME, GSS_C_INDEFINITE,
+ GSS_C_NO_OID_SET, cred_usage,
+ &acquired_cred, NULL, NULL);
+ if (GSS_ERROR(maj)) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, req,
+ "%s", mag_error(req, "gss_acquire_cred_from()"
+ " failed", maj, min));
+ goto done;
+ }
+ }
+ maj = gss_inquire_cred(&min, acquired_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));
+ goto done;
+ }
+
+ if (cfg->deleg_ccache_dir) {
+ /* delegate ourselves credentials so we store them as requested */
+ init_flags |= GSS_C_DELEG_FLAG;
+ }