|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | +// SPDX-FileCopyrightText: 2025 grommunio GmbH |
| 3 | +// This file is part of Gromox. |
| 4 | +/* |
| 5 | + * Minimal GSS auth helper loosely working similar to |
| 6 | + * `/usr/libexec/squid/negotiate_kerberos_auth -s GSS_C_NO_NAME`. |
| 7 | + * Use for testing with http.cfg:gss_program only. |
| 8 | + */ |
| 9 | +#include <string> |
| 10 | +#include <unistd.h> |
| 11 | +#include <gssapi/gssapi.h> |
| 12 | +#include <libHX/string.h> |
| 13 | +#include <gromox/fileio.h> |
| 14 | + |
| 15 | +using namespace gromox; |
| 16 | + |
| 17 | +int main(int argc, char **argv) |
| 18 | +{ |
| 19 | + gss_cred_id_t srv_creds{}; |
| 20 | + gss_ctx_id_t ctx{}; |
| 21 | + OM_uint32 status{}; |
| 22 | + hxmc_t *line = nullptr; |
| 23 | + setvbuf(stdout, NULL, _IOLBF, 0); |
| 24 | + |
| 25 | + while (HX_getl(&line, stdin)) { |
| 26 | + HX_chomp(line); |
| 27 | + bool yr = line[0] == 'Y' && line[1] == 'R' && line[2] == ' '; |
| 28 | + bool kk = line[0] == 'K' && line[1] == 'K' && line[2] == ' '; |
| 29 | + if (yr) { |
| 30 | + auto ret = gss_acquire_cred(&status, nullptr, |
| 31 | + GSS_C_INDEFINITE, GSS_C_NO_OID_SET, |
| 32 | + GSS_C_ACCEPT, &srv_creds, nullptr, nullptr); |
| 33 | + if (ret != GSS_S_COMPLETE) { |
| 34 | + fprintf(stderr, "BH gss_acquire_cred failed\n"); |
| 35 | + continue; |
| 36 | + } |
| 37 | + } |
| 38 | + if (yr || kk) { |
| 39 | + auto vss = base64_decode(&line[3]); |
| 40 | + gss_buffer_desc input_buf{}, user_buf{}, output_token{}; |
| 41 | + gss_name_t username{}; |
| 42 | + input_buf.value = vss.data(); |
| 43 | + input_buf.length = vss.size(); |
| 44 | + auto ret = gss_accept_sec_context(&status, &ctx, srv_creds, |
| 45 | + &input_buf, GSS_C_NO_CHANNEL_BINDINGS, &username, |
| 46 | + nullptr, &output_token, nullptr, nullptr, |
| 47 | + nullptr); |
| 48 | + if (ret == GSS_S_CONTINUE_NEEDED) { |
| 49 | + std::string_view sv(static_cast<char *>(output_token.value), output_token.length); |
| 50 | + printf("TT %s\n", base64_encode(sv).c_str()); |
| 51 | + continue; |
| 52 | + } else if (ret != 0) { |
| 53 | + fprintf(stderr, "BH gss_accept_sec_context failed\n"); |
| 54 | + return 1; |
| 55 | + } |
| 56 | + ret = gss_display_name(&status, username, &user_buf, nullptr); |
| 57 | + if (ret != 0) { |
| 58 | + fprintf(stderr, "BH no username determined\n"); |
| 59 | + continue; |
| 60 | + } |
| 61 | + std::string sv(static_cast<char *>(user_buf.value), user_buf.length); |
| 62 | + printf("AF = %s\n", sv.c_str()); |
| 63 | + continue; |
| 64 | + } |
| 65 | + printf("BH what?\n"); |
| 66 | + } |
| 67 | + return 0; |
| 68 | +} |
0 commit comments