File: core.c

package info (click to toggle)
shishi 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 22,144 kB
  • ctags: 7,865
  • sloc: ansic: 61,153; sh: 12,212; yacc: 1,219; makefile: 1,164; perl: 624; sed: 16
file content (439 lines) | stat: -rw-r--r-- 13,055 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* core.c --- Core Shisa database API.
 * Copyright (C) 2003, 2007, 2010  Simon Josefsson
 *
 * This file is part of Shishi.
 *
 * Shishi is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Shishi is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Shishi; if not, see http://www.gnu.org/licenses or write
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA
 *
 */

#include "info.h"

/**
 * shisa_enumerate_realms:
 * @dbh: Shisa library handle created by shisa().
 * @realms: Pointer to newly allocated array of newly allocated
 *   zero-terminated UTF-8 strings indicating name of realm.
 * @nrealms: Pointer to number indicating number of allocated realm strings.
 *
 * Extract a list of all realm names in backend, as zero-terminated
 * UTF-8 strings.  The caller must deallocate the strings.
 *
 * Return value: Returns SHISA_OK on success, or error code.
 **/
int
shisa_enumerate_realms (Shisa * dbh, char ***realms, size_t * nrealms)
{
  _Shisa_db *db;
  size_t i;
  int rc;

  *nrealms = 0;
  if (realms)
    *realms = NULL;

  for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
    {
      rc = db->backend->enumerate_realms (dbh, db->state, realms, nrealms);
      if (rc != SHISA_OK)
	/* XXX mem leak. */
	return rc;
    }

  return SHISA_OK;
}

/**
 * shisa_enumerate_principals:
 * @dbh: Shisa library handle created by shisa().
 * @realm: Name of realm, as zero-terminated UTF-8 string.
 * @principal: Pointer to newly allocated array of newly allocated
 *   zero-terminated UTF-8 strings indicating name of principal.
 * @nprincipals: Pointer to number indicating number of allocated
 *   realm strings.
 *
 * Extract a list of all principal names in realm in backend, as
 * zero-terminated UTF-8 strings.  The caller must deallocate the
 * strings.
 *
 * Return value: Returns SHISA_OK on success, SHISA_NO_REALM if the
 *   specified realm does not exist, or error code.
 **/
int
shisa_enumerate_principals (Shisa * dbh,
			    const char *realm,
			    char ***principals, size_t * nprincipals)
{
  _Shisa_db *db;
  size_t i;
  int rc;

  *nprincipals = 0;
  if (principals)
    *principals = NULL;

  for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
    {
      rc = db->backend->enumerate_principals (dbh, db->state, realm,
					      principals, nprincipals);
      if (rc != SHISA_OK)
	/* XXX mem leak. */
	return rc;
    }

  return SHISA_OK;
}

/**
 * shisa_principal_find:
 * @dbh: Shisa library handle created by shisa().
 * @realm: Name of realm the principal belongs in.
 * @principal: Name of principal to get information on.
 * @ph: Pointer to previously allocated principal structure to fill
 *   out with information about principal.
 *
 * Extract information about given PRINCIPAL@REALM.
 *
 * Return value: Returns %SHISA_OK iff successful, %SHISA_NO_REALM if
 *   the indicated realm does not exist, %SHISA_NO_PRINCIPAL if the
 *   indicated principal does not exist, or an error code.
 **/
int
shisa_principal_find (Shisa * dbh,
		      const char *realm,
		      const char *principal, Shisa_principal * ph)
{
  _Shisa_db *db;
  size_t i;
  int rc;

  for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
    {
      rc = db->backend->principal_find (dbh, db->state, realm, principal, ph);
      if (rc == SHISA_OK || (rc != SHISA_OK && rc != SHISA_NO_PRINCIPAL))
	return rc;
    }

  return SHISA_NO_PRINCIPAL;
}

/**
 * shisa_principal_update:
 * @dbh: Shisa library handle created by shisa().
 * @realm: Name of realm the principal belongs in.
 * @principal: Name of principal to get information on.
 * @ph: Pointer to principal structure with information to store in database.
 *
 * Modify information stored for given PRINCIPAL@REALM.  Note that it
 * is usually a good idea to only set the fields in @ph that you
 * actually want to update.  Specifically, first calling
 * shisa_principal_find() to get the current information, then
 * modifying one field, and calling shisa_principal_update() is not
 * recommended in general, as this will 1) overwrite any modifications
 * made to other fields between the two calls (by other processes) and
 * 2) will cause all values to be written again, which may generate
 * more overhead.
 *
 * Return value: Returns SHISA_OK if successful, %SHISA_NO_REALM if
 *   the indicated realm does not exist, %SHISA_NO_PRINCIPAL if the
 *   indicated principal does not exist, or an error code.
 **/
int
shisa_principal_update (Shisa * dbh,
			const char *realm,
			const char *principal, const Shisa_principal * ph)
{
  _Shisa_db *db;
  size_t i;
  int rc;

  for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
    {
      /* XXX ignore read-only backends. */
      rc =
	db->backend->principal_update (dbh, db->state, realm, principal, ph);
      /* XXX ignore error and continue for ignore-error backends. */
      return rc;
    }

  return SHISA_ADD_PRINCIPAL_ERROR;
}

/**
 * shisa_principal_add:
 * @dbh: Shisa library handle created by shisa().
 * @realm: Name of realm the principal belongs in.
 * @principal: Name of principal to add, may be %NULL to indicate that
 *   the @realm should be created, in which case @ph and @key are not used.
 * @ph: Pointer to principal structure with information to store in database.
 * @key: Pointer to key structure with information to store in database.
 *
 * Add given information to database as PRINCIPAL@REALM.
 *
 * Return value: Returns SHISA_OK iff successfully added, or an error code.
 **/
int
shisa_principal_add (Shisa * dbh,
		     const char *realm,
		     const char *principal,
		     const Shisa_principal * ph, const Shisa_key * key)
{
  _Shisa_db *db;
  size_t i;
  int rc;

  if (realm == NULL)
    return SHISA_NO_REALM;

  for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
    {
      /* XXX ignore read-only backends. */
      rc = db->backend->principal_add (dbh, db->state, realm,
				       principal, ph, key);
      /* XXX ignore error and continue for ignore-error backends. */
      return rc;
    }

  return SHISA_ADD_PRINCIPAL_ERROR;
}

/**
 * shisa_principal_remove:
 * @dbh: Shisa library handle created by shisa().
 * @realm: Name of realm the principal belongs in.
 * @principal: Name of principal to remove, may be %NULL to indicate
 *   that the @realm itself should be removed (requires that the realm
 *   to be empty).
 *
 * Remove all information stored in the database for given PRINCIPAL@REALM.
 *
 * Return value: Returns %SHISA_OK if successful, or an error code.
 **/
int
shisa_principal_remove (Shisa * dbh, const char *realm, const char *principal)
{
  _Shisa_db *db;
  size_t i;
  int rc;

  if (realm == NULL)
    return SHISA_NO_REALM;

  for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
    {
      /* XXX ignore read-only backends. */
      rc = db->backend->principal_remove (dbh, db->state, realm, principal);
      /* XXX ignore error and continue for ignore-error backends. */
      return rc;
    }

  return SHISA_REMOVE_PRINCIPAL_ERROR;
}

/**
 * shisa_keys_find:
 * @dbh: Shisa library handle created by shisa().
 * @realm: Name of realm the principal belongs in.
 * @principal: Name of principal to add key for.
 * @hint: Pointer to Shisa key structure with hints on matching the key
 *   to modify, may be %NULL to match all keys.
 * @keys: pointer to newly allocated array with Shisa key structures.
 * @nkeys: pointer to number of newly allocated Shisa key structures in @keys.
 *
 * Iterate through keys for given PRINCIPAL@REALM and extract any keys
 * that match @hint.  Not all elements of @hint need to be filled out,
 * only use the fields you are interested in.  For example, if you
 * want to extract all keys with an etype of 3 (DES-CBC-MD5), set the
 * @key->etype field to 3, and set all other fields to 0.
 *
 * Return value: Returns %SHISA_OK iff successful, or an error code.
 **/
int
shisa_keys_find (Shisa * dbh,
		 const char *realm,
		 const char *principal,
		 const Shisa_key * hint, Shisa_key *** keys, size_t * nkeys)
{
  _Shisa_db *db;
  size_t i;
  int rc;

  *nkeys = 0;
  if (keys)
    *keys = NULL;

  for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
    {
      rc = db->backend->keys_find (dbh, db->state, realm, principal, hint,
				   keys, nkeys);
      if (rc != SHISA_OK)
	/* XXX mem leak. */
	return rc;
    }

  return SHISA_OK;
}

/**
 * shisa_key_add:
 * @dbh: Shisa library handle created by shisa().
 * @realm: Name of realm the principal belongs in.
 * @principal: Name of principal to add key for.
 * @key: Pointer to Shisa key structure with key to add.
 *
 * Add key to database for given PRINCIPAL@REALM.
 *
 * Return value: Returns %SHISA_OK iff successful, or an error code.
 **/
int
shisa_key_add (Shisa * dbh,
	       const char *realm,
	       const char *principal, const Shisa_key * key)
{
  _Shisa_db *db;
  size_t i;
  int rc;

  for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
    {
      rc = db->backend->key_add (dbh, db->state, realm, principal, key);
      if (rc != SHISA_OK)
	return rc;
    }

  return SHISA_OK;
}


/**
 * shisa_key_update:
 * @dbh: Shisa library handle created by shisa().
 * @realm: Name of realm the principal belongs in.
 * @principal: Name of principal to remove key for.
 * @oldkey: Pointer to Shisa key structure with hints on matching the key
 *   to modify.
 * @newkey: Pointer to Shisa key structure with new values for the
 *   key, note that all fields are used (and not just the ones specified
 *   by @oldkey).
 *
 * Modify data about a key in the database, for the given
 * PRINCIPAL@REALM.  First the @oldkey is used to locate the key to
 * update (similar to shisa_keys_find()), then that key is modified to
 * contain whatever information is stored in @newkey.  Not all
 * elements of @oldkey need to be filled out, only enough as to
 * identify the key uniquely.  For example, if you want to modify the
 * information stored for the only key with an etype of 3
 * (DES-CBC-MD5), set the @key->etype field to 3, and set all other
 * fields to 0.
 *
 * Return value: Returns %SHISA_OK on success, %SHISA_NO_KEY if no key
 *   could be identified, and %SHISA_MULTIPLE_KEY_MATCH if more than one
 *   key matched the given criteria, or an error code.
 **/
int
shisa_key_update (Shisa * dbh,
		  const char *realm,
		  const char *principal,
		  const Shisa_key * oldkey, const Shisa_key * newkey)
{
  _Shisa_db *db;
  size_t i;
  int rc;

  for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
    {
      rc = db->backend->key_update (dbh, db->state, realm, principal,
				    oldkey, newkey);
      if (rc != SHISA_OK)
	return rc;
    }

  return SHISA_OK;
}

/**
 * shisa_key_remove:
 * @dbh: Shisa library handle created by shisa().
 * @realm: Name of realm the principal belongs in.
 * @principal: Name of principal to remove key for.
 * @key: Pointer to Shisa key structure with hints on matching the key
 *   to remove.
 *
 * Remove a key, matching the hints in @key, from the Shisa database
 * for the user PRINCIPAL@REALM.  Not all elements of @key need to be
 * filled out, only those you are interested in.  For example, if you
 * want to remove the only key with an etype of 3 (DES-CBC-MD5), set
 * the @key->etype field to 3, and set all other fields to 0.
 *
 * Return value: Returns %SHISA_OK on success, %SHISA_NO_KEY if no key
 *   could be identified, and %SHISA_MULTIPLE_KEY_MATCH if more than one
 *   key matched the given criteria, or an error code.
 **/
int
shisa_key_remove (Shisa * dbh,
		  const char *realm,
		  const char *principal, const Shisa_key * key)
{
  _Shisa_db *db;
  size_t i;
  int rc;

  for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
    {
      rc = db->backend->key_remove (dbh, db->state, realm, principal, key);
      if (rc != SHISA_OK)
	return rc;
    }

  return SHISA_OK;
}

/**
 * shisa_key_free:
 * @dbh: Shisa library handle created by shisa().
 * @key: Pointer to Shisa key structure to deallocate.
 *
 * Deallocate the fields of a Shisa key structure, and the structure
 * itself.
 **/
void
shisa_key_free (Shisa * dbh, Shisa_key * key)
{
  free (key->key);
  free (key->salt);
  free (key->str2keyparam);
  free (key->password);
  free (key);
}


/**
 * shisa_keys_free:
 * @dbh: Shisa library handle created by shisa().
 * @keys: Pointer to array with @nkeys elements of keys.
 * @nkeys: Number of key elements in @keys array.
 *
 * Deallocate each element of an array with Shisa database keys, using
 * shisa_key_free().
 **/
void
shisa_keys_free (Shisa * dbh, Shisa_key ** keys, size_t nkeys)
{
  size_t i;

  for (i = 0; i < nkeys; i++)
    shisa_key_free (dbh, keys[i]);
}