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