2 * Copyright (c) 2011, Secure Endpoints Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "krb5_locl.h"
46 main(int argc
, char **argv
)
50 krb5_ccache src_cc
= NULL
;
51 krb5_ccache dst_cc
= NULL
;
52 krb5_cc_cursor cursor
;
53 krb5_principal me
= NULL
;
58 int make_kvno_absent
= 0;
61 memset(&cred
, 0, sizeof (cred
));
62 during
= "init_context";
63 ret
= krb5_init_context(&context
);
66 while ((opt
= getopt(argc
, argv
, "c:n")) != -1) {
69 during
= "cc_resolve of source ccache";
70 ret
= krb5_cc_resolve(context
, optarg
, &src_cc
);
78 fprintf(stderr
, "Usage: %s [-n] [-c ccache]\n"
79 "\tThis utility edits a ccache, setting all ticket\n"
80 "\tenc_part kvnos to zero or absent (if -n is set).\n",
87 during
= "cc_default";
88 ret
= krb5_cc_default(context
, &src_cc
);
92 during
= "cc_get_principal";
93 ret
= krb5_cc_get_principal(context
, src_cc
, &me
);
97 fprintf(stderr
, "Usage: %s [-n] [-c ccache]\n"
98 "\tThis utility edits a ccache, setting all ticket\n"
99 "\tenc_part kvnos to zero or absent (if -n is set).\n",
104 during
= "cc_new_unique of temporary ccache";
105 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, src_cc
),
108 during
= "cc_initialize of temporary ccache";
109 ret
= krb5_cc_initialize(context
, dst_cc
, me
);
112 during
= "cc_start_seq_get";
113 ret
= krb5_cc_start_seq_get(context
, src_cc
, &cursor
);
116 while ((ret
= krb5_cc_next_cred(context
, src_cc
, &cursor
, &cred
)) == 0) {
119 during
= "decode_Ticket";
120 memset(&t
, 0, sizeof (t
));
121 ret
= decode_Ticket(cred
.ticket
.data
, cred
.ticket
.length
, &t
, &len
);
122 if (ret
== ASN1_MISSING_FIELD
)
125 if (t
.enc_part
.kvno
) {
126 *t
.enc_part
.kvno
= 0;
127 if (make_kvno_absent
) {
128 free(t
.enc_part
.kvno
);
129 t
.enc_part
.kvno
= NULL
;
132 * The new Ticket has to need less or same space as before, so
133 * we reuse cred->icket.data.
135 during
= "encode_Ticket";
136 ASN1_MALLOC_ENCODE(Ticket
, data
.data
, data
.length
, &t
, &len
, ret
);
141 krb5_data_free(&cred
.ticket
);
145 during
= "cc_store_cred";
146 ret
= krb5_cc_store_cred(context
, dst_cc
, &cred
);
148 krb5_free_cred_contents(context
, &cred
);
149 memset(&cred
, 0, sizeof (cred
));
151 during
= "cc_next_cred";
152 if (ret
!= KRB5_CC_END
) goto err
;
154 during
= "cc_end_seq_get";
155 ret
= krb5_cc_end_seq_get(context
, src_cc
, &cursor
);
159 ret
= krb5_cc_move(context
, dst_cc
, src_cc
);
163 during
= "cc_switch";
164 ret
= krb5_cc_switch(context
, src_cc
);
168 (void) krb5_free_principal(context
, me
);
170 (void) krb5_cc_close(context
, src_cc
);
172 (void) krb5_cc_destroy(context
, dst_cc
);
174 fprintf(stderr
, "Failed while doing %s (%d)\n", during
, ret
);