summaryrefslogtreecommitdiff
path: root/kerberosIV/kpropd/kpropd.c
blob: df5edd45beeec81f279de821efd2eb7aef2c3e43 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*	$OpenBSD: kpropd.c,v 1.5 1998/02/18 11:53:59 art Exp $	*/

/*
 * This software may now be redistributed outside the US.
 */

/*-
 * Copyright (C) 1987 by the Massachusetts Institute of Technology
 *
 * Export of this software from the United States of America is assumed
 * to require a specific license from the United States Government.
 * It is the responsibility of any person or organization contemplating
 * export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 */

/*
 * This program is run on slave servers, to catch updates "pushed"
 * from the master kerberos server in a realm.
 */

#include <slav_locl.h>
#include <kprop.h>
#include <sys/param.h>

static char *kdb_util_path = "kdb_util";

static char kprop_version[KPROP_PROT_VERSION_LEN] = KPROP_PROT_VERSION;

int     debug = 0;

char   *logfile = K_LOGFIL;

char    errmsg[256];
int     pause_int = 300;	/* 5 minutes in seconds */

static char    buf[KPROP_BUFSIZ+64 /* leave room for private msg overhead */];

static void 
SlowDeath(void)
{
  klog(L_KRB_PERR, "kpropd will pause before dying so as not to loop init");
  sleep(pause_int);
  klog(L_KRB_PERR, "AAAAAHHHHhhhh....");
  exit(1);
}

static void
usage(void)
{
  fprintf(stderr, "\nUsage: kpropd [-r realm] [-s srvtab] [-l logfile] fname\n\n");
  SlowDeath();
}

static void
recv_auth (int in, int out, int private, struct sockaddr_in *remote, struct sockaddr_in *local, AUTH_DAT *ad)
{
  u_long length;
  long kerror;
  int n;
  MSG_DAT msg_data;
  des_key_schedule session_sched;

  if (private)
#ifdef NOENCRYPTION
    bzero((char *)session_sched, sizeof(session_sched));
#else
  if (des_key_sched (&ad->session, session_sched)) {
    klog (L_KRB_PERR, "kpropd: can't make key schedule");
    SlowDeath();
  }
#endif

  while (1) {
    n = krb_net_read (in, (char *)&length, sizeof length);
    if (n == 0) break;
    if (n < 0) {
      snprintf (errmsg, sizeof(errmsg), "kpropd: read: %s", strerror(errno));
      klog (L_KRB_PERR, errmsg);
      SlowDeath();
    }
    length = ntohl (length);
    if (length > sizeof buf) {
      snprintf (errmsg, sizeof(errmsg),
		"kpropd: read length %ld, bigger than buf %d",
	        length, (int)(sizeof(buf)));
      klog (L_KRB_PERR, errmsg);
      SlowDeath();
    }
    n = krb_net_read(in, buf, length);
    if (n < 0) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: read: %s", strerror(errno));
      klog(L_KRB_PERR, errmsg);
      SlowDeath();
    }
    if (private)
      kerror = krb_rd_priv (buf, n, session_sched, &ad->session, 
			    remote, local, &msg_data);
    else
      kerror = krb_rd_safe (buf, n, &ad->session,
			    remote, local, &msg_data);
    if (kerror != KSUCCESS) {
      snprintf (errmsg, sizeof(errmsg), "kpropd: %s: %s",
	       private ? "krb_rd_priv" : "krb_rd_safe",
	       krb_err_txt[kerror]);
      klog (L_KRB_PERR, errmsg);
      SlowDeath();
    }
    if (write(out, msg_data.app_data, msg_data.app_length) != 
	msg_data.app_length) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: write: %s", strerror(errno));
      klog(L_KRB_PERR, errmsg);
      SlowDeath();
    }
  }
}

static void
recv_clear (int in, int out)
{
  int n;

  while (1) {
    n = read (in, buf, sizeof buf);
    if (n == 0) break;
    if (n < 0) {
      snprintf (errmsg, sizeof(errmsg), "kpropd: read: %s", strerror(errno));
      klog (L_KRB_PERR, errmsg);
      SlowDeath();
    }
    if (write(out, buf, n) != n) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: write: %s", strerror(errno));
      klog(L_KRB_PERR, errmsg);
      SlowDeath();
    }
  }
}

int
main(int argc, char **argv)
{
  struct sockaddr_in from;
  struct sockaddr_in sin;
  struct servent *sp;
  int     s, s2, fd, n, fdlock;
  int     from_len;
  char    local_file[256];
  char    local_temp[256];
  struct hostent *hp;
  char    *dot, admin[MAXHOSTNAMELEN];
  char    hostname[MAXHOSTNAMELEN];
  char    from_str[128];
  long    kerror;
  AUTH_DAT auth_dat;
  KTEXT_ST ticket;
  char my_instance[INST_SZ];
  char my_realm[REALM_SZ];
  char cmd[1024];
  short net_transfer_mode, transfer_mode;
  des_key_schedule session_sched;
  char version[9];
  int c;
  extern char *optarg;
  extern int optind;
  int rflag = 0;
  char *srvtab = "";
  char *local_db = DBM_FILE;

  if (argv[argc - 1][0] == 'k' && isdigit(argv[argc - 1][1])) {
    argc--;			/* ttys file hack */
  }
  while ((c = getopt(argc, argv, "r:s:d:l:p:P:")) != -1) {
    switch(c) {
    case 'r':
      rflag++;
      strcpy(my_realm, optarg);
      break;
    case 's':
      srvtab = optarg;
      break;
    case 'd':
      local_db = optarg;
      break;
    case 'l':
      logfile = optarg;
      break;	    
    case 'p':
    case 'P':
      kdb_util_path = optarg;
      break;
    default:
      usage();
      break;
    }
  }
  if (optind != argc-1)
    usage();

  kset_logfile(logfile);

  klog(L_KRB_PERR, "\n\n***** kpropd started *****");

  strcpy(local_file, argv[optind]);
  strcat(strcpy(local_temp, argv[optind]), ".tmp");

  if ((sp = getservbyname("krb_prop", "tcp")) == NULL) {
    klog(L_KRB_PERR, "kpropd: tcp/krb_prop: unknown service.");
    SlowDeath();
  }
  bzero((char *)&sin, sizeof(sin));
  sin.sin_port = sp->s_port;
  sin.sin_family = AF_INET;

  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    snprintf(errmsg, sizeof(errmsg), "kpropd: socket: %s", strerror(errno));
    klog(L_KRB_PERR, errmsg);
    SlowDeath();
  }
  if (bind(s, (struct sockaddr*)&sin, sizeof sin) < 0) {
    snprintf(errmsg, sizeof(errmsg), "kpropd: bind: %s", strerror(errno));
    klog(L_KRB_PERR, errmsg);
    SlowDeath();
  }
 
  if (!rflag) {
    kerror = krb_get_lrealm(my_realm,1);
    if (kerror != KSUCCESS) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: Can't get local realm. %s",
	       krb_err_txt[kerror]);
      klog (L_KRB_PERR, errmsg);
      SlowDeath();
    }
  }
    
  /* Responder uses KPROP_SERVICE_NAME.'hostname' and requestor always
   uses KPROP_SERVICE_NAME.KRB_MASTER (rcmd.kerberos) */
  strcpy(my_instance, "*");
    
  klog(L_KRB_PERR, "Established socket");

  listen(s, 5);
  for (;;) {
    from_len = sizeof from;
    if ((s2 = accept(s, (struct sockaddr *) &from, &from_len)) < 0) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: accept: %s", strerror(errno));
      klog(L_KRB_PERR, errmsg);
      continue;
    }
    strcpy(from_str, inet_ntoa(from.sin_addr));

    if ((hp = gethostbyaddr((char*)&(from.sin_addr.s_addr), from_len, AF_INET)) == NULL) {
      strcpy(hostname, "UNKNOWN");
    } else {
      strcpy(hostname, hp->h_name);
    }

    snprintf(errmsg, sizeof(errmsg), "Connection from %s, %s", hostname,
	     from_str);
    klog(L_KRB_PERR, errmsg);

    /* for krb_rd_{priv, safe} */
    n = sizeof sin;
    if (getsockname (s2, (struct sockaddr *) &sin, &n) != 0) {
      fprintf (stderr, "kpropd: can't get socketname.\n");
      perror ("getsockname");
      SlowDeath();
    }
    if (n != sizeof (sin)) {
      fprintf (stderr, "kpropd: can't get socketname. len");
      SlowDeath();
    }

    if ((fdlock = open(local_temp, O_WRONLY | O_CREAT, 0600)) < 0) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: open: %s", strerror(errno));
      klog(L_KRB_PERR, errmsg);
      SlowDeath();
    }
    if (flock(fdlock, LOCK_EX | LOCK_NB)) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: flock: %s", strerror(errno));
      klog(L_KRB_PERR, errmsg);
      SlowDeath();
    }
    if ((fd = creat(local_temp, 0600)) < 0) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: creat: %s", strerror(errno));
      klog(L_KRB_PERR, errmsg);
      SlowDeath();
    }
    if ((n = read (s2, buf, sizeof (kprop_version)))
	!= sizeof (kprop_version)) {
      klog (L_KRB_PERR, "kpropd: can't read kprop protocol version str.");
      SlowDeath();
    }
    if (strncmp (buf, kprop_version, sizeof (kprop_version))
	!= 0) {
      snprintf (errmsg, sizeof(errmsg), "kpropd: unsupported version %s", buf);
      klog (L_KRB_PERR, errmsg);
      SlowDeath();
    }

    if ((n = read (s2, &net_transfer_mode, sizeof (net_transfer_mode)))
	!= sizeof (net_transfer_mode)) {
      klog (L_KRB_PERR, "kpropd: can't read transfer mode.");
      SlowDeath();
    }
    transfer_mode = ntohs (net_transfer_mode);
    kerror = krb_recvauth(KOPT_DO_MUTUAL, s2, &ticket,
			  KPROP_SERVICE_NAME,
			  my_instance,
			  &from,
			  &sin,
			  &auth_dat,
			  srvtab,
			  session_sched,
			  version);
    if (kerror != KSUCCESS) {
      snprintf (errmsg, sizeof(errmsg), "kpropd: %s: Calling getkdata", 
	        krb_err_txt[kerror]);
      klog (L_KRB_PERR, errmsg);
      SlowDeath();
    }
	
    snprintf (errmsg, sizeof(errmsg), "kpropd: Connection from %s.%s@%s",
	      auth_dat.pname, auth_dat.pinst, auth_dat.prealm);
    klog (L_KRB_PERR, errmsg);

    /* AUTHORIZATION is done here.  We might want to expand this to
     * read an acl file at some point, but allowing for now
     * KPROP_SERVICE_NAME.KRB_MASTER@local-realm is fine ... */
    if (krb_get_admhst(admin, my_realm, 1) != KSUCCESS) {
      klog (L_KRB_PERR, "Unable to get admin host");
      SlowDeath();
    }
    if ((dot = strchr(admin, '.')) != NULL)
	*dot = '\0';

    if ((strcmp (KPROP_SERVICE_NAME, auth_dat.pname) != 0) ||
	(strcmp (admin, auth_dat.pinst) != 0) ||
	(strcmp (my_realm, auth_dat.prealm) != 0)) {
      klog (L_KRB_PERR, "Authorization denied!");
      SlowDeath();
    }

    switch (transfer_mode) {
    case KPROP_TRANSFER_PRIVATE: 
      recv_auth (s2, fd, 1	/* private */, &from, &sin, &auth_dat);
      break;
    case KPROP_TRANSFER_SAFE: 
      recv_auth (s2, fd, 0	/* safe */, &from, &sin, &auth_dat);
      break;
    case KPROP_TRANSFER_CLEAR: 
      recv_clear (s2, fd);
      break;
    default: 
      snprintf (errmsg, sizeof(errmsg), "kpropd: bad transfer mode %d",
		transfer_mode);
      klog (L_KRB_PERR, errmsg);
      SlowDeath();
    }

    if (transfer_mode != KPROP_TRANSFER_PRIVATE) {
      klog(L_KRB_PERR, "kpropd: non-private transfers not supported\n");
      SlowDeath();
#ifdef doesnt_work_yet
      lseek(fd, (long) 0, L_SET);
      if (auth_dat.checksum != get_data_checksum (fd, session_sched)) {
	klog(L_KRB_PERR, "kpropd: checksum doesn't match");
	SlowDeath();
      }
#endif
    } else

      {
	struct stat st;
	fstat(fd, &st);
	if (st.st_size != auth_dat.checksum) {
	  klog(L_KRB_PERR, "kpropd: length doesn't match");
	  SlowDeath();
	}
      }
    close(fd);
    close(s2);
    klog(L_KRB_PERR, "File received.");

    if (rename(local_temp, local_file) < 0) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: rename: %s", strerror(errno));
      klog(L_KRB_PERR, errmsg);
      SlowDeath();
    }
    klog(L_KRB_PERR, "Temp file renamed to %s", local_file);

    if (flock(fdlock, LOCK_UN)) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: flock (unlock): %s",
	       strerror(errno));
      klog(L_KRB_PERR, errmsg);
      SlowDeath();
    }
    close(fdlock);
    snprintf(cmd, sizeof(cmd), "%s load %s %s\n", kdb_util_path, local_file,
	     local_db);
    if (system (cmd) != 0) {
      klog (L_KRB_PERR, "Couldn't load database");
      SlowDeath();
    }
  }
}

#ifdef doesnt_work_yet
unsigned long get_data_checksum(fd, key_sched)
     int fd;
     des_key_schedule key_sched;
{
  unsigned long cksum = 0;
  unsigned long cbc_cksum();
  int n;
  char buf[BUFSIZ];
  char obuf[8];

  while (n = read(fd, buf, sizeof buf)) {
    if (n < 0) {
      snprintf(errmsg, sizeof(errmsg), "kpropd: read (in checksum test): %s",
	       strerror(errno));
      klog(L_KRB_PERR, errmsg);
      SlowDeath();
    }
#ifndef NOENCRYPTION
    cksum += des_cbc_cksum(buf, obuf, n, key_sched, key_sched);
#endif
  }
  return cksum;
}
#endif