xref: /freebsd/crypto/krb5/src/windows/ms2mit/mit2ms.c (revision 24e4dcf4ba5e9dedcf89efd358ea3e1fe5867020)
1 /* windows/ms2mit/mit2ms.c */
2 /*
3  * Copyright (C) 2003,2004 by the Massachusetts Institute of Technology.
4  * All rights reserved.
5  *
6  * Export of this software from the United States of America may
7  *   require a specific license from the United States Government.
8  *   It is the responsibility of any person or organization contemplating
9  *   export to obtain such a license before exporting.
10  *
11  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12  * distribute this software and its documentation for any purpose and
13  * without fee is hereby granted, provided that the above copyright
14  * notice appear in all copies and that both that copyright notice and
15  * this permission notice appear in supporting documentation, and that
16  * the name of M.I.T. not be used in advertising or publicity pertaining
17  * to distribution of the software without specific, written prior
18  * permission.  Furthermore if you modify this software you must label
19  * your software as modified software and not distribute it in such a
20  * fashion that it might be confused with the original M.I.T. software.
21  * M.I.T. makes no representations about the suitability of
22  * this software for any purpose.  It is provided "as is" without express
23  * or implied warranty.
24  */
25 
26 #include "krb5.h"
27 #include <stdio.h>
28 #include <string.h>
29 #include "k5-platform.h"
30 
31 static char *prog;
32 
33 static void
34 xusage(void)
35 {
36     fprintf(stderr, "xusage: %s [-c ccache]\n", prog);
37     exit(1);
38 }
39 
40 void
41 main(
42     int argc,
43     char *argv[]
44     )
45 {
46     krb5_context kcontext;
47     krb5_error_code code;
48     krb5_ccache ccache=NULL;
49     krb5_ccache mslsa_ccache=NULL;
50     krb5_cc_cursor cursor;
51     krb5_creds creds;
52     krb5_principal princ;
53     int initial_ticket = 0;
54     int option;
55     char * ccachestr = 0;
56 
57     prog = strrchr(argv[0], '/');
58     prog = prog ? (prog + 1) : argv[0];
59 
60     while ((option = getopt(argc, argv, "c:h")) != -1) {
61         switch (option) {
62         case 'c':
63             ccachestr = optarg;
64             break;
65         case 'h':
66         default:
67             xusage();
68             break;
69         }
70     }
71 
72     if (code = krb5_init_context(&kcontext)) {
73         com_err(argv[0], code, "while initializing kerberos library");
74         exit(1);
75     }
76 
77     if (ccachestr)
78         code = krb5_cc_resolve(kcontext, ccachestr, &ccache);
79     else
80         code = krb5_cc_default(kcontext, &ccache);
81     if (code) {
82         com_err(argv[0], code, "while getting default ccache");
83         krb5_free_principal(kcontext, princ);
84         krb5_free_context(kcontext);
85         exit(1);
86     }
87 
88     /* Enumerate tickets from cache looking for an initial ticket */
89     if ((code = krb5_cc_start_seq_get(kcontext, ccache, &cursor))) {
90         com_err(argv[0], code, "while initiating the cred sequence of MS LSA ccache");
91         krb5_cc_close(kcontext, ccache);
92         krb5_free_context(kcontext);
93         exit(1);
94     }
95 
96     while (!(code = krb5_cc_next_cred(kcontext, ccache, &cursor, &creds)))
97     {
98         if ( creds.ticket_flags & TKT_FLG_INITIAL ) {
99             krb5_free_cred_contents(kcontext, &creds);
100             initial_ticket = 1;
101             break;
102         }
103         krb5_free_cred_contents(kcontext, &creds);
104     }
105     krb5_cc_end_seq_get(kcontext, ccache, &cursor);
106 
107     if ( !initial_ticket ) {
108         fprintf(stderr, "%s: Initial Ticket Getting Tickets are not available from the MIT default cache\n",
109                 argv[0]);
110         krb5_cc_close(kcontext, ccache);
111         krb5_free_context(kcontext);
112         exit(1);
113     }
114 
115     if (code = krb5_cc_get_principal(kcontext, ccache, &princ)) {
116         com_err(argv[0], code, "while obtaining default MIT principal");
117         krb5_cc_close(kcontext, ccache);
118         krb5_free_context(kcontext);
119         exit(1);
120     }
121 
122     if (code = krb5_cc_resolve(kcontext, "MSLSA:", &mslsa_ccache)) {
123         com_err(argv[0], code, "while opening MS LSA ccache");
124         krb5_cc_close(kcontext, ccache);
125         krb5_free_context(kcontext);
126         exit(1);
127     }
128 
129     if (code = krb5_cc_copy_creds(kcontext, ccache, mslsa_ccache)) {
130         com_err (argv[0], code, "while copying default MIT ccache to MSLSA ccache");
131         krb5_free_principal(kcontext, princ);
132         krb5_cc_close(kcontext, ccache);
133         krb5_cc_close(kcontext, mslsa_ccache);
134         krb5_free_context(kcontext);
135         exit(1);
136     }
137 
138     krb5_free_principal(kcontext, princ);
139     krb5_cc_close(kcontext, ccache);
140     krb5_cc_close(kcontext, mslsa_ccache);
141     krb5_free_context(kcontext);
142     return(0);
143 }
144