summaryrefslogtreecommitdiffstats
path: root/eBones/usr.sbin/fix_kdb_keys/fix_kdb_keys.c
blob: 3719e784c854bec967b11ccde3518ffda8bb15fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * $Source: /afs/net/project/krb4/src/admin/RCS/kdb_edit.c,v $
 * $Author: tytso $
 *
 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
 * of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * This routine changes the Kerberos encryption keys for principals,
 * i.e., users or services. 
 */

/*
 * exit returns 	 0 ==> success -1 ==> error 
 */

#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/file.h>

#ifdef NEED_TIME_H
#include <time.h>
#endif
#include <sys/time.h>

#include <des.h>
#include <krb.h>
#include <krb_db.h>
/* MKEYFILE is now defined in kdc.h */
#include <kdc.h>

char    prog[32];
char   *progname = prog;
int     nflag = 0;
int     debug = 0;
extern  int krb_debug;

Principal principal_data;

static C_Block master_key;
static Key_schedule master_key_schedule;
static long master_key_version;

static char realm[REALM_SZ];

void fatal_error(), cleanup();
void Usage();
void change_principal();

int main(argc, argv)
     int     argc;
     char   *argv[];
{
  int i;

  prog[sizeof prog - 1] = '\0';	/* make sure terminated */
  strncpy(prog, argv[0], sizeof prog - 1);	/* salt away invoking
						 * program */

  /* Assume a long is four bytes */
  if (sizeof(long) != 4) {
    fprintf(stderr, "%s: size of long is %d.\n", prog, sizeof(long));
    exit(-1);
  }
  while (--argc > 0 && (*++argv)[0] == '-')
    for (i = 1; argv[0][i] != '\0'; i++) {
      switch (argv[0][i]) {
	
	/* debug flag */
      case 'd':
	debug = 1;
	continue;

	/* debug flag */
      case 'l':
	krb_debug |= 1;
	continue;

      case 'n':		/* read MKEYFILE for master key */
	nflag = 1;
	continue;
	
      default:
	fprintf(stderr, "%s: illegal flag \"%c\"\n", progname, argv[0][i]);
	Usage();	/* Give message and die */
      }
    };

  if (krb_get_lrealm(realm, 1)) {
	  fprintf(stderr, "Couldn't get local realm information.\n");
	  fatal_error();
  }

  kerb_init();
  if (argc > 0) {
    if (kerb_db_set_name(*argv) != 0) {
      fprintf(stderr, "Could not open altername database name\n");
      fatal_error();
    }
  }

  if (kdb_get_master_key ((nflag == 0), 
			  master_key, master_key_schedule) != 0) {
    fprintf (stderr, "Couldn't read master key.\n");
    fatal_error();
  }

  if ((master_key_version = kdb_verify_master_key(master_key,
						  master_key_schedule,
						  stdout)) < 0)
	  fatal_error();

  des_init_random_number_generator(master_key);

  change_principal("krbtgt", realm);
  change_principal("changepw", KRB_MASTER);

  cleanup();

  printf("\nKerberos database updated successfully.  Note that all\n");
  printf("existing ticket-granting tickets have been invalidated.\n\n");

  return(0);
}

void change_principal(input_name, input_instance)
     char *input_name;
     char *input_instance;
{
    int     n, more;
    C_Block new_key;

    n = kerb_get_principal(input_name, input_instance, &principal_data,
			   1, &more);
    if (!n) {
      fprintf(stderr, "Can't find principal database for %s.%s.\n", 
	      input_name, input_instance);
      fatal_error();
    }
    if (more) {
      fprintf(stderr, "More than one entry for %s.%s.\n", input_name, 
	      input_instance);
      fatal_error();
    }
      
    des_new_random_key(new_key);

    /* seal it under the kerberos master key */
    kdb_encrypt_key (new_key, new_key, 
		     master_key, master_key_schedule,
				 ENCRYPT);
    memcpy(&principal_data.key_low, new_key, 4);
    memcpy(&principal_data.key_high, ((long *) new_key) + 1, 4);
    memset(new_key, 0, sizeof(new_key));

    principal_data.key_version++;

    if (kerb_put_principal(&principal_data, 1)) {
      fprintf(stderr, "\nError updating Kerberos database");
      fatal_error();
    }

    memset(&principal_data.key_low, 0, 4);
    memset(&principal_data.key_high, 0, 4);
}

void fatal_error()
{
	cleanup();
	exit(1);
}

void cleanup()
{

  memset(master_key, 0, sizeof(master_key));
  memset(master_key_schedule, 0, sizeof(master_key_schedule));
  memset(&principal_data, 0, sizeof(principal_data));
}

void Usage()
{
    fprintf(stderr, "Usage: %s [-n]\n", progname);
    exit(1);
}
OpenPOWER on IntegriCloud