07d2f1669708285cad151a1b984b80e616768fd8
[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 RCSID("$Header$");
14  
15 #include <sys/types.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #ifdef AFS_NT40_ENV
19 #include <winsock2.h>
20 #include <WINNT/afsreg.h>
21 #include <WINNT/afsevent.h>
22 #endif
23 #ifdef HAVE_STRINGS_H
24 #include <strings.h>
25 #else
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 #endif
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 #ifdef HAVE_NETDB_H
34 #include <netdb.h>
35 #endif
36 #include "cellconfig.h"
37 #include "keys.h"
38 #include <afs/afsutil.h>
39
40 #include "AFS_component_version_number.c"
41 static int char2hex(char c);
42 static int hex2char(char c);
43
44 int
45 main(argc, argv)
46 int argc;
47 char **argv; {
48     struct afsconf_dir *tdir;
49     register afs_int32 code;
50     int i;
51     char *cp;
52
53     if (argc == 1) {
54         printf("setkey: usage is 'setkey <opcode> options, e.g.\n");
55         printf("    setkey add <kvno> <key>\n");
56         printf("      note: <key> should be an 8byte hex representation \n");
57         printf("            Ex: setkey add 0 \"80b6a7cd7a9dadb6\"\n");
58         printf("    setkey delete <kvno>\n");
59         printf("    setkey list\n");
60         exit(1);
61     }
62
63     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
64     if (!tdir) {
65         printf("setkey: can't initialize conf dir '%s'\n", AFSDIR_SERVER_ETC_DIRPATH);
66         exit(1);
67     }
68     if (strcmp(argv[1], "add")==0) {
69         char tkey[8];
70         if (argc != 4) {
71             printf("setkey add: usage is 'setkey add <kvno> <key>\n");
72             exit(1);
73         }
74         if (strlen(argv[3]) != 16) {
75           printf("key %s is not in right format\n", argv[3]);
76           printf(" <key> should be an 8byte hex representation \n");
77           printf("  Ex: setkey add 0 \"80b6a7cd7a9dadb6\"\n");
78           exit(1);
79         }
80         bzero(tkey, sizeof(tkey));
81         for(i=7, cp = argv[3] + 15;i>=0; i--,cp-=2)
82           tkey[i] = char2hex(*cp) + char2hex(*(cp-1))*16;
83
84         code = afsconf_AddKey(tdir, atoi(argv[2]), tkey, 1);
85         if (code) {
86             printf("setkey: failed to set key, code %d.\n", (int) code);
87             exit(1);
88         }
89     }
90     else if (strcmp(argv[1], "delete")==0) {
91         afs_int32 kvno;
92         if (argc != 3) {
93             printf("setkey delete: usage is 'setkey delete <kvno>\n");
94             exit(1);
95         }
96         kvno = atoi(argv[2]);
97         code = afsconf_DeleteKey(tdir, kvno);
98         if (code) {
99             printf("setkey: failed to delete key %d, (code %d)\n", (int) kvno, (int) code);
100             exit(1);
101         }
102     }
103     else if (strcmp(argv[1], "list") == 0) {
104         struct afsconf_keys tkeys;
105         register int i;
106         char tbuffer[9];
107         
108         code = afsconf_GetKeys(tdir, &tkeys);
109         if (code) {
110             printf("setkey: failed to get keys, code %d\n", (int) code);
111             exit(1);
112         }
113         for(i=0;i<tkeys.nkeys;i++) {
114           if (tkeys.key[i].kvno != -1) {
115             char hexbuf[17];
116             unsigned char c;
117             int j;
118             bcopy(tkeys.key[i].key, tbuffer, 8);
119             tbuffer[8] = 0;
120             for(j=0;j<8;j++) {
121               c = tbuffer[j];
122               hexbuf[j*2] = hex2char( c / 16 );
123               hexbuf[j*2+1] =  hex2char( c  % 16);
124             }
125             hexbuf[16]='\0';
126             printf("kvno %4d: key is '%s' (0x%s)\n", (int) tkeys.key[i].kvno, tbuffer, hexbuf);
127           }
128         }
129         printf("All done.\n");
130     }
131     else {
132         printf("setkey: unknown operation '%s', type 'setkey' for assistance\n", argv[1]);
133         exit(1);
134     }
135     exit(0);
136 }
137
138 static int char2hex(char c)
139 {
140   if (c >= '0' && c <='9')
141     return ( c - 48);
142   if ( (c >= 'a') && (c <= 'f') )
143     return (c-'a' + 10);
144   
145   if ( (c >= 'A') && (c <='F') )
146     return (c-'A' + 10);
147   
148   return -1;
149 }
150
151 static int hex2char(char c)
152 {
153   if (c <=9)
154     return (c+48);
155
156   return (c - 10 + 'a');
157 }