summaryrefslogtreecommitdiffstats
path: root/src/lib/ipwd.c
blob: 8ac81fa2d4746feacf9f2d26446d1e733f4d9b1c (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
/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */

#define _POSIX_PTHREAD_SEMANTICS /* for Solaris */
#include "lib.h"
#include "ipwd.h"

#include <unistd.h>

#define PWBUF_MIN_SIZE 128
#define GRBUF_MIN_SIZE 128

static void *pwbuf = NULL, *grbuf = NULL;
static size_t pwbuf_size, grbuf_size;

static void pw_init(void)
{
	size_t old_pwbuf_size = pwbuf_size;

	if (pwbuf == NULL || errno == ERANGE) {
		pwbuf_size = nearest_power(old_pwbuf_size + 1);
		if (pwbuf_size < PWBUF_MIN_SIZE)
			pwbuf_size = PWBUF_MIN_SIZE;
		pwbuf = i_realloc(pwbuf, old_pwbuf_size, pwbuf_size);
	}
}

static void gr_init(void)
{
	size_t old_grbuf_size = grbuf_size;

	if (grbuf == NULL || errno == ERANGE) {
		grbuf_size = nearest_power(old_grbuf_size + 1);
		if (grbuf_size < PWBUF_MIN_SIZE)
			grbuf_size = PWBUF_MIN_SIZE;
		grbuf = i_realloc(grbuf, old_grbuf_size, grbuf_size);
	}
}

void ipwd_deinit(void)
{
	i_free_and_null(pwbuf);
	i_free_and_null(grbuf);
}

int i_getpwnam(const char *name, struct passwd *pwd_r)
{
	struct passwd *result;

	errno = 0;
	do {
		pw_init();
		errno = getpwnam_r(name, pwd_r, pwbuf, pwbuf_size, &result);
	} while (errno == ERANGE);
	if (result != NULL)
		return 1;
	if (errno == EINVAL) {
		/* FreeBSD fails here when name="user@domain" */
		return 0;
	}
	return errno == 0 ? 0 : -1;
}

int i_getpwuid(uid_t uid, struct passwd *pwd_r)
{
	struct passwd *result;

	errno = 0;
	do {
		pw_init();
		errno = getpwuid_r(uid, pwd_r, pwbuf, pwbuf_size, &result);
	} while (errno == ERANGE);
	if (result != NULL)
		return 1;
	return errno == 0 ? 0 : -1;
}

int i_getgrnam(const char *name, struct group *grp_r)
{
	struct group *result;

	errno = 0;
	do {
		gr_init();
		errno = getgrnam_r(name, grp_r, grbuf, grbuf_size, &result);
	} while (errno == ERANGE);
	if (result != NULL)
		return 1;
	return errno == 0 ? 0 : -1;
}

int i_getgrgid(gid_t gid, struct group *grp_r)
{
	struct group *result;

	errno = 0;
	do {
		gr_init();
		errno = getgrgid_r(gid, grp_r, grbuf, grbuf_size, &result);
	} while (errno == ERANGE);
	if (result != NULL)
		return 1;
	return errno == 0 ? 0 : -1;
}