File: authmysql.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 (380 lines) | stat: -rw-r--r-- 8,065 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
** Copyright 2000-2001 Double Precision, Inc.  See COPYING for
** distribution information.
*/
#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<errno.h>
#include	<pwd.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif

#include	"auth.h"
#include	"authmod.h"
#include	"authmysql.h"
#include	"authstaticlist.h"
#include	"debug.h"

static const char rcsid[]="$Id: authmysql.c,v 1.17 2004/05/09 02:52:23 mrsam Exp $";

extern void auth_mysql_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 char *auth_mysql_login(const char *service, char *authdata,
	int issession,
	void (*callback_func)(struct authinfo *, void *), void *callback_arg)
{
char *user, *pass;
struct authmysqluserinfo *authinfo;

	if ((user=strtok(authdata, "\n")) == 0 ||
		(pass=strtok(0, "\n")) == 0)
	{
		errno=EPERM;
		return (0);
	}

	authinfo=auth_mysql_getuserinfo(user, service);

	if (!callback_func)
		auth_mysql_cleanup();

	if (!authinfo)		/* Fatal error - such as MySQL being down */
	{
		errno=EACCES;
		return (0);
	}

	if (authinfo->cryptpw)
	{
		if (authcheckpassword(pass,authinfo->cryptpw))
		{
			errno=EPERM;
			return (0);	/* User/Password not found. */
		}
	}
	else if (authinfo->clearpw)
	{
		if (strcmp(pass, authinfo->clearpw))
		{
			if (auth_debug_login_level >= 2)
				dprintf("supplied password '%s' does not match clearpasswd '%s'",
					pass, authinfo->clearpw);
			else
				dprintf("supplied password does not match clearpasswd");
			errno=EPERM;
			return (0);
		}
	}
	else
	{
		dprintf("no password available to compare");
		errno=EPERM;
		return (0);		/* Username not found */
	}

	if (callback_func == 0)
	{
	static char *maildir=0;
	static char *quota=0;
	static char *options=0;

		authsuccess(authinfo->home, 0, &authinfo->uid,
			    &authinfo->gid, authinfo->username,
			    authinfo->fullname ? authinfo->fullname:"");

		if (authinfo->maildir && authinfo->maildir[0])
		{
			if (maildir)	free(maildir);
			maildir=malloc(sizeof("MAILDIR=")+
					strlen(authinfo->maildir));
			if (!maildir)
			{
				perror("malloc");
				exit(1);
			}
			strcat(strcpy(maildir, "MAILDIR="), authinfo->maildir);
			putenv(maildir);
		}
		else
		{
			putenv("MAILDIR=");
		}

		if (authinfo->quota && authinfo->quota[0])
                {
                        if (quota)    free(quota);
                        quota=malloc(sizeof("MAILDIRQUOTA=")+
                                        strlen(authinfo->quota));
                        if (!quota)
                        {
                                perror("malloc");
                                exit(1);
                        }
                        strcat(strcpy(quota, "MAILDIRQUOTA="), authinfo->quota);
                        putenv(quota);
                }
                else
                {
                        putenv("MAILDIRQUOTA=");
                }

		if (authinfo->options)
		{
                        if (options)    free(options);
                        options=malloc(sizeof("OPTIONS=")+
                                        strlen(authinfo->options));
                        if (!options)
                        {
                                perror("malloc");
                                exit(1);
                        }
                        strcat(strcpy(options, "OPTIONS="), authinfo->options);
                        putenv(options);
		}
		else
			putenv("OPTIONS=");

	}
	else
	{
	struct	authinfo	aa;

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

		/*aa.sysusername=user;*/
		aa.sysuserid= &authinfo->uid;
		aa.sysgroupid= authinfo->gid;
		aa.homedir=authinfo->home;
		aa.maildir=authinfo->maildir && authinfo->maildir[0] ?
			authinfo->maildir:0;
		aa.address=authinfo->username;
		aa.quota=authinfo->quota && authinfo->quota[0] ?
			authinfo->quota:0;
		aa.fullname=authinfo->fullname;
		aa.options=authinfo->options;
		auth_debug_authinfo("DEBUG: authmysql: ", &aa,
			authinfo->clearpw, authinfo->cryptpw);
		(*callback_func)(&aa, callback_arg);
	}

	return (strdup(authinfo->username));
}

static int auth_mysql_changepw(const char *service, const char *user,
				 const char *pass,
				 const char *newpass)
{
struct authmysqluserinfo *authinfo;

	authinfo=auth_mysql_getuserinfo(user, service);

	if (!authinfo)
	{
		errno=ENOENT;
		return (-1);
	}

	if (authinfo->cryptpw)
	{
		if (authcheckpassword(pass,authinfo->cryptpw))
		{
			errno=EPERM;
			return (-1);	/* User/Password not found. */
		}
	}
	else if (authinfo->clearpw)
	{
		if (strcmp(pass, authinfo->clearpw))
		{
			errno=EPERM;
			return (-1);
		}
	}
	else
	{
		errno=EPERM;
		return (-1);
	}

	if (auth_mysql_setpass(user, newpass))
	{
		errno=EPERM;
		return (-1);
	}
	return (0);
}

#if HAVE_HMACLIB

#include	"../libhmac/hmac.h"
#include	"cramlib.h"

struct cram_callback_info {
	struct hmac_hashinfo *h;
	char *user;
	char *challenge;
	char *response;
	char *userret;
	int issession;
	void (*callback_func)(struct authinfo *, void *);
	void *callback_arg;
	};

static int callback_cram(struct authinfo *a, void *vp)
{
struct cram_callback_info *cci=(struct cram_callback_info *)vp;
unsigned char *hashbuf;
unsigned char *p;
unsigned i;
static const char hex[]="0123456789abcdef";
int	rc;

	if (!a->clearpasswd)
		return (-1);

	/*
		hmac->hh_L*2 will be the size of the binary hash.

		hmac->hh_L*4+1 will therefore be size of the binary hash,
		as a hexadecimal string.
	*/

	if ((hashbuf=malloc(cci->h->hh_L*6+1)) == 0)
		return (1);

	hmac_hashkey(cci->h, a->clearpasswd, strlen(a->clearpasswd),
		hashbuf, hashbuf+cci->h->hh_L);

	p=hashbuf+cci->h->hh_L*2;

	for (i=0; i<cci->h->hh_L*2; i++)
	{
	char	c;

		c = hex[ (hashbuf[i] >> 4) & 0x0F];
		*p++=c;

		c = hex[ hashbuf[i] & 0x0F];
		*p++=c;

		*p=0;
	}

	rc=auth_verify_cram(cci->h, cci->challenge, cci->response,
		(const char *)hashbuf+cci->h->hh_L*2);
	free(hashbuf);

	if (rc)	return (rc);

	if ((cci->userret=strdup(a->address)) == 0)
	{
		perror("malloc");
		return (1);
	}

	if (cci->callback_func)
		(*cci->callback_func)(a, cci->callback_arg);
	else
	{
		authsuccess(a->homedir, a->sysusername, a->sysuserid,
			&a->sysgroupid,
			a->address,
			a->quota);

		if (a->maildir && a->maildir[0])
		{
		static char *maildir=0;

			if (maildir)	free(maildir);
			maildir=malloc(sizeof("MAILDIR=")+strlen(a->maildir));
			if (!maildir)
			{
				perror("malloc");
				exit(1);
			}
			strcat(strcpy(maildir, "MAILDIR="), a->maildir);
			putenv(maildir);
		}
		else
		{
			putenv("MAILDIR=");
		}
	}

	return (0);
}

static char *auth_mysql_cram(const char *service,
	const char *authtype, char *authdata, int issession,
	void (*callback_func)(struct authinfo *, void *), void *callback_arg)
{
struct	cram_callback_info	cci;
int	rc;

	if (auth_get_cram(authtype, authdata,
		&cci.h, &cci.user, &cci.challenge, &cci.response))
		return (0);

	cci.issession=issession;
	cci.callback_func=callback_func;
	cci.callback_arg=callback_arg;

	rc=auth_mysql_pre(cci.user, service, &callback_cram, &cci);

	if (callback_func == 0)
		auth_mysql_cleanup();

	if (rc < 0)
	{
		errno=EPERM;
		return (0);
	}
	if (rc > 0)
	{
		errno=EACCES;
		return (0);
	}
	return (cci.userret);
}
#endif

char *auth_mysql(const char *service, const char *authtype, char *authdata,
		int issession,
	void (*callback_func)(struct authinfo *, void *), void *callback_arg)
{
	if (strcmp(authtype, AUTHTYPE_LOGIN) == 0)
		return (auth_mysql_login(service, authdata, issession,
			callback_func, callback_arg));

#if HAVE_HMACLIB
	return (auth_mysql_cram(service, authtype, authdata, issession,
			callback_func, callback_arg));
#else
	errno=EPERM;
	return (0);
#endif
}

extern int auth_mysql_pre(const char *user, const char *service,
			  int (*callback)(struct authinfo *, void *),
			  void *arg);

struct authstaticinfo authmysql_info={
	"authmysql",
	auth_mysql,
	auth_mysql_pre,
	auth_mysql_cleanup,
	auth_mysql_changepw,
	NULL,
	auth_mysql_enumerate};