File: authdaemon.c

package info (click to toggle)
courier 0.47-4sarge5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 32,680 kB
  • ctags: 12,265
  • sloc: ansic: 164,045; cpp: 23,863; sh: 19,569; perl: 7,225; makefile: 4,192; yacc: 316; sed: 16
file content (240 lines) | stat: -rw-r--r-- 5,066 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
** Copyright 2000-2004 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#include	"auth.h"
#include	"authmod.h"
#include	"authstaticlist.h"
#include	"authsasl.h"
#include	"authwait.h"
#include	"debug.h"
#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>
#include	<signal.h>
#include	<unistd.h>
#include	<errno.h>
#include	<sys/time.h>
#include	<sys/select.h>
#include	"numlib/numlib.h"
#include	"authchangepwdir.h"

static const char rcsid[]="$Id: authdaemon.c,v 1.12 2004/04/18 15:54:38 mrsam Exp $";

extern int authdaemondo(const char *authreq,
	int (*func)(struct authinfo *, void *), void *arg);

struct authdaemon_info {
	char *username;
	void (*callback_func)(struct authinfo *, void *);
	void *callback_arg;
} ;

extern void auth_daemon_enumerate( void(*cb_func)(const char *name,
						  uid_t uid,
						  gid_t gid,
						  const char *homedir,
						  const char *maildir,
						  void *void_arg),
				   void *void_arg);

static int callback(struct authinfo *a, void *p)
{
	struct authdaemon_info *ai=(struct authdaemon_info *)p;

        if ((ai->username=strdup(a->address)) == 0)
        {
                perror("authdaemon: malloc");
                return (1);
        }

        {
	static  char *prevp=0;
	const char *cp=a->maildir;
	char    *p;

		if (!cp)        cp="";
		p=malloc(sizeof("MAILDIR=")+strlen(cp));
		if (!p)
		{
			perror("authdaemon: malloc");
			free(ai->username);
			return (1);
		}
		strcat(strcpy(p, "MAILDIR="), cp);
		putenv(p);
		if (prevp)      free(prevp);
		prevp=p;
	}

        {
		static  char *prevp=0;
		const char *cp=a->options;
		char    *p;

		if (!cp)        cp="";
		p=malloc(sizeof("OPTIONS=")+strlen(cp));
		if (!p)
		{
			perror("authdaemon: malloc");
			free(ai->username);
			return (1);
		}
		strcat(strcpy(p, "OPTIONS="), cp);
		putenv(p);
		if (prevp)      free(prevp);
		prevp=p;
	}

	if (a->sysusername && a->sysuserid)
		a->sysusername=0;

	if (ai->callback_func == 0)
		authsuccess(a->homedir, a->sysusername, a->sysuserid,
			    &a->sysgroupid, a->address, a->fullname);
	else
	{
		if (!a->address)
			a->address=a->sysusername;
		(*ai->callback_func)(a, ai->callback_arg);
	}
	return (0);
}

char *auth_daemon(const char *service, const char *authtype, char *authdata,
        int issession,
	void (*callback_func)(struct authinfo *, void *), void *callback_arg)

{
	char	tbuf[NUMBUFSIZE];
	size_t	l=strlen(service)+strlen(authtype)+strlen(authdata)+1;
	char	*n=libmail_str_size_t(l, tbuf);
	char	*buf=malloc(strlen(n)+l+20);
	int	rc;

	struct authdaemon_info ai;

	if (!buf)
	{
		perror("authdaemon: malloc");
		errno=EACCES;
		return (0);
	}
	strcat(strcat(strcpy(buf, "AUTH "), n), "\n");
	strcat(strcat(buf, service), "\n");
	strcat(strcat(buf, authtype), "\n");
	strcat(buf, authdata);

	ai.username=NULL;
	ai.callback_func=callback_func;
	ai.callback_arg=callback_arg;

	rc=authdaemondo(buf, callback, &ai);
	free(buf);

	if (auth_debug_login_level)
	{
	struct timeval t;

		/* short delay to try and allow authdaemond's courierlogger
		   to finish writing; otherwise items can appear out of order */
		t.tv_sec = 0;
		t.tv_usec = 100000;
		select(0, 0, 0, 0, &t);
	}

        if (rc < 0)
        {
                errno=EPERM;
                return (0);
        }
        if (rc > 0)
        {
                errno=EACCES;
                return (0);
        }

	return (ai.username);
}

extern int auth_daemon_pre(const char *uid, const char *service,
        int (*callback)(struct authinfo *, void *),
		    void *arg);

extern void auth_daemon_cleanup();

#define PROG AUTHCHANGEPWDIR "/authdaemon.passwd"

static int auth_daemon_passwd(const char *service,
			      const char *uid,
			      const char *opwd,
			      const char *npwd)
{
	pid_t p, p2;
	int waitstat;
	int pipefd[2];
	FILE *fp;

	signal(SIGCHLD, SIG_DFL);
	signal(SIGPIPE, SIG_IGN);

        if (pipe(pipefd) < 0)
        {
                perror("CRIT: authdaemon: pipe() failed");
                errno=EINVAL;
                return (-1);
        }

        p=fork();

        if (p == 0)
        {
                char *argv[2];

		argv[0]=PROG;
		argv[1]=0;
		close(0);
		dup(pipefd[0]);
		close(pipefd[0]);
		close(pipefd[1]);
		execv(argv[0], argv);
                perror("CRIT: authdaemon: failed to execute " PROG);
		exit(1);
	}

	close(pipefd[0]);

	if ((fp=fdopen(pipefd[1], "w")) != 0)
	{
		fprintf(fp, "%s\t%s\t%s\t%s\n",
			service, uid, opwd, npwd);
		fclose(fp);
	}
	close(pipefd[1]);

        while ((p2=wait(&waitstat)) != p)
        {
                if (p2 < 0 && errno == ECHILD)
                {
                        perror("CRIT: authdaemon: wait() failed");
                        errno=EPERM;
                        return (-1);
                }
        }

        if (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)
                return (0);

	errno=EPERM;
	return (-1);
}

struct authstaticinfo authdaemon_info={
	"authdaemon",
	auth_daemon,
	auth_daemon_pre,
	auth_daemon_cleanup,
	auth_daemon_passwd,
	NULL,
	auth_daemon_enumerate};