Sync usage with man page.
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / kadm5 / get_s.c
blob2e8f972a450911c3c4aa9c77241f418a1d8be154
1 /*
2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kadm5_locl.h"
36 __RCSID("$Heimdal: get_s.c 21745 2007-07-31 16:11:25Z lha $"
37 "$NetBSD$");
39 static kadm5_ret_t
40 add_tl_data(kadm5_principal_ent_t ent, int16_t type,
41 const void *data, size_t size)
43 krb5_tl_data *tl;
45 tl = calloc(1, sizeof(*tl));
46 if (tl == NULL)
47 return _kadm5_error_code(ENOMEM);
49 tl->tl_data_type = type;
50 tl->tl_data_length = size;
51 tl->tl_data_contents = malloc(size);
52 if (tl->tl_data_contents == NULL) {
53 free(tl);
54 return _kadm5_error_code(ENOMEM);
56 memcpy(tl->tl_data_contents, data, size);
58 tl->tl_data_next = ent->tl_data;
59 ent->tl_data = tl;
60 ent->n_tl_data++;
62 return 0;
65 krb5_ssize_t KRB5_LIB_FUNCTION
66 _krb5_put_int(void *buffer, unsigned long value, size_t size); /* XXX */
68 kadm5_ret_t
69 kadm5_s_get_principal(void *server_handle,
70 krb5_principal princ,
71 kadm5_principal_ent_t out,
72 uint32_t mask)
74 kadm5_server_context *context = server_handle;
75 kadm5_ret_t ret;
76 hdb_entry_ex ent;
78 memset(&ent, 0, sizeof(ent));
79 ret = context->db->hdb_open(context->context, context->db, O_RDONLY, 0);
80 if(ret)
81 return ret;
82 ret = context->db->hdb_fetch(context->context, context->db, princ,
83 HDB_F_DECRYPT|HDB_F_GET_ANY, &ent);
84 context->db->hdb_close(context->context, context->db);
85 if(ret)
86 return _kadm5_error_code(ret);
88 memset(out, 0, sizeof(*out));
89 if(mask & KADM5_PRINCIPAL)
90 ret = krb5_copy_principal(context->context, ent.entry.principal,
91 &out->principal);
92 if(ret)
93 goto out;
94 if(mask & KADM5_PRINC_EXPIRE_TIME && ent.entry.valid_end)
95 out->princ_expire_time = *ent.entry.valid_end;
96 if(mask & KADM5_PW_EXPIRATION && ent.entry.pw_end)
97 out->pw_expiration = *ent.entry.pw_end;
98 if(mask & KADM5_LAST_PWD_CHANGE)
99 hdb_entry_get_pw_change_time(&ent.entry, &out->last_pwd_change);
100 if(mask & KADM5_ATTRIBUTES){
101 out->attributes |= ent.entry.flags.postdate ? 0 : KRB5_KDB_DISALLOW_POSTDATED;
102 out->attributes |= ent.entry.flags.forwardable ? 0 : KRB5_KDB_DISALLOW_FORWARDABLE;
103 out->attributes |= ent.entry.flags.initial ? KRB5_KDB_DISALLOW_TGT_BASED : 0;
104 out->attributes |= ent.entry.flags.renewable ? 0 : KRB5_KDB_DISALLOW_RENEWABLE;
105 out->attributes |= ent.entry.flags.proxiable ? 0 : KRB5_KDB_DISALLOW_PROXIABLE;
106 out->attributes |= ent.entry.flags.invalid ? KRB5_KDB_DISALLOW_ALL_TIX : 0;
107 out->attributes |= ent.entry.flags.require_preauth ? KRB5_KDB_REQUIRES_PRE_AUTH : 0;
108 out->attributes |= ent.entry.flags.server ? 0 : KRB5_KDB_DISALLOW_SVR;
109 out->attributes |= ent.entry.flags.change_pw ? KRB5_KDB_PWCHANGE_SERVICE : 0;
110 out->attributes |= ent.entry.flags.ok_as_delegate ? KRB5_KDB_OK_AS_DELEGATE : 0;
111 out->attributes |= ent.entry.flags.trusted_for_delegation ? KRB5_KDB_TRUSTED_FOR_DELEGATION : 0;
112 out->attributes |= ent.entry.flags.allow_kerberos4 ? KRB5_KDB_ALLOW_KERBEROS4 : 0;
113 out->attributes |= ent.entry.flags.allow_digest ? KRB5_KDB_ALLOW_DIGEST : 0;
115 if(mask & KADM5_MAX_LIFE) {
116 if(ent.entry.max_life)
117 out->max_life = *ent.entry.max_life;
118 else
119 out->max_life = INT_MAX;
121 if(mask & KADM5_MOD_TIME) {
122 if(ent.entry.modified_by)
123 out->mod_date = ent.entry.modified_by->time;
124 else
125 out->mod_date = ent.entry.created_by.time;
127 if(mask & KADM5_MOD_NAME) {
128 if(ent.entry.modified_by) {
129 if (ent.entry.modified_by->principal != NULL)
130 ret = krb5_copy_principal(context->context,
131 ent.entry.modified_by->principal,
132 &out->mod_name);
133 } else if(ent.entry.created_by.principal != NULL)
134 ret = krb5_copy_principal(context->context,
135 ent.entry.created_by.principal,
136 &out->mod_name);
137 else
138 out->mod_name = NULL;
140 if(ret)
141 goto out;
143 if(mask & KADM5_KVNO)
144 out->kvno = ent.entry.kvno;
145 if(mask & KADM5_MKVNO) {
146 int n;
147 out->mkvno = 0; /* XXX */
148 for(n = 0; n < ent.entry.keys.len; n++)
149 if(ent.entry.keys.val[n].mkvno) {
150 out->mkvno = *ent.entry.keys.val[n].mkvno; /* XXX this isn't right */
151 break;
154 if(mask & KADM5_AUX_ATTRIBUTES)
155 /* XXX implement */;
156 if(mask & KADM5_POLICY)
157 out->policy = NULL;
158 if(mask & KADM5_MAX_RLIFE) {
159 if(ent.entry.max_renew)
160 out->max_renewable_life = *ent.entry.max_renew;
161 else
162 out->max_renewable_life = INT_MAX;
164 if(mask & KADM5_LAST_SUCCESS)
165 /* XXX implement */;
166 if(mask & KADM5_LAST_FAILED)
167 /* XXX implement */;
168 if(mask & KADM5_FAIL_AUTH_COUNT)
169 /* XXX implement */;
170 if(mask & KADM5_KEY_DATA){
171 int i;
172 Key *key;
173 krb5_key_data *kd;
174 krb5_salt salt;
175 krb5_data *sp;
176 krb5_get_pw_salt(context->context, ent.entry.principal, &salt);
177 out->key_data = malloc(ent.entry.keys.len * sizeof(*out->key_data));
178 if (out->key_data == NULL) {
179 ret = ENOMEM;
180 goto out;
182 for(i = 0; i < ent.entry.keys.len; i++){
183 key = &ent.entry.keys.val[i];
184 kd = &out->key_data[i];
185 kd->key_data_ver = 2;
186 kd->key_data_kvno = ent.entry.kvno;
187 kd->key_data_type[0] = key->key.keytype;
188 if(key->salt)
189 kd->key_data_type[1] = key->salt->type;
190 else
191 kd->key_data_type[1] = KRB5_PADATA_PW_SALT;
192 /* setup key */
193 kd->key_data_length[0] = key->key.keyvalue.length;
194 kd->key_data_contents[0] = malloc(kd->key_data_length[0]);
195 if(kd->key_data_contents[0] == NULL){
196 ret = ENOMEM;
197 break;
199 memcpy(kd->key_data_contents[0], key->key.keyvalue.data,
200 kd->key_data_length[0]);
201 /* setup salt */
202 if(key->salt)
203 sp = &key->salt->salt;
204 else
205 sp = &salt.saltvalue;
206 kd->key_data_length[1] = sp->length;
207 kd->key_data_contents[1] = malloc(kd->key_data_length[1]);
208 if(kd->key_data_length[1] != 0
209 && kd->key_data_contents[1] == NULL) {
210 memset(kd->key_data_contents[0], 0, kd->key_data_length[0]);
211 ret = ENOMEM;
212 break;
214 memcpy(kd->key_data_contents[1], sp->data, kd->key_data_length[1]);
215 out->n_key_data = i + 1;
217 krb5_free_salt(context->context, salt);
219 if(ret){
220 kadm5_free_principal_ent(context, out);
221 goto out;
223 if(mask & KADM5_TL_DATA) {
224 time_t last_pw_expire;
225 const HDB_Ext_Aliases *aliases;
227 ret = hdb_entry_get_pw_change_time(&ent.entry, &last_pw_expire);
228 if (ret == 0 && last_pw_expire) {
229 unsigned char buf[4];
230 _krb5_put_int(buf, last_pw_expire, sizeof(buf));
231 ret = add_tl_data(out, KRB5_TL_LAST_PWD_CHANGE, buf, sizeof(buf));
233 if(ret){
234 kadm5_free_principal_ent(context, out);
235 goto out;
238 * If the client was allowed to get key data, let it have the
239 * password too.
241 if(mask & KADM5_KEY_DATA) {
242 heim_utf8_string pw;
244 ret = hdb_entry_get_password(context->context,
245 context->db, &ent.entry, &pw);
246 if (ret == 0) {
247 ret = add_tl_data(out, KRB5_TL_PASSWORD, pw, strlen(pw) + 1);
248 free(pw);
250 krb5_clear_error_string(context->context);
251 ret = 0;
254 ret = hdb_entry_get_aliases(&ent.entry, &aliases);
255 if (ret == 0 && aliases) {
256 krb5_data buf;
257 size_t len;
259 ASN1_MALLOC_ENCODE(HDB_Ext_Aliases, buf.data, buf.length,
260 aliases, &len, ret);
261 if (ret) {
262 kadm5_free_principal_ent(context, out);
263 goto out;
265 if (len != buf.length)
266 krb5_abortx(context->context,
267 "internal ASN.1 encoder error");
268 ret = add_tl_data(out, KRB5_TL_ALIASES, buf.data, buf.length);
269 free(buf.data);
270 if (ret) {
271 kadm5_free_principal_ent(context, out);
272 goto out;
275 if(ret){
276 kadm5_free_principal_ent(context, out);
277 goto out;
281 out:
282 hdb_free_entry(context->context, &ent);
284 return _kadm5_error_code(ret);