summaryrefslogtreecommitdiffstats
path: root/src/auth/userdb.c
blob: 21751f92970b006da00dfd07c7ea8a0639b2374f (plain)
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
/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */

#include "auth-common.h"
#include "array.h"
#include "ipwd.h"
#include "auth-worker-server.h"
#include "userdb.h"

static ARRAY(struct userdb_module_interface *) userdb_interfaces;
static ARRAY(struct userdb_module *) userdb_modules;

static const struct userdb_module_interface userdb_iface_deinit = {
	.name = "deinit"
};

static struct userdb_module_interface *userdb_interface_find(const char *name)
{
	struct userdb_module_interface *iface;

	array_foreach_elem(&userdb_interfaces, iface) {
		if (strcmp(iface->name, name) == 0)
			return iface;
	}
	return NULL;
}

void userdb_register_module(struct userdb_module_interface *iface)
{
	struct userdb_module_interface *old_iface;

	old_iface = userdb_interface_find(iface->name);
	if (old_iface != NULL && old_iface->lookup == NULL) {
		/* replacing a "support not compiled in" userdb */
		userdb_unregister_module(old_iface);
	} else if (old_iface != NULL) {
		i_panic("userdb_register_module(%s): Already registered",
			iface->name);
	}
	array_push_back(&userdb_interfaces, &iface);
}

void userdb_unregister_module(struct userdb_module_interface *iface)
{
	struct userdb_module_interface *const *ifaces;
	unsigned int idx;

	array_foreach(&userdb_interfaces, ifaces) {
		if (*ifaces == iface) {
			idx = array_foreach_idx(&userdb_interfaces, ifaces);
			array_delete(&userdb_interfaces, idx, 1);
			return;
		}
	}
	i_panic("userdb_unregister_module(%s): Not registered", iface->name);
}

uid_t userdb_parse_uid(struct auth_request *request, const char *str)
{
	struct passwd pw;
	uid_t uid;

	if (str == NULL)
		return (uid_t)-1;

	if (str_to_uid(str, &uid) == 0)
		return uid;

	switch (i_getpwnam(str, &pw)) {
	case -1:
		e_error(request == NULL ? auth_event : authdb_event(request),
			"getpwnam() failed: %m");
		return (uid_t)-1;
	case 0:
		e_error(request == NULL ? auth_event : authdb_event(request),
			"Invalid UID value '%s'", str);
		return (uid_t)-1;
	default:
		return pw.pw_uid;
	}
}

gid_t userdb_parse_gid(struct auth_request *request, const char *str)
{
	struct group gr;
	gid_t gid;

	if (str == NULL)
		return (gid_t)-1;

	if (str_to_gid(str, &gid) == 0)
		return gid;

	switch (i_getgrnam(str, &gr)) {
	case -1:
		e_error(request == NULL ? auth_event : authdb_event(request),
			"getgrnam() failed: %m");
		return (gid_t)-1;
	case 0:
		e_error(request == NULL ? auth_event : authdb_event(request),
			"Invalid GID value '%s'", str);
		return (gid_t)-1;
	default:
		return gr.gr_gid;
	}
}

static struct userdb_module *
userdb_find(const char *driver, const char *args, unsigned int *idx_r)
{
	struct userdb_module *const *userdbs;
	unsigned int i, count;

	userdbs = array_get(&userdb_modules, &count);
	for (i = 0; i < count; i++) {
		if (strcmp(userdbs[i]->iface->name, driver) == 0 &&
		    strcmp(userdbs[i]->args, args) == 0) {
			*idx_r = i;
			return userdbs[i];
		}
	}
	return NULL;
}

struct userdb_module *
userdb_preinit(pool_t pool, const struct auth_userdb_settings *set)
{
	static unsigned int auth_userdb_id = 0;
	struct userdb_module_interface *iface;
	struct userdb_module *userdb;
	unsigned int idx;

	iface = userdb_interface_find(set->driver);
	if (iface == NULL || iface->lookup == NULL) {
		/* maybe it's a plugin. try to load it. */
		auth_module_load(t_strconcat("authdb_", set->driver, NULL));
		iface = userdb_interface_find(set->driver);
	}
	if (iface == NULL)
		i_fatal("Unknown userdb driver '%s'", set->driver);
	if (iface->lookup == NULL) {
		i_fatal("Support not compiled in for userdb driver '%s'",
			set->driver);
	}
	if (iface->preinit == NULL && iface->init == NULL &&
	    *set->args != '\0') {
		i_fatal("userdb %s: No args are supported: %s",
			set->driver, set->args);
	}

	userdb = userdb_find(set->driver, set->args, &idx);
	if (userdb != NULL)
		return userdb;

	if (iface->preinit == NULL)
		userdb = p_new(pool, struct userdb_module, 1);
	else
		userdb = iface->preinit(pool, set->args);
	userdb->id = ++auth_userdb_id;
	userdb->iface = iface;
	userdb->args = p_strdup(pool, set->args);

	array_push_back(&userdb_modules, &userdb);
	return userdb;
}

void userdb_init(struct userdb_module *userdb)
{
	if (userdb->iface->init != NULL && userdb->init_refcount == 0)
		userdb->iface->init(userdb);
	userdb->init_refcount++;
}

void userdb_deinit(struct userdb_module *userdb)
{
	unsigned int idx;

	i_assert(userdb->init_refcount > 0);

	if (--userdb->init_refcount > 0)
		return;

	if (userdb_find(userdb->iface->name, userdb->args, &idx) == NULL)
		i_unreached();
	array_delete(&userdb_modules, idx, 1);

	if (userdb->iface->deinit != NULL)
		userdb->iface->deinit(userdb);

	/* make sure userdb isn't accessed again */
	userdb->iface = &userdb_iface_deinit;
}

void userdbs_generate_md5(unsigned char md5[STATIC_ARRAY MD5_RESULTLEN])
{
	struct md5_context ctx;
	struct userdb_module *const *userdbs;
	unsigned int i, count;

	md5_init(&ctx);
	userdbs = array_get(&userdb_modules, &count);
	for (i = 0; i < count; i++) {
		md5_update(&ctx, &userdbs[i]->id, sizeof(userdbs[i]->id));
		md5_update(&ctx, userdbs[i]->iface->name,
			   strlen(userdbs[i]->iface->name));
		md5_update(&ctx, userdbs[i]->args, strlen(userdbs[i]->args));
	}
	md5_final(&ctx, md5);
}

const char *userdb_result_to_string(enum userdb_result result)
{
	switch (result) {
	case USERDB_RESULT_INTERNAL_FAILURE:
		return "internal_failure";
	case USERDB_RESULT_USER_UNKNOWN:
		return "user_unknown";
	case USERDB_RESULT_OK:
		return "ok";
	}
	i_unreached();
}

extern struct userdb_module_interface userdb_prefetch;
extern struct userdb_module_interface userdb_static;
extern struct userdb_module_interface userdb_passwd;
extern struct userdb_module_interface userdb_passwd_file;
extern struct userdb_module_interface userdb_ldap;
extern struct userdb_module_interface userdb_sql;
extern struct userdb_module_interface userdb_checkpassword;
extern struct userdb_module_interface userdb_dict;
#ifdef HAVE_LUA
extern struct userdb_module_interface userdb_lua;
#endif

void userdbs_init(void)
{
	i_array_init(&userdb_interfaces, 16);
	i_array_init(&userdb_modules, 16);
	userdb_register_module(&userdb_passwd);
	userdb_register_module(&userdb_passwd_file);
	userdb_register_module(&userdb_prefetch);
	userdb_register_module(&userdb_static);
	userdb_register_module(&userdb_ldap);
	userdb_register_module(&userdb_sql);
	userdb_register_module(&userdb_checkpassword);
	userdb_register_module(&userdb_dict);
#ifdef HAVE_LUA
	userdb_register_module(&userdb_lua);
#endif
}

void userdbs_deinit(void)
{
	array_free(&userdb_modules);
	array_free(&userdb_interfaces);
}