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