Revert "preauthuserdbcommon.c: Move token-getting code to below callback."
[hcoop/debian/courier-authlib.git] / authdaemon.c
CommitLineData
d9898ee8 1/*
2** Copyright 2000-2005 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
21static const char rcsid[]="$Id: authdaemon.c,v 1.17 2005/06/30 16:16:07 mrsam Exp $";
22
23extern int authdaemondo(const char *authreq,
24 int (*func)(struct authinfo *, void *), void *arg);
25
26extern 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
36int 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=authdaemondo(buf, callback_func, callback_arg);
59 free(buf);
60
61 if (courier_authdebug_login_level)
62 {
63 struct timeval t;
64
65 /* short delay to try and allow authdaemond's courierlogger
66 to finish writing; otherwise items can appear out of order */
67 t.tv_sec = 0;
68 t.tv_usec = 100000;
69 select(0, 0, 0, 0, &t);
70 }
71
72 return rc;
73}
74
75int auth_callback_default(struct authinfo *ainfo)
76{
77 if (ainfo->address == NULL)
78 {
79 fprintf(stderr, "WARN: No address!!\n");
80 return (-1);
81 }
82
83 if (ainfo->sysusername)
84 libmail_changeusername(ainfo->sysusername,
85 &ainfo->sysgroupid);
86 else if (ainfo->sysuserid)
87 libmail_changeuidgid(*ainfo->sysuserid,
88 ainfo->sysgroupid);
89 else
90 {
91 fprintf(stderr, "WARN: %s: No UID/GID!!\n", ainfo->address);
92 return (-1);
93 }
94
95 if (!ainfo->homedir)
96 {
97 errno=EINVAL;
98 fprintf(stderr, "WARN: %s: No homedir!!\n", ainfo->address);
99 return (1);
100 }
101
102 if (chdir(ainfo->homedir))
103 {
104 fprintf(stderr, "WARN: %s: chdir(%s) failed!!\n",
105 ainfo->address, ainfo->homedir);
106 perror("WARN: error");
107 return (1);
108 }
109
110 return 0;
111}