Imported Upstream version 0.66.1
[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
17 extern int userdb_debug_level;
18
19 char *userdbshadow(const char *sh, const char *u)
20 {
21 struct dbobj d;
22 char *p,*q;
23 size_t l;
24
25 dbobj_init(&d);
26
27 if (dbobj_open(&d, sh, "R"))
28 {
29 if (userdb_debug_level)
30 fprintf(stderr,
31 "DEBUG: userdbshadow: unable to open %s\n", sh);
32 return (0);
33 }
34
35 q=dbobj_fetch(&d, u, strlen(u), &l, "");
36 dbobj_close(&d);
37 if (!q)
38 {
39 if (userdb_debug_level)
40 fprintf(stderr,
41 "DEBUG: userdbshadow: entry not found\n");
42 errno=ENOENT;
43 return(0);
44 }
45
46 p=malloc(l+1);
47 if (!p)
48 {
49 free(q);
50 return (0);
51 }
52
53 if (l) memcpy(p, q, l);
54 free(q);
55 p[l]=0;
56 return (p);
57 }