Path: blob/main/crypto/krb5/src/windows/ms2mit/ms2mit.c
34907 views
/* windows/ms2mit/ms2mit.c */1/*2* Copyright (C) 2003 by the Massachusetts Institute of Technology.3* All rights reserved.4*5* Export of this software from the United States of America may6* require a specific license from the United States Government.7* It is the responsibility of any person or organization contemplating8* export to obtain such a license before exporting.9*10* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and11* distribute this software and its documentation for any purpose and12* without fee is hereby granted, provided that the above copyright13* notice appear in all copies and that both that copyright notice and14* this permission notice appear in supporting documentation, and that15* the name of M.I.T. not be used in advertising or publicity pertaining16* to distribution of the software without specific, written prior17* permission. Furthermore if you modify this software you must label18* your software as modified software and not distribute it in such a19* fashion that it might be confused with the original M.I.T. software.20* M.I.T. makes no representations about the suitability of21* this software for any purpose. It is provided "as is" without express22* or implied warranty.23*/2425#include "k5-int.h"26#include "krb5.h"27#include <stdio.h>28#include <string.h>29#include <time.h>3031static char *prog;3233static void34xusage(void)35{36fprintf(stderr, "xusage: %s [-c ccache]\n", prog);37exit(1);38}3940/* Return true if princ is a local (not cross-realm) krbtgt principal. */41krb5_boolean42is_local_tgt(krb5_principal princ)43{44return princ->length == 2 &&45data_eq_string(princ->data[0], KRB5_TGS_NAME) &&46data_eq(princ->realm, princ->data[1]);47}4849/*50* Check if a ccache has any tickets.51*/52static krb5_error_code53cc_has_tickets(krb5_context kcontext, krb5_ccache ccache, int *has_tickets)54{55krb5_error_code code;56krb5_cc_cursor cursor;57krb5_creds creds;58krb5_timestamp now = time(0);5960*has_tickets = 0;6162code = krb5_cc_set_flags(kcontext, ccache, KRB5_TC_NOTICKET);63if (code)64return code;6566code = krb5_cc_start_seq_get(kcontext, ccache, &cursor);67if (code)68return code;6970while (!*has_tickets) {71code = krb5_cc_next_cred(kcontext, ccache, &cursor, &creds);72if (code)73break;7475if (!krb5_is_config_principal(kcontext, creds.server) &&76ts_after(creds.times.endtime, now))77*has_tickets = 1;7879krb5_free_cred_contents(kcontext, &creds);80}81krb5_cc_end_seq_get(kcontext, ccache, &cursor);8283return 0;84}8586int87main(int argc, char *argv[])88{89krb5_context kcontext = NULL;90krb5_error_code code;91krb5_ccache ccache=NULL;92krb5_ccache mslsa_ccache=NULL;93krb5_cc_cursor cursor;94krb5_creds creds;95krb5_principal princ = NULL;96int found_tgt = 0;97int has_tickets;98int option;99char * ccachestr = 0;100101prog = strrchr(argv[0], '/');102prog = prog ? (prog + 1) : argv[0];103104while ((option = getopt(argc, argv, "c:h")) != -1) {105switch (option) {106case 'c':107ccachestr = optarg;108break;109case 'h':110default:111xusage();112break;113}114}115116if (code = krb5_init_context(&kcontext)) {117com_err(argv[0], code, "while initializing kerberos library");118goto cleanup;119}120121if (code = krb5_cc_resolve(kcontext, "MSLSA:", &mslsa_ccache)) {122com_err(argv[0], code, "while opening MS LSA ccache");123goto cleanup;124}125126/* Enumerate tickets from cache looking for a TGT */127if ((code = krb5_cc_start_seq_get(kcontext, mslsa_ccache, &cursor))) {128com_err(argv[0], code, "while initiating the cred sequence of MS LSA ccache");129goto cleanup;130}131132while (!found_tgt) {133code = krb5_cc_next_cred(kcontext, mslsa_ccache, &cursor, &creds);134if (code)135break;136137/* Check if the ticket is a TGT */138if (is_local_tgt(creds.server))139found_tgt = 1;140141krb5_free_cred_contents(kcontext, &creds);142}143krb5_cc_end_seq_get(kcontext, mslsa_ccache, &cursor);144145if (!found_tgt) {146fprintf(stderr, "%s: Initial Ticket Getting Tickets are not available from the MS LSA\n",147argv[0]);148/* Only set the LSA cache as the default if it actually has tickets. */149code = cc_has_tickets(kcontext, mslsa_ccache, &has_tickets);150if (code)151goto cleanup;152153if (has_tickets)154code = krb5int_cc_user_set_default_name(kcontext, "MSLSA:");155156goto cleanup;157}158159if (code = krb5_cc_get_principal(kcontext, mslsa_ccache, &princ)) {160com_err(argv[0], code, "while obtaining MS LSA principal");161goto cleanup;162}163164if (ccachestr)165code = krb5_cc_resolve(kcontext, ccachestr, &ccache);166else167code = krb5_cc_resolve(kcontext, "API:", &ccache);168if (code) {169com_err(argv[0], code, "while getting default ccache");170goto cleanup;171}172if (code = krb5_cc_initialize(kcontext, ccache, princ)) {173com_err (argv[0], code, "when initializing ccache");174goto cleanup;175}176177if (code = krb5_cc_copy_creds(kcontext, mslsa_ccache, ccache)) {178com_err (argv[0], code, "while copying MS LSA ccache to default ccache");179goto cleanup;180}181182/* Don't try and set the default cache if the cache name was specified. */183if (ccachestr == NULL) {184/* On success set the default cache to API. */185code = krb5int_cc_user_set_default_name(kcontext, "API:");186if (code) {187com_err(argv[0], code, "when setting default to API");188goto cleanup;189}190}191192cleanup:193krb5_free_principal(kcontext, princ);194if (ccache != NULL)195krb5_cc_close(kcontext, ccache);196if (mslsa_ccache != NULL)197krb5_cc_close(kcontext, mslsa_ccache);198krb5_free_context(kcontext);199return code ? 1 : 0;200}201202203