Sync usage with man page.
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / kadm5 / create_s.c
blob4356cecbb0f59d9f0cb210ff610c2710385e427e
1 /*
2 * Copyright (c) 1997-2001 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: create_s.c 20607 2007-05-08 07:11:11Z lha $"
37 "$NetBSD$");
39 static kadm5_ret_t
40 get_default(kadm5_server_context *context, krb5_principal princ,
41 kadm5_principal_ent_t def)
43 kadm5_ret_t ret;
44 krb5_principal def_principal;
45 krb5_realm *realm = krb5_princ_realm(context->context, princ);
47 ret = krb5_make_principal(context->context, &def_principal,
48 *realm, "default", NULL);
49 if (ret)
50 return ret;
51 ret = kadm5_s_get_principal(context, def_principal, def,
52 KADM5_PRINCIPAL_NORMAL_MASK);
53 krb5_free_principal (context->context, def_principal);
54 return ret;
57 static kadm5_ret_t
58 create_principal(kadm5_server_context *context,
59 kadm5_principal_ent_t princ,
60 uint32_t mask,
61 hdb_entry_ex *ent,
62 uint32_t required_mask,
63 uint32_t forbidden_mask)
65 kadm5_ret_t ret;
66 kadm5_principal_ent_rec defrec, *defent;
67 uint32_t def_mask;
69 if((mask & required_mask) != required_mask)
70 return KADM5_BAD_MASK;
71 if((mask & forbidden_mask))
72 return KADM5_BAD_MASK;
73 if((mask & KADM5_POLICY) && strcmp(princ->policy, "default"))
74 /* XXX no real policies for now */
75 return KADM5_UNK_POLICY;
76 memset(ent, 0, sizeof(*ent));
77 ret = krb5_copy_principal(context->context, princ->principal,
78 &ent->entry.principal);
79 if(ret)
80 return ret;
82 defent = &defrec;
83 ret = get_default(context, princ->principal, defent);
84 if(ret) {
85 defent = NULL;
86 def_mask = 0;
87 } else {
88 def_mask = KADM5_ATTRIBUTES | KADM5_MAX_LIFE | KADM5_MAX_RLIFE;
91 ret = _kadm5_setup_entry(context,
92 ent, mask | def_mask,
93 princ, mask,
94 defent, def_mask);
95 if(defent)
96 kadm5_free_principal_ent(context, defent);
98 ent->entry.created_by.time = time(NULL);
99 ret = krb5_copy_principal(context->context, context->caller,
100 &ent->entry.created_by.principal);
102 return ret;
105 kadm5_ret_t
106 kadm5_s_create_principal_with_key(void *server_handle,
107 kadm5_principal_ent_t princ,
108 uint32_t mask)
110 kadm5_ret_t ret;
111 hdb_entry_ex ent;
112 kadm5_server_context *context = server_handle;
114 ret = create_principal(context, princ, mask, &ent,
115 KADM5_PRINCIPAL | KADM5_KEY_DATA,
116 KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME
117 | KADM5_MOD_NAME | KADM5_MKVNO
118 | KADM5_AUX_ATTRIBUTES
119 | KADM5_POLICY_CLR | KADM5_LAST_SUCCESS
120 | KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT);
121 if(ret)
122 goto out;
124 if ((mask & KADM5_KVNO) == 0)
125 ent.entry.kvno = 1;
127 ret = hdb_seal_keys(context->context, context->db, &ent.entry);
128 if (ret)
129 goto out;
131 ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
132 if(ret)
133 goto out;
134 ret = context->db->hdb_store(context->context, context->db, 0, &ent);
135 context->db->hdb_close(context->context, context->db);
136 if (ret)
137 goto out;
138 kadm5_log_create (context, &ent.entry);
140 out:
141 hdb_free_entry(context->context, &ent);
142 return _kadm5_error_code(ret);
146 kadm5_ret_t
147 kadm5_s_create_principal(void *server_handle,
148 kadm5_principal_ent_t princ,
149 uint32_t mask,
150 const char *password)
152 kadm5_ret_t ret;
153 hdb_entry_ex ent;
154 kadm5_server_context *context = server_handle;
156 ret = create_principal(context, princ, mask, &ent,
157 KADM5_PRINCIPAL,
158 KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME
159 | KADM5_MOD_NAME | KADM5_MKVNO
160 | KADM5_AUX_ATTRIBUTES | KADM5_KEY_DATA
161 | KADM5_POLICY_CLR | KADM5_LAST_SUCCESS
162 | KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT);
163 if(ret)
164 goto out;
166 if ((mask & KADM5_KVNO) == 0)
167 ent.entry.kvno = 1;
169 ent.entry.keys.len = 0;
170 ent.entry.keys.val = NULL;
172 ret = _kadm5_set_keys(context, &ent.entry, password);
173 if (ret)
174 goto out;
176 ret = hdb_seal_keys(context->context, context->db, &ent.entry);
177 if (ret)
178 goto out;
180 ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
181 if(ret)
182 goto out;
183 ret = context->db->hdb_store(context->context, context->db, 0, &ent);
184 context->db->hdb_close(context->context, context->db);
185 if (ret)
186 goto out;
188 kadm5_log_create (context, &ent.entry);
190 out:
191 hdb_free_entry(context->context, &ent);
192 return _kadm5_error_code(ret);