*** empty log message ***
[heimdal.git] / admin / srvconvert.c
blob1646eae7580f5549a387aca6d7bc71287f47d87a
1 /*
2 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "ktutil_locl.h"
36 RCSID("$Id$");
38 /* convert a version 4 srvtab to a version 5 keytab */
40 #ifndef KEYFILE
41 #define KEYFILE "/etc/srvtab"
42 #endif
44 static char *srvtab = KEYFILE;
45 static int help_flag;
46 static int verbose;
48 static struct getargs args[] = {
49 { "srvtab", 's', arg_string, &srvtab, "srvtab to convert", "file" },
50 { "help", 'h', arg_flag, &help_flag },
51 { "verbose", 'v', arg_flag, &verbose },
54 static int num_args = sizeof(args) / sizeof(args[0]);
56 int
57 srvconv(int argc, char **argv)
59 krb5_error_code ret;
60 int optind = 0;
61 int fd;
62 krb5_storage *sp;
64 if(getarg(args, num_args, argc, argv, &optind)){
65 arg_printusage(args, num_args, "ktutil srvconvert", "");
66 return 1;
68 if(help_flag){
69 arg_printusage(args, num_args, "ktutil srvconvert", "");
70 return 0;
73 argc -= optind;
74 argv += optind;
76 if (argc != 0) {
77 arg_printusage(args, num_args, "ktutil srvconvert", "");
78 return 1;
81 fd = open(srvtab, O_RDONLY);
82 if(fd < 0){
83 krb5_warn(context, errno, "%s", srvtab);
84 return 1;
86 sp = krb5_storage_from_fd(fd);
87 if(sp == NULL){
88 close(fd);
89 return 1;
91 while(1){
92 char *service, *instance, *realm;
93 int8_t kvno;
94 des_cblock key;
95 krb5_keytab_entry entry;
97 ret = krb5_ret_stringz(sp, &service);
98 if(ret == KRB5_CC_END) {
99 ret = 0;
100 break;
102 if(ret) {
103 krb5_warn(context, ret, "reading service");
104 break;
106 ret = krb5_ret_stringz(sp, &instance);
107 if(ret) {
108 krb5_warn(context, ret, "reading instance");
109 free(service);
110 break;
112 ret = krb5_ret_stringz(sp, &realm);
113 if(ret) {
114 krb5_warn(context, ret, "reading realm");
115 free(service);
116 free(instance);
117 break;
119 ret = krb5_425_conv_principal(context, service, instance, realm,
120 &entry.principal);
121 free(service);
122 free(instance);
123 free(realm);
124 if (ret) {
125 krb5_warn(context, ret, "krb5_425_conv_principal (%s.%s@%s)",
126 service, instance, realm);
127 break;
130 ret = krb5_ret_int8(sp, &kvno);
131 if(ret) {
132 krb5_warn(context, ret, "reading kvno");
133 krb5_free_principal(context, entry.principal);
134 break;
136 ret = sp->fetch(sp, key, 8);
137 if(ret < 0){
138 krb5_warn(context, errno, "reading key");
139 krb5_free_principal(context, entry.principal);
140 break;
142 if(ret < 8) {
143 krb5_warn(context, errno, "end of file while reading key");
144 krb5_free_principal(context, entry.principal);
145 break;
148 entry.vno = kvno;
149 entry.timestamp = time (NULL);
150 entry.keyblock.keyvalue.data = key;
151 entry.keyblock.keyvalue.length = 8;
153 if(verbose){
154 char *p;
155 ret = krb5_unparse_name(context, entry.principal, &p);
156 if(ret){
157 krb5_warn(context, ret, "krb5_unparse_name");
158 krb5_free_principal(context, entry.principal);
159 break;
160 } else{
161 fprintf(stderr, "Storing keytab for %s\n", p);
162 free(p);
166 entry.keyblock.keytype = ETYPE_DES_CBC_MD5;
167 ret = krb5_kt_add_entry(context, keytab, &entry);
168 entry.keyblock.keytype = ETYPE_DES_CBC_MD4;
169 ret = krb5_kt_add_entry(context, keytab, &entry);
170 entry.keyblock.keytype = ETYPE_DES_CBC_CRC;
171 ret = krb5_kt_add_entry(context, keytab, &entry);
172 krb5_free_principal(context, entry.principal);
173 if(ret) {
174 krb5_warn(context, ret, "krb5_kt_add_entry");
175 break;
178 krb5_storage_free(sp);
179 close(fd);
180 return ret;