File: authdaemon.c

package info (click to toggle)
courier-authlib 0.72.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,824 kB
  • sloc: ansic: 15,828; sh: 5,047; cpp: 4,205; makefile: 853; perl: 761
file content (311 lines) | stat: -rw-r--r-- 6,384 bytes parent folder | download | duplicates (4)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
** Copyright 2000-2015 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#include	"auth.h"
#include	"courierauthstaticlist.h"
#include	"courierauthsasl.h"
#include	"authwait.h"
#include	"courierauthdebug.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	<sys/stat.h>
#include	<pwd.h>
#include	<sys/types.h>
#include	<dirent.h>

extern int authdaemondo_meta(struct auth_meta *meta,
			     const char *authreq,
			     int (*func)(struct authinfo *, void *),
			     void *arg);

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

int auth_generic(const char *service,
		 const char *authtype,
		 char *authdata,
		 int (*callback_func)(struct authinfo *, void *),
		 void *callback_arg)
{
	struct auth_meta dummy;

	memset(&dummy, 0, sizeof(dummy));

	return auth_generic_meta(&dummy, service, authtype, authdata,
				 callback_func, callback_arg);
}

int auth_generic_meta(struct auth_meta *meta,
		      const char *service,
		      const char *authtype,
		      char *authdata,
		      int (*callback_func)(struct authinfo *, void *),
		      void *callback_arg)
{
	char	tbuf[NUMBUFSIZE];
	size_t	l=strlen(service)+strlen(authtype)+strlen(authdata)+2;
	char	*n=libmail_str_size_t(l, tbuf);
	char	*buf=malloc(strlen(n)+l+20);
	int	rc;

	courier_authdebug_login_init();

	if (!buf)
		return 1;

	strcat(strcat(strcpy(buf, "AUTH "), n), "\n");
	strcat(strcat(buf, service), "\n");
	strcat(strcat(buf, authtype), "\n");
	strcat(buf, authdata);

	rc=strcmp(authtype, "EXTERNAL") == 0
		? auth_getuserinfo_meta(meta, service, authdata, callback_func,
					callback_arg)
		: authdaemondo_meta(meta, buf, callback_func, callback_arg);
	free(buf);

	if (courier_authdebug_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);
	}

	return rc;
}

int auth_callback_default(struct authinfo *ainfo)
{
	if (ainfo->address == NULL)
	{
		fprintf(stderr, "WARN: No address!!\n");
		return (-1);
	}

	if (ainfo->sysusername)
		libmail_changeusername(ainfo->sysusername,
				       &ainfo->sysgroupid);
	else if (ainfo->sysuserid)
		libmail_changeuidgid(*ainfo->sysuserid,
				     ainfo->sysgroupid);
	else
	{
		fprintf(stderr, "WARN: %s: No UID/GID!!\n", ainfo->address);
		return (-1);
	}

	if (!ainfo->homedir)
	{
		errno=EINVAL;
		fprintf(stderr, "WARN: %s: No homedir!!\n", ainfo->address);
		return (1);
	}

	if (chdir(ainfo->homedir))
	{
		fprintf(stderr, "WARN: %s: chdir(%s) failed!!\n",
			ainfo->address, ainfo->homedir);
		perror("WARN: error");
		return (1);
	}

	return 0;
}

static void do_copy(const char *from,
		    const char *to,
		    struct stat *stat_buf,
		    uid_t uid,
		    gid_t gid)
{
	FILE *fp=fopen(from, "r");
	FILE *fp2;
	int c;

	if (!fp)
	{
		fprintf(stderr, "WARN: %s: %s!!\n", from, strerror(errno));
		return;
	}

	fp2=fopen(to, "w");

	if (!fp2)
	{
		fprintf(stderr, "WARN: %s: %s!!\n", to, strerror(errno));
		fclose(fp);
		return;
	}

	while ((c=getc(fp)) != EOF)
		putc(c, fp2);
	fclose(fp);
	fclose(fp2);

	if (chmod(to, stat_buf->st_mode & ~S_IFMT) ||
	    chown(to, uid, gid) < 0)
	{
		fprintf(stderr, "WARN: %s: %s!!\n", to, strerror(errno));
	}
}

static void do_symlink(const char *from,
		       const char *to,
		       uid_t uid,
		       gid_t gid)
{
	char buf[1024];
	ssize_t l=readlink(from, buf, sizeof(buf)-1);

	if (l < 0)
	{
		fprintf(stderr, "WARN: %s: %s!!\n", from, strerror(errno));
		return;
	}
	buf[l]=0;

	if (symlink(buf, to) < 0 || lchown(to, uid, gid) < 0)
	{
		fprintf(stderr, "WARN: %s: %s!!\n", from, strerror(errno));
		return;
	}
}

static int do_mkhomedir(const char *skeldir,
			const char *homedir,
			uid_t uid,
			gid_t gid)
{
	struct stat stat_buf;
	DIR *d;
	struct dirent *de;

	mkdir(homedir, 0);

	if (stat(skeldir, &stat_buf) < 0 ||
	    chmod(homedir, stat_buf.st_mode & ~S_IFMT) ||
	    chown(homedir, uid, gid) < 0)
	{
		fprintf(stderr, "WARN: %s: %s!!\n", skeldir, strerror(errno));
		return 0;
	}

	if ((d=opendir(skeldir)) != NULL)
	{
		while ((de=readdir(d)) != NULL)
		{
			char *fskeldir, *fhomedir;

			if (strcmp(de->d_name, ".") == 0)
				continue;
			if (strcmp(de->d_name, "..") == 0)
				continue;

			fskeldir=malloc(strlen(skeldir)+2+strlen(de->d_name));
			fhomedir=malloc(strlen(homedir)+2+strlen(de->d_name));

			if (fskeldir && fhomedir)
			{
				strcat(strcat(strcpy(fskeldir, skeldir), "/"),
				       de->d_name);
				strcat(strcat(strcpy(fhomedir, homedir), "/"),
				       de->d_name);

				if (lstat(fskeldir, &stat_buf) == 0)
				{
					if (S_ISDIR(stat_buf.st_mode))
					{
						do_mkhomedir(fskeldir, fhomedir,
							     uid, gid);
					}
					else if (S_ISLNK(stat_buf.st_mode))
					{
						do_symlink(fskeldir, fhomedir,
							   uid, gid);
					}
					else if (S_ISREG(stat_buf.st_mode))
					{
						do_copy(fskeldir, fhomedir,
							&stat_buf,
							uid, gid);
					}
				}
			}

			if (fskeldir)
				free(fskeldir);
			if (fhomedir)
				free(fhomedir);
		}
		closedir(d);
	}
	return 0;
}

static int mkhomedir(struct authinfo *ainfo,
		     const char *skel)
{
	if (ainfo->sysusername)
	{
		struct passwd *pw=getpwnam(ainfo->sysusername);
		return do_mkhomedir(skel, ainfo->homedir,
				    pw->pw_uid,
				    pw->pw_gid);
	}

	return do_mkhomedir(skel, ainfo->homedir,
			    *ainfo->sysuserid, ainfo->sysgroupid);
}

int auth_mkhomedir(struct authinfo *ainfo)
{
	struct stat stat_buf;

	const char *p=getenv("AUTH_MKHOMEDIR_SKEL");

	if (p && *p && ainfo->address &&
	    (ainfo->sysusername || ainfo->sysuserid) &&
	    ainfo->homedir && stat(ainfo->homedir, &stat_buf) < 0 &&
	    errno == ENOENT && stat(p, &stat_buf) == 0)
	{
		int rc;
		mode_t old_umask=umask(0777);

		rc=mkhomedir(ainfo, p);

		umask(old_umask);

		if (rc)
			return rc;
	}
	return 0;
}

int auth_callback_default_autocreate(struct authinfo *ainfo)
{
	int rc=auth_mkhomedir(ainfo);

	if (rc)
		return rc;

	return auth_callback_default(ainfo);
}