| 1 | /* |
| 2 | * Handle the mapping of uid/gid and user/group names between systems. |
| 3 | * |
| 4 | * Copyright (C) 1996 Andrew Tridgell |
| 5 | * Copyright (C) 1996 Paul Mackerras |
| 6 | * Copyright (C) 2004-2007 Wayne Davison |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 3 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, visit the http://fsf.org website. |
| 19 | */ |
| 20 | |
| 21 | /* If the source username/group does not exist on the target then use |
| 22 | * the numeric IDs. Never do any mapping for uid=0 or gid=0 as these |
| 23 | * are special. */ |
| 24 | |
| 25 | #include "rsync.h" |
| 26 | #include "io.h" |
| 27 | |
| 28 | extern int verbose; |
| 29 | extern int am_root; |
| 30 | extern int preserve_uid; |
| 31 | extern int preserve_gid; |
| 32 | extern int preserve_acls; |
| 33 | extern int numeric_ids; |
| 34 | |
| 35 | #ifdef HAVE_GETGROUPS |
| 36 | # ifndef GETGROUPS_T |
| 37 | # define GETGROUPS_T gid_t |
| 38 | # endif |
| 39 | #endif |
| 40 | |
| 41 | #define GID_NONE ((gid_t)-1) |
| 42 | |
| 43 | struct idlist { |
| 44 | struct idlist *next; |
| 45 | char *name; |
| 46 | id_t id, id2; |
| 47 | uint16 flags; |
| 48 | }; |
| 49 | |
| 50 | static struct idlist *uidlist; |
| 51 | static struct idlist *gidlist; |
| 52 | |
| 53 | static struct idlist *add_to_list(struct idlist **root, id_t id, char *name, |
| 54 | id_t id2, uint16 flags) |
| 55 | { |
| 56 | struct idlist *node = new(struct idlist); |
| 57 | if (!node) |
| 58 | out_of_memory("add_to_list"); |
| 59 | node->next = *root; |
| 60 | node->name = name; |
| 61 | node->id = id; |
| 62 | node->id2 = id2; |
| 63 | node->flags = flags; |
| 64 | *root = node; |
| 65 | return node; |
| 66 | } |
| 67 | |
| 68 | /* turn a uid into a user name */ |
| 69 | static char *uid_to_name(uid_t uid) |
| 70 | { |
| 71 | struct passwd *pass = getpwuid(uid); |
| 72 | if (pass) |
| 73 | return strdup(pass->pw_name); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | /* turn a gid into a group name */ |
| 78 | static char *gid_to_name(gid_t gid) |
| 79 | { |
| 80 | struct group *grp = getgrgid(gid); |
| 81 | if (grp) |
| 82 | return strdup(grp->gr_name); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | static uid_t map_uid(uid_t id, char *name) |
| 87 | { |
| 88 | uid_t uid; |
| 89 | if (id != 0 && name_to_uid(name, &uid)) |
| 90 | return uid; |
| 91 | return id; |
| 92 | } |
| 93 | |
| 94 | static gid_t map_gid(gid_t id, char *name) |
| 95 | { |
| 96 | gid_t gid; |
| 97 | if (id != 0 && name_to_gid(name, &gid)) |
| 98 | return gid; |
| 99 | return id; |
| 100 | } |
| 101 | |
| 102 | static int is_in_group(gid_t gid) |
| 103 | { |
| 104 | #ifdef HAVE_GETGROUPS |
| 105 | static gid_t last_in = GID_NONE, last_out; |
| 106 | static int ngroups = -2; |
| 107 | static GETGROUPS_T *gidset; |
| 108 | int n; |
| 109 | |
| 110 | if (gid == last_in) |
| 111 | return last_out; |
| 112 | if (ngroups < -1) { |
| 113 | gid_t mygid = MY_GID(); |
| 114 | if ((ngroups = getgroups(0, NULL)) < 0) |
| 115 | ngroups = 0; |
| 116 | gidset = new_array(GETGROUPS_T, ngroups+1); |
| 117 | if (!gidset) |
| 118 | out_of_memory("is_in_group"); |
| 119 | if (ngroups > 0) |
| 120 | ngroups = getgroups(ngroups, gidset); |
| 121 | /* The default gid might not be in the list on some systems. */ |
| 122 | for (n = 0; n < ngroups; n++) { |
| 123 | if (gidset[n] == mygid) |
| 124 | break; |
| 125 | } |
| 126 | if (n == ngroups) |
| 127 | gidset[ngroups++] = mygid; |
| 128 | if (verbose > 3) { |
| 129 | int pos; |
| 130 | char *gidbuf = new_array(char, ngroups*21+32); |
| 131 | if (!gidbuf) |
| 132 | out_of_memory("is_in_group"); |
| 133 | pos = snprintf(gidbuf, 32, "process has %d gid%s: ", |
| 134 | ngroups, ngroups == 1? "" : "s"); |
| 135 | for (n = 0; n < ngroups; n++) { |
| 136 | pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]); |
| 137 | } |
| 138 | rprintf(FINFO, "%s\n", gidbuf); |
| 139 | free(gidbuf); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | last_in = gid; |
| 144 | for (n = 0; n < ngroups; n++) { |
| 145 | if (gidset[n] == gid) |
| 146 | return last_out = 1; |
| 147 | } |
| 148 | return last_out = 0; |
| 149 | |
| 150 | #else |
| 151 | static gid_t mygid = GID_NONE; |
| 152 | if (mygid == GID_NONE) { |
| 153 | mygid = MY_GID(); |
| 154 | if (verbose > 3) |
| 155 | rprintf(FINFO, "process has gid %u\n", (unsigned)mygid); |
| 156 | } |
| 157 | return gid == mygid; |
| 158 | #endif |
| 159 | } |
| 160 | |
| 161 | /* Add a uid to the list of uids. Only called on receiving side. */ |
| 162 | static struct idlist *recv_add_uid(uid_t id, char *name) |
| 163 | { |
| 164 | uid_t id2 = name ? map_uid(id, name) : id; |
| 165 | struct idlist *node; |
| 166 | |
| 167 | node = add_to_list(&uidlist, id, name, id2, 0); |
| 168 | |
| 169 | if (verbose > 3) { |
| 170 | rprintf(FINFO, "uid %u(%s) maps to %u\n", |
| 171 | (unsigned)id, name ? name : "", (unsigned)id2); |
| 172 | } |
| 173 | |
| 174 | return node; |
| 175 | } |
| 176 | |
| 177 | /* Add a gid to the list of gids. Only called on receiving side. */ |
| 178 | static struct idlist *recv_add_gid(gid_t id, char *name) |
| 179 | { |
| 180 | gid_t id2 = name ? map_gid(id, name) : id; |
| 181 | struct idlist *node; |
| 182 | |
| 183 | node = add_to_list(&gidlist, id, name, id2, |
| 184 | !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0); |
| 185 | |
| 186 | if (verbose > 3) { |
| 187 | rprintf(FINFO, "gid %u(%s) maps to %u\n", |
| 188 | (unsigned)id, name ? name : "", (unsigned)id2); |
| 189 | } |
| 190 | |
| 191 | return node; |
| 192 | } |
| 193 | |
| 194 | /* this function is a definate candidate for a faster algorithm */ |
| 195 | uid_t match_uid(uid_t uid) |
| 196 | { |
| 197 | static uid_t last_in, last_out; |
| 198 | struct idlist *list; |
| 199 | |
| 200 | if (uid == 0) |
| 201 | return 0; |
| 202 | |
| 203 | if (uid == last_in) |
| 204 | return last_out; |
| 205 | |
| 206 | last_in = uid; |
| 207 | |
| 208 | for (list = uidlist; list; list = list->next) { |
| 209 | if (list->id == uid) |
| 210 | return last_out = list->id2; |
| 211 | } |
| 212 | |
| 213 | return last_out = uid; |
| 214 | } |
| 215 | |
| 216 | gid_t match_gid(gid_t gid, uint16 *flags_ptr) |
| 217 | { |
| 218 | static struct idlist *last = NULL; |
| 219 | struct idlist *list; |
| 220 | |
| 221 | if (last && gid == last->id) |
| 222 | list = last; |
| 223 | else { |
| 224 | for (list = gidlist; list; list = list->next) { |
| 225 | if (list->id == gid) |
| 226 | break; |
| 227 | } |
| 228 | if (!list) |
| 229 | list = recv_add_gid(gid, NULL); |
| 230 | last = list; |
| 231 | } |
| 232 | |
| 233 | if (flags_ptr && list->flags & FLAG_SKIP_GROUP) |
| 234 | *flags_ptr |= FLAG_SKIP_GROUP; |
| 235 | return list->id2; |
| 236 | } |
| 237 | |
| 238 | /* Add a uid to the list of uids. Only called on sending side. */ |
| 239 | char *add_uid(uid_t uid) |
| 240 | { |
| 241 | struct idlist *list; |
| 242 | struct idlist *node; |
| 243 | |
| 244 | if (uid == 0) /* don't map root */ |
| 245 | return NULL; |
| 246 | |
| 247 | for (list = uidlist; list; list = list->next) { |
| 248 | if (list->id == uid) |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | node = add_to_list(&uidlist, uid, uid_to_name(uid), 0, 0); |
| 253 | return node->name; |
| 254 | } |
| 255 | |
| 256 | /* Add a gid to the list of gids. Only called on sending side. */ |
| 257 | char *add_gid(gid_t gid) |
| 258 | { |
| 259 | struct idlist *list; |
| 260 | struct idlist *node; |
| 261 | |
| 262 | if (gid == 0) /* don't map root */ |
| 263 | return NULL; |
| 264 | |
| 265 | for (list = gidlist; list; list = list->next) { |
| 266 | if (list->id == gid) |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | node = add_to_list(&gidlist, gid, gid_to_name(gid), 0, 0); |
| 271 | return node->name; |
| 272 | } |
| 273 | |
| 274 | /* send a complete uid/gid mapping to the peer */ |
| 275 | void send_id_list(int f) |
| 276 | { |
| 277 | struct idlist *list; |
| 278 | |
| 279 | if (preserve_uid || preserve_acls) { |
| 280 | int len; |
| 281 | /* we send sequences of uid/byte-length/name */ |
| 282 | for (list = uidlist; list; list = list->next) { |
| 283 | if (!list->name) |
| 284 | continue; |
| 285 | len = strlen(list->name); |
| 286 | write_varint30(f, list->id); |
| 287 | write_byte(f, len); |
| 288 | write_buf(f, list->name, len); |
| 289 | } |
| 290 | |
| 291 | /* terminate the uid list with a 0 uid. We explicitly exclude |
| 292 | * 0 from the list */ |
| 293 | write_varint30(f, 0); |
| 294 | } |
| 295 | |
| 296 | if (preserve_gid || preserve_acls) { |
| 297 | int len; |
| 298 | for (list = gidlist; list; list = list->next) { |
| 299 | if (!list->name) |
| 300 | continue; |
| 301 | len = strlen(list->name); |
| 302 | write_varint30(f, list->id); |
| 303 | write_byte(f, len); |
| 304 | write_buf(f, list->name, len); |
| 305 | } |
| 306 | write_varint30(f, 0); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | uid_t recv_user_name(int f, uid_t uid) |
| 311 | { |
| 312 | struct idlist *node; |
| 313 | int len = read_byte(f); |
| 314 | char *name = new_array(char, len+1); |
| 315 | if (!name) |
| 316 | out_of_memory("recv_user_name"); |
| 317 | read_sbuf(f, name, len); |
| 318 | node = recv_add_uid(uid, name); /* node keeps name's memory */ |
| 319 | return node->id2; |
| 320 | } |
| 321 | |
| 322 | gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr) |
| 323 | { |
| 324 | struct idlist *node; |
| 325 | int len = read_byte(f); |
| 326 | char *name = new_array(char, len+1); |
| 327 | if (!name) |
| 328 | out_of_memory("recv_group_name"); |
| 329 | read_sbuf(f, name, len); |
| 330 | node = recv_add_gid(gid, name); /* node keeps name's memory */ |
| 331 | if (flags_ptr && node->flags & FLAG_SKIP_GROUP) |
| 332 | *flags_ptr |= FLAG_SKIP_GROUP; |
| 333 | return node->id2; |
| 334 | } |
| 335 | |
| 336 | /* recv a complete uid/gid mapping from the peer and map the uid/gid |
| 337 | * in the file list to local names */ |
| 338 | void recv_id_list(int f, struct file_list *flist) |
| 339 | { |
| 340 | id_t id; |
| 341 | int i; |
| 342 | |
| 343 | if ((preserve_uid || preserve_acls) && !numeric_ids) { |
| 344 | /* read the uid list */ |
| 345 | while ((id = read_varint30(f)) != 0) |
| 346 | recv_user_name(f, id); |
| 347 | } |
| 348 | |
| 349 | if ((preserve_gid || preserve_acls) && !numeric_ids) { |
| 350 | /* read the gid list */ |
| 351 | while ((id = read_varint30(f)) != 0) |
| 352 | recv_group_name(f, id, NULL); |
| 353 | } |
| 354 | |
| 355 | /* Now convert all the uids/gids from sender values to our values. */ |
| 356 | #ifdef SUPPORT_ACLS |
| 357 | if (preserve_acls && !numeric_ids) |
| 358 | match_acl_ids(); |
| 359 | #endif |
| 360 | if (am_root && preserve_uid && !numeric_ids) { |
| 361 | for (i = 0; i < flist->used; i++) |
| 362 | F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i])); |
| 363 | } |
| 364 | if (preserve_gid && (!am_root || !numeric_ids)) { |
| 365 | for (i = 0; i < flist->used; i++) { |
| 366 | F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]), |
| 367 | &flist->files[i]->flags); |
| 368 | } |
| 369 | } |
| 370 | } |