| 1 | /* |
| 2 | ** Copyright 1998 - 1999 Double Precision, Inc. See COPYING for |
| 3 | ** distribution information. |
| 4 | */ |
| 5 | |
| 6 | #if HAVE_CONFIG_H |
| 7 | #include "courier_auth_config.h" |
| 8 | #endif |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <errno.h> |
| 13 | #include <pwd.h> |
| 14 | #if HAVE_UNISTD_H |
| 15 | #include <unistd.h> |
| 16 | #endif |
| 17 | |
| 18 | #include "auth.h" |
| 19 | #include "courierauthdebug.h" |
| 20 | #include "userdb/userdb.h" |
| 21 | |
| 22 | static const char rcsid[]="$Id: preauthuserdbcommon.c,v 1.21 2006/10/28 19:22:52 mrsam Exp $"; |
| 23 | |
| 24 | int auth_userdb_pre_common(const char *userid, const char *service, |
| 25 | int needpass, |
| 26 | int (*callback)(struct authinfo *, void *), |
| 27 | void *arg) |
| 28 | { |
| 29 | char *u; |
| 30 | struct userdbs *udb; |
| 31 | struct authinfo auth; |
| 32 | char *udbs; |
| 33 | char *services; |
| 34 | char *passwords=0; |
| 35 | int rc; |
| 36 | |
| 37 | userdb_set_debug(courier_authdebug_login_level); |
| 38 | userdb_init(USERDB ".dat"); |
| 39 | /* We rely on DPRINTF doing 'safe' printing */ |
| 40 | DPRINTF("userdb: looking up '%s'", userid); |
| 41 | if ( (u=userdb(userid)) == 0) |
| 42 | { |
| 43 | userdb_close(); |
| 44 | errno=EPERM; |
| 45 | return (-1); |
| 46 | } |
| 47 | |
| 48 | if ((udb=userdb_creates(u)) == 0) |
| 49 | { |
| 50 | free(u); |
| 51 | return (-1); |
| 52 | } |
| 53 | free(u); |
| 54 | |
| 55 | memset(&auth, 0, sizeof(auth)); |
| 56 | |
| 57 | auth.sysuserid= &udb->udb_uid; |
| 58 | auth.sysgroupid=udb->udb_gid; |
| 59 | auth.homedir=udb->udb_dir; |
| 60 | auth.address=userid; |
| 61 | auth.fullname=udb->udb_gecos; |
| 62 | auth.options=udb->udb_options; |
| 63 | |
| 64 | if (needpass) |
| 65 | { |
| 66 | udbs=userdbshadow(USERDB "shadow.dat", userid); |
| 67 | |
| 68 | if (udbs) |
| 69 | { |
| 70 | if ((services=malloc(strlen(service)+sizeof("pw"))) == 0) |
| 71 | { |
| 72 | perror("malloc"); |
| 73 | free(udbs); |
| 74 | userdb_frees(udb); |
| 75 | return (1); |
| 76 | } |
| 77 | |
| 78 | strcat(strcpy(services, service), "pw"); |
| 79 | |
| 80 | passwords=userdb_gets(udbs, services); |
| 81 | |
| 82 | if (passwords) |
| 83 | { |
| 84 | DPRINTF("found %s in userdbshadow", services); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | passwords=userdb_gets(udbs, "systempw"); |
| 89 | if (passwords) |
| 90 | { |
| 91 | DPRINTF("found systempw in userdbshadow"); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | DPRINTF("no %s or systempw value in userdbshadow for %s", |
| 96 | services, userid); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | free(services); |
| 101 | free(udbs); |
| 102 | } |
| 103 | auth.passwd=passwords; |
| 104 | } |
| 105 | |
| 106 | auth.maildir=udb->udb_mailbox; |
| 107 | auth.quota=udb->udb_quota; |
| 108 | |
| 109 | courier_authdebug_authinfo("DEBUG: authuserdb: ", &auth, 0, passwords); |
| 110 | rc= (*callback)(&auth, arg); |
| 111 | if (passwords) free(passwords); |
| 112 | userdb_frees(udb); |
| 113 | return (rc); |
| 114 | } |
| 115 | |
| 116 | void auth_userdb_cleanup() |
| 117 | { |
| 118 | userdb_close(); |
| 119 | } |
| 120 | |
| 121 | void auth_userdb_enumerate( void(*cb_func)(const char *name, |
| 122 | uid_t uid, |
| 123 | gid_t gid, |
| 124 | const char *homedir, |
| 125 | const char *maildir, |
| 126 | const char *options, |
| 127 | void *void_arg), |
| 128 | void *void_arg) |
| 129 | { |
| 130 | struct userdbs *u; |
| 131 | |
| 132 | userdb_init(USERDB ".dat"); |
| 133 | |
| 134 | for (u=userdb_enum_first(); u; u=userdb_enum_next()) |
| 135 | { |
| 136 | (*cb_func)(u->udb_name, |
| 137 | u->udb_uid, |
| 138 | u->udb_gid, |
| 139 | u->udb_dir, |
| 140 | u->udb_mailbox, |
| 141 | u->udb_options, |
| 142 | void_arg); |
| 143 | userdb_frees(u); |
| 144 | } |
| 145 | (*cb_func)(NULL, 0, 0, NULL, NULL, NULL, void_arg); |
| 146 | } |
| 147 | |