1 /*	$NetBSD: ent_setup.c,v 1.1.1.2 2014/04/24 12:45:48 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * 3. Neither the name of the Institute nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include "kadm5_locl.h"
39 
40 __RCSID("NetBSD");
41 
42 #define set_value(X, V) do { if((X) == NULL) (X) = malloc(sizeof(*(X))); *(X) = V; } while(0)
43 #define set_null(X)     do { if((X) != NULL) free((X)); (X) = NULL; } while (0)
44 
45 static void
attr_to_flags(unsigned attr,HDBFlags * flags)46 attr_to_flags(unsigned attr, HDBFlags *flags)
47 {
48     flags->postdate =		!(attr & KRB5_KDB_DISALLOW_POSTDATED);
49     flags->forwardable =	!(attr & KRB5_KDB_DISALLOW_FORWARDABLE);
50     flags->initial =	       !!(attr & KRB5_KDB_DISALLOW_TGT_BASED);
51     flags->renewable =		!(attr & KRB5_KDB_DISALLOW_RENEWABLE);
52     flags->proxiable =		!(attr & KRB5_KDB_DISALLOW_PROXIABLE);
53     /* DUP_SKEY */
54     flags->invalid =	       !!(attr & KRB5_KDB_DISALLOW_ALL_TIX);
55     flags->require_preauth =   !!(attr & KRB5_KDB_REQUIRES_PRE_AUTH);
56     /* HW_AUTH */
57     flags->server =		!(attr & KRB5_KDB_DISALLOW_SVR);
58     flags->change_pw = 	       !!(attr & KRB5_KDB_PWCHANGE_SERVICE);
59     flags->client =	        1; /* XXX */
60     flags->ok_as_delegate =    !!(attr & KRB5_KDB_OK_AS_DELEGATE);
61     flags->trusted_for_delegation = !!(attr & KRB5_KDB_TRUSTED_FOR_DELEGATION);
62     flags->allow_kerberos4 =   !!(attr & KRB5_KDB_ALLOW_KERBEROS4);
63     flags->allow_digest =      !!(attr & KRB5_KDB_ALLOW_DIGEST);
64 }
65 
66 /*
67  * Modify the `ent' according to `tl_data'.
68  */
69 
70 static kadm5_ret_t
perform_tl_data(krb5_context context,HDB * db,hdb_entry_ex * ent,const krb5_tl_data * tl_data)71 perform_tl_data(krb5_context context,
72 		HDB *db,
73 		hdb_entry_ex *ent,
74 		const krb5_tl_data *tl_data)
75 {
76     kadm5_ret_t ret = 0;
77 
78     if (tl_data->tl_data_type == KRB5_TL_PASSWORD) {
79 	heim_utf8_string pw = tl_data->tl_data_contents;
80 
81 	if (pw[tl_data->tl_data_length] != '\0')
82 	    return KADM5_BAD_TL_TYPE;
83 
84 	ret = hdb_entry_set_password(context, db, &ent->entry, pw);
85 
86     } else if (tl_data->tl_data_type == KRB5_TL_LAST_PWD_CHANGE) {
87 	unsigned char *s;
88 	time_t t;
89 
90 	if (tl_data->tl_data_length != 4)
91 	    return KADM5_BAD_TL_TYPE;
92 
93 	s = tl_data->tl_data_contents;
94 
95 	t = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
96 
97 	ret = hdb_entry_set_pw_change_time(context, &ent->entry, t);
98 
99     } else if (tl_data->tl_data_type == KRB5_TL_EXTENSION) {
100 	HDB_extension ext;
101 
102 	ret = decode_HDB_extension(tl_data->tl_data_contents,
103 				   tl_data->tl_data_length,
104 				   &ext,
105 				   NULL);
106 	if (ret)
107 	    return KADM5_BAD_TL_TYPE;
108 
109 	ret = hdb_replace_extension(context, &ent->entry, &ext);
110 	free_HDB_extension(&ext);
111     } else {
112 	return KADM5_BAD_TL_TYPE;
113     }
114     return ret;
115 }
116 
117 static void
default_flags(hdb_entry_ex * ent,int server)118 default_flags(hdb_entry_ex *ent, int server)
119 {
120     ent->entry.flags.client      = 1;
121     ent->entry.flags.server      = !!server;
122     ent->entry.flags.forwardable = 1;
123     ent->entry.flags.proxiable   = 1;
124     ent->entry.flags.renewable   = 1;
125     ent->entry.flags.postdate    = 1;
126 }
127 
128 
129 /*
130  * Create the hdb entry `ent' based on data from `princ' with
131  * `princ_mask' specifying what fields to be gotten from there and
132  * `mask' specifying what fields we want filled in.
133  */
134 
135 kadm5_ret_t
_kadm5_setup_entry(kadm5_server_context * context,hdb_entry_ex * ent,uint32_t mask,kadm5_principal_ent_t princ,uint32_t princ_mask,kadm5_principal_ent_t def,uint32_t def_mask)136 _kadm5_setup_entry(kadm5_server_context *context,
137 		   hdb_entry_ex *ent,
138 		   uint32_t mask,
139 		   kadm5_principal_ent_t princ,
140 		   uint32_t princ_mask,
141 		   kadm5_principal_ent_t def,
142 		   uint32_t def_mask)
143 {
144     if(mask & KADM5_PRINC_EXPIRE_TIME
145        && princ_mask & KADM5_PRINC_EXPIRE_TIME) {
146 	if (princ->princ_expire_time)
147 	    set_value(ent->entry.valid_end, princ->princ_expire_time);
148 	else
149 	    set_null(ent->entry.valid_end);
150     }
151     if(mask & KADM5_PW_EXPIRATION
152        && princ_mask & KADM5_PW_EXPIRATION) {
153 	if (princ->pw_expiration)
154 	    set_value(ent->entry.pw_end, princ->pw_expiration);
155 	else
156 	    set_null(ent->entry.pw_end);
157     }
158     if(mask & KADM5_ATTRIBUTES) {
159 	if (princ_mask & KADM5_ATTRIBUTES) {
160 	    attr_to_flags(princ->attributes, &ent->entry.flags);
161 	} else if(def_mask & KADM5_ATTRIBUTES) {
162 	    attr_to_flags(def->attributes, &ent->entry.flags);
163 	    ent->entry.flags.invalid = 0;
164 	} else {
165 	    default_flags(ent, 1);
166 	}
167     }
168 
169     if(mask & KADM5_MAX_LIFE) {
170 	if(princ_mask & KADM5_MAX_LIFE) {
171 	    if(princ->max_life)
172 	      set_value(ent->entry.max_life, princ->max_life);
173 	    else
174 	      set_null(ent->entry.max_life);
175 	} else if(def_mask & KADM5_MAX_LIFE) {
176 	    if(def->max_life)
177 	      set_value(ent->entry.max_life, def->max_life);
178 	    else
179 	      set_null(ent->entry.max_life);
180 	}
181     }
182     if(mask & KADM5_KVNO
183        && princ_mask & KADM5_KVNO)
184 	ent->entry.kvno = princ->kvno;
185     if(mask & KADM5_MAX_RLIFE) {
186 	if(princ_mask & KADM5_MAX_RLIFE) {
187 	  if(princ->max_renewable_life)
188 	    set_value(ent->entry.max_renew, princ->max_renewable_life);
189 	  else
190 	    set_null(ent->entry.max_renew);
191 	} else if(def_mask & KADM5_MAX_RLIFE) {
192 	  if(def->max_renewable_life)
193 	    set_value(ent->entry.max_renew, def->max_renewable_life);
194 	  else
195 	    set_null(ent->entry.max_renew);
196 	}
197     }
198     if(mask & KADM5_KEY_DATA
199        && princ_mask & KADM5_KEY_DATA) {
200 	_kadm5_set_keys2(context, &ent->entry,
201 			 princ->n_key_data, princ->key_data);
202     }
203     if(mask & KADM5_TL_DATA) {
204 	krb5_tl_data *tl;
205 
206 	for (tl = princ->tl_data; tl != NULL; tl = tl->tl_data_next) {
207 	    kadm5_ret_t ret;
208 	    ret = perform_tl_data(context->context, context->db, ent, tl);
209 	    if (ret)
210 		return ret;
211 	}
212     }
213     if(mask & KADM5_FAIL_AUTH_COUNT) {
214 	/* XXX */
215     }
216     return 0;
217 }
218