xref: /freebsd/crypto/heimdal/lib/kadm5/get_s.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #include "kadm5_locl.h"
35 
36 RCSID("$Id: get_s.c,v 1.11 1999/12/26 19:38:23 assar Exp $");
37 
38 kadm5_ret_t
39 kadm5_s_get_principal(void *server_handle,
40 		      krb5_principal princ,
41 		      kadm5_principal_ent_t out,
42 		      u_int32_t mask)
43 {
44     kadm5_server_context *context = server_handle;
45     kadm5_ret_t ret;
46     hdb_entry ent;
47 
48     ent.principal = princ;
49     ret = context->db->open(context->context, context->db, O_RDONLY, 0);
50     if(ret)
51 	return ret;
52     ret = context->db->fetch(context->context, context->db,
53 			     HDB_F_DECRYPT, &ent);
54     context->db->close(context->context, context->db);
55     if(ret)
56 	return _kadm5_error_code(ret);
57 
58     memset(out, 0, sizeof(*out));
59     if(mask & KADM5_PRINCIPAL)
60 	ret  = krb5_copy_principal(context->context, ent.principal,
61 				   &out->principal);
62     if(ret)
63 	goto out;
64     if(mask & KADM5_PRINC_EXPIRE_TIME && ent.valid_end)
65 	out->princ_expire_time = *ent.valid_end;
66     if(mask & KADM5_PW_EXPIRATION && ent.pw_end)
67 	out->pw_expiration = *ent.pw_end;
68     if(mask & KADM5_LAST_PWD_CHANGE)
69 	/* XXX implement */;
70     if(mask & KADM5_ATTRIBUTES){
71 	out->attributes |= ent.flags.postdate ? 0 : KRB5_KDB_DISALLOW_POSTDATED;
72 	out->attributes |= ent.flags.forwardable ? 0 : KRB5_KDB_DISALLOW_FORWARDABLE;
73 	out->attributes |= ent.flags.initial ? KRB5_KDB_DISALLOW_TGT_BASED : 0;
74 	out->attributes |= ent.flags.renewable ? 0 : KRB5_KDB_DISALLOW_RENEWABLE;
75 	out->attributes |= ent.flags.proxiable ? 0 : KRB5_KDB_DISALLOW_PROXIABLE;
76 	out->attributes |= ent.flags.invalid ? KRB5_KDB_DISALLOW_ALL_TIX : 0;
77 	out->attributes |= ent.flags.require_preauth ? KRB5_KDB_REQUIRES_PRE_AUTH : 0;
78 	out->attributes |= ent.flags.server ? 0 : KRB5_KDB_DISALLOW_SVR;
79 	out->attributes |= ent.flags.change_pw ? KRB5_KDB_PWCHANGE_SERVICE : 0;
80     }
81     if(mask & KADM5_MAX_LIFE && ent.max_life)
82 	out->max_life = *ent.max_life;
83     if(mask & KADM5_MOD_TIME) {
84 	if(ent.modified_by)
85 	    out->mod_date = ent.modified_by->time;
86 	else
87 	    out->mod_date = ent.created_by.time;
88     }
89     if(mask & KADM5_MOD_NAME) {
90 	if(ent.modified_by) {
91 	    if (ent.modified_by->principal != NULL)
92 		ret = krb5_copy_principal(context->context,
93 					  ent.modified_by->principal,
94 					  &out->mod_name);
95 	} else
96 	    ret = krb5_copy_principal(context->context,
97 				      ent.created_by.principal,
98 				      &out->mod_name);
99     }
100     if(ret)
101 	goto out;
102 
103     if(mask & KADM5_KVNO)
104 	out->kvno = ent.kvno;
105     if(mask & KADM5_MKVNO) {
106 	int n;
107 	out->mkvno = 0; /* XXX */
108 	for(n = 0; n < ent.keys.len; n++)
109 	    if(ent.keys.val[n].mkvno) {
110 		out->mkvno = *ent.keys.val[n].mkvno; /* XXX this isn't right */
111 		break;
112 	    }
113     }
114     if(mask & KADM5_AUX_ATTRIBUTES)
115 	/* XXX implement */;
116     if(mask & KADM5_POLICY)
117 	out->policy = NULL;
118     if(mask & KADM5_MAX_RLIFE && ent.max_renew)
119 	out->max_renewable_life = *ent.max_renew;
120     if(mask & KADM5_LAST_SUCCESS)
121 	/* XXX implement */;
122     if(mask & KADM5_LAST_FAILED)
123 	/* XXX implement */;
124     if(mask & KADM5_FAIL_AUTH_COUNT)
125 	/* XXX implement */;
126     if(mask & KADM5_KEY_DATA){
127 	int i;
128 	Key *key;
129 	krb5_key_data *kd;
130 	krb5_salt salt;
131 	krb5_data *sp;
132 	krb5_get_pw_salt(context->context, ent.principal, &salt);
133 	out->key_data = malloc(ent.keys.len * sizeof(*out->key_data));
134 	for(i = 0; i < ent.keys.len; i++){
135 	    key = &ent.keys.val[i];
136 	    kd = &out->key_data[i];
137 	    kd->key_data_ver = 2;
138 	    kd->key_data_kvno = ent.kvno;
139 	    kd->key_data_type[0] = key->key.keytype;
140 	    if(key->salt)
141 		kd->key_data_type[1] = key->salt->type;
142 	    else
143 		kd->key_data_type[1] = pa_pw_salt;
144 	    /* setup key */
145 	    kd->key_data_length[0] = key->key.keyvalue.length;
146 	    kd->key_data_contents[0] = malloc(kd->key_data_length[0]);
147 	    if(kd->key_data_contents[0] == NULL){
148 		ret = ENOMEM;
149 		break;
150 	    }
151 	    memcpy(kd->key_data_contents[0], key->key.keyvalue.data,
152 		   kd->key_data_length[0]);
153 	    /* setup salt */
154 	    if(key->salt)
155 		sp = &key->salt->salt;
156 	    else
157 		sp = &salt.saltvalue;
158 	    kd->key_data_length[1] = sp->length;
159 	    kd->key_data_contents[1] = malloc(kd->key_data_length[1]);
160 	    if(kd->key_data_length[1] != 0
161 	       && kd->key_data_contents[1] == NULL) {
162 		memset(kd->key_data_contents[0], 0, kd->key_data_length[0]);
163 		ret = ENOMEM;
164 		break;
165 	    }
166 	    memcpy(kd->key_data_contents[1], sp->data, kd->key_data_length[1]);
167 	    out->n_key_data = i + 1;
168 	}
169 	krb5_free_salt(context->context, salt);
170     }
171     if(ret){
172 	kadm5_free_principal_ent(context, out);
173 	goto out;
174     }
175     if(mask & KADM5_TL_DATA)
176 	/* XXX implement */;
177 out:
178     hdb_free_entry(context->context, &ent);
179 
180     return _kadm5_error_code(ret);
181 }
182