home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / kerberosIV / kdb_init / kdb_init.c next >
Encoding:
C/C++ Source or Header  |  1991-02-25  |  4.7 KB  |  182 lines

  1. /*
  2.  * $Source: /usr/src/kerberosIV/kdb_init/RCS/kdb_init.c,v $
  3.  * $Author: bostic $ 
  4.  *
  5.  * Copyright 1987, 1988 by the Massachusetts Institute of Technology. 
  6.  *
  7.  * For copying and distribution information, please see the file
  8.  * <mit-copyright.h>. 
  9.  *
  10.  * program to initialize the database,  reports error if database file
  11.  * already exists. 
  12.  */
  13.  
  14. #ifndef    lint
  15. static char rcsid_kdb_init_c[] =
  16. "$Header: /usr/src/kerberosIV/kdb_init/RCS/kdb_init.c,v 4.1 91/02/25 15:36:30 bostic Exp $";
  17. #endif    lint
  18.  
  19. #include <mit-copyright.h>
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <sys/file.h>
  23. #include <sys/time.h>
  24. #include <des.h>
  25. #include <krb.h>
  26. #include <krb_db.h>
  27. #include <string.h>
  28.  
  29. #define    TRUE    1
  30.  
  31. enum ap_op {
  32.     NULL_KEY,            /* setup null keys */
  33.     MASTER_KEY,                 /* use master key as new key */
  34.     RANDOM_KEY,            /* choose a random key */
  35. };
  36.  
  37. int     debug = 0;
  38. char   *progname, *rindex();
  39. C_Block master_key;
  40. Key_schedule master_key_schedule;
  41.  
  42. main(argc, argv)
  43.     char   *argv[];
  44. {
  45.     char    realm[REALM_SZ];
  46.     char   *cp;
  47.     int code;
  48.     extern char *sys_errlist[];
  49.     char *database;
  50.     
  51.     progname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
  52.  
  53.     if (argc > 3) {
  54.     fprintf(stderr, "Usage: %s [realm-name] [database-name]\n", argv[0]);
  55.     exit(1);
  56.     }
  57.     if (argc == 3) {
  58.     database = argv[2];
  59.     --argc;
  60.     } else
  61.     database = DBM_FILE;
  62.  
  63.     /* Do this first, it'll fail if the database exists */
  64.     if ((code = kerb_db_create(database)) != 0) {
  65.     fprintf(stderr, "Couldn't create database: %s\n",
  66.         sys_errlist[code]);
  67.     exit(1);
  68.     }
  69.     kerb_db_set_name(database);
  70.  
  71.     if (argc == 2)
  72.     strncpy(realm, argv[1], REALM_SZ);
  73.     else {
  74.     fprintf(stderr, "Realm name [default  %s ]: ", KRB_REALM);
  75.     if (fgets(realm, sizeof(realm), stdin) == NULL) {
  76.         fprintf(stderr, "\nEOF reading realm\n");
  77.         exit(1);
  78.     }
  79.     if (cp = index(realm, '\n'))
  80.         *cp = '\0';
  81.     if (!*realm)            /* no realm given */
  82.         strcpy(realm, KRB_REALM);
  83.     }
  84.     if (!k_isrealm(realm)) {
  85.     fprintf(stderr, "%s: Bad kerberos realm name \"%s\"\n",
  86.         progname, realm);
  87.     exit(1);
  88.     }
  89.     printf("You will be prompted for the database Master Password.\n");
  90.     printf("It is important that you NOT FORGET this password.\n");
  91.     fflush(stdout);
  92.  
  93.     if (kdb_get_master_key (TRUE, master_key, master_key_schedule) != 0) {
  94.       fprintf (stderr, "Couldn't read master key.\n");
  95.       exit (-1);
  96.     }
  97.  
  98.     if (
  99.     add_principal(KERB_M_NAME, KERB_M_INST, MASTER_KEY) ||
  100.     add_principal(KERB_DEFAULT_NAME, KERB_DEFAULT_INST, NULL_KEY) ||
  101.     add_principal("krbtgt", realm, RANDOM_KEY) ||
  102.     add_principal("changepw", KRB_MASTER, RANDOM_KEY) 
  103.     ) {
  104.     fprintf(stderr, "\n%s: couldn't initialize database.\n",
  105.         progname);
  106.     exit(1);
  107.     }
  108.  
  109.     /* play it safe */
  110.     bzero (master_key, sizeof (C_Block));
  111.     bzero (master_key_schedule, sizeof (Key_schedule));
  112.     exit(0);
  113. }
  114.  
  115. /* use a return code to indicate success or failure.  check the return */
  116. /* values of the routines called by this routine. */
  117.  
  118. add_principal(name, instance, aap_op)
  119.     char   *name, *instance;
  120.     enum ap_op aap_op;
  121. {
  122.     Principal principal;
  123.     char    datestring[50];
  124.     char    pw_str[255];
  125.     void    read_pw_string();
  126.     void    string_to_key();
  127.     void    random_key();
  128.     struct tm *tm, *localtime();
  129.     C_Block new_key;
  130.  
  131.     bzero(&principal, sizeof(principal));
  132.     strncpy(principal.name, name, ANAME_SZ);
  133.     strncpy(principal.instance, instance, INST_SZ);
  134.     switch (aap_op) {
  135.     case NULL_KEY:
  136.     principal.key_low = 0;
  137.     principal.key_high = 0;
  138.     break;
  139.     case RANDOM_KEY:
  140. #ifdef NOENCRYPTION
  141.     bzero(new_key, sizeof(C_Block));
  142.     new_key[0] = 127;
  143. #else
  144.     random_key(new_key);
  145. #endif
  146.     kdb_encrypt_key (new_key, new_key, master_key, master_key_schedule,
  147.              ENCRYPT);
  148.     bcopy(new_key, &principal.key_low, 4);
  149.     bcopy(((long *) new_key) + 1, &principal.key_high, 4);
  150.     break;
  151.     case MASTER_KEY:
  152.     bcopy (master_key, new_key, sizeof (C_Block));
  153.     kdb_encrypt_key (new_key, new_key, master_key, master_key_schedule,
  154.              ENCRYPT);
  155.     bcopy(new_key, &principal.key_low, 4);
  156.     bcopy(((long *) new_key) + 1, &principal.key_high, 4);
  157.     break;
  158.     }
  159.     principal.exp_date = 946702799;    /* Happy new century */
  160.     strncpy(principal.exp_date_txt, "12/31/99", DATE_SZ);
  161.     principal.mod_date = time(0);
  162.  
  163.     tm = localtime((time_t *)&principal.mod_date);
  164.     principal.attributes = 0;
  165.     principal.max_life = 255;
  166.  
  167.     principal.kdc_key_ver = 1;
  168.     principal.key_version = 1;
  169.  
  170.     strncpy(principal.mod_name, "db_creation", ANAME_SZ);
  171.     strncpy(principal.mod_instance, "", INST_SZ);
  172.     principal.old = 0;
  173.  
  174.     kerb_db_put_principal(&principal, 1);
  175.     
  176.     /* let's play it safe */
  177.     bzero (new_key, sizeof (C_Block));
  178.     bzero (&principal.key_low, 4);
  179.     bzero (&principal.key_high, 4);
  180.     return 0;
  181. }
  182.