Blame src/windows/ms2mit/ms2mit.c

Packit fd8b60
/* windows/ms2mit/ms2mit.c */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2003 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Export of this software from the United States of America may
Packit fd8b60
 *   require a specific license from the United States Government.
Packit fd8b60
 *   It is the responsibility of any person or organization contemplating
Packit fd8b60
 *   export to obtain such a license before exporting.
Packit fd8b60
 *
Packit fd8b60
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit fd8b60
 * distribute this software and its documentation for any purpose and
Packit fd8b60
 * without fee is hereby granted, provided that the above copyright
Packit fd8b60
 * notice appear in all copies and that both that copyright notice and
Packit fd8b60
 * this permission notice appear in supporting documentation, and that
Packit fd8b60
 * the name of M.I.T. not be used in advertising or publicity pertaining
Packit fd8b60
 * to distribution of the software without specific, written prior
Packit fd8b60
 * permission.  Furthermore if you modify this software you must label
Packit fd8b60
 * your software as modified software and not distribute it in such a
Packit fd8b60
 * fashion that it might be confused with the original M.I.T. software.
Packit fd8b60
 * M.I.T. makes no representations about the suitability of
Packit fd8b60
 * this software for any purpose.  It is provided "as is" without express
Packit fd8b60
 * or implied warranty.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include "k5-int.h"
Packit fd8b60
#include "krb5.h"
Packit fd8b60
#include <stdio.h>
Packit fd8b60
#include <string.h>
Packit fd8b60
#include <time.h>
Packit fd8b60
Packit fd8b60
static char *prog;
Packit fd8b60
Packit fd8b60
static void
Packit fd8b60
xusage(void)
Packit fd8b60
{
Packit fd8b60
    fprintf(stderr, "xusage: %s [-c ccache]\n", prog);
Packit fd8b60
    exit(1);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* Return true if princ is a local (not cross-realm) krbtgt principal. */
Packit fd8b60
krb5_boolean
Packit fd8b60
is_local_tgt(krb5_principal princ)
Packit fd8b60
{
Packit fd8b60
    return princ->length == 2 &&
Packit fd8b60
        data_eq_string(princ->data[0], KRB5_TGS_NAME) &&
Packit fd8b60
        data_eq(princ->realm, princ->data[1]);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Check if a ccache has any tickets.
Packit fd8b60
 */
Packit fd8b60
static krb5_error_code
Packit fd8b60
cc_has_tickets(krb5_context kcontext, krb5_ccache ccache, int *has_tickets)
Packit fd8b60
{
Packit fd8b60
    krb5_error_code code;
Packit fd8b60
    krb5_cc_cursor cursor;
Packit fd8b60
    krb5_creds creds;
Packit fd8b60
    krb5_timestamp now = time(0);
Packit fd8b60
Packit fd8b60
    *has_tickets = 0;
Packit fd8b60
Packit fd8b60
    code = krb5_cc_set_flags(kcontext, ccache, KRB5_TC_NOTICKET);
Packit fd8b60
    if (code)
Packit fd8b60
        return code;
Packit fd8b60
Packit fd8b60
    code = krb5_cc_start_seq_get(kcontext, ccache, &cursor);
Packit fd8b60
    if (code)
Packit fd8b60
        return code;
Packit fd8b60
Packit fd8b60
    while (!*has_tickets) {
Packit fd8b60
        code = krb5_cc_next_cred(kcontext, ccache, &cursor, &creds);
Packit fd8b60
        if (code)
Packit fd8b60
            break;
Packit fd8b60
Packit fd8b60
        if (!krb5_is_config_principal(kcontext, creds.server) &&
Packit fd8b60
            ts_after(creds.times.endtime, now))
Packit fd8b60
            *has_tickets = 1;
Packit fd8b60
Packit fd8b60
        krb5_free_cred_contents(kcontext, &creds);
Packit fd8b60
    }
Packit fd8b60
    krb5_cc_end_seq_get(kcontext, ccache, &cursor);
Packit fd8b60
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
int
Packit fd8b60
main(int argc, char *argv[])
Packit fd8b60
{
Packit fd8b60
    krb5_context kcontext = NULL;
Packit fd8b60
    krb5_error_code code;
Packit fd8b60
    krb5_ccache ccache=NULL;
Packit fd8b60
    krb5_ccache mslsa_ccache=NULL;
Packit fd8b60
    krb5_cc_cursor cursor;
Packit fd8b60
    krb5_creds creds;
Packit fd8b60
    krb5_principal princ = NULL;
Packit fd8b60
    int found_tgt = 0;
Packit fd8b60
    int has_tickets;
Packit fd8b60
    int option;
Packit fd8b60
    char * ccachestr = 0;
Packit fd8b60
Packit fd8b60
    prog = strrchr(argv[0], '/');
Packit fd8b60
    prog = prog ? (prog + 1) : argv[0];
Packit fd8b60
Packit fd8b60
    while ((option = getopt(argc, argv, "c:h")) != -1) {
Packit fd8b60
        switch (option) {
Packit fd8b60
        case 'c':
Packit fd8b60
            ccachestr = optarg;
Packit fd8b60
            break;
Packit fd8b60
        case 'h':
Packit fd8b60
        default:
Packit fd8b60
            xusage();
Packit fd8b60
            break;
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    if (code = krb5_init_context(&kcontext)) {
Packit fd8b60
        com_err(argv[0], code, "while initializing kerberos library");
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    if (code = krb5_cc_resolve(kcontext, "MSLSA:", &mslsa_ccache)) {
Packit fd8b60
        com_err(argv[0], code, "while opening MS LSA ccache");
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* Enumerate tickets from cache looking for a TGT */
Packit fd8b60
    if ((code = krb5_cc_start_seq_get(kcontext, mslsa_ccache, &cursor))) {
Packit fd8b60
        com_err(argv[0], code, "while initiating the cred sequence of MS LSA ccache");
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    while (!found_tgt) {
Packit fd8b60
        code = krb5_cc_next_cred(kcontext, mslsa_ccache, &cursor, &creds);
Packit fd8b60
        if (code)
Packit fd8b60
            break;
Packit fd8b60
Packit fd8b60
        /* Check if the ticket is a TGT */
Packit fd8b60
        if (is_local_tgt(creds.server))
Packit fd8b60
            found_tgt = 1;
Packit fd8b60
Packit fd8b60
        krb5_free_cred_contents(kcontext, &creds);
Packit fd8b60
    }
Packit fd8b60
    krb5_cc_end_seq_get(kcontext, mslsa_ccache, &cursor);
Packit fd8b60
Packit fd8b60
    if (!found_tgt) {
Packit fd8b60
        fprintf(stderr, "%s: Initial Ticket Getting Tickets are not available from the MS LSA\n",
Packit fd8b60
                argv[0]);
Packit fd8b60
        /* Only set the LSA cache as the default if it actually has tickets. */
Packit fd8b60
        code = cc_has_tickets(kcontext, mslsa_ccache, &has_tickets);
Packit fd8b60
        if (code)
Packit fd8b60
            goto cleanup;
Packit fd8b60
Packit fd8b60
        if (has_tickets)
Packit fd8b60
            code = krb5int_cc_user_set_default_name(kcontext, "MSLSA:");
Packit fd8b60
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    if (code = krb5_cc_get_principal(kcontext, mslsa_ccache, &princ)) {
Packit fd8b60
        com_err(argv[0], code, "while obtaining MS LSA principal");
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    if (ccachestr)
Packit fd8b60
        code = krb5_cc_resolve(kcontext, ccachestr, &ccache);
Packit fd8b60
    else
Packit fd8b60
        code = krb5_cc_resolve(kcontext, "API:", &ccache);
Packit fd8b60
    if (code) {
Packit fd8b60
        com_err(argv[0], code, "while getting default ccache");
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
    if (code = krb5_cc_initialize(kcontext, ccache, princ)) {
Packit fd8b60
        com_err (argv[0], code, "when initializing ccache");
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    if (code = krb5_cc_copy_creds(kcontext, mslsa_ccache, ccache)) {
Packit fd8b60
        com_err (argv[0], code, "while copying MS LSA ccache to default ccache");
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* Don't try and set the default cache if the cache name was specified. */
Packit fd8b60
    if (ccachestr == NULL) {
Packit fd8b60
        /* On success set the default cache to API. */
Packit fd8b60
        code = krb5int_cc_user_set_default_name(kcontext, "API:");
Packit fd8b60
        if (code) {
Packit fd8b60
            com_err(argv[0], code, "when setting default to API");
Packit fd8b60
            goto cleanup;
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
cleanup:
Packit fd8b60
    krb5_free_principal(kcontext, princ);
Packit fd8b60
    if (ccache != NULL)
Packit fd8b60
        krb5_cc_close(kcontext, ccache);
Packit fd8b60
    if (mslsa_ccache != NULL)
Packit fd8b60
        krb5_cc_close(kcontext, mslsa_ccache);
Packit fd8b60
    krb5_free_context(kcontext);
Packit fd8b60
    return code ? 1 : 0;
Packit fd8b60
}