| 1 | /* |
| 2 | ** Copyright 2000-2008 Double Precision, Inc. See COPYING for |
| 3 | ** distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include "auth.h" |
| 7 | #include "authstaticlist.h" |
| 8 | #include "courierauthsasl.h" |
| 9 | #include "authwait.h" |
| 10 | #include "courierauthdebug.h" |
| 11 | #include <stdlib.h> |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <signal.h> |
| 15 | #include <unistd.h> |
| 16 | #include <errno.h> |
| 17 | #include <sys/time.h> |
| 18 | #include <sys/select.h> |
| 19 | #include "numlib/numlib.h" |
| 20 | |
| 21 | static const char rcsid[]="$Id: authdaemon.c,v 1.18 2008/06/29 16:39:24 mrsam Exp $"; |
| 22 | |
| 23 | extern int authdaemondo(const char *authreq, |
| 24 | int (*func)(struct authinfo *, void *), void *arg); |
| 25 | |
| 26 | extern void auth_daemon_enumerate( void(*cb_func)(const char *name, |
| 27 | uid_t uid, |
| 28 | gid_t gid, |
| 29 | const char *homedir, |
| 30 | const char *maildir, |
| 31 | const char *options, |
| 32 | void *void_arg), |
| 33 | void *void_arg); |
| 34 | |
| 35 | |
| 36 | int auth_generic(const char *service, |
| 37 | const char *authtype, |
| 38 | char *authdata, |
| 39 | int (*callback_func)(struct authinfo *, void *), |
| 40 | void *callback_arg) |
| 41 | { |
| 42 | char tbuf[NUMBUFSIZE]; |
| 43 | size_t l=strlen(service)+strlen(authtype)+strlen(authdata)+2; |
| 44 | char *n=libmail_str_size_t(l, tbuf); |
| 45 | char *buf=malloc(strlen(n)+l+20); |
| 46 | int rc; |
| 47 | |
| 48 | courier_authdebug_login_init(); |
| 49 | |
| 50 | if (!buf) |
| 51 | return 1; |
| 52 | |
| 53 | strcat(strcat(strcpy(buf, "AUTH "), n), "\n"); |
| 54 | strcat(strcat(buf, service), "\n"); |
| 55 | strcat(strcat(buf, authtype), "\n"); |
| 56 | strcat(buf, authdata); |
| 57 | |
| 58 | rc=strcmp(authtype, "EXTERNAL") == 0 |
| 59 | ? auth_getuserinfo(service, authdata, callback_func, |
| 60 | callback_arg) |
| 61 | : authdaemondo(buf, callback_func, callback_arg); |
| 62 | free(buf); |
| 63 | |
| 64 | if (courier_authdebug_login_level) |
| 65 | { |
| 66 | struct timeval t; |
| 67 | |
| 68 | /* short delay to try and allow authdaemond's courierlogger |
| 69 | to finish writing; otherwise items can appear out of order */ |
| 70 | t.tv_sec = 0; |
| 71 | t.tv_usec = 100000; |
| 72 | select(0, 0, 0, 0, &t); |
| 73 | } |
| 74 | |
| 75 | return rc; |
| 76 | } |
| 77 | |
| 78 | int auth_callback_default(struct authinfo *ainfo) |
| 79 | { |
| 80 | if (ainfo->address == NULL) |
| 81 | { |
| 82 | fprintf(stderr, "WARN: No address!!\n"); |
| 83 | return (-1); |
| 84 | } |
| 85 | |
| 86 | if (ainfo->sysusername) |
| 87 | libmail_changeusername(ainfo->sysusername, |
| 88 | &ainfo->sysgroupid); |
| 89 | else if (ainfo->sysuserid) |
| 90 | libmail_changeuidgid(*ainfo->sysuserid, |
| 91 | ainfo->sysgroupid); |
| 92 | else |
| 93 | { |
| 94 | fprintf(stderr, "WARN: %s: No UID/GID!!\n", ainfo->address); |
| 95 | return (-1); |
| 96 | } |
| 97 | |
| 98 | if (!ainfo->homedir) |
| 99 | { |
| 100 | errno=EINVAL; |
| 101 | fprintf(stderr, "WARN: %s: No homedir!!\n", ainfo->address); |
| 102 | return (1); |
| 103 | } |
| 104 | |
| 105 | if (chdir(ainfo->homedir)) |
| 106 | { |
| 107 | fprintf(stderr, "WARN: %s: chdir(%s) failed!!\n", |
| 108 | ainfo->address, ainfo->homedir); |
| 109 | perror("WARN: error"); |
| 110 | return (1); |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |