File: authsqlite.cpp

package info (click to toggle)
courier-authlib 0.72.6-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 17,844 kB
  • sloc: ansic: 25,772; cpp: 12,475; sh: 5,588; makefile: 938; perl: 761
file content (218 lines) | stat: -rw-r--r-- 5,120 bytes parent folder | download | duplicates (7)
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
/*
** Copyright 2000-2008 Double Precision, Inc.  See COPYING for
** distribution information.
*/
#if	HAVE_CONFIG_H
#include	"courier_auth_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

extern "C" {
#include	"auth.h"
#include	"courierauthstaticlist.h"
#include	"courierauth.h"
#include	"courierauthdebug.h"
#include	"libhmac/hmac.h"
}

#include	"authsqlite.h"

static int auth_sqlite_pre(const char *user, const char *service,
			   int (*callback)(struct authinfo *, void *), void *arg)
{
	authsqliteuserinfo authinfo;
	struct	authinfo	aa;

	if (!auth_sqlite_getuserinfo(user, service, authinfo))
		// Fatal error - such as Sqlite being down
		return (1);

	if (authinfo.home.empty()) // User not found
		return (-1);

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

	/*aa.sysusername=user;*/
	aa.sysuserid= &authinfo.uid;
	aa.sysgroupid= authinfo.gid;
	aa.homedir=authinfo.home.c_str();
	aa.maildir=authinfo.maildir.empty() ? NULL:authinfo.maildir.c_str();
	aa.address=authinfo.username.c_str();
	aa.passwd=authinfo.cryptpw.c_str();
	aa.clearpasswd=authinfo.clearpw.c_str();
	aa.fullname=authinfo.fullname.c_str();
	aa.quota=authinfo.quota.empty() ? NULL:authinfo.quota.c_str();
	aa.options=authinfo.options.c_str();
	return ((*callback)(&aa, arg));
}

extern void auth_sqlite_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);

static bool docheckpw(authsqliteuserinfo &authinfo, const char *pass)
{
	if (!authinfo.cryptpw.empty())
	{
		if (authcheckpassword(pass, authinfo.cryptpw.c_str()))
		{
			errno=EPERM;
			return false;	/* User/Password not found. */
		}
	}
	else if (!authinfo.clearpw.empty())
	{
		if (authinfo.clearpw != pass)
		{
			if (courier_authdebug_login_level >= 2)
			{
				DPRINTF("supplied password '%s' does not match clearpasswd '%s'",
					pass, authinfo.clearpw.c_str());
			}
			else
			{
				DPRINTF("supplied password does not match clearpasswd");
			}
			errno=EPERM;
			return false;
		}
	}
	else
	{
		DPRINTF("no password available to compare");
		errno=EPERM;
		return false;		/* Username not found */
	}
	return true;
}

static int auth_sqlite_login(const char *service, char *authdata,
			     int (*callback_func)(struct authinfo *, void *),
			     void *callback_arg)
{
	char *user, *pass;
	authsqliteuserinfo authinfo;
	struct	authinfo	aa;


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

	if (!auth_sqlite_getuserinfo(user, service, authinfo))
		// Fatal error - such as Sqlite being down
	{
		errno=EACCES;
		return (1);
	}

	if (!docheckpw(authinfo, pass))
		return (-1);

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

	aa.sysuserid= &authinfo.uid;
	aa.sysgroupid= authinfo.gid;
	aa.homedir=authinfo.home.c_str();
	aa.maildir=authinfo.maildir.empty() ? NULL:authinfo.maildir.c_str();
	aa.address=authinfo.username.c_str();
	aa.quota=authinfo.quota.empty() ? NULL:authinfo.quota.c_str();
	aa.fullname=authinfo.fullname.c_str();
	aa.options=authinfo.options.c_str();
	aa.clearpasswd=pass;
	aa.passwd=authinfo.cryptpw.c_str();
	courier_authdebug_authinfo("DEBUG: authsqlite: ", &aa,
				   authinfo.clearpw.c_str(),
				   authinfo.cryptpw.c_str());

	return (*callback_func)(&aa, callback_arg);
}

static int auth_sqlite_changepw(const char *service, const char *user,
				const char *pass,
				const char *newpass)
{
	authsqliteuserinfo authinfo;

	if (!auth_sqlite_getuserinfo(user, service, authinfo))
	{
		errno=ENOENT;
		return (-1);
	}

	if (!docheckpw(authinfo, pass))
	{
		errno=EPERM;
		return (-1);	/* User/Password not found. */
	}

	if (auth_sqlite_setpass(user, newpass, authinfo.cryptpw.c_str()))
	{
		errno=EPERM;
		return (-1);
	}
	return (0);
}

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

	if (auth_get_cram(authtype, authdata, &cci))
		return (-1);

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

	return auth_sqlite_pre(cci.user, service, &auth_cram_callback, &cci);
}

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

	return (auth_sqlite_cram(service, authtype, authdata,
			callback_func, callback_arg));
}

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

static struct authstaticinfo authsqlite_info={
	"authsqlite",
	auth_sqlite,
	auth_sqlite_pre,
	auth_sqlite_cleanup,
	auth_sqlite_changepw,
	auth_sqlite_cleanup,
	auth_sqlite_enumerate};

extern "C" {
	struct authstaticinfo *courier_authsqlite_init()
	{
		return &authsqlite_info;
	}
}