Build courier-authlib 0.60.2-0hcoop1.
[hcoop/debian/courier-authlib.git] / userdb / userdb2.c
1 /*
2 ** Copyright 1998 - 2007 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5
6 #if HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9 #include "dbobj.h"
10 #include "userdb.h"
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <errno.h>
15
16 static const char rcsid[]="$Id: userdb2.c,v 1.6 2007/04/14 03:20:55 mrsam Exp $";
17
18 extern int userdb_debug_level;
19
20 char *userdbshadow(const char *sh, const char *u)
21 {
22 struct dbobj d;
23 char *p,*q;
24 size_t l;
25
26 dbobj_init(&d);
27
28 if (dbobj_open(&d, sh, "R"))
29 {
30 if (userdb_debug_level)
31 fprintf(stderr,
32 "DEBUG: userdbshadow: unable to open %s\n", sh);
33 return (0);
34 }
35
36 q=dbobj_fetch(&d, u, strlen(u), &l, "");
37 dbobj_close(&d);
38 if (!q)
39 {
40 if (userdb_debug_level)
41 fprintf(stderr,
42 "DEBUG: userdbshadow: entry not found\n");
43 errno=ENOENT;
44 return(0);
45 }
46
47 p=malloc(l+1);
48 if (!p)
49 {
50 free(q);
51 return (0);
52 }
53
54 if (l) memcpy(p, q, l);
55 free(q);
56 p[l]=0;
57 return (p);
58 }