aboutsummaryrefslogtreecommitdiff
path: root/src/kadmin/v4server/kadm_server.c
blob: 687259bf5d5fbf862e88feef0c885f0cd4aa0256 (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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
 * kadmin/v4server/kadm_server.c
 *
 * Copyright 1988 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Kerberos administration server-side subroutines
 */


#include <mit-copyright.h>

#include "k5-int.h"

#include <stdio.h>
#include <string.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#ifdef TIME_WITH_SYS_TIME
#include <time.h>
#endif
#else
#include <time.h>
#endif
#include <syslog.h>

#ifdef KADM5
#include <com_err.h>
#include <kadm5/admin.h>
#include <kadm5/chpass_util_strings.h>
#include <krb5/kdb.h>
extern void *kadm5_handle;
#endif

#include <kadm.h>
#include <kadm_err.h>

extern krb5_context kadm_context;
int fascist_cpw = 0;		/* Be fascist about insecure passwords? */

char pw_required[] = "The version of kpasswd that you are using is not compatible with the\nOpenV*Secure V4 Administration Server.  Please contact your security\nadministrator.\n\n";

char bad_pw_err[] =
	"\007\007\007ERROR: Insecure password not accepted.  Please choose another.\n\n";

char bad_pw_warn[] =
	"\007\007\007WARNING: You have chosen an insecure password.  You may wish to\nchoose a better password.\n\n";

char check_pw_msg[] =
	"You have entered an insecure password.  You should choose another.\n\n";

char pw_blurb[] =
"A good password is something which is easy for you to remember, but that\npeople who know you won't easily guess; so don't use your name, or your\ndog's name, or a word from the dictionary.  Passwords should be at least\n6 characters long, and may contain UPPER- and lower-case letters,\nnumbers, or punctuation.  A good password can be:\n\n   -- some initials, like \"GykoR-66\" for \"Get your kicks on Rte 66.\"\n   -- an easily pronounced nonsense word, like \"slaRooBey\" or \"krang-its\"\n   -- a mis-spelled phrase, like \"2HotPeetzas\" or \"ItzAGurl\"\n\nPlease Note: It is important that you do not tell ANYONE your password,\nincluding your friends, or even people from Athena or Information\nSystems.  Remember, *YOU* are assumed to be responsible for anything\ndone using your password.\n";

/* from V4 month_sname.c --  was not part of API */
/*
 * Given an integer 1-12, month_sname() returns a string
 * containing the first three letters of the corresponding
 * month.  Returns 0 if the argument is out of range.
 */

static char *month_sname(n)
    int n;
{
    static char *name[] = {
        "Jan","Feb","Mar","Apr","May","Jun",
        "Jul","Aug","Sep","Oct","Nov","Dec"
    };
    return((n < 1 || n > 12) ? 0 : name [n-1]);
}

/* from V4 log.c --  was not part of API */

/*
 * krb_log() is used to add entries to the logfile (see krb_set_logfile()
 * below).  Note that it is probably not portable since it makes
 * assumptions about what the compiler will do when it is called
 * with less than the correct number of arguments which is the
 * way it is usually called.
 *
 * The log entry consists of a timestamp and the given arguments
 * printed according to the given "format".
 *
 * The log file is opened and closed for each log entry.
 *
 * The return value is undefined.
 */

/* static char *log_name = KRBLOG; */
/* KRBLOG is in the V4 klog.h but may not correspond to anything installed. */
static char *log_name = KADM_SYSLOG; 

static void krb_log(format,a1,a2,a3,a4,a5,a6,a7,a8,a9,a0)
    char *format;
    char *a1,*a2,*a3,*a4,*a5,*a6,*a7,*a8,*a9,*a0;
{
    FILE *logfile;
    time_t now;
    struct tm *tm;

    if ((logfile = fopen(log_name,"a")) == NULL)
        return;

    (void) time(&now);
    tm = localtime(&now);

    fprintf(logfile,"%2d-%s-%d %02d:%02d:%02d ",tm->tm_mday,
            month_sname(tm->tm_mon + 1),1900+tm->tm_year,
            tm->tm_hour, tm->tm_min, tm->tm_sec);
    fprintf(logfile,format,a1,a2,a3,a4,a5,a6,a7,a8,a9,a0);
    fprintf(logfile,"\n");
    (void) fclose(logfile);
    return;
}


/* 
kadm_ser_cpw - the server side of the change_password routine
  recieves    : KTEXT, {key}
  returns     : CKSUM, RETCODE
  acl         : caller can change only own password

Replaces the password (i.e. des key) of the caller with that specified in key.
Returns no actual data from the master server, since this is called by a user
*/
kadm_ser_cpw(dat, len, ad, datout, outlen)
u_char *dat;
int len;
AUTH_DAT *ad;
u_char **datout;
int *outlen;
{
    krb5_int32 keylow, keyhigh;
    char pword[MAX_KPW_LEN];
    int no_pword = 0;
    des_cblock newkey;
    int status, stvlen = 0;
    int	retval;
    extern int kadm_approve_pw();
#ifdef KADM5
    kadm5_principal_ent_rec princ_ent;
    kadm5_policy_ent_rec pol_ent;
    krb5_principal user_princ;
    char msg_ret[1024], *time_string, *ptr;
    const char *msg_ptr;
    krb5_int32 now;
    time_t until;
    krb5_keyblock newkb;
#endif

    /* take key off the stream, and change the database */

    if ((status = stv_long(dat, &keyhigh, 0, len)) < 0)
	return(KADM_LENGTH_ERROR);
    stvlen += status;
    if ((status = stv_long(dat, &keylow, stvlen, len)) < 0)
	return(KADM_LENGTH_ERROR);
    stvlen += status;
    if ((stvlen = stv_string(dat, pword, stvlen, sizeof(pword), len)) < 0) {
	no_pword++;
	pword[0]='\0';
    }
    stvlen += status;

    keylow = ntohl(keylow);
    keyhigh = ntohl(keyhigh);
    memcpy((char *)(((krb5_int32 *)newkey) + 1), (char *)&keyhigh, 4);
    memcpy((char *)newkey, (char *)&keylow, 4);

    if (retval = kadm_approve_pw(ad->pname, ad->pinst, ad->prealm,
			newkey, no_pword ? 0 : pword)) {
	    if (retval == KADM_PW_MISMATCH) {
		    /*
		     * Very strange!!!  This means that the cleartext
		     * password which was sent and the DES cblock
		     * didn't match!
		     */
		    syslog(LOG_ERR, "'%s.%s@%s' sent a password string which didn't match with the DES key?!?",
				   ad->pname, ad->pinst, ad->prealm);
		    return(retval);
	    }
	    if (fascist_cpw) {
		    *outlen = strlen(bad_pw_err)+strlen(pw_blurb)+1;
		    if (*datout = (u_char *) malloc(*outlen)) {
			    strcpy((char *) *datout, bad_pw_err);
			    strcat((char *) *datout, pw_blurb);
		    } else
			    *outlen = 0;
		    syslog(LOG_ERR, "'%s.%s@%s' tried to use an insecure password in changepw",
				   ad->pname, ad->pinst, ad->prealm);
#ifdef notdef
		    /* For debugging only, probably a bad idea */
		    if (!no_pword)
			    (void) krb_log("The password was %s\n", pword);
#endif
		    return(retval);
	    } else {
		    *outlen = strlen(bad_pw_warn) + strlen(pw_blurb)+1;
		    if (*datout = (u_char *) malloc(*outlen)) {
			    strcpy((char *) *datout, bad_pw_warn);
			    strcat((char *) *datout, pw_blurb);
		    } else
			    *outlen = 0;
		    syslog(LOG_ERR, "'%s.%s@%s' used an insecure password in changepw",
				   ad->pname, ad->pinst, ad->prealm);
	    }
    } else {
	    *datout = 0;
	    *outlen = 0;
    }

#ifdef KADM5
    if (no_pword)
	syslog(LOG_WARNING,
	       "Old-style change password request from '%s.%s@%s'!",
	       ad->pname, ad->pinst, ad->prealm);
    else {
	/*
	 * We don't use the client-provided key itself, if there is a
	 * password provided.
	 */
	keylow = keyhigh = 0;
	memset(newkey, 0, sizeof(newkey));
    }

    syslog(LOG_INFO, "'%s.%s@%s' wants to change its password",
	   ad->pname, ad->pinst, ad->prealm);
    if ((krb5_425_conv_principal(kadm_context, ad->pname,
				 *ad->pinst ? ad->pinst : 0,
				 ad->prealm, &user_princ))) {
	/* this should never happen */
	memset(newkey, 0, sizeof(newkey));
	return KADM_NOENTRY;
    }

    *outlen = 0;

    if (retval = krb5_timeofday(kadm_context, &now)) {
	 msg_ptr = error_message(retval);
	 goto send_response;
    }

    /* Use the default mask for now. */
    retval = kadm5_get_principal(kadm5_handle, user_princ,
				 &princ_ent,
				 KADM5_PRINCIPAL_NORMAL_MASK);
    if (retval != 0) {
	 msg_ptr = error_message(retval);
	 goto send_response;
    }

    /*
     * This daemon necessarily has the modify privilege, so
     * kadm5_chpass_principal will allow it to violate the
     * policy's minimum lifetime.  Since that's A Bad Thing, we need
     * to enforce it ourselves.  Unfortunately, this means we are
     * duplicating code from both ovsec_adm_server and
     * kadm5_chpass_util().
     */
    if (princ_ent.aux_attributes & KADM5_POLICY) {
	 retval = kadm5_get_policy(kadm5_handle,
				   princ_ent.policy,
				   &pol_ent);
	 if (retval != 0) {
	      (void) kadm5_free_principal_ent(kadm5_handle, &princ_ent);
	      msg_ptr = error_message(retval);
	      goto send_response;
	 }

	 /* make "now" a boolean, true == too soon */
	 now = ((now - princ_ent.last_pwd_change) < pol_ent.pw_min_life);

	 (void) kadm5_free_policy_ent(kadm5_handle, &pol_ent);
	 
	 if(now && !(princ_ent.attributes & KRB5_KDB_REQUIRES_PWCHANGE)) {
	      (void) kadm5_free_principal_ent(kadm5_handle, &princ_ent);
	      retval = CHPASS_UTIL_PASSWORD_TOO_SOON;

	      until = princ_ent.last_pwd_change + pol_ent.pw_min_life;
	      time_string = ctime(&until);
				  
	      if (*(ptr = &time_string[strlen(time_string)-1]) == '\n')
		   *ptr = '\0';
	      
	      sprintf(msg_ret, error_message(CHPASS_UTIL_PASSWORD_TOO_SOON), 
		      time_string);
	      msg_ptr = msg_ret;
	      
	      goto send_response;
	 }
    }

    (void) kadm5_free_principal_ent(kadm5_handle, &princ_ent);

    if (no_pword) {
	newkb.magic = KV5M_KEYBLOCK;
	if ((newkb.contents = (krb5_octet *)malloc(8)) == NULL) {
	    retval = KADM_NOMEM;
	    goto send_response;
	}
	newkb.length = 8;
	newkb.enctype = ENCTYPE_DES_CBC_CRC;
	memcpy((char *)newkb.contents, newkey, 8);
	retval = kadm5_setv4key_principal(kadm5_handle, user_princ,
					  &newkb);
	memset(newkb.contents, 0, 8);
	*msg_ret = '\0';
    } else {
	retval = kadm5_chpass_principal_util(kadm5_handle, user_princ,
					     pword, NULL, msg_ret);
	msg_ptr = msg_ret;
    }
    (void) krb5_free_principal(kadm_context, user_princ);

send_response:

    memset(newkey, 0, sizeof(newkey));
    retval = convert_kadm5_to_kadm(retval);

    if (retval) {
	 /* don't send message on success because kpasswd.v4 will */
	 /* print "password changed" too */
	 *outlen = strlen(msg_ptr)+2;
	 if (*datout = (u_char *) malloc(*outlen)) {
	      strcpy(*datout, msg_ptr);
	      strcat(*datout, "\n");
	 } else
	      *outlen = 0;
    } else {
         syslog(LOG_INFO,
		"'%s.%s@%s' password changed.", 
		ad->pname, ad->pinst, ad->prealm);
    }
    if (retval == KADM_INSECURE_PW) {
          syslog(LOG_ERR, 
		 "'%s.%s@%s' tried to use an insecure password in changepw",
		 ad->pname, ad->pinst, ad->prealm);
    }
#else /* KADM5 */
    retval = kadm_change(ad->pname, ad->pinst, ad->prealm, newkey);
    keylow = keyhigh = 0;
    memset(newkey, 0, sizeof(newkey));
#endif /* KADM5 */
    
    return retval;
}

/*
kadm_ser_add - the server side of the add_entry routine
  recieves    : KTEXT, {values}
  returns     : CKSUM, RETCODE, {values}
  acl         : su, sms (as alloc)

Adds and entry containing values to the database
returns the values of the entry, so if you leave certain fields blank you will
   be able to determine the default values they are set to
*/
int
kadm_ser_add(dat,len,ad, datout, outlen)
u_char *dat;
int len;
AUTH_DAT *ad;
u_char **datout;
int *outlen;
{
  Kadm_vals values, retvals;
  int status;

  if ((status = stream_to_vals(dat, &values, len)) < 0)
      return(KADM_LENGTH_ERROR);
  if ((status = kadm_add_entry(ad->pname, ad->pinst, ad->prealm,
			      &values, &retvals)) == KADM_DATA) {
      *outlen = vals_to_stream(&retvals,datout);
      retvals.key_low = retvals.key_high = 0;
      return KADM_SUCCESS;
  } else {
      *outlen = 0;
      return status;
  }
}

#ifndef KADM5
/*
kadm_ser_del - the server side of the del_entry routine
  recieves    : KTEXT, {values}
  returns     : CKSUM, RETCODE, {values}
  acl         : su, sms (as alloc)

Deletes an entry containing values to the database
returns the values of the entry, so if you leave certain fields blank you will
   be able to determine the default values they are set to
*/
kadm_ser_del(dat,len,ad, datout, outlen)
u_char *dat;
int len;
AUTH_DAT *ad;
u_char **datout;
int *outlen;
{
  Kadm_vals values, retvals;
  int status;

  if ((status = stream_to_vals(dat, &values, len)) < 0)
      return(KADM_LENGTH_ERROR);
  if ((status = kadm_del_entry(ad->pname, ad->pinst, ad->prealm,
			      &values, &retvals)) == KADM_DATA) {
      *outlen = vals_to_stream(&retvals,datout);
      retvals.key_low = retvals.key_high = 0;
      return KADM_SUCCESS;
  } else {
      *outlen = 0;
      return status;
  }
}

#endif /* !KADM5 */

/*
kadm_ser_mod - the server side of the mod_entry routine
  recieves    : KTEXT, {values, values}
  returns     : CKSUM, RETCODE, {values}
  acl         : su, sms (as register or dealloc)

Modifies all entries corresponding to the first values so they match the
   second values.
returns the values for the changed entries
*/
kadm_ser_mod(dat,len,ad, datout, outlen)
u_char *dat;
int len;
AUTH_DAT *ad;
u_char **datout;
int *outlen;
{
  Kadm_vals vals1, vals2, retvals;
  int wh;
  int status;

  if ((wh = stream_to_vals(dat, &vals1, len)) < 0)
      return KADM_LENGTH_ERROR;
  if ((status = stream_to_vals(dat+wh,&vals2, len-wh)) < 0)
      return KADM_LENGTH_ERROR;
  if ((status = kadm_mod_entry(ad->pname, ad->pinst, ad->prealm, &vals1,
			       &vals2, &retvals)) == KADM_DATA) {
      *outlen = vals_to_stream(&retvals,datout);
      retvals.key_low = retvals.key_high = 0;
      return KADM_SUCCESS;
  } else {
      *outlen = 0;
      return status;
  }
}

/*
kadm_ser_get
  recieves   : KTEXT, {values, flags}
  returns    : CKSUM, RETCODE, {count, values, values, values}
  acl        : su

gets the fields requested by flags from all entries matching values
returns this data for each matching recipient, after a count of how many such
  matches there were
*/
kadm_ser_get(dat,len,ad, datout, outlen)
u_char *dat;
int len;
AUTH_DAT *ad;
u_char **datout;
int *outlen;
{
  Kadm_vals values, retvals;
  u_char fl[FLDSZ];
  int loop,wh;
  int status;

  if ((wh = stream_to_vals(dat, &values, len)) < 0)
      return KADM_LENGTH_ERROR;
  if (wh + FLDSZ > len)
      return KADM_LENGTH_ERROR;
  for (loop=FLDSZ-1; loop>=0; loop--)
    fl[loop] = dat[wh++];
  if ((status = kadm_get_entry(ad->pname, ad->pinst, ad->prealm,
			      &values, fl, &retvals)) == KADM_DATA) {
      *outlen = vals_to_stream(&retvals,datout);
      retvals.key_low = retvals.key_high = 0;
      return KADM_SUCCESS;
  } else {
      *outlen = 0;
      return status;
  }
}

/* 
kadm_ser_ckpw - the server side of the check_password routine
  recieves    : KTEXT, {key}
  returns     : CKSUM, RETCODE
  acl         : none

Checks to see if the des key passed from the caller is a "secure" password.
*/
kadm_ser_ckpw(dat, len, ad, datout, outlen)
u_char *dat;
int len;
AUTH_DAT *ad;
u_char **datout;
int *outlen;
{
    krb5_ui_4 keylow, keyhigh;
    char pword[MAX_KPW_LEN];
    int no_pword = 0;
    des_cblock newkey;
    int stvlen = 0,status;
    int	retval;
    extern int kadm_approve_pw();

    /* take key off the stream, and check it */

    if ((status = stv_long(dat, &keyhigh, 0, len)) < 0)
	return(KADM_LENGTH_ERROR);
    stvlen += status;
    if ((status = stv_long(dat, &keylow, stvlen, len)) < 0)
	return(KADM_LENGTH_ERROR);
    stvlen += status;
    if ((status = stv_string(dat, pword, stvlen, sizeof(pword), len)) < 0) {
	no_pword++;
	pword[0]='\0';
    }
    stvlen += status;

    keylow = ntohl(keylow);
    keyhigh = ntohl(keyhigh);
    memcpy((char *)(((krb5_int32 *)newkey) + 1), (char *)&keyhigh, 4);
    memcpy((char *)newkey, (char *)&keylow, 4);
    keylow = keyhigh = 0;
    retval = kadm_approve_pw(ad->pname, ad->pinst, ad->prealm, newkey,
			no_pword ? 0 : pword);
    memset(newkey, 0, sizeof(newkey));
    if (retval) {
	    *outlen = strlen(check_pw_msg)+strlen(pw_blurb)+1;
	    if (*datout = (u_char *) malloc(*outlen)) {
		    strcpy((char *) *datout, check_pw_msg);
		    strcat((char *) *datout, pw_blurb);
	    } else
		    *outlen = 0;
	    (void) krb_log("'%s.%s@%s' sent an insecure password to be checked",
			   ad->pname, ad->pinst, ad->prealm);
	    return(retval);
    } else {
	    *datout = 0;
	    *outlen = 0;
	    (void) krb_log("'%s.%s@%s' sent a secure password to be checked",
			   ad->pname, ad->pinst, ad->prealm);
    }
    return(0);
}

/*
kadm_ser_stab - the server side of the change_srvtab routine
  recieves    : KTEXT, {values}
  returns     : CKSUM, RETCODE, {values}
  acl         : su, sms (as register or dealloc)

Creates or modifies the specified service principal to have a random
key, which is sent back to the client.  The key version is returned in
the max_life field of the values structure.  It's a hack, but it's a
backwards compatible hack....
*/
kadm_ser_stab(dat, len, ad, datout, outlen)
u_char *dat;
int len;
AUTH_DAT *ad;
u_char **datout;
int *outlen;
{
  Kadm_vals values;
  int status;

  if ((status = stream_to_vals(dat, &values, len)) < 0)
	  return KADM_LENGTH_ERROR;
  status = kadm_chg_srvtab(ad->pname, ad->pinst, ad->prealm, &values);
  if (status == KADM_DATA) {
      *outlen = vals_to_stream(&values,datout);
      values.key_low = values.key_high = 0;
      return KADM_SUCCESS;
  } else {
      *outlen = 0;
      return status;
  }
}