summaryrefslogtreecommitdiffstats
path: root/numlib/changeuidgid.c
blob: ce69839fbd8505e86320dea94d14ff592cd14286 (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
/*
** Copyright 1998 - 2002 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	<sys/types.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<grp.h>
#include	<pwd.h>
#include	<errno.h>

#include	"numlib.h"


void libmail_changegroup(gid_t gid)
{
	if ( setgid(gid))
	{
		perror("setgid");
		exit(1);
	}

#if HAVE_SETGROUPS
	if ( getuid() == 0 && setgroups(1, &gid) )
	{
		perror("setgroups");
		exit(1);
	}
#endif
}

void libmail_changeuidgid(uid_t uid, gid_t gid)
{
	libmail_changegroup(gid);
	if ( setuid(uid))
	{
		perror("setuid");
		exit(1);
	}
}

/**
 * Obtain the uid associated to uname and, optionally, the user primary gid
 */
uid_t libmail_getuid(const char *uname, gid_t *pw_gid)
{
	size_t bufsize;
	char *buf;
	struct passwd pwbuf;
	struct passwd *pw;

	/*
	** uname might be a pointer returned from a previous called to getpw(),
	** and libc has a problem getting it back.
	*/
	char	*p=malloc(strlen(uname)+1);

	if (!p)
	{
		perror("malloc");
		exit(1);
	}
	strcpy(p, uname);

#ifdef _SC_GETGR_R_SIZE_MAX
	bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
	if (bufsize == -1)          /* Value was indeterminate */
	{
#endif
		bufsize = 16384;        /* Should be more than enough */
	}

	buf = malloc(bufsize);
	if (buf == NULL)
	{
		perror("malloc");
		exit(1);
	}


	errno=ENOENT;

	getpwnam_r(p, &pwbuf, buf, bufsize, &pw);

	free(buf);

	if (pw == 0)
	{
		free(p);
		perror("getpwnam");
		exit(1);
	}
	free(p);

	if ( pw_gid ) *pw_gid = pw->pw_gid;

	return pw->pw_uid;
}

void libmail_changeusername(const char *uname, const gid_t *forcegrp)
{
uid_t	changeuid;
gid_t	changegid;

	changeuid=libmail_getuid(uname, &changegid);

	if ( forcegrp )	changegid= *forcegrp;

	if ( setgid( changegid ))
	{
		perror("setgid");
		exit(1);
	}

#if HAVE_INITGROUPS
	if ( getuid() == 0 && initgroups(uname, changegid) )
	{
		perror("initgroups");
		exit(1);
	}
#else
#if HAVE_SETGROUPS
	if ( getuid() == 0 && setgroups(1, &changegid) )
	{
		perror("setgroups");
		exit(1);
	}
#endif
#endif

	if (setuid(changeuid))
	{
		perror("setuid");
		exit(1);
	}
}

gid_t libmail_getgid(const char *gname)
{
	gid_t g;
	struct group grp;
	struct group *result;
	char *buf;
	size_t bufsize;
	int s;
	char	*p=malloc(strlen(gname)+1);

	if (!p)
	{
		perror("malloc");
		exit(1);
	}
	strcpy(p, gname);

#ifdef _SC_GETGR_R_SIZE_MAX
	bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
	if (bufsize == -1)          /* Value was indeterminate */
#endif
	{
		bufsize = 16384;        /* Should be more than enough */
	}

	buf = malloc(bufsize);
	if (buf == NULL)
	{
		perror("malloc");
		exit(1);
	}

	s = getgrnam_r(p, &grp, buf, bufsize, &result);
	free(p);

	if (result == NULL)
	{
		if (s == 0)
		{
			fprintf(stderr, "CRIT: Group %s not found\n", gname);
		}
		else
		{
			errno = s;
			perror("getpwnam_r");
		}
		exit(1);
	}

	g = grp.gr_gid;
	free(buf);

	return g;
}