xref: /minix3/crypto/external/bsd/heimdal/dist/lib/kadm5/chpass_s.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: chpass_s.c,v 1.1.1.2 2014/04/24 12:45:48 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997-2006 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "kadm5_locl.h"
37ebfedea0SLionel Sambuc 
38*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc static kadm5_ret_t
change(void * server_handle,krb5_principal princ,const char * password,int cond)41ebfedea0SLionel Sambuc change(void *server_handle,
42ebfedea0SLionel Sambuc        krb5_principal princ,
43ebfedea0SLionel Sambuc        const char *password,
44ebfedea0SLionel Sambuc        int cond)
45ebfedea0SLionel Sambuc {
46ebfedea0SLionel Sambuc     kadm5_server_context *context = server_handle;
47ebfedea0SLionel Sambuc     hdb_entry_ex ent;
48ebfedea0SLionel Sambuc     kadm5_ret_t ret;
49ebfedea0SLionel Sambuc     Key *keys;
50ebfedea0SLionel Sambuc     size_t num_keys;
51ebfedea0SLionel Sambuc     int existsp = 0;
52ebfedea0SLionel Sambuc 
53ebfedea0SLionel Sambuc     memset(&ent, 0, sizeof(ent));
54ebfedea0SLionel Sambuc     ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
55ebfedea0SLionel Sambuc     if(ret)
56ebfedea0SLionel Sambuc 	return ret;
57ebfedea0SLionel Sambuc 
58ebfedea0SLionel Sambuc     ret = context->db->hdb_fetch_kvno(context->context, context->db, princ,
59ebfedea0SLionel Sambuc 				      HDB_F_DECRYPT|HDB_F_GET_ANY|HDB_F_ADMIN_DATA, 0, &ent);
60ebfedea0SLionel Sambuc     if(ret)
61ebfedea0SLionel Sambuc 	goto out;
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc     if (context->db->hdb_capability_flags & HDB_CAP_F_HANDLE_PASSWORDS) {
64ebfedea0SLionel Sambuc 	ret = context->db->hdb_password(context->context, context->db,
65ebfedea0SLionel Sambuc 					&ent, password, cond);
66ebfedea0SLionel Sambuc 	if (ret)
67ebfedea0SLionel Sambuc 	    goto out2;
68ebfedea0SLionel Sambuc     } else {
69ebfedea0SLionel Sambuc 
70ebfedea0SLionel Sambuc 	num_keys = ent.entry.keys.len;
71ebfedea0SLionel Sambuc 	keys     = ent.entry.keys.val;
72ebfedea0SLionel Sambuc 
73ebfedea0SLionel Sambuc 	ent.entry.keys.len = 0;
74ebfedea0SLionel Sambuc 	ent.entry.keys.val = NULL;
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc 	ret = _kadm5_set_keys(context, &ent.entry, password);
77ebfedea0SLionel Sambuc 	if(ret) {
78ebfedea0SLionel Sambuc 	    _kadm5_free_keys (context->context, num_keys, keys);
79ebfedea0SLionel Sambuc 	    goto out2;
80ebfedea0SLionel Sambuc 	}
81ebfedea0SLionel Sambuc 
82ebfedea0SLionel Sambuc 	if (cond)
83ebfedea0SLionel Sambuc 	    existsp = _kadm5_exists_keys (ent.entry.keys.val,
84ebfedea0SLionel Sambuc 					  ent.entry.keys.len,
85ebfedea0SLionel Sambuc 					  keys, num_keys);
86ebfedea0SLionel Sambuc 	_kadm5_free_keys (context->context, num_keys, keys);
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc 	if (existsp) {
89ebfedea0SLionel Sambuc 	    ret = KADM5_PASS_REUSE;
90ebfedea0SLionel Sambuc 	    krb5_set_error_message(context->context, ret,
91ebfedea0SLionel Sambuc 				   "Password reuse forbidden");
92ebfedea0SLionel Sambuc 	    goto out2;
93ebfedea0SLionel Sambuc 	}
94ebfedea0SLionel Sambuc 
95ebfedea0SLionel Sambuc 	ret = hdb_seal_keys(context->context, context->db, &ent.entry);
96ebfedea0SLionel Sambuc 	if (ret)
97ebfedea0SLionel Sambuc 	    goto out2;
98ebfedea0SLionel Sambuc     }
99ebfedea0SLionel Sambuc     ent.entry.kvno++;
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc     ret = _kadm5_set_modifier(context, &ent.entry);
102ebfedea0SLionel Sambuc     if(ret)
103ebfedea0SLionel Sambuc 	goto out2;
104ebfedea0SLionel Sambuc 
105ebfedea0SLionel Sambuc     ret = _kadm5_bump_pw_expire(context, &ent.entry);
106ebfedea0SLionel Sambuc     if (ret)
107ebfedea0SLionel Sambuc 	goto out2;
108ebfedea0SLionel Sambuc 
109ebfedea0SLionel Sambuc     ret = context->db->hdb_store(context->context, context->db,
110ebfedea0SLionel Sambuc 				 HDB_F_REPLACE, &ent);
111ebfedea0SLionel Sambuc     if (ret)
112ebfedea0SLionel Sambuc 	goto out2;
113ebfedea0SLionel Sambuc 
114ebfedea0SLionel Sambuc     kadm5_log_modify (context,
115ebfedea0SLionel Sambuc 		      &ent.entry,
116ebfedea0SLionel Sambuc 		      KADM5_PRINCIPAL | KADM5_MOD_NAME | KADM5_MOD_TIME |
117ebfedea0SLionel Sambuc 		      KADM5_KEY_DATA | KADM5_KVNO | KADM5_PW_EXPIRATION |
118ebfedea0SLionel Sambuc 		      KADM5_TL_DATA);
119ebfedea0SLionel Sambuc 
120ebfedea0SLionel Sambuc out2:
121ebfedea0SLionel Sambuc     hdb_free_entry(context->context, &ent);
122ebfedea0SLionel Sambuc out:
123ebfedea0SLionel Sambuc     context->db->hdb_close(context->context, context->db);
124ebfedea0SLionel Sambuc     return _kadm5_error_code(ret);
125ebfedea0SLionel Sambuc }
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc 
129ebfedea0SLionel Sambuc /*
130ebfedea0SLionel Sambuc  * change the password of `princ' to `password' if it's not already that.
131ebfedea0SLionel Sambuc  */
132ebfedea0SLionel Sambuc 
133ebfedea0SLionel Sambuc kadm5_ret_t
kadm5_s_chpass_principal_cond(void * server_handle,krb5_principal princ,const char * password)134ebfedea0SLionel Sambuc kadm5_s_chpass_principal_cond(void *server_handle,
135ebfedea0SLionel Sambuc 			      krb5_principal princ,
136ebfedea0SLionel Sambuc 			      const char *password)
137ebfedea0SLionel Sambuc {
138ebfedea0SLionel Sambuc     return change (server_handle, princ, password, 1);
139ebfedea0SLionel Sambuc }
140ebfedea0SLionel Sambuc 
141ebfedea0SLionel Sambuc /*
142ebfedea0SLionel Sambuc  * change the password of `princ' to `password'
143ebfedea0SLionel Sambuc  */
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc kadm5_ret_t
kadm5_s_chpass_principal(void * server_handle,krb5_principal princ,const char * password)146ebfedea0SLionel Sambuc kadm5_s_chpass_principal(void *server_handle,
147ebfedea0SLionel Sambuc 			 krb5_principal princ,
148ebfedea0SLionel Sambuc 			 const char *password)
149ebfedea0SLionel Sambuc {
150ebfedea0SLionel Sambuc     return change (server_handle, princ, password, 0);
151ebfedea0SLionel Sambuc }
152ebfedea0SLionel Sambuc 
153ebfedea0SLionel Sambuc /*
154ebfedea0SLionel Sambuc  * change keys for `princ' to `keys'
155ebfedea0SLionel Sambuc  */
156ebfedea0SLionel Sambuc 
157ebfedea0SLionel Sambuc kadm5_ret_t
kadm5_s_chpass_principal_with_key(void * server_handle,krb5_principal princ,int n_key_data,krb5_key_data * key_data)158ebfedea0SLionel Sambuc kadm5_s_chpass_principal_with_key(void *server_handle,
159ebfedea0SLionel Sambuc 				  krb5_principal princ,
160ebfedea0SLionel Sambuc 				  int n_key_data,
161ebfedea0SLionel Sambuc 				  krb5_key_data *key_data)
162ebfedea0SLionel Sambuc {
163ebfedea0SLionel Sambuc     kadm5_server_context *context = server_handle;
164ebfedea0SLionel Sambuc     hdb_entry_ex ent;
165ebfedea0SLionel Sambuc     kadm5_ret_t ret;
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc     memset(&ent, 0, sizeof(ent));
168ebfedea0SLionel Sambuc     ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
169ebfedea0SLionel Sambuc     if(ret)
170ebfedea0SLionel Sambuc 	return ret;
171ebfedea0SLionel Sambuc     ret = context->db->hdb_fetch_kvno(context->context, context->db, princ, 0,
172ebfedea0SLionel Sambuc 				      HDB_F_GET_ANY|HDB_F_ADMIN_DATA, &ent);
173ebfedea0SLionel Sambuc     if(ret == HDB_ERR_NOENTRY)
174ebfedea0SLionel Sambuc 	goto out;
175ebfedea0SLionel Sambuc     ret = _kadm5_set_keys2(context, &ent.entry, n_key_data, key_data);
176ebfedea0SLionel Sambuc     if(ret)
177ebfedea0SLionel Sambuc 	goto out2;
178ebfedea0SLionel Sambuc     ent.entry.kvno++;
179ebfedea0SLionel Sambuc     ret = _kadm5_set_modifier(context, &ent.entry);
180ebfedea0SLionel Sambuc     if(ret)
181ebfedea0SLionel Sambuc 	goto out2;
182ebfedea0SLionel Sambuc     ret = _kadm5_bump_pw_expire(context, &ent.entry);
183ebfedea0SLionel Sambuc     if (ret)
184ebfedea0SLionel Sambuc 	goto out2;
185ebfedea0SLionel Sambuc 
186ebfedea0SLionel Sambuc     ret = hdb_seal_keys(context->context, context->db, &ent.entry);
187ebfedea0SLionel Sambuc     if (ret)
188ebfedea0SLionel Sambuc 	goto out2;
189ebfedea0SLionel Sambuc 
190ebfedea0SLionel Sambuc     ret = context->db->hdb_store(context->context, context->db,
191ebfedea0SLionel Sambuc 				 HDB_F_REPLACE, &ent);
192ebfedea0SLionel Sambuc     if (ret)
193ebfedea0SLionel Sambuc 	goto out2;
194ebfedea0SLionel Sambuc 
195ebfedea0SLionel Sambuc     kadm5_log_modify (context,
196ebfedea0SLionel Sambuc 		      &ent.entry,
197ebfedea0SLionel Sambuc 		      KADM5_PRINCIPAL | KADM5_MOD_NAME | KADM5_MOD_TIME |
198ebfedea0SLionel Sambuc 		      KADM5_KEY_DATA | KADM5_KVNO | KADM5_PW_EXPIRATION |
199ebfedea0SLionel Sambuc 		      KADM5_TL_DATA);
200ebfedea0SLionel Sambuc 
201ebfedea0SLionel Sambuc out2:
202ebfedea0SLionel Sambuc     hdb_free_entry(context->context, &ent);
203ebfedea0SLionel Sambuc out:
204ebfedea0SLionel Sambuc     context->db->hdb_close(context->context, context->db);
205ebfedea0SLionel Sambuc     return _kadm5_error_code(ret);
206ebfedea0SLionel Sambuc }
207