xref: /onnv-gate/usr/src/cmd/krb5/kadmin/dbutil/kadm5_create.c (revision 4960:a4746a82a247)
10Sstevel@tonic-gate /*
2*4960Swillf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * $Id: kadm5_create.c,v 1.6 1998/10/30 02:52:37 marc Exp $
110Sstevel@tonic-gate  * $Source: /cvs/krbdev/krb5/src/kadmin/dbutil/kadm5_create.c,v $
120Sstevel@tonic-gate  */
130Sstevel@tonic-gate 
140Sstevel@tonic-gate /*
150Sstevel@tonic-gate  * Copyright (C) 1998 by the FundsXpress, INC.
160Sstevel@tonic-gate  *
170Sstevel@tonic-gate  * All rights reserved.
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * Export of this software from the United States of America may require
200Sstevel@tonic-gate  * a specific license from the United States Government.  It is the
210Sstevel@tonic-gate  * responsibility of any person or organization contemplating export to
220Sstevel@tonic-gate  * obtain such a license before exporting.
230Sstevel@tonic-gate  *
240Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
250Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
260Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
270Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
280Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
290Sstevel@tonic-gate  * the name of FundsXpress. not be used in advertising or publicity pertaining
300Sstevel@tonic-gate  * to distribution of the software without specific, written prior
310Sstevel@tonic-gate  * permission.  FundsXpress makes no representations about the suitability of
320Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
330Sstevel@tonic-gate  * or implied warranty.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
360Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
370Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "string_table.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <stdlib.h>
440Sstevel@tonic-gate #include <string.h>
45*4960Swillf #include <k5-int.h>
46*4960Swillf #include <kdb.h>
470Sstevel@tonic-gate #include <kadm5/admin.h>
482881Smp153739 #include <krb5/adm_proto.h>
492881Smp153739 
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <krb5.h>
520Sstevel@tonic-gate #include <krb5/kdb.h>
53*4960Swillf #include "kdb5_util.h"
540Sstevel@tonic-gate #include <libintl.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate int
570Sstevel@tonic-gate add_admin_old_princ(void *handle, krb5_context context,
580Sstevel@tonic-gate 		    char *name, char *realm, int attrs, int lifetime);
590Sstevel@tonic-gate int
600Sstevel@tonic-gate add_admin_sname_princ(void *handle, krb5_context context,
610Sstevel@tonic-gate     char *sname, int attrs, int lifetime);
620Sstevel@tonic-gate int
630Sstevel@tonic-gate add_admin_princ(void *handle, krb5_context context,
640Sstevel@tonic-gate     krb5_principal principal, int attrs, int lifetime);
650Sstevel@tonic-gate 
662881Smp153739 static int add_admin_princs(void *handle, krb5_context context, char *realm);
672881Smp153739 
682881Smp153739 #define ERR 1
692881Smp153739 #define OK 0
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #define ADMIN_LIFETIME 60*60*3 /* 3 hours */
720Sstevel@tonic-gate #define CHANGEPW_LIFETIME 60*5 /* 5 minutes */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate extern char *progname;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Function: kadm5_create
780Sstevel@tonic-gate  *
790Sstevel@tonic-gate  * Purpose: create admin principals in KDC database
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * Arguments:	params	(r) configuration parameters to use
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * Effects:  Creates KADM5_ADMIN_SERVICE and KADM5_CHANGEPW_SERVICE
840Sstevel@tonic-gate  * principals in the KDC database and sets their attributes
850Sstevel@tonic-gate  * appropriately.
860Sstevel@tonic-gate  */
872881Smp153739 int kadm5_create(kadm5_config_params *params)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate      int retval;
900Sstevel@tonic-gate      krb5_context context;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate      kadm5_config_params lparams;
930Sstevel@tonic-gate 
94*4960Swillf      if ((retval = kadm5_init_krb5_context(&context)))
952881Smp153739 	  exit(ERR);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate      (void) memset(&lparams, 0, sizeof (kadm5_config_params));
980Sstevel@tonic-gate 
990Sstevel@tonic-gate      /*
1000Sstevel@tonic-gate       * The lock file has to exist before calling kadm5_init, but
1010Sstevel@tonic-gate       * params->admin_lockfile may not be set yet...
1020Sstevel@tonic-gate       */
1032881Smp153739      if ((retval = kadm5_get_config_params(context, NULL, NULL,
1042881Smp153739 					   params, &lparams))) {
1052881Smp153739 	com_err(progname, retval, gettext("while looking up the Kerberos configuration"));
1062881Smp153739 	  return 1;
1070Sstevel@tonic-gate      }
1082881Smp153739 
1090Sstevel@tonic-gate      retval = kadm5_create_magic_princs(&lparams, context);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate      kadm5_free_config_params(context, &lparams);
1120Sstevel@tonic-gate      krb5_free_context(context);
1130Sstevel@tonic-gate 
1142881Smp153739      return retval;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1172881Smp153739 int kadm5_create_magic_princs(kadm5_config_params *params,
1182881Smp153739 			      krb5_context context)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate      int retval;
1210Sstevel@tonic-gate      void *handle;
1220Sstevel@tonic-gate 
1232881Smp153739      retval = krb5_klog_init(context, "admin_server", progname, 0);
1242881Smp153739      if (retval)
1252881Smp153739 	  return retval;
1260Sstevel@tonic-gate      if ((retval = kadm5_init(progname, NULL, NULL, params,
1270Sstevel@tonic-gate 			      KADM5_STRUCT_VERSION,
1280Sstevel@tonic-gate 			      KADM5_API_VERSION_2,
129*4960Swillf 			      db5util_db_args,
1300Sstevel@tonic-gate 			      &handle))) {
1312881Smp153739 	com_err(progname, retval,  gettext("while initializing the Kerberos admin interface"));
1322881Smp153739 	  return retval;
1330Sstevel@tonic-gate      }
1342881Smp153739 
1350Sstevel@tonic-gate      retval = add_admin_princs(handle, context, params->realm);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate      kadm5_destroy(handle);
1380Sstevel@tonic-gate 
1392881Smp153739      krb5_klog_close(context);
1402881Smp153739 
1412881Smp153739      return retval;
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * Function: build_name_with_realm
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  * Purpose: concatenate a name and a realm to form a krb5 name
1480Sstevel@tonic-gate  *
1490Sstevel@tonic-gate  * Arguments:
1500Sstevel@tonic-gate  *
1510Sstevel@tonic-gate  * 	name	(input) the name
1520Sstevel@tonic-gate  * 	realm	(input) the realm
1530Sstevel@tonic-gate  *
1540Sstevel@tonic-gate  * Returns:
1550Sstevel@tonic-gate  *
1560Sstevel@tonic-gate  * 	pointer to name@realm, in allocated memory, or NULL if it
1570Sstevel@tonic-gate  * 	cannot be allocated
1580Sstevel@tonic-gate  *
1590Sstevel@tonic-gate  * Requires: both strings are null-terminated
1600Sstevel@tonic-gate  */
1612881Smp153739 static char *build_name_with_realm(char *name, char *realm)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate      char *n;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate      n = (char *) malloc(strlen(name) + strlen(realm) + 2);
1660Sstevel@tonic-gate      sprintf(n, "%s@%s", name, realm);
1672881Smp153739      return n;
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * Function: add_admin_princs
1720Sstevel@tonic-gate  *
1730Sstevel@tonic-gate  * Purpose: create admin principals
1740Sstevel@tonic-gate  *
1750Sstevel@tonic-gate  * Arguments:
1760Sstevel@tonic-gate  *
1770Sstevel@tonic-gate  * 	rseed		(input) random seed
1780Sstevel@tonic-gate  * 	realm		(input) realm, or NULL for default realm
1790Sstevel@tonic-gate  *      <return value>  (output) status, 0 for success, 1 for serious error
1800Sstevel@tonic-gate  *
1810Sstevel@tonic-gate  * Requires:
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * Effects:
1840Sstevel@tonic-gate  *
1850Sstevel@tonic-gate  * add_admin_princs creates KADM5_ADMIN_SERVICE,
1860Sstevel@tonic-gate  * KADM5_CHANGEPW_SERVICE.  If any of these exist a message is
1870Sstevel@tonic-gate  * printed.  If any of these existing principal do not have the proper
1880Sstevel@tonic-gate  * attributes, a warning message is printed.
1890Sstevel@tonic-gate  */
1902881Smp153739 static int add_admin_princs(void *handle, krb5_context context, char *realm)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate   krb5_error_code ret = 0;
1931508Smp153739 
1941508Smp153739 /*
1951508Smp153739  * Solaris Kerberos:
1961508Smp153739  * The kadmin/admin principal is unused on Solaris. This principal is used
1971508Smp153739  * in AUTH_GSSAPI but Solaris doesn't support AUTH_GSSAPI. RPCSEC_GSS can only
1981508Smp153739  * be used with host-based principals.
1991508Smp153739  *
2001508Smp153739  */
2011508Smp153739 
2021508Smp153739 #if 0
2031508Smp153739   if ((ret = add_admin_old_princ(handle, context,
2041508Smp153739   		     KADM5_ADMIN_SERVICE, realm,
2051508Smp153739   		     KRB5_KDB_DISALLOW_TGT_BASED,
2061508Smp153739   		     ADMIN_LIFETIME)))
2071508Smp153739      goto clean_and_exit;
2081508Smp153739 #endif
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if ((ret = add_admin_old_princ(handle, context,
2110Sstevel@tonic-gate 			     KADM5_CHANGEPW_SERVICE, realm,
2120Sstevel@tonic-gate 			     KRB5_KDB_DISALLOW_TGT_BASED |
2130Sstevel@tonic-gate 			     KRB5_KDB_PWCHANGE_SERVICE,
2140Sstevel@tonic-gate 			     CHANGEPW_LIFETIME)))
2150Sstevel@tonic-gate        goto clean_and_exit;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if ((ret = add_admin_sname_princ(handle, context,
2180Sstevel@tonic-gate 		    KADM5_ADMIN_HOST_SERVICE,
2190Sstevel@tonic-gate 		    KRB5_KDB_DISALLOW_TGT_BASED,
2200Sstevel@tonic-gate 		    ADMIN_LIFETIME)))
2210Sstevel@tonic-gate 		goto clean_and_exit;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if ((ret = add_admin_sname_princ(handle, context,
2240Sstevel@tonic-gate 		    KADM5_CHANGEPW_HOST_SERVICE,
2250Sstevel@tonic-gate 		    KRB5_KDB_DISALLOW_TGT_BASED |
2260Sstevel@tonic-gate 		    KRB5_KDB_PWCHANGE_SERVICE,
2270Sstevel@tonic-gate 		    ADMIN_LIFETIME)))
2280Sstevel@tonic-gate 		goto clean_and_exit;
2290Sstevel@tonic-gate 
2301508Smp153739 	if ((ret = add_admin_sname_princ(handle, context,
2311508Smp153739 		    KADM5_KIPROP_HOST_SERVICE,
2321508Smp153739 		    KRB5_KDB_DISALLOW_TGT_BASED,
2331508Smp153739 		    ADMIN_LIFETIME)))
2341508Smp153739 		goto clean_and_exit;
2351508Smp153739 
2360Sstevel@tonic-gate clean_and_exit:
2370Sstevel@tonic-gate 
2382881Smp153739   return ret;
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * Function: add_admin_princ
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * Arguments:
2450Sstevel@tonic-gate  *
2460Sstevel@tonic-gate  * 	creator		(r) principal to use as "mod_by"
2470Sstevel@tonic-gate  * 	rseed		(r) seed for random key generator
2480Sstevel@tonic-gate  *	principal	(r) kerberos principal to add
2490Sstevel@tonic-gate  * 	attrs		(r) principal's attributes
2500Sstevel@tonic-gate  * 	lifetime	(r) principal's max life, or 0
2510Sstevel@tonic-gate  * 	not_unique	(r) error message for multiple entries, never used
2520Sstevel@tonic-gate  * 	exists		(r) warning message for principal exists
2530Sstevel@tonic-gate  * 	wrong_attrs	(r) warning message for wrong attributes
2540Sstevel@tonic-gate  *
2550Sstevel@tonic-gate  * Returns:
2560Sstevel@tonic-gate  *
2572881Smp153739  * 	OK on success
2582881Smp153739  * 	ERR on serious errors
2590Sstevel@tonic-gate  *
2600Sstevel@tonic-gate  * Effects:
2610Sstevel@tonic-gate  *
2620Sstevel@tonic-gate  * If the principal is not unique, not_unique is printed (but this
2630Sstevel@tonic-gate  * never happens).  If the principal exists, then exists is printed
2640Sstevel@tonic-gate  * and if the principals attributes != attrs, wrong_attrs is printed.
2650Sstevel@tonic-gate  * Otherwise, the principal is created with mod_by creator and
2660Sstevel@tonic-gate  * attributes attrs and max life of lifetime (if not zero).
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate 
2692881Smp153739 int add_admin_princ(void *handle, krb5_context context,
2700Sstevel@tonic-gate     krb5_principal principal, int attrs, int lifetime)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate      char *fullname;
2730Sstevel@tonic-gate      krb5_error_code ret;
2740Sstevel@tonic-gate      kadm5_principal_ent_rec ent;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate      memset(&ent, 0, sizeof(ent));
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (krb5_unparse_name(context, principal, &fullname))
2792881Smp153739 		return ERR;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate      ent.principal = principal;
2820Sstevel@tonic-gate      ent.max_life = lifetime;
2830Sstevel@tonic-gate      ent.attributes = attrs | KRB5_KDB_DISALLOW_ALL_TIX;
2840Sstevel@tonic-gate 
2852881Smp153739      ret = kadm5_create_principal(handle, &ent,
2862881Smp153739 				  (KADM5_PRINCIPAL | KADM5_MAX_LIFE |
2872881Smp153739 				   KADM5_ATTRIBUTES),
2882881Smp153739 				  "to-be-random");
2892881Smp153739      if (ret) {
2900Sstevel@tonic-gate 	  if (ret != KADM5_DUP) {
2910Sstevel@tonic-gate 		com_err(progname, ret,
2920Sstevel@tonic-gate 			gettext(str_PUT_PRINC), fullname);
2930Sstevel@tonic-gate 	       krb5_free_principal(context, ent.principal);
2940Sstevel@tonic-gate 	       free(fullname);
2952881Smp153739 	       return ERR;
2960Sstevel@tonic-gate 	  }
2970Sstevel@tonic-gate      } else {
2980Sstevel@tonic-gate 	  /* only randomize key if we created the principal */
2990Sstevel@tonic-gate 	ret = kadm5_randkey_principal(handle, ent.principal, NULL, NULL);
3000Sstevel@tonic-gate 	if (ret) {
3010Sstevel@tonic-gate 		com_err(progname, ret,
3020Sstevel@tonic-gate 			gettext(str_RANDOM_KEY), fullname);
3032881Smp153739 	       krb5_free_principal(context, ent.principal);
3042881Smp153739 	       free(fullname);
3052881Smp153739 	       return ERR;
3062881Smp153739 	  }
3072881Smp153739 
3082881Smp153739 	  ent.attributes = attrs;
3092881Smp153739 	  ret = kadm5_modify_principal(handle, &ent, KADM5_ATTRIBUTES);
3102881Smp153739 	  if (ret) {
3112881Smp153739 	      com_err(progname, ret,
3122881Smp153739 	       gettext(str_PUT_PRINC), fullname);
3132881Smp153739 	       krb5_free_principal(context, ent.principal);
3142881Smp153739 	       free(fullname);
3152881Smp153739 	       return ERR;
3162881Smp153739 	  }
3172881Smp153739      }
3180Sstevel@tonic-gate 
3192881Smp153739      krb5_free_principal(context, ent.principal);
3202881Smp153739      free(fullname);
3210Sstevel@tonic-gate 
3222881Smp153739      return OK;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate int
3260Sstevel@tonic-gate add_admin_old_princ(void *handle, krb5_context context,
3270Sstevel@tonic-gate     char *name, char *realm, int attrs, int lifetime)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	char *fullname;
3300Sstevel@tonic-gate 	krb5_error_code ret;
3310Sstevel@tonic-gate 	krb5_principal principal;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	fullname = build_name_with_realm(name, realm);
3340Sstevel@tonic-gate 	if (ret = krb5_parse_name(context, fullname, &principal)) {
3350Sstevel@tonic-gate 		com_err(progname, ret, gettext(str_PARSE_NAME));
3362881Smp153739 		return (ERR);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	return (add_admin_princ(handle, context, principal, attrs, lifetime));
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate int
3430Sstevel@tonic-gate add_admin_sname_princ(void *handle, krb5_context context,
3440Sstevel@tonic-gate 	     char *sname, int attrs, int lifetime)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate 	krb5_error_code ret;
3470Sstevel@tonic-gate 	krb5_principal principal;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	if (ret = krb5_sname_to_principal(context, NULL, sname,
3500Sstevel@tonic-gate 					  KRB5_NT_SRV_HST, &principal)) {
3510Sstevel@tonic-gate 		com_err(progname, ret,
3520Sstevel@tonic-gate 			gettext("Could not get host based "
3530Sstevel@tonic-gate 				"service name for %s principal\n"), sname);
3542881Smp153739 		return (ERR);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 	return (add_admin_princ(handle, context, principal, attrs, lifetime));
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 
361