/
usergroup.c
231 lines (205 loc) · 5.5 KB
/
usergroup.c
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
/*
* Copyright (c) 2011-2012 Baptiste Daroussin <bapt@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/types.h> /* needed type libutl.h on 8.X */
#include <grp.h>
#include <pwd.h>
#include <libutil.h>
#include <string.h>
#if __FreeBSD_version < 1000003
#include "private/gr_util.h"
#endif
#include "pkg.h"
#include "private/event.h"
#include "private/pkg.h"
int
pkg_add_user_group(struct pkg *pkg)
{
struct pkg_group *g = NULL;
struct pkg_user *u = NULL;
struct group *gr = NULL;
struct passwd *pw = NULL;
int tfd, pfd;
/* loop just to check group and users contains string */
while (pkg_groups(pkg, &g) == EPKG_OK) {
if (g->gidstr[0] == '\0') {
/*
* old style group ignorring, this is created from
* scripts
*/
return (EPKG_OK);
}
}
/* loop just to check group and users contains string */
while (pkg_users(pkg, &u) == EPKG_OK) {
if (u->uidstr[0] == '\0') {
/*
* old style group ignorring, this is created from
* scripts
*/
return (EPKG_OK);
}
}
g = NULL;
u = NULL;
while (pkg_groups(pkg, &g) == EPKG_OK) {
char *gr_create_str, *tmp;
/* simply create the group */
if ((gr = getgrnam(pkg_group_name(g))) == NULL) {
/* remove the members if any */
if (g->gidstr[strlen(g->gidstr) - 1] == ':') {
gr = gr_scan(g->gidstr);
} else {
gr_create_str = strdup(g->gidstr);
tmp = strrchr(gr_create_str, ':');
tmp++;
tmp[0] = '\0';
gr = gr_scan(gr_create_str);
free(gr_create_str);
}
} else {
/* no need to create the group */
continue;
}
if (gr == NULL) {
pkg_emit_error("Bad group line, ignoring");
continue;
}
gr_init(NULL, NULL);
if ((pfd = gr_lock()) == -1) {
gr_fini();
continue;
}
if ((tfd = gr_tmp(-1)) == -1) {
gr_fini();
continue;
}
if (gr_copy(pfd, tfd, gr, NULL) == -1) {
gr_fini();
continue;
}
if (gr_mkdb() == -1) {
gr_fini();
continue;
}
free(gr);
gr_fini();
}
while (pkg_users(pkg, &u) == EPKG_OK) {
if ((pw = getpwnam(pkg_user_name(u))) == NULL) {
/* simply create the user */
pw = pw_scan(u->uidstr, PWSCAN_WARN|PWSCAN_MASTER);
} else {
/* user already exists */
continue;
}
pw_init(NULL, NULL);
if ((pfd = pw_lock()) == -1) {
pw_fini();
continue;
}
if ((tfd = gr_tmp(-1)) == -1) {
pw_fini();
continue;
}
if (pw_copy(pfd, tfd, pw, NULL) == -1) {
pw_fini();
continue;
}
if (pw_mkdb(pkg_user_name(u)) == -1) {
pw_fini();
}
pw_fini();
if (strcmp(pw->pw_dir, "/nonexistent") && strcmp(pw->pw_dir, "/var/empty")) {
/* now create the homedir if it doesn't exists */
/* TODO: do it recursively */
mkdir(pw->pw_dir, 0644);
chown(pw->pw_dir, pw->pw_uid, pw->pw_gid);
}
free(pw);
}
/* now add members to groups if they also are listed in users */
g = NULL;
while (pkg_groups(pkg, &g) == EPKG_OK) {
struct group *grlocal;
struct group *grnew;
int i, j;
int nx = 0;
if (g->gidstr[strlen(g->gidstr) - 1] == ':')
continue; /* no members, next */
gr = gr_scan(g->gidstr);
grlocal = getgrnam(pkg_group_name(g));
grnew = NULL;/* gr_dup(grlocal);*/
u = NULL;
for (i = 0; gr->gr_mem[i] != NULL; i++) {
while (pkg_users(pkg, &u) == EPKG_OK) {
if (!strcmp(pkg_user_name(u), gr->gr_mem[i])) {
/* check if the user is not already in the local group */
for (j = 0; grlocal->gr_mem[j] != NULL; j++) {
if (!strcmp(grlocal->gr_mem[j], gr->gr_mem[i]))
break;
}
if (grlocal->gr_mem[j] != NULL)
continue; /* already in the group */
/* adding the user to the group */
if (grnew == NULL) {
nx = j - 1;
grnew = gr_dup(grlocal);
}
if (nx == 0)
grnew->gr_mem = NULL;
nx++;
grnew->gr_mem = reallocf(grnew->gr_mem, sizeof(*grnew->gr_mem) * (nx + 1));
grnew->gr_mem[nx - 1] = __DECONST(char *, pkg_user_name(u));
grnew->gr_mem[nx] = NULL;
}
}
}
if (grnew == NULL) {
continue;
}
gr_init(NULL, NULL);
if ((pfd = gr_lock()) == -1) {
gr_fini();
continue;
}
if ((tfd = gr_tmp(-1)) == -1) {
gr_fini();
continue;
}
if (gr_copy(pfd, tfd, grnew, grlocal) == -1) {
gr_fini();
continue;
}
if (gr_mkdb() == -1) {
gr_fini();
continue;
}
free(grnew);
gr_fini();
}
return (EPKG_OK);
}