death to register
[openafs.git] / src / auth / setkey.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <sys/types.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #ifdef AFS_NT40_ENV
18 #include <winsock2.h>
19 #include <WINNT/afsreg.h>
20 #include <WINNT/afsevent.h>
21 #endif
22 #include <string.h>
23 #ifdef HAVE_NETINET_IN_H
24 #include <netinet/in.h>
25 #endif
26 #ifdef HAVE_NETDB_H
27 #include <netdb.h>
28 #endif
29 #include <rx/rxkad.h>
30 #include "cellconfig.h"
31 #include "keys.h"
32 #include <afs/afsutil.h>
33
34 #include "AFS_component_version_number.c"
35 static int char2hex(char c);
36 static int hex2char(char c);
37
38 int
39 main(int argc, char **argv)
40 {
41     struct afsconf_dir *tdir;
42     afs_int32 code;
43     int i;
44     char *cp;
45
46     if (argc == 1) {
47         printf("setkey: usage is 'setkey <opcode> options, e.g.\n");
48         printf("    setkey add <kvno> <key>\n");
49         printf("      note: <key> should be an 8byte hex representation \n");
50         printf("            Ex: setkey add 0 \"80b6a7cd7a9dadb6\"\n");
51         printf("    setkey delete <kvno>\n");
52         printf("    setkey list\n");
53         exit(1);
54     }
55
56     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
57     if (!tdir) {
58         printf("setkey: can't initialize conf dir '%s'\n",
59                AFSDIR_SERVER_ETC_DIRPATH);
60         exit(1);
61     }
62     if (strcmp(argv[1], "add") == 0) {
63         char tkey[8];
64         if (argc != 4) {
65             printf("setkey add: usage is 'setkey add <kvno> <key>\n");
66             exit(1);
67         }
68         if (strlen(argv[3]) != 16) {
69             printf("key %s is not in right format\n", argv[3]);
70             printf(" <key> should be an 8byte hex representation \n");
71             printf("  Ex: setkey add 0 \"80b6a7cd7a9dadb6\"\n");
72             exit(1);
73         }
74         memset(tkey, 0, sizeof(tkey));
75         for (i = 7, cp = argv[3] + 15; i >= 0; i--, cp -= 2)
76             tkey[i] = char2hex(*cp) + char2hex(*(cp - 1)) * 16;
77
78         code = afsconf_AddKey(tdir, atoi(argv[2]), tkey, 1);
79         if (code) {
80             printf("setkey: failed to set key, code %d.\n", (int)code);
81             exit(1);
82         }
83     } else if (strcmp(argv[1], "delete") == 0) {
84         afs_int32 kvno;
85         if (argc != 3) {
86             printf("setkey delete: usage is 'setkey delete <kvno>\n");
87             exit(1);
88         }
89         kvno = atoi(argv[2]);
90         code = afsconf_DeleteKey(tdir, kvno);
91         if (code) {
92             printf("setkey: failed to delete key %d, (code %d)\n", (int)kvno,
93                    (int)code);
94             exit(1);
95         }
96     } else if (strcmp(argv[1], "list") == 0) {
97         struct afsconf_keys tkeys;
98         int i;
99         char tbuffer[9];
100
101         code = afsconf_GetKeys(tdir, &tkeys);
102         if (code) {
103             printf("setkey: failed to get keys, code %d\n", (int)code);
104             exit(1);
105         }
106         for (i = 0; i < tkeys.nkeys; i++) {
107             if (tkeys.key[i].kvno != -1) {
108                 char hexbuf[17];
109                 unsigned char c;
110                 int j;
111                 memcpy(tbuffer, tkeys.key[i].key, 8);
112                 tbuffer[8] = 0;
113                 for (j = 0; j < 8; j++) {
114                     c = tbuffer[j];
115                     hexbuf[j * 2] = hex2char(c / 16);
116                     hexbuf[j * 2 + 1] = hex2char(c % 16);
117                 }
118                 hexbuf[16] = '\0';
119                 printf("kvno %4d: key is '%s' (0x%s)\n",
120                        (int)tkeys.key[i].kvno, tbuffer, hexbuf);
121             }
122         }
123         printf("All done.\n");
124     } else {
125         printf
126             ("setkey: unknown operation '%s', type 'setkey' for assistance\n",
127              argv[1]);
128         exit(1);
129     }
130     exit(0);
131 }
132
133 static int
134 char2hex(char c)
135 {
136     if (c >= '0' && c <= '9')
137         return (c - 48);
138     if ((c >= 'a') && (c <= 'f'))
139         return (c - 'a' + 10);
140
141     if ((c >= 'A') && (c <= 'F'))
142         return (c - 'A' + 10);
143
144     return -1;
145 }
146
147 static int
148 hex2char(char c)
149 {
150     if (c <= 9)
151         return (c + 48);
152
153     return (c - 10 + 'a');
154 }