2 * Handle the mapping of uid/gid and user/group names between systems.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004-2009 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
22 /* If the source username/group does not exist on the target then use
23 * the numeric IDs. Never do any mapping for uid=0 or gid=0 as these
32 extern int preserve_uid
;
33 extern int preserve_gid
;
34 extern int preserve_acls
;
35 extern int numeric_ids
;
38 extern char *groupmap
;
42 # define GETGROUPS_T gid_t
46 #define NFLAGS_WILD_NAME_MATCH (1<<0)
47 #define NFLAGS_NAME_MATCH (1<<1)
56 static struct idlist
*uidlist
, *uidmap
;
57 static struct idlist
*gidlist
, *gidmap
;
59 static id_t
id_parse(const char *num_str
)
62 const char *cp
= num_str
;
67 rprintf(FERROR
, "Invalid ID number: %s\n", num_str
);
68 exit_cleanup(RERR_SYNTAX
);
70 tmp
= num
* 10 + *cp
++ - '0';
79 static struct idlist
*add_to_list(struct idlist
**root
, id_t id
, const char *name
,
80 id_t id2
, uint16 flags
)
82 struct idlist
*node
= new(struct idlist
);
84 out_of_memory("add_to_list");
94 /* turn a uid into a user name */
95 char *uid_to_user(uid_t uid
)
97 struct passwd
*pass
= getpwuid(uid
);
99 return strdup(pass
->pw_name
);
103 /* turn a gid into a group name */
104 char *gid_to_group(gid_t gid
)
106 struct group
*grp
= getgrgid(gid
);
108 return strdup(grp
->gr_name
);
112 /* Parse a user name or (optionally) a number into a uid */
113 int user_to_uid(const char *name
, uid_t
*uid_p
, BOOL num_ok
)
118 if (num_ok
&& name
[strspn(name
, "0123456789")] == '\0') {
119 *uid_p
= id_parse(name
);
122 if (!(pass
= getpwnam(name
)))
124 *uid_p
= pass
->pw_uid
;
128 /* Parse a group name or (optionally) a number into a gid */
129 int group_to_gid(const char *name
, gid_t
*gid_p
, BOOL num_ok
)
134 if (num_ok
&& name
[strspn(name
, "0123456789")] == '\0') {
135 *gid_p
= id_parse(name
);
138 if (!(grp
= getgrnam(name
)))
140 *gid_p
= grp
->gr_gid
;
144 static int is_in_group(gid_t gid
)
146 #ifdef HAVE_GETGROUPS
147 static gid_t last_in
;
148 static int ngroups
= -2, last_out
= -1;
149 static GETGROUPS_T
*gidset
;
152 if (gid
== last_in
&& last_out
>= 0)
155 if ((ngroups
= getgroups(0, NULL
)) < 0)
157 gidset
= new_array(GETGROUPS_T
, ngroups
+1);
159 out_of_memory("is_in_group");
161 ngroups
= getgroups(ngroups
, gidset
);
162 /* The default gid might not be in the list on some systems. */
163 for (n
= 0; n
< ngroups
; n
++) {
164 if (gidset
[n
] == our_gid
)
168 gidset
[ngroups
++] = our_gid
;
169 if (DEBUG_GTE(OWN
, 2)) {
171 char *gidbuf
= new_array(char, ngroups
*21+32);
173 out_of_memory("is_in_group");
174 pos
= snprintf(gidbuf
, 32, "process has %d gid%s: ",
175 ngroups
, ngroups
== 1? "" : "s");
176 for (n
= 0; n
< ngroups
; n
++) {
177 pos
+= snprintf(gidbuf
+pos
, 21, " %d", (int)gidset
[n
]);
179 rprintf(FINFO
, "%s\n", gidbuf
);
185 for (n
= 0; n
< ngroups
; n
++) {
186 if (gidset
[n
] == gid
)
192 return gid
== our_gid
;
196 /* Add a uid/gid to its list of ids. Only called on receiving side. */
197 static struct idlist
*recv_add_id(struct idlist
**idlist_ptr
, struct idlist
*idmap
,
198 id_t id
, const char *name
)
207 for (node
= idmap
; node
; node
= node
->next
) {
208 if (node
->flags
& NFLAGS_WILD_NAME_MATCH
) {
209 if (!wildmatch(node
->name
, name
))
211 } else if (node
->flags
& NFLAGS_NAME_MATCH
) {
212 if (strcmp(node
->name
, name
) != 0)
214 } else if (node
->name
) {
215 if (id
< node
->id
|| (unsigned long)id
> (unsigned long)node
->name
)
225 else if (*name
&& id
) {
226 if (idlist_ptr
== &uidlist
) {
228 id2
= user_to_uid(name
, &uid
, False
) ? uid
: id
;
231 id2
= group_to_gid(name
, &gid
, False
) ? gid
: id
;
236 flag
= idlist_ptr
== &gidlist
&& !am_root
&& !is_in_group(id2
) ? FLAG_SKIP_GROUP
: 0;
237 node
= add_to_list(idlist_ptr
, id
, *name
? name
: NULL
, id2
, flag
);
239 if (DEBUG_GTE(OWN
, 2)) {
240 rprintf(FINFO
, "%sid %u(%s) maps to %u\n",
241 idlist_ptr
== &uidlist
? "u" : "g",
242 (unsigned)id
, name
, (unsigned)id2
);
248 /* this function is a definate candidate for a faster algorithm */
249 uid_t
match_uid(uid_t uid
)
251 static struct idlist
*last
= NULL
;
254 if (last
&& uid
== last
->id
)
257 for (list
= uidlist
; list
; list
= list
->next
) {
263 list
= recv_add_id(&uidlist
, uidmap
, uid
, NULL
);
269 gid_t
match_gid(gid_t gid
, uint16
*flags_ptr
)
271 static struct idlist
*last
= NULL
;
274 if (last
&& gid
== last
->id
)
277 for (list
= gidlist
; list
; list
= list
->next
) {
282 list
= recv_add_id(&gidlist
, gidmap
, gid
, NULL
);
286 if (flags_ptr
&& list
->flags
& FLAG_SKIP_GROUP
)
287 *flags_ptr
|= FLAG_SKIP_GROUP
;
291 /* Add a uid to the list of uids. Only called on sending side. */
292 const char *add_uid(uid_t uid
)
297 if (uid
== 0) /* don't map root */
300 for (list
= uidlist
; list
; list
= list
->next
) {
305 node
= add_to_list(&uidlist
, uid
, uid_to_user(uid
), 0, 0);
309 /* Add a gid to the list of gids. Only called on sending side. */
310 const char *add_gid(gid_t gid
)
315 if (gid
== 0) /* don't map root */
318 for (list
= gidlist
; list
; list
= list
->next
) {
323 node
= add_to_list(&gidlist
, gid
, gid_to_group(gid
), 0, 0);
327 /* send a complete uid/gid mapping to the peer */
328 void send_id_list(int f
)
332 if (preserve_uid
|| preserve_acls
) {
334 /* we send sequences of uid/byte-length/name */
335 for (list
= uidlist
; list
; list
= list
->next
) {
338 len
= strlen(list
->name
);
339 write_varint30(f
, list
->id
);
341 write_buf(f
, list
->name
, len
);
344 /* terminate the uid list with a 0 uid. We explicitly exclude
346 write_varint30(f
, 0);
349 if (preserve_gid
|| preserve_acls
) {
351 for (list
= gidlist
; list
; list
= list
->next
) {
354 len
= strlen(list
->name
);
355 write_varint30(f
, list
->id
);
357 write_buf(f
, list
->name
, len
);
359 write_varint30(f
, 0);
363 uid_t
recv_user_name(int f
, uid_t uid
)
366 int len
= read_byte(f
);
367 char *name
= new_array(char, len
+1);
369 out_of_memory("recv_user_name");
370 read_sbuf(f
, name
, len
);
371 if (numeric_ids
< 0) {
375 node
= recv_add_id(&uidlist
, uidmap
, uid
, name
); /* node keeps name's memory */
379 gid_t
recv_group_name(int f
, gid_t gid
, uint16
*flags_ptr
)
382 int len
= read_byte(f
);
383 char *name
= new_array(char, len
+1);
385 out_of_memory("recv_group_name");
386 read_sbuf(f
, name
, len
);
387 if (numeric_ids
< 0) {
391 node
= recv_add_id(&gidlist
, gidmap
, gid
, name
); /* node keeps name's memory */
392 if (flags_ptr
&& node
->flags
& FLAG_SKIP_GROUP
)
393 *flags_ptr
|= FLAG_SKIP_GROUP
;
397 /* recv a complete uid/gid mapping from the peer and map the uid/gid
398 * in the file list to local names */
399 void recv_id_list(int f
, struct file_list
*flist
)
404 if ((preserve_uid
|| preserve_acls
) && numeric_ids
<= 0) {
405 /* read the uid list */
406 while ((id
= read_varint30(f
)) != 0)
407 recv_user_name(f
, id
);
410 if ((preserve_gid
|| preserve_acls
) && numeric_ids
<= 0) {
411 /* read the gid list */
412 while ((id
= read_varint30(f
)) != 0)
413 recv_group_name(f
, id
, NULL
);
416 /* Now convert all the uids/gids from sender values to our values. */
418 if (preserve_acls
&& (!numeric_ids
|| usermap
|| groupmap
))
421 if (am_root
&& preserve_uid
&& (!numeric_ids
|| usermap
)) {
422 for (i
= 0; i
< flist
->used
; i
++)
423 F_OWNER(flist
->files
[i
]) = match_uid(F_OWNER(flist
->files
[i
]));
425 if (preserve_gid
&& (!am_root
|| !numeric_ids
|| groupmap
)) {
426 for (i
= 0; i
< flist
->used
; i
++) {
427 F_GROUP(flist
->files
[i
]) = match_gid(F_GROUP(flist
->files
[i
]),
428 &flist
->files
[i
]->flags
);
433 void parse_name_map(char *map
, BOOL usernames
)
435 struct idlist
**idmap_ptr
= usernames
? &uidmap
: &gidmap
;
436 struct idlist
**idlist_ptr
= usernames
? &uidlist
: &gidlist
;
437 char *colon
, *end
, *name
, *cp
= map
+ strlen(map
);
441 /* Parse the list in reverse, so the order in the struct is right. */
444 while (cp
> map
&& cp
[-1] != ',') cp
--;
445 if (!(colon
= strchr(cp
, ':'))) {
446 rprintf(FERROR
, "No colon found in --%smap: %s\n",
447 usernames
? "user" : "group", cp
);
448 exit_cleanup(RERR_SYNTAX
);
451 rprintf(FERROR
, "No name found after colon --%smap: %s\n",
452 usernames
? "user" : "group", cp
);
453 exit_cleanup(RERR_SYNTAX
);
458 char *dash
= strchr(cp
, '-');
459 if (strspn(cp
, "0123456789-") != (size_t)(colon
- cp
)
460 || (dash
&& (!dash
[1] || strchr(dash
+1, '-')))) {
461 rprintf(FERROR
, "Invalid number in --%smap: %s\n",
462 usernames
? "user" : "group", cp
);
463 exit_cleanup(RERR_SYNTAX
);
466 name
= (char *)id_parse(dash
+1);
471 } else if (strpbrk(cp
, "*[?")) {
472 flags
= NFLAGS_WILD_NAME_MATCH
;
476 flags
= NFLAGS_NAME_MATCH
;
483 if (user_to_uid(colon
+1, &uid
, True
))
484 add_to_list(idmap_ptr
, id1
, name
, uid
, flags
);
487 "Unknown --usermap name on receiver: %s\n",
492 if (group_to_gid(colon
+1, &gid
, True
))
493 add_to_list(idmap_ptr
, id1
, name
, gid
, flags
);
496 "Unknown --groupmap name on receiver: %s\n",
504 *--cp
= '\0'; /* replace comma */
507 /* The 0 user/group doesn't get its name sent, so add it explicitly. */
508 recv_add_id(idlist_ptr
, *idmap_ptr
, 0,
509 numeric_ids
? NULL
: usernames
? uid_to_user(0) : gid_to_group(0));
512 #ifdef HAVE_GETGROUPLIST
513 const char *getallgroups(uid_t uid
, gid_t
*gid_list
, int *size_ptr
)
516 if ((pw
= getpwuid(uid
)) == NULL
)
517 return "getpwuid failed";
518 /* Get all the process's groups, with the pw_gid group first. */
519 if (getgrouplist(pw
->pw_name
, pw
->pw_gid
, gid_list
, size_ptr
) < 0)
520 return "getgrouplist failed";
521 /* Paranoia: is the default group not first in the list? */
522 if (gid_list
[0] != pw
->pw_gid
) {
524 for (j
= 0; j
< *size_ptr
; j
++) {
525 if (gid_list
[j
] == pw
->pw_gid
) {
526 gid_list
[j
] = gid_list
[0];
527 gid_list
[0] = pw
->pw_gid
;