Windows: remove trailing whitespace
[openafs.git] / src / WINNT / aklog / asetkey.c
1 /*
2  * $Id$
3  *
4  * asetkey - Manipulates an AFS KeyFile
5  *
6  * Updated for Kerberos 5
7  */
8 /*
9  * Copyright (c) 2007 Secure Endpoints Inc.
10  *
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  *     * Redistributions of source code must retain the above copyright
18  *       notice, this list of conditions and the following disclaimer.
19  *     * Neither the name of the Secure Endpoints Inc. nor the names of its
20  *       contributors may be used to endorse or promote products derived
21  *       from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include <winsock.h>
37
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <krb5.h>
41
42 #include <afs/stds.h>
43 #include <afs/cellconfig.h>
44 #include <afs/keys.h>
45 #ifndef PRE_AFS35
46 #include <afs/dirpath.h>
47 #endif /* !PRE_AFS35 */
48 #include <afs/com_err.h>
49
50 void
51 validate_krb5_availability(void)
52 {
53 #ifndef _WIN64
54 #define KRB5LIB "krb5_32.dll"
55 #else
56 #define KRB5LIB "krb5_64.dll"
57 #endif
58     HINSTANCE h = LoadLibrary(KRB5LIB);
59     if (h)
60         FreeLibrary(h);
61     else {
62         fprintf(stderr, "Kerberos for Windows library %s is not available.\n", KRB5LIB);
63         exit(2);
64     }
65 }
66
67 int
68 main(int argc, char **argv)
69 {
70     struct afsconf_dir *tdir;
71     register long code;
72     const char *confdir;
73
74     validate_krb5_availability();
75
76     if (argc == 1) {
77         printf("asetkey: usage is 'setkey <opcode> options, e.g.\n");
78         printf("    asetkey add <kvno> <keyfile> <princ>\n");
79         printf("    asetkey delete <kvno>\n");
80         printf("    asetkey list\n");
81         exit(1);
82     }
83
84 #ifdef PRE_AFS35
85     confdir = AFSCONF_SERVERNAME;
86 #else /* PRE_AFS35 */
87     confdir = AFSDIR_SERVER_ETC_DIRPATH;
88 #endif /* PRE_AFS35 */
89
90     tdir = afsconf_Open(confdir);
91     if (!tdir) {
92         printf("asetkey: can't initialize conf dir '%s'\n", confdir);
93         exit(1);
94     }
95     if (strcmp(argv[1], "add")==0) {
96         krb5_context context;
97         krb5_principal principal;
98         krb5_keyblock *key;
99         krb5_error_code retval;
100         int kvno;
101
102         if (argc != 5) {
103             printf("asetkey add: usage is 'asetkey add <kvno> <keyfile> <princ>\n");
104             exit(1);
105         }
106
107         krb5_init_context(&context);
108
109         kvno = atoi(argv[2]);
110         retval = krb5_parse_name(context, argv[4], &principal);
111         if (retval != 0) {
112                 afs_com_err(argv[0], retval, "while parsing AFS principal");
113                 exit(1);
114         }
115         retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
116                                           ENCTYPE_DES_CBC_CRC, &key);
117         if (retval == KRB5_KT_NOTFOUND)
118             retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
119                                                ENCTYPE_DES_CBC_MD5, &key);
120         if (retval == KRB5_KT_NOTFOUND)
121             retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
122                                                ENCTYPE_DES_CBC_MD4, &key);
123         if (retval == KRB5_KT_NOTFOUND) {
124             char * princname = NULL;
125
126             krb5_unparse_name(context, principal, &princname);
127
128             afs_com_err(argv[0], retval,
129                         "for keytab entry with Principal %s, kvno %u, DES-CBC-CRC/MD5/MD4",
130                         princname ? princname : argv[4],
131                         kvno);
132             exit(1);
133         } else if (retval != 0) {
134             afs_com_err(argv[0], retval, "while extracting AFS service key");
135                 exit(1);
136         }
137
138         if (key->length != 8) {
139                 printf("Key length should be 8, but is really %d!\n",
140                        key->length);
141                 exit(1);
142         }
143
144         code = afsconf_AddKey(tdir, kvno, key->contents, 1);
145         if (code) {
146             printf("asetkey: failed to set key, code %d.\n", code);
147             exit(1);
148         }
149         krb5_free_principal(context, principal);
150         krb5_free_keyblock(context, key);
151     }
152     else if (strcmp(argv[1], "delete")==0) {
153         long kvno;
154         if (argc != 3) {
155             printf("asetkey delete: usage is 'asetkey delete <kvno>\n");
156             exit(1);
157         }
158         kvno = atoi(argv[2]);
159         code = afsconf_DeleteKey(tdir, kvno);
160         if (code) {
161             printf("asetkey: failed to delete key %d, (code %d)\n", kvno, code);
162             exit(1);
163         }
164     }
165     else if (strcmp(argv[1], "list") == 0) {
166         struct afsconf_keys tkeys;
167         register int i, j;
168
169         code = afsconf_GetKeys(tdir, &tkeys);
170         if (code) {
171             printf("asetkey: failed to get keys, code %d\n", code);
172             exit(1);
173         }
174         for(i=0;i<tkeys.nkeys;i++) {
175             if (tkeys.key[i].kvno != -1) {
176                 printf("kvno %4d: key is: ", tkeys.key[i].kvno);
177                 for (j = 0; j < 8; j++)
178                         printf("%02x", (unsigned char) tkeys.key[i].key[j]);
179                 printf("\n");
180             }
181         }
182         printf("All done.\n");
183     }
184     else {
185         printf("asetkey: unknown operation '%s', type 'asetkey' for assistance\n", argv[1]);
186         exit(1);
187     }
188     exit(0);
189 }