1 /*        $NetBSD: test_set_kvno0.c,v 1.2 2017/01/28 21:31:49 christos Exp $    */
2 
3 /*
4  * Copyright (c) 2011, Secure Endpoints Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * - Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #include "krb5_locl.h"
35 #include <err.h>
36 #include <krb5/getarg.h>
37 
38 #if 0
39 #include <stdio.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <krb5/krb5.h>
45 #endif
46 
47 int
main(int argc,char ** argv)48 main(int argc, char **argv)
49 {
50     krb5_error_code ret;
51     krb5_context context;
52     krb5_ccache src_cc = NULL;
53     krb5_ccache dst_cc = NULL;
54     krb5_cc_cursor cursor;
55     krb5_principal me = NULL;
56     krb5_creds cred;
57     const char *during;
58     Ticket t;
59     size_t len;
60     int make_kvno_absent = 0;
61     int opt;
62 
63     memset(&cred, 0, sizeof (cred));
64     during = "init_context";
65     ret = krb5_init_context(&context);
66     if (ret) goto err;
67 
68     while ((opt = getopt(argc, argv, "c:n")) != -1) {
69           switch (opt) {
70           case 'c':
71               during = "cc_resolve of source ccache";
72               ret = krb5_cc_resolve(context, optarg, &src_cc);
73               if (ret) goto err;
74               break;
75           case 'n':
76               make_kvno_absent++;
77               break;
78           case 'h':
79           default:
80               fprintf(stderr, "Usage: %s [-n] [-c ccache]\n"
81                         "\tThis utility edits a ccache, setting all ticket\n"
82                         "\tenc_part kvnos to zero or absent (if -n is set).\n",
83                         argv[0]);
84               return 1;
85           }
86     }
87 
88     if (!src_cc) {
89           during = "cc_default";
90           ret = krb5_cc_default(context, &src_cc);
91           if (ret) goto err;
92     }
93 
94     during = "cc_get_principal";
95     ret = krb5_cc_get_principal(context, src_cc, &me);
96     if (ret) goto err;
97 
98     if (optind != argc) {
99           fprintf(stderr, "Usage: %s [-n] [-c ccache]\n"
100                     "\tThis utility edits a ccache, setting all ticket\n"
101                     "\tenc_part kvnos to zero or absent (if -n is set).\n",
102                     argv[0]);
103           return 1;
104     }
105 
106     during = "cc_new_unique of temporary ccache";
107     ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, src_cc),
108                                    NULL, &dst_cc);
109 
110     during = "cc_initialize of temporary ccache";
111     ret = krb5_cc_initialize(context, dst_cc, me);
112     if (ret) goto err;
113 
114     during = "cc_start_seq_get";
115     ret = krb5_cc_start_seq_get(context, src_cc, &cursor);
116     if (ret) goto err;
117 
118     while ((ret = krb5_cc_next_cred(context, src_cc, &cursor, &cred)) == 0) {
119           krb5_data data;
120 
121           during = "decode_Ticket";
122           memset(&t, 0, sizeof (t));
123           ret = decode_Ticket(cred.ticket.data, cred.ticket.length, &t, &len);
124           if (ret == ASN1_MISSING_FIELD)
125               continue;
126           if (ret) goto err;
127           if (t.enc_part.kvno) {
128               *t.enc_part.kvno = 0;
129               if (make_kvno_absent) {
130                     free(t.enc_part.kvno);
131                     t.enc_part.kvno = NULL;
132               }
133               /*
134                * The new Ticket has to need less or same space as before, so
135                * we reuse cred->icket.data.
136                */
137               during = "encode_Ticket";
138               ASN1_MALLOC_ENCODE(Ticket, data.data, data.length, &t, &len, ret);
139               if (ret) {
140                     free_Ticket(&t);
141                     goto err;
142               }
143               krb5_data_free(&cred.ticket);
144               cred.ticket = data;
145           }
146           free_Ticket(&t);
147           during = "cc_store_cred";
148           ret = krb5_cc_store_cred(context, dst_cc, &cred);
149           if (ret) goto err;
150           krb5_free_cred_contents(context, &cred);
151           memset(&cred, 0, sizeof (cred));
152     }
153     during = "cc_next_cred";
154     if (ret != KRB5_CC_END) goto err;
155 
156     during = "cc_end_seq_get";
157     ret = krb5_cc_end_seq_get(context, src_cc, &cursor);
158     if (ret) goto err;
159 
160     during = "cc_move";
161     ret = krb5_cc_move(context, dst_cc, src_cc);
162     if (ret) goto err;
163     dst_cc = NULL;
164 
165     during = "cc_switch";
166     ret = krb5_cc_switch(context, src_cc);
167     if (ret) goto err;
168 
169 err:
170     (void) krb5_free_principal(context, me);
171     if (src_cc)
172           (void) krb5_cc_close(context, src_cc);
173     if (dst_cc)
174           (void) krb5_cc_destroy(context, dst_cc);
175     if (ret) {
176           fprintf(stderr, "Failed while doing %s (%d)\n", during, ret);
177           ret = 1;
178     }
179     return (ret);
180 }
181 
182