forked from kristapsdz/openrsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ids.c
329 lines (289 loc) · 7.2 KB
/
ids.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <grp.h>
#include <inttypes.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
/*
* Free a list of struct ident previously allocated with idents_add().
* Does nothing if the pointer is NULL.
*/
void
idents_free(struct ident *p, size_t sz)
{
size_t i;
if (p == NULL)
return;
for (i = 0; i < sz; i++)
free(p[i].name);
free(p);
}
/*
* Given a list of files with the identifiers as set by the sender,
* re-assign the identifiers from the list of remapped ones.
* Don't ever remap wheel/root.
* If we can't find the gid in the list (when, e.g., being sent by the
* daemon), don't try to map it.
*/
void
idents_assign_gid(struct sess *sess, struct flist *fl, size_t flsz,
const struct ident *ids, size_t idsz)
{
size_t i, j;
assert(!sess->opts->numeric_ids);
for (i = 0; i < flsz; i++) {
if (fl[i].st.gid == 0)
continue;
for (j = 0; j < idsz; j++)
if ((int32_t)fl[i].st.gid == ids[j].id)
break;
if (j < idsz)
fl[i].st.gid = ids[j].mapped;
}
}
/*
* Like idents_assign_gid().
*/
void
idents_assign_uid(struct sess *sess, struct flist *fl, size_t flsz,
const struct ident *ids, size_t idsz)
{
size_t i, j;
assert(!sess->opts->numeric_ids);
for (i = 0; i < flsz; i++) {
if (fl[i].st.uid == 0)
continue;
for (j = 0; j < idsz; j++)
if ((int32_t)fl[i].st.uid == ids[j].id)
break;
if (j < idsz)
fl[i].st.uid = ids[j].mapped;
}
}
/*
* Given a list of identifiers from the remote host, fill in our local
* identifiers of the same names.
* Use the remote numeric identifier if we can't find the identifier OR the
* identifier is zero (wheel/root).
* FIXME: we should at least warn when we can't find an identifier, use
* the numeric id, and that numeric id is assigned to a different user.
*/
void
idents_remap(struct sess *sess, int isgid, struct ident *ids, size_t idsz)
{
size_t i;
struct group *grp;
struct passwd *usr;
uint32_t id;
int valid;
assert(!sess->opts->numeric_ids);
for (i = 0; i < idsz; i++) {
assert(ids[i].id != 0);
/* Start by getting our local representation. */
valid = id = 0;
if (isgid) {
grp = getgrnam(ids[i].name);
if (grp) {
id = grp->gr_gid;
valid = 1;
}
} else {
usr = getpwnam(ids[i].name);
if (usr) {
id = usr->pw_uid;
valid = 1;
}
}
/*
* (1) Empty names inherit.
* (2) Unknown identifier names inherit.
* (3) Wheel/root inherits.
* (4) Otherwise, use the local identifier.
*/
if (ids[i].name[0] == '\0')
ids[i].mapped = ids[i].id;
else if (!valid)
ids[i].mapped = ids[i].id;
else
ids[i].mapped = id;
LOG4("remapped identifier %s: %" PRId32 " -> %" PRId32,
ids[i].name, ids[i].id, ids[i].mapped);
}
}
/*
* If "id" is not part of the list of known users or groups (depending
* upon "isgid", add it.
* This also verifies that the name isn't too long.
* Does nothing with user/group zero.
* Return zero on failure, non-zero on success.
*/
int
idents_add(int isgid, struct ident **ids, size_t *idsz, int32_t id)
{
struct group *grp;
struct passwd *usr;
size_t i, sz;
void *pp;
const char *name;
if (id == 0)
return 1;
for (i = 0; i < *idsz; i++)
if ((*ids)[i].id == id)
return 1;
/*
* Look up the reference in a type-specific way.
* Make sure that the name length is sane: we transmit it using
* a single byte. Note that failing to resolve is not an issue, we
* simply don't transmit a name and the other side cannot remap it.
*/
assert(i == *idsz);
if (isgid) {
errno = 0;
if ((grp = getgrgid((gid_t)id)) == NULL) {
if (errno != 0) {
ERR("%" PRIu32 ": unknown gid", id);
return 0;
}
return 1;
}
name = grp->gr_name;
} else {
errno = 0;
if ((usr = getpwuid((uid_t)id)) == NULL) {
if (errno != 0) {
ERR("%" PRIu32 ": unknown uid", id);
return 0;
}
return 1;
}
name = usr->pw_name;
}
if ((sz = strlen(name)) > UINT8_MAX) {
ERRX("%" PRIu32 ": name too long: %s", id, name);
return 0;
} else if (sz == 0) {
ERRX("%" PRIu32 ": zero-length name", id);
return 0;
}
/* Add the identifier to the array. */
pp = reallocarray(*ids, *idsz + 1, sizeof(struct ident));
if (pp == NULL) {
ERR("reallocarray");
return 0;
}
*ids = pp;
(*ids)[*idsz].id = id;
(*ids)[*idsz].name = strdup(name);
if ((*ids)[*idsz].name == NULL) {
ERR("strdup");
return 0;
}
LOG4("adding identifier to list: %s (%u)",
(*ids)[*idsz].name, (*ids)[*idsz].id);
(*idsz)++;
return 1;
}
/*
* Send a list of struct ident.
* See idents_recv().
* We should only do this if we're preserving gids/uids.
* Return zero on failure, non-zero on success.
*/
int
idents_send(struct sess *sess,
int fd, const struct ident *ids, size_t idsz)
{
size_t i, sz;
for (i = 0; i < idsz; i++) {
assert(ids[i].name != NULL);
assert(ids[i].id != 0);
sz = strlen(ids[i].name);
assert(sz > 0 && sz <= UINT8_MAX);
if (!io_write_uint(sess, fd, ids[i].id)) {
ERRX1("io_write_uint");
return 0;
} else if (!io_write_byte(sess, fd, sz)) {
ERRX1("io_write_byte");
return 0;
} else if (!io_write_buf(sess, fd, ids[i].name, sz)) {
ERRX1("io_write_buf");
return 0;
}
}
if (!io_write_int(sess, fd, 0)) {
ERRX1("io_write_int");
return 0;
}
return 1;
}
/*
* Receive a list of struct ident.
* See idents_send().
* We should only do this if we're preserving gids/uids.
* Return zero on failure, non-zero on success.
*/
int
idents_recv(struct sess *sess,
int fd, struct ident **ids, size_t *idsz)
{
uint32_t id;
uint8_t sz;
void *pp;
for (;;) {
if (!io_read_uint(sess, fd, &id)) {
ERRX1("io_read_uint");
return 0;
} else if (id == 0)
break;
pp = reallocarray(*ids,
*idsz + 1, sizeof(struct ident));
if (pp == NULL) {
ERR("reallocarray");
return 0;
}
*ids = pp;
memset(&(*ids)[*idsz], 0, sizeof(struct ident));
/*
* When reading the size, warn if we get a size of zero.
* The spec doesn't allow this, but we might have a
* noncomformant or adversarial sender.
*/
if (!io_read_byte(sess, fd, &sz)) {
ERRX1("io_read_byte");
return 0;
} else if (sz == 0)
WARNX("zero-length name in identifier list");
(*ids)[*idsz].id = id;
(*ids)[*idsz].name = calloc(sz + 1, 1);
if ((*ids)[*idsz].name == NULL) {
ERR("calloc");
return 0;
}
if (!io_read_buf(sess, fd, (*ids)[*idsz].name, sz)) {
ERRX1("io_read_buf");
return 0;
}
(*idsz)++;
}
return 1;
}