Change __signed to signed.
[dragonfly.git] / crypto / kerberosIV / admin / kdb_init.c
1 /*
2  * Copyright 1987, 1988 by the Massachusetts Institute of Technology. 
3  *
4  * For copying and distribution information, please see the file
5  * <mit-copyright.h>. 
6  *
7  * program to initialize the database,  reports error if database file
8  * already exists. 
9  */
10 /* $FreeBSD: src/crypto/kerberosIV/admin/kdb_init.c,v 1.1.1.3.2.1 2003/02/14 22:37:37 nectar Exp $ */
11 /* $DragonFly: src/crypto/kerberosIV/admin/Attic/kdb_init.c,v 1.2 2003/06/17 04:24:36 dillon Exp $ */
12
13 #include "adm_locl.h"
14
15 RCSID("$Id: kdb_init.c,v 1.25 1999/09/16 20:37:21 assar Exp $");
16
17 enum ap_op {
18     NULL_KEY,                   /* setup null keys */
19     MASTER_KEY,                 /* use master key as new key */
20     RANDOM_KEY                  /* choose a random key */
21 };
22
23 static des_cblock master_key;
24 static des_key_schedule master_key_schedule;
25
26 /* use a return code to indicate success or failure.  check the return */
27 /* values of the routines called by this routine. */
28
29 static int
30 add_principal(char *name, char *instance, enum ap_op aap_op, int maxlife)
31 {
32     Principal principal;
33     des_cblock new_key;
34
35     memset(&principal, 0, sizeof(principal));
36     strlcpy(principal.name, name, ANAME_SZ);
37     strlcpy(principal.instance, instance, INST_SZ);
38     switch (aap_op) {
39     case NULL_KEY:
40         principal.key_low = 0;
41         principal.key_high = 0;
42         break;
43     case RANDOM_KEY:
44 #ifdef NOENCRYPTION
45         memset(new_key, 0, sizeof(des_cblock));
46         new_key[0] = 127;
47 #else
48         des_random_key(new_key);
49 #endif
50         kdb_encrypt_key (&new_key, &new_key, &master_key, master_key_schedule,
51                          DES_ENCRYPT);
52         copy_from_key(new_key, &principal.key_low, &principal.key_high);
53         memset(new_key, 0, sizeof(new_key));
54         break;
55     case MASTER_KEY:
56         memcpy(new_key, master_key, sizeof (des_cblock));
57         kdb_encrypt_key (&new_key, &new_key, &master_key, master_key_schedule,
58                          DES_ENCRYPT);
59         copy_from_key(new_key, &principal.key_low, &principal.key_high);
60         break;
61     }
62     principal.mod_date = time(0);
63     *principal.mod_date_txt = '\0';
64     principal.exp_date = principal.mod_date + 5 * 365 * 24 * 60 * 60;
65     *principal.exp_date_txt = '\0';
66
67     principal.attributes = 0;
68     principal.max_life = maxlife;
69
70     principal.kdc_key_ver = 1;
71     principal.key_version = 1;
72
73     strlcpy(principal.mod_name, "db_creation", ANAME_SZ);
74     strlcpy(principal.mod_instance, "", INST_SZ);
75     principal.old = 0;
76
77     if (kerb_db_put_principal(&principal, 1) != 1)
78         return -1;              /* FAIL */
79     
80     /* let's play it safe */
81     memset(new_key, 0, sizeof (des_cblock));
82     memset(&principal.key_low, 0, 4);
83     memset(&principal.key_high, 0, 4);
84     return 0;
85 }
86
87 int
88 main(int argc, char **argv)
89 {
90     char    realm[REALM_SZ];
91     char   *cp;
92     int code;
93     char *database;
94     
95     set_progname (argv[0]);
96
97     if (argc > 3) {
98         fprintf(stderr, "Usage: %s [realm-name] [database-name]\n", argv[0]);
99         return 1;
100     }
101     if (argc == 3) {
102         database = argv[2];
103         --argc;
104     } else
105         database = DBM_FILE;
106
107     /* Do this first, it'll fail if the database exists */
108     if ((code = kerb_db_create(database)) != 0)
109         err (1, "Couldn't create database %s", database);
110     kerb_db_set_name(database);
111
112     if (argc == 2)
113         strlcpy(realm, argv[1], REALM_SZ);
114     else {
115         if (krb_get_lrealm(realm, 1) != KSUCCESS)
116             strlcpy(realm, KRB_REALM, REALM_SZ);
117         fprintf(stderr, "Realm name [default  %s ]: ", realm);
118         if (fgets(realm, sizeof(realm), stdin) == NULL)
119             errx (1, "\nEOF reading realm");
120         if ((cp = strchr(realm, '\n')))
121             *cp = '\0';
122         if (!*realm)                    /* no realm given */
123                 if (krb_get_lrealm(realm, 1) != KSUCCESS)
124                     strlcpy(realm, KRB_REALM, REALM_SZ);
125     }
126     if (!k_isrealm(realm))
127         errx (1, "Bad kerberos realm name \"%s\"", realm);
128 #ifndef RANDOM_MKEY
129     printf("You will be prompted for the database Master Password.\n");
130     printf("It is important that you NOT FORGET this password.\n");
131 #else
132     printf("To generate a master key, please enter some random data.\n");
133     printf("You do not have to remember this.\n");
134 #endif
135     fflush(stdout);
136
137     if (kdb_get_master_key (KDB_GET_TWICE, &master_key,
138                             master_key_schedule) != 0)
139         errx (1, "Couldn't read master key.");
140
141 #ifdef RANDOM_MKEY
142     if(kdb_kstash(&master_key, MKEYFILE) < 0)
143         err (1, "Error writing master key");
144     fprintf(stderr, "Wrote master key to %s\n", MKEYFILE);
145 #endif
146
147     /* Maximum lifetime for changepw.kerberos (kadmin) tickets, 10 minutes */
148 #define ADMLIFE (1 + (CLOCK_SKEW/(5*60)))
149
150     /* Maximum lifetime for ticket granting tickets, 4 days or 21.25h */
151 #define TGTLIFE ((krb_life_to_time(0, 162) >= 24*60*60) ? 161 : 255)
152
153     /* This means that default lifetimes have not been initialized */
154 #define DEFLIFE 255
155
156 #define NOLIFE 0
157
158     if (
159         add_principal(KERB_M_NAME, KERB_M_INST, MASTER_KEY, NOLIFE) ||
160         add_principal(KERB_DEFAULT_NAME, KERB_DEFAULT_INST, NULL_KEY,DEFLIFE)||
161         add_principal(KRB_TICKET_GRANTING_TICKET, realm, RANDOM_KEY, TGTLIFE)||
162         add_principal(PWSERV_NAME, KRB_MASTER, RANDOM_KEY, ADMLIFE) 
163         ) {
164       putc ('\n', stderr);
165       errx (1, "couldn't initialize database.");
166     }
167
168     /* play it safe */
169     memset(master_key, 0, sizeof (des_cblock));
170     memset(master_key_schedule, 0, sizeof (des_key_schedule));
171     return 0;
172 }