xref: /freebsd/crypto/heimdal/lib/kadm5/ent_setup.c (revision b528cefc6b8f9670b31a865051741d946cb37085)
1b528cefcSMark Murray /*
2b528cefcSMark Murray  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H�gskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "kadm5_locl.h"
35b528cefcSMark Murray 
36b528cefcSMark Murray RCSID("$Id: ent_setup.c,v 1.11 1999/12/02 17:05:06 joda Exp $");
37b528cefcSMark Murray 
38b528cefcSMark Murray #define set_value(X, V) do { if((X) == NULL) (X) = malloc(sizeof(*(X))); *(X) = V; } while(0)
39b528cefcSMark Murray #define set_null(X)     do { if((X) != NULL) free((X)); (X) = NULL; } while (0)
40b528cefcSMark Murray 
41b528cefcSMark Murray static void
42b528cefcSMark Murray attr_to_flags(unsigned attr, HDBFlags *flags)
43b528cefcSMark Murray {
44b528cefcSMark Murray     flags->postdate =		!(attr & KRB5_KDB_DISALLOW_POSTDATED);
45b528cefcSMark Murray     flags->forwardable =	!(attr & KRB5_KDB_DISALLOW_FORWARDABLE);
46b528cefcSMark Murray     flags->initial =	       !!(attr & KRB5_KDB_DISALLOW_TGT_BASED);
47b528cefcSMark Murray     flags->renewable =		!(attr & KRB5_KDB_DISALLOW_RENEWABLE);
48b528cefcSMark Murray     flags->proxiable =		!(attr & KRB5_KDB_DISALLOW_PROXIABLE);
49b528cefcSMark Murray     /* DUP_SKEY */
50b528cefcSMark Murray     flags->invalid =	       !!(attr & KRB5_KDB_DISALLOW_ALL_TIX);
51b528cefcSMark Murray     flags->require_preauth =   !!(attr & KRB5_KDB_REQUIRES_PRE_AUTH);
52b528cefcSMark Murray     /* HW_AUTH */
53b528cefcSMark Murray     flags->server =		!(attr & KRB5_KDB_DISALLOW_SVR);
54b528cefcSMark Murray     flags->change_pw = 	       !!(attr & KRB5_KDB_PWCHANGE_SERVICE);
55b528cefcSMark Murray     flags->client =	        1; /* XXX */
56b528cefcSMark Murray }
57b528cefcSMark Murray 
58b528cefcSMark Murray /*
59b528cefcSMark Murray  * Create the hdb entry `ent' based on data from `princ' with
60b528cefcSMark Murray  * `princ_mask' specifying what fields to be gotten from there and
61b528cefcSMark Murray  * `mask' specifying what fields we want filled in.
62b528cefcSMark Murray  */
63b528cefcSMark Murray 
64b528cefcSMark Murray kadm5_ret_t
65b528cefcSMark Murray _kadm5_setup_entry(hdb_entry *ent,
66b528cefcSMark Murray 		   u_int32_t mask,
67b528cefcSMark Murray 		   kadm5_principal_ent_t princ,
68b528cefcSMark Murray 		   u_int32_t princ_mask,
69b528cefcSMark Murray 		   kadm5_principal_ent_t def,
70b528cefcSMark Murray 		   u_int32_t def_mask)
71b528cefcSMark Murray {
72b528cefcSMark Murray     if(mask & KADM5_PRINC_EXPIRE_TIME
73b528cefcSMark Murray        && princ_mask & KADM5_PRINC_EXPIRE_TIME) {
74b528cefcSMark Murray 	if (princ->princ_expire_time)
75b528cefcSMark Murray 	    set_value(ent->valid_end, princ->princ_expire_time);
76b528cefcSMark Murray 	else
77b528cefcSMark Murray 	    set_null(ent->valid_end);
78b528cefcSMark Murray     }
79b528cefcSMark Murray     if(mask & KADM5_PW_EXPIRATION
80b528cefcSMark Murray        && princ_mask & KADM5_PW_EXPIRATION) {
81b528cefcSMark Murray 	if (princ->pw_expiration)
82b528cefcSMark Murray 	    set_value(ent->pw_end, princ->pw_expiration);
83b528cefcSMark Murray 	else
84b528cefcSMark Murray 	    set_null(ent->pw_end);
85b528cefcSMark Murray     }
86b528cefcSMark Murray     if(mask & KADM5_ATTRIBUTES) {
87b528cefcSMark Murray 	if (princ_mask & KADM5_ATTRIBUTES) {
88b528cefcSMark Murray 	    attr_to_flags(princ->attributes, &ent->flags);
89b528cefcSMark Murray 	} else if(def_mask & KADM5_ATTRIBUTES) {
90b528cefcSMark Murray 	    attr_to_flags(def->attributes, &ent->flags);
91b528cefcSMark Murray 	    ent->flags.invalid = 0;
92b528cefcSMark Murray 	} else {
93b528cefcSMark Murray 	    ent->flags.client      = 1;
94b528cefcSMark Murray 	    ent->flags.server      = 1;
95b528cefcSMark Murray 	    ent->flags.forwardable = 1;
96b528cefcSMark Murray 	    ent->flags.proxiable   = 1;
97b528cefcSMark Murray 	    ent->flags.renewable   = 1;
98b528cefcSMark Murray 	    ent->flags.postdate    = 1;
99b528cefcSMark Murray 	}
100b528cefcSMark Murray     }
101b528cefcSMark Murray     if(mask & KADM5_MAX_LIFE) {
102b528cefcSMark Murray 	if(princ_mask & KADM5_MAX_LIFE) {
103b528cefcSMark Murray 	    if(princ->max_life)
104b528cefcSMark Murray 	      set_value(ent->max_life, princ->max_life);
105b528cefcSMark Murray 	    else
106b528cefcSMark Murray 	      set_null(ent->max_life);
107b528cefcSMark Murray 	} else if(def_mask & KADM5_MAX_LIFE) {
108b528cefcSMark Murray 	    if(def->max_life)
109b528cefcSMark Murray 	      set_value(ent->max_life, def->max_life);
110b528cefcSMark Murray 	    else
111b528cefcSMark Murray 	      set_null(ent->max_life);
112b528cefcSMark Murray 	}
113b528cefcSMark Murray     }
114b528cefcSMark Murray     if(mask & KADM5_KVNO
115b528cefcSMark Murray        && princ_mask & KADM5_KVNO)
116b528cefcSMark Murray 	ent->kvno = princ->kvno;
117b528cefcSMark Murray     if(mask & KADM5_MAX_RLIFE) {
118b528cefcSMark Murray 	if(princ_mask & KADM5_MAX_RLIFE) {
119b528cefcSMark Murray 	  if(princ->max_renewable_life)
120b528cefcSMark Murray 	    set_value(ent->max_renew, princ->max_renewable_life);
121b528cefcSMark Murray 	  else
122b528cefcSMark Murray 	    set_null(ent->max_renew);
123b528cefcSMark Murray 	} else if(def_mask & KADM5_MAX_RLIFE) {
124b528cefcSMark Murray 	  if(def->max_renewable_life)
125b528cefcSMark Murray 	    set_value(ent->max_renew, def->max_renewable_life);
126b528cefcSMark Murray 	  else
127b528cefcSMark Murray 	    set_null(ent->max_renew);
128b528cefcSMark Murray 	}
129b528cefcSMark Murray     }
130b528cefcSMark Murray     if(mask & KADM5_KEY_DATA
131b528cefcSMark Murray        && princ_mask & KADM5_KEY_DATA) {
132b528cefcSMark Murray 	_kadm5_set_keys2(ent, princ->n_key_data, princ->key_data);
133b528cefcSMark Murray     }
134b528cefcSMark Murray     if(mask & KADM5_TL_DATA) {
135b528cefcSMark Murray 	/* XXX */
136b528cefcSMark Murray     }
137b528cefcSMark Murray     if(mask & KADM5_FAIL_AUTH_COUNT) {
138b528cefcSMark Murray 	/* XXX */
139b528cefcSMark Murray     }
140b528cefcSMark Murray     return 0;
141b528cefcSMark Murray }
142