1 | /* |
---|
2 | * kstash.c |
---|
3 | * |
---|
4 | * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute |
---|
5 | * of Technology |
---|
6 | * |
---|
7 | * For copying and distribution information, please see the file |
---|
8 | * <mit-copyright.h>. |
---|
9 | * |
---|
10 | * Description. |
---|
11 | */ |
---|
12 | |
---|
13 | #include <mit-copyright.h> |
---|
14 | |
---|
15 | #include <stdio.h> |
---|
16 | #include <sys/types.h> |
---|
17 | #include <sys/file.h> |
---|
18 | #ifdef NEED_SYS_FCNTL_H |
---|
19 | #include <sys/fcntl.h> |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <krb.h> |
---|
23 | #include <krb_db.h> |
---|
24 | #include <kdc.h> |
---|
25 | |
---|
26 | /* change this later, but krblib_dbm needs it for now */ |
---|
27 | char *progname; |
---|
28 | |
---|
29 | static C_Block master_key; |
---|
30 | static Key_schedule master_key_schedule; |
---|
31 | int debug; |
---|
32 | static int kfile; |
---|
33 | static void clear_secrets(); |
---|
34 | char * progname; |
---|
35 | |
---|
36 | usage() |
---|
37 | { |
---|
38 | fprintf(stderr, "Usage: %s [-d database name] [-k master key file]\n", |
---|
39 | progname); |
---|
40 | exit(1); |
---|
41 | } |
---|
42 | |
---|
43 | void |
---|
44 | main(argc, argv) |
---|
45 | int argc; |
---|
46 | char **argv; |
---|
47 | { |
---|
48 | long n; |
---|
49 | char *stashfile = 0; |
---|
50 | int c; |
---|
51 | extern char *optarg; |
---|
52 | extern int optind; |
---|
53 | |
---|
54 | progname = argv[0]; |
---|
55 | |
---|
56 | while ((c = getopt(argc, argv, "d:k:")) != EOF) { |
---|
57 | switch (c) { |
---|
58 | case 'd': |
---|
59 | if (kerb_db_set_name(optarg) != 0) { |
---|
60 | fprintf(stderr, "Couldn't set alternate database name (%s)\n", |
---|
61 | optarg); |
---|
62 | exit(1); |
---|
63 | } |
---|
64 | break; |
---|
65 | case 'k': |
---|
66 | stashfile = optarg; |
---|
67 | break; |
---|
68 | default: |
---|
69 | usage(); |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | if (optind != argc) |
---|
74 | usage(); |
---|
75 | |
---|
76 | n = kerb_init(); |
---|
77 | if (n) { |
---|
78 | fprintf(stderr, "Kerberos db and cache init failed = %ld\n", n); |
---|
79 | exit(1); |
---|
80 | } |
---|
81 | |
---|
82 | if (kdb_get_master_key_from (TRUE, master_key, master_key_schedule, 0, stashfile) != 0) { |
---|
83 | fprintf (stderr, "%s: Couldn't read master key.\n", argv[0]); |
---|
84 | fflush (stderr); |
---|
85 | clear_secrets(); |
---|
86 | exit (-1); |
---|
87 | } |
---|
88 | |
---|
89 | if (kdb_verify_master_key (master_key, master_key_schedule, stderr) < 0) { |
---|
90 | clear_secrets(); |
---|
91 | exit (-1); |
---|
92 | } |
---|
93 | |
---|
94 | kfile = open(stashfile?stashfile:MKEYFILE, O_TRUNC | O_RDWR | O_CREAT, 0600); |
---|
95 | if (kfile < 0) { |
---|
96 | clear_secrets(); |
---|
97 | fprintf(stderr, "\n\07\07%s: Unable to open master key file\n", |
---|
98 | argv[0]); |
---|
99 | exit(1); |
---|
100 | } |
---|
101 | if (write(kfile, (char *) master_key, 8) < 0) { |
---|
102 | clear_secrets(); |
---|
103 | fprintf(stderr, "\n%s: Write I/O error on master key file\n", |
---|
104 | argv[0]); |
---|
105 | exit(1); |
---|
106 | } |
---|
107 | (void) close(kfile); |
---|
108 | clear_secrets(); |
---|
109 | exit (0); |
---|
110 | } |
---|
111 | |
---|
112 | static void |
---|
113 | clear_secrets() |
---|
114 | { |
---|
115 | memset(master_key_schedule, 0, sizeof(master_key_schedule)); |
---|
116 | memset(master_key, 0, sizeof(master_key)); |
---|
117 | } |
---|