s4: smbtorture: Add fsync_resource_fork test to fruit tests.
[Samba.git] / source4 / kdc / sdb_to_hdb.c
blobf1976573d0bbcd6c46edfafa647f469d943988f5
1 /*
2 Unix SMB/CIFS implementation.
4 Database Glue between Samba and the KDC
6 Copyright (C) Guenther Deschner <gd@samba.org> 2014
7 Copyright (C) Andreas Schneider <asn@samba.org> 2014
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include <hdb.h>
26 #include "sdb.h"
27 #include "sdb_hdb.h"
28 #include "lib/krb5_wrap/krb5_samba.h"
29 #include "kdc/samba_kdc.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_KERBEROS
34 static void sdb_flags_to_hdb_flags(const struct SDBFlags *s,
35 HDBFlags *h)
37 SMB_ASSERT(sizeof(struct SDBFlags) == sizeof(HDBFlags));
39 h->initial = s->initial;
40 h->forwardable = s->forwardable;
41 h->proxiable = s->proxiable;
42 h->renewable = s->renewable;
43 h->postdate = s->postdate;
44 h->server = s->server;
45 h->client = s->client;
46 h->invalid = s->invalid;
47 h->require_preauth = s->require_preauth;
48 h->change_pw = s->change_pw;
49 h->require_hwauth = s->require_hwauth;
50 h->ok_as_delegate = s->ok_as_delegate;
51 h->user_to_user = s->user_to_user;
52 h->immutable = s->immutable;
53 h->trusted_for_delegation = s->trusted_for_delegation;
54 h->allow_kerberos4 = s->allow_kerberos4;
55 h->allow_digest = s->allow_digest;
56 h->locked_out = s->locked_out;
57 h->require_pwchange = s->require_pwchange;
58 h->materialize = s->materialize;
59 h->virtual_keys = s->virtual_keys;
60 h->virtual = s->virtual;
61 h->synthetic = s->synthetic;
62 h->no_auth_data_reqd = s->no_auth_data_reqd;
63 h->_unused24 = s->_unused24;
64 h->_unused25 = s->_unused25;
65 h->_unused26 = s->_unused26;
66 h->_unused27 = s->_unused27;
67 h->_unused28 = s->_unused28;
68 h->_unused29 = s->_unused29;
69 h->force_canonicalize = s->force_canonicalize;
70 h->do_not_store = s->do_not_store;
73 static int sdb_salt_to_Salt(const struct sdb_salt *s, Salt *h)
75 int ret;
77 h->type = s->type;
78 ret = smb_krb5_copy_data_contents(&h->salt, s->salt.data, s->salt.length);
79 if (ret != 0) {
80 free_Salt(h);
81 return ENOMEM;
83 h->opaque = NULL;
85 return 0;
88 static int sdb_key_to_Key(const struct sdb_key *s, Key *h)
90 int rc;
92 ZERO_STRUCTP(h);
94 h->key.keytype = s->key.keytype;
95 rc = smb_krb5_copy_data_contents(&h->key.keyvalue,
96 s->key.keyvalue.data,
97 s->key.keyvalue.length);
98 if (rc != 0) {
99 goto error_nomem;
102 if (s->salt != NULL) {
103 h->salt = malloc(sizeof(Salt));
104 if (h->salt == NULL) {
105 goto error_nomem;
108 rc = sdb_salt_to_Salt(s->salt,
109 h->salt);
110 if (rc != 0) {
111 goto error_nomem;
113 } else {
114 h->salt = NULL;
117 return 0;
119 error_nomem:
120 free_Key(h);
121 return ENOMEM;
124 static int sdb_keys_to_Keys(const struct sdb_keys *s, Keys *h)
126 int ret, i;
128 h->len = s->len;
129 if (s->val != NULL) {
130 h->val = malloc(h->len * sizeof(Key));
131 if (h->val == NULL) {
132 return ENOMEM;
134 for (i = 0; i < h->len; i++) {
135 ret = sdb_key_to_Key(&s->val[i],
136 &h->val[i]);
137 if (ret != 0) {
138 free_Keys(h);
139 return ENOMEM;
142 } else {
143 h->val = NULL;
146 return 0;
149 static int sdb_event_to_Event(krb5_context context,
150 const struct sdb_event *s, Event *h)
152 int ret;
154 if (s->principal != NULL) {
155 ret = krb5_copy_principal(context,
156 s->principal,
157 &h->principal);
158 if (ret != 0) {
159 free_Event(h);
160 return ret;
162 } else {
163 h->principal = NULL;
165 h->time = s->time;
167 return 0;
170 int sdb_entry_to_hdb_entry(krb5_context context,
171 const struct sdb_entry *s,
172 hdb_entry *h)
174 struct samba_kdc_entry *ske = s->skdc_entry;
175 unsigned int i;
176 int rc;
178 ZERO_STRUCTP(h);
180 rc = krb5_copy_principal(context,
181 s->principal,
182 &h->principal);
183 if (rc != 0) {
184 return rc;
187 h->kvno = s->kvno;
189 rc = sdb_keys_to_Keys(&s->keys, &h->keys);
190 if (rc != 0) {
191 goto error;
194 rc = sdb_event_to_Event(context,
195 &s->created_by,
196 &h->created_by);
197 if (rc != 0) {
198 goto error;
201 if (s->modified_by) {
202 h->modified_by = malloc(sizeof(Event));
203 if (h->modified_by == NULL) {
204 rc = ENOMEM;
205 goto error;
208 rc = sdb_event_to_Event(context,
209 s->modified_by,
210 h->modified_by);
211 if (rc != 0) {
212 goto error;
214 } else {
215 h->modified_by = NULL;
218 if (s->valid_start != NULL) {
219 h->valid_start = malloc(sizeof(KerberosTime));
220 if (h->valid_start == NULL) {
221 rc = ENOMEM;
222 goto error;
224 *h->valid_start = *s->valid_start;
225 } else {
226 h->valid_start = NULL;
229 if (s->valid_end != NULL) {
230 h->valid_end = malloc(sizeof(KerberosTime));
231 if (h->valid_end == NULL) {
232 rc = ENOMEM;
233 goto error;
235 *h->valid_end = *s->valid_end;
236 } else {
237 h->valid_end = NULL;
240 if (s->pw_end != NULL) {
241 h->pw_end = malloc(sizeof(KerberosTime));
242 if (h->pw_end == NULL) {
243 rc = ENOMEM;
244 goto error;
246 *h->pw_end = *s->pw_end;
247 } else {
248 h->pw_end = NULL;
251 if (s->max_life != NULL) {
252 h->max_life = malloc(sizeof(unsigned int));
253 if (h->max_life == NULL) {
254 rc = ENOMEM;
255 goto error;
257 *h->max_life = *s->max_life;
258 } else {
259 h->max_life = NULL;
262 if (s->max_renew != NULL) {
263 h->max_renew = malloc(sizeof(unsigned int));
264 if (h->max_renew == NULL) {
265 rc = ENOMEM;
266 goto error;
268 *h->max_renew = *s->max_renew;
269 } else {
270 h->max_renew = NULL;
273 sdb_flags_to_hdb_flags(&s->flags, &h->flags);
275 h->etypes = NULL;
276 if (h->keys.val != NULL) {
277 h->etypes = malloc(sizeof(*h->etypes));
278 if (h->etypes == NULL) {
279 rc = ENOMEM;
280 goto error;
283 h->etypes->len = s->keys.len;
285 h->etypes->val = calloc(h->etypes->len, sizeof(int));
286 if (h->etypes->val == NULL) {
287 rc = ENOMEM;
288 goto error;
291 for (i = 0; i < h->etypes->len; i++) {
292 Key k = h->keys.val[i];
294 h->etypes->val[i] = KRB5_KEY_TYPE(&(k.key));
298 h->context = ske;
299 if (ske != NULL) {
300 ske->kdc_entry = h;
302 return 0;
303 error:
304 free_hdb_entry(h);
305 return rc;