xref: /freebsd/crypto/krb5/src/windows/ms2mit/ms2mit.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* windows/ms2mit/ms2mit.c */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright (C) 2003 by the Massachusetts Institute of Technology.
4*7f2fe78bSCy Schubert  * All rights reserved.
5*7f2fe78bSCy Schubert  *
6*7f2fe78bSCy Schubert  * Export of this software from the United States of America may
7*7f2fe78bSCy Schubert  *   require a specific license from the United States Government.
8*7f2fe78bSCy Schubert  *   It is the responsibility of any person or organization contemplating
9*7f2fe78bSCy Schubert  *   export to obtain such a license before exporting.
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12*7f2fe78bSCy Schubert  * distribute this software and its documentation for any purpose and
13*7f2fe78bSCy Schubert  * without fee is hereby granted, provided that the above copyright
14*7f2fe78bSCy Schubert  * notice appear in all copies and that both that copyright notice and
15*7f2fe78bSCy Schubert  * this permission notice appear in supporting documentation, and that
16*7f2fe78bSCy Schubert  * the name of M.I.T. not be used in advertising or publicity pertaining
17*7f2fe78bSCy Schubert  * to distribution of the software without specific, written prior
18*7f2fe78bSCy Schubert  * permission.  Furthermore if you modify this software you must label
19*7f2fe78bSCy Schubert  * your software as modified software and not distribute it in such a
20*7f2fe78bSCy Schubert  * fashion that it might be confused with the original M.I.T. software.
21*7f2fe78bSCy Schubert  * M.I.T. makes no representations about the suitability of
22*7f2fe78bSCy Schubert  * this software for any purpose.  It is provided "as is" without express
23*7f2fe78bSCy Schubert  * or implied warranty.
24*7f2fe78bSCy Schubert  */
25*7f2fe78bSCy Schubert 
26*7f2fe78bSCy Schubert #include "k5-int.h"
27*7f2fe78bSCy Schubert #include "krb5.h"
28*7f2fe78bSCy Schubert #include <stdio.h>
29*7f2fe78bSCy Schubert #include <string.h>
30*7f2fe78bSCy Schubert #include <time.h>
31*7f2fe78bSCy Schubert 
32*7f2fe78bSCy Schubert static char *prog;
33*7f2fe78bSCy Schubert 
34*7f2fe78bSCy Schubert static void
xusage(void)35*7f2fe78bSCy Schubert xusage(void)
36*7f2fe78bSCy Schubert {
37*7f2fe78bSCy Schubert     fprintf(stderr, "xusage: %s [-c ccache]\n", prog);
38*7f2fe78bSCy Schubert     exit(1);
39*7f2fe78bSCy Schubert }
40*7f2fe78bSCy Schubert 
41*7f2fe78bSCy Schubert /* Return true if princ is a local (not cross-realm) krbtgt principal. */
42*7f2fe78bSCy Schubert krb5_boolean
is_local_tgt(krb5_principal princ)43*7f2fe78bSCy Schubert is_local_tgt(krb5_principal princ)
44*7f2fe78bSCy Schubert {
45*7f2fe78bSCy Schubert     return princ->length == 2 &&
46*7f2fe78bSCy Schubert         data_eq_string(princ->data[0], KRB5_TGS_NAME) &&
47*7f2fe78bSCy Schubert         data_eq(princ->realm, princ->data[1]);
48*7f2fe78bSCy Schubert }
49*7f2fe78bSCy Schubert 
50*7f2fe78bSCy Schubert /*
51*7f2fe78bSCy Schubert  * Check if a ccache has any tickets.
52*7f2fe78bSCy Schubert  */
53*7f2fe78bSCy Schubert static krb5_error_code
cc_has_tickets(krb5_context kcontext,krb5_ccache ccache,int * has_tickets)54*7f2fe78bSCy Schubert cc_has_tickets(krb5_context kcontext, krb5_ccache ccache, int *has_tickets)
55*7f2fe78bSCy Schubert {
56*7f2fe78bSCy Schubert     krb5_error_code code;
57*7f2fe78bSCy Schubert     krb5_cc_cursor cursor;
58*7f2fe78bSCy Schubert     krb5_creds creds;
59*7f2fe78bSCy Schubert     krb5_timestamp now = time(0);
60*7f2fe78bSCy Schubert 
61*7f2fe78bSCy Schubert     *has_tickets = 0;
62*7f2fe78bSCy Schubert 
63*7f2fe78bSCy Schubert     code = krb5_cc_set_flags(kcontext, ccache, KRB5_TC_NOTICKET);
64*7f2fe78bSCy Schubert     if (code)
65*7f2fe78bSCy Schubert         return code;
66*7f2fe78bSCy Schubert 
67*7f2fe78bSCy Schubert     code = krb5_cc_start_seq_get(kcontext, ccache, &cursor);
68*7f2fe78bSCy Schubert     if (code)
69*7f2fe78bSCy Schubert         return code;
70*7f2fe78bSCy Schubert 
71*7f2fe78bSCy Schubert     while (!*has_tickets) {
72*7f2fe78bSCy Schubert         code = krb5_cc_next_cred(kcontext, ccache, &cursor, &creds);
73*7f2fe78bSCy Schubert         if (code)
74*7f2fe78bSCy Schubert             break;
75*7f2fe78bSCy Schubert 
76*7f2fe78bSCy Schubert         if (!krb5_is_config_principal(kcontext, creds.server) &&
77*7f2fe78bSCy Schubert             ts_after(creds.times.endtime, now))
78*7f2fe78bSCy Schubert             *has_tickets = 1;
79*7f2fe78bSCy Schubert 
80*7f2fe78bSCy Schubert         krb5_free_cred_contents(kcontext, &creds);
81*7f2fe78bSCy Schubert     }
82*7f2fe78bSCy Schubert     krb5_cc_end_seq_get(kcontext, ccache, &cursor);
83*7f2fe78bSCy Schubert 
84*7f2fe78bSCy Schubert     return 0;
85*7f2fe78bSCy Schubert }
86*7f2fe78bSCy Schubert 
87*7f2fe78bSCy Schubert int
main(int argc,char * argv[])88*7f2fe78bSCy Schubert main(int argc, char *argv[])
89*7f2fe78bSCy Schubert {
90*7f2fe78bSCy Schubert     krb5_context kcontext = NULL;
91*7f2fe78bSCy Schubert     krb5_error_code code;
92*7f2fe78bSCy Schubert     krb5_ccache ccache=NULL;
93*7f2fe78bSCy Schubert     krb5_ccache mslsa_ccache=NULL;
94*7f2fe78bSCy Schubert     krb5_cc_cursor cursor;
95*7f2fe78bSCy Schubert     krb5_creds creds;
96*7f2fe78bSCy Schubert     krb5_principal princ = NULL;
97*7f2fe78bSCy Schubert     int found_tgt = 0;
98*7f2fe78bSCy Schubert     int has_tickets;
99*7f2fe78bSCy Schubert     int option;
100*7f2fe78bSCy Schubert     char * ccachestr = 0;
101*7f2fe78bSCy Schubert 
102*7f2fe78bSCy Schubert     prog = strrchr(argv[0], '/');
103*7f2fe78bSCy Schubert     prog = prog ? (prog + 1) : argv[0];
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert     while ((option = getopt(argc, argv, "c:h")) != -1) {
106*7f2fe78bSCy Schubert         switch (option) {
107*7f2fe78bSCy Schubert         case 'c':
108*7f2fe78bSCy Schubert             ccachestr = optarg;
109*7f2fe78bSCy Schubert             break;
110*7f2fe78bSCy Schubert         case 'h':
111*7f2fe78bSCy Schubert         default:
112*7f2fe78bSCy Schubert             xusage();
113*7f2fe78bSCy Schubert             break;
114*7f2fe78bSCy Schubert         }
115*7f2fe78bSCy Schubert     }
116*7f2fe78bSCy Schubert 
117*7f2fe78bSCy Schubert     if (code = krb5_init_context(&kcontext)) {
118*7f2fe78bSCy Schubert         com_err(argv[0], code, "while initializing kerberos library");
119*7f2fe78bSCy Schubert         goto cleanup;
120*7f2fe78bSCy Schubert     }
121*7f2fe78bSCy Schubert 
122*7f2fe78bSCy Schubert     if (code = krb5_cc_resolve(kcontext, "MSLSA:", &mslsa_ccache)) {
123*7f2fe78bSCy Schubert         com_err(argv[0], code, "while opening MS LSA ccache");
124*7f2fe78bSCy Schubert         goto cleanup;
125*7f2fe78bSCy Schubert     }
126*7f2fe78bSCy Schubert 
127*7f2fe78bSCy Schubert     /* Enumerate tickets from cache looking for a TGT */
128*7f2fe78bSCy Schubert     if ((code = krb5_cc_start_seq_get(kcontext, mslsa_ccache, &cursor))) {
129*7f2fe78bSCy Schubert         com_err(argv[0], code, "while initiating the cred sequence of MS LSA ccache");
130*7f2fe78bSCy Schubert         goto cleanup;
131*7f2fe78bSCy Schubert     }
132*7f2fe78bSCy Schubert 
133*7f2fe78bSCy Schubert     while (!found_tgt) {
134*7f2fe78bSCy Schubert         code = krb5_cc_next_cred(kcontext, mslsa_ccache, &cursor, &creds);
135*7f2fe78bSCy Schubert         if (code)
136*7f2fe78bSCy Schubert             break;
137*7f2fe78bSCy Schubert 
138*7f2fe78bSCy Schubert         /* Check if the ticket is a TGT */
139*7f2fe78bSCy Schubert         if (is_local_tgt(creds.server))
140*7f2fe78bSCy Schubert             found_tgt = 1;
141*7f2fe78bSCy Schubert 
142*7f2fe78bSCy Schubert         krb5_free_cred_contents(kcontext, &creds);
143*7f2fe78bSCy Schubert     }
144*7f2fe78bSCy Schubert     krb5_cc_end_seq_get(kcontext, mslsa_ccache, &cursor);
145*7f2fe78bSCy Schubert 
146*7f2fe78bSCy Schubert     if (!found_tgt) {
147*7f2fe78bSCy Schubert         fprintf(stderr, "%s: Initial Ticket Getting Tickets are not available from the MS LSA\n",
148*7f2fe78bSCy Schubert                 argv[0]);
149*7f2fe78bSCy Schubert         /* Only set the LSA cache as the default if it actually has tickets. */
150*7f2fe78bSCy Schubert         code = cc_has_tickets(kcontext, mslsa_ccache, &has_tickets);
151*7f2fe78bSCy Schubert         if (code)
152*7f2fe78bSCy Schubert             goto cleanup;
153*7f2fe78bSCy Schubert 
154*7f2fe78bSCy Schubert         if (has_tickets)
155*7f2fe78bSCy Schubert             code = krb5int_cc_user_set_default_name(kcontext, "MSLSA:");
156*7f2fe78bSCy Schubert 
157*7f2fe78bSCy Schubert         goto cleanup;
158*7f2fe78bSCy Schubert     }
159*7f2fe78bSCy Schubert 
160*7f2fe78bSCy Schubert     if (code = krb5_cc_get_principal(kcontext, mslsa_ccache, &princ)) {
161*7f2fe78bSCy Schubert         com_err(argv[0], code, "while obtaining MS LSA principal");
162*7f2fe78bSCy Schubert         goto cleanup;
163*7f2fe78bSCy Schubert     }
164*7f2fe78bSCy Schubert 
165*7f2fe78bSCy Schubert     if (ccachestr)
166*7f2fe78bSCy Schubert         code = krb5_cc_resolve(kcontext, ccachestr, &ccache);
167*7f2fe78bSCy Schubert     else
168*7f2fe78bSCy Schubert         code = krb5_cc_resolve(kcontext, "API:", &ccache);
169*7f2fe78bSCy Schubert     if (code) {
170*7f2fe78bSCy Schubert         com_err(argv[0], code, "while getting default ccache");
171*7f2fe78bSCy Schubert         goto cleanup;
172*7f2fe78bSCy Schubert     }
173*7f2fe78bSCy Schubert     if (code = krb5_cc_initialize(kcontext, ccache, princ)) {
174*7f2fe78bSCy Schubert         com_err (argv[0], code, "when initializing ccache");
175*7f2fe78bSCy Schubert         goto cleanup;
176*7f2fe78bSCy Schubert     }
177*7f2fe78bSCy Schubert 
178*7f2fe78bSCy Schubert     if (code = krb5_cc_copy_creds(kcontext, mslsa_ccache, ccache)) {
179*7f2fe78bSCy Schubert         com_err (argv[0], code, "while copying MS LSA ccache to default ccache");
180*7f2fe78bSCy Schubert         goto cleanup;
181*7f2fe78bSCy Schubert     }
182*7f2fe78bSCy Schubert 
183*7f2fe78bSCy Schubert     /* Don't try and set the default cache if the cache name was specified. */
184*7f2fe78bSCy Schubert     if (ccachestr == NULL) {
185*7f2fe78bSCy Schubert         /* On success set the default cache to API. */
186*7f2fe78bSCy Schubert         code = krb5int_cc_user_set_default_name(kcontext, "API:");
187*7f2fe78bSCy Schubert         if (code) {
188*7f2fe78bSCy Schubert             com_err(argv[0], code, "when setting default to API");
189*7f2fe78bSCy Schubert             goto cleanup;
190*7f2fe78bSCy Schubert         }
191*7f2fe78bSCy Schubert     }
192*7f2fe78bSCy Schubert 
193*7f2fe78bSCy Schubert cleanup:
194*7f2fe78bSCy Schubert     krb5_free_principal(kcontext, princ);
195*7f2fe78bSCy Schubert     if (ccache != NULL)
196*7f2fe78bSCy Schubert         krb5_cc_close(kcontext, ccache);
197*7f2fe78bSCy Schubert     if (mslsa_ccache != NULL)
198*7f2fe78bSCy Schubert         krb5_cc_close(kcontext, mslsa_ccache);
199*7f2fe78bSCy Schubert     krb5_free_context(kcontext);
200*7f2fe78bSCy Schubert     return code ? 1 : 0;
201*7f2fe78bSCy Schubert }
202