summaryrefslogtreecommitdiff
path: root/kerberosIV/kdb_init
diff options
context:
space:
mode:
authorArtur Grabowski <art@cvs.openbsd.org>1997-12-17 10:50:47 +0000
committerArtur Grabowski <art@cvs.openbsd.org>1997-12-17 10:50:47 +0000
commit2e9a156b058ee8b38cef763f7e8d87bc994a55c6 (patch)
tree0d766b3d35147d01dd209b5ece5f4c219b467ba7 /kerberosIV/kdb_init
parent911002ab774ddfdc5235dbf7963b341186575abd (diff)
Even this one is kth-krb4.0.9.8 now. (with some clean-ups)
Diffstat (limited to 'kerberosIV/kdb_init')
-rw-r--r--kerberosIV/kdb_init/kdb_init.c143
1 files changed, 77 insertions, 66 deletions
diff --git a/kerberosIV/kdb_init/kdb_init.c b/kerberosIV/kdb_init/kdb_init.c
index 035a2b6e16a..6908d97adb8 100644
--- a/kerberosIV/kdb_init/kdb_init.c
+++ b/kerberosIV/kdb_init/kdb_init.c
@@ -1,8 +1,8 @@
-/* $Id: kdb_init.c,v 1.2 1995/12/14 08:43:47 tholo Exp $ */
+/* $KTH: kdb_init.c,v 1.23 1997/03/30 17:45:05 assar Exp $ */
/*-
* Copyright 1987, 1988 by the Student Information Processing Board
- * of the Massachusetts Institute of Technology
+ * of the Massachusetts Institute of Technology
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for any purpose and without fee is
@@ -22,8 +22,7 @@
* already exists.
*/
-#include <adm_locl.h>
-#include <sys/param.h>
+#include "adm_locl.h"
enum ap_op {
NULL_KEY, /* setup null keys */
@@ -31,23 +30,27 @@ enum ap_op {
RANDOM_KEY /* choose a random key */
};
-char *progname;
static des_cblock master_key;
static des_key_schedule master_key_schedule;
+static char progname[]="kdb_init";
+
/* use a return code to indicate success or failure. check the return */
/* values of the routines called by this routine. */
static int
-add_principal(char *name, char *instance, enum ap_op aap_op)
+add_principal(char *name, char *instance, enum ap_op aap_op, int maxlife)
{
Principal principal;
struct tm *tm;
des_cblock new_key;
- bzero(&principal, sizeof(principal));
- strncpy(principal.name, name, ANAME_SZ);
- strncpy(principal.instance, instance, INST_SZ);
+ memset(&principal, 0, sizeof(principal));
+ strncpy(principal.name, name, ANAME_SZ - 1);
+ principal.name[ANAME_SZ - 1] = '\0';
+ strncpy(principal.instance, instance, INST_SZ - 1);
+ principal.instance[INST_SZ - 1] = '\0';
+
switch (aap_op) {
case NULL_KEY:
principal.key_low = 0;
@@ -55,22 +58,21 @@ add_principal(char *name, char *instance, enum ap_op aap_op)
break;
case RANDOM_KEY:
#ifdef NOENCRYPTION
- bzero(new_key, sizeof(des_cblock));
+ memset(new_key, 0, sizeof(des_cblock));
new_key[0] = 127;
#else
des_new_random_key(&new_key);
#endif
kdb_encrypt_key (&new_key, &new_key, &master_key, master_key_schedule,
DES_ENCRYPT);
- bcopy(new_key, &principal.key_low, 4);
- bcopy(((long *) new_key) + 1, &principal.key_high, 4);
+ copy_from_key(new_key, &principal.key_low, &principal.key_high);
+ memset(new_key, 0, sizeof(new_key));
break;
case MASTER_KEY:
- bcopy (master_key, new_key, sizeof (des_cblock));
+ memcpy(new_key, master_key, sizeof (des_cblock));
kdb_encrypt_key (&new_key, &new_key, &master_key, master_key_schedule,
DES_ENCRYPT);
- bcopy(new_key, &principal.key_low, 4);
- bcopy(((long *) new_key) + 1, &principal.key_high, 4);
+ copy_from_key(new_key, &principal.key_low, &principal.key_high);
break;
}
principal.exp_date = 946702799; /* Happy new century */
@@ -79,39 +81,38 @@ add_principal(char *name, char *instance, enum ap_op aap_op)
tm = k_localtime(&principal.mod_date);
principal.attributes = 0;
- principal.max_life = 255;
+ principal.max_life = maxlife;
principal.kdc_key_ver = 1;
principal.key_version = 1;
- strncpy(principal.mod_name, "db_creation", ANAME_SZ);
- strncpy(principal.mod_instance, "", INST_SZ);
+ strncpy(principal.mod_name, "db_creation", ANAME_SZ - 1);
+ principal.mod_name[ANAME_SZ - 1] = '\0';
+ strncpy(principal.mod_instance, "", INST_SZ - 1);
+ principal.mod_instance[INST_SZ - 1] = '\0';
principal.old = 0;
if (kerb_db_put_principal(&principal, 1) != 1)
return -1; /* FAIL */
/* let's play it safe */
- bzero (new_key, sizeof (des_cblock));
- bzero (&principal.key_low, 4);
- bzero (&principal.key_high, 4);
+ memset(new_key, 0, sizeof (des_cblock));
+ memset(&principal.key_low, 0, 4);
+ memset(&principal.key_high, 0, 4);
return 0;
}
int
main(int argc, char **argv)
{
- char admin[MAXHOSTNAMELEN];
- char realm[REALM_SZ], defrealm[REALM_SZ];
- char *cp, *dot;
+ char realm[REALM_SZ];
+ char *cp;
int code;
char *database;
- progname = (cp = strrchr(*argv, '/')) ? cp + 1 : *argv;
-
if (argc > 3) {
- fprintf(stderr, "Usage: %s [realm-name] [database-name]\n", argv[0]);
- exit(1);
+ fprintf(stderr, "Usage: %s [realm-name] [database-name]\n", progname);
+ return 1;
}
if (argc == 3) {
database = argv[2];
@@ -120,65 +121,75 @@ main(int argc, char **argv)
database = DBM_FILE;
/* Do this first, it'll fail if the database exists */
- if ((code = kerb_db_create(database)) != 0) {
- fprintf(stderr, "Couldn't create database: %s\n",
- strerror(code));
- exit(1);
- }
+ if ((code = kerb_db_create(database)) != 0)
+ err (1, "Couldn't create database %s", database);
kerb_db_set_name(database);
-
+
if (argc == 2)
strncpy(realm, argv[1], REALM_SZ);
else {
- if (krb_get_lrealm(defrealm, 1) != KSUCCESS)
- strcpy(defrealm, "NONE");
- fprintf(stderr, "Realm name [default %s ]: ", defrealm);
- if (fgets(realm, sizeof(realm), stdin) == NULL) {
- fprintf(stderr, "\nEOF reading realm\n");
- exit(1);
+ if (krb_get_lrealm(realm, 1) != KSUCCESS) {
+ strncpy(realm, KRB_REALM, REALM_SZ - 1);
+ realm[REALM_SZ - 1] = '\0';
}
+ fprintf(stderr, "Realm name [default %s ]: ", realm);
+ if (fgets(realm, sizeof(realm), stdin) == NULL)
+ errx (1, "\nEOF reading realm");
if ((cp = strchr(realm, '\n')))
*cp = '\0';
if (!*realm) /* no realm given */
- strcpy(realm, defrealm);
- }
- if (!k_isrealm(realm)) {
- fprintf(stderr, "%s: Bad kerberos realm name \"%s\"\n",
- progname, realm);
- exit(1);
+ if (krb_get_lrealm(realm, 1) != KSUCCESS) {
+ strncpy(realm, KRB_REALM, REALM_SZ - 1);
+ realm[REALM_SZ - 1] = '\0';
+ }
}
+ if (!k_isrealm(realm))
+ errx (1, "Bad kerberos realm name \"%s\"", realm);
+#ifndef RANDOM_MKEY
printf("You will be prompted for the database Master Password.\n");
printf("It is important that you NOT FORGET this password.\n");
+#else
+ printf("To generate a master key, please enter some random data.\n");
+ printf("You do not have to remember this.\n");
+#endif
fflush(stdout);
- if (kdb_get_master_key (TRUE, &master_key, master_key_schedule) != 0) {
- fprintf (stderr, "Couldn't read master key.\n");
- exit (-1);
- }
+ if (kdb_get_master_key (KDB_GET_TWICE, &master_key,
+ master_key_schedule) != 0)
+ errx (1, "Couldn't read master key.");
- if (krb_get_admhst(admin, realm, 1) != KSUCCESS) {
- fprintf (stderr, "Couldn't get admin server.\n");
- exit (-1);
- }
- if ((dot = strchr(admin, '.')) != NULL)
- *dot = '\0';
+#ifdef RANDOM_MKEY
+ if(kdb_kstash(&master_key, MKEYFILE) < 0)
+ err (1, "Error writing master key");
+ fprintf(stderr, "Wrote master key to %s\n", MKEYFILE);
+#endif
/* Initialize non shared random sequence */
des_init_random_number_generator(&master_key);
+ /* Maximum lifetime for changepw.kerberos (kadmin) tickets, 10 minutes */
+#define ADMLIFE (1 + (CLOCK_SKEW/(5*60)))
+
+ /* Maximum lifetime for ticket granting tickets, 4 days or 21.25h */
+#define TGTLIFE ((krb_life_to_time(0, 162) >= 24*60*60) ? 161 : 255)
+
+ /* This means that default lifetimes have not been initialized */
+#define DEFLIFE 255
+
+#define NOLIFE 0
+
if (
- add_principal(KERB_M_NAME, KERB_M_INST, MASTER_KEY) ||
- add_principal(KERB_DEFAULT_NAME, KERB_DEFAULT_INST, NULL_KEY) ||
- add_principal("krbtgt", realm, RANDOM_KEY) ||
- add_principal("changepw", admin, RANDOM_KEY)
+ add_principal(KERB_M_NAME, KERB_M_INST, MASTER_KEY, NOLIFE) ||
+ add_principal(KERB_DEFAULT_NAME, KERB_DEFAULT_INST, NULL_KEY,DEFLIFE)||
+ add_principal(KRB_TICKET_GRANTING_TICKET, realm, RANDOM_KEY, TGTLIFE)||
+ add_principal(PWSERV_NAME, KRB_MASTER, RANDOM_KEY, ADMLIFE)
) {
- fprintf(stderr, "\n%s: couldn't initialize database.\n",
- progname);
- exit(1);
+ putc ('\n', stderr);
+ errx (1, "couldn't initialize database.");
}
/* play it safe */
- bzero (master_key, sizeof (des_cblock));
- bzero (master_key_schedule, sizeof (des_key_schedule));
- exit(0);
+ memset(master_key, 0, sizeof (des_cblock));
+ memset(master_key_schedule, 0, sizeof (des_key_schedule));
+ return 0;
}