00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "rsync.h"
00027
00028 #ifdef HAVE_GETGROUPS
00029 # ifndef GETGROUPS_T
00030 # define GETGROUPS_T gid_t
00031 # endif
00032 #endif
00033
00034 extern int verbose;
00035 extern int preserve_uid;
00036 extern int preserve_gid;
00037 extern int preserve_acls;
00038 extern int numeric_ids;
00039 extern int am_root;
00040
00041 struct idlist {
00042 struct idlist *next;
00043 int id, id2;
00044 char *name;
00045 };
00046
00047 static struct idlist *uidlist;
00048 static struct idlist *gidlist;
00049
00050 static struct idlist *add_to_list(struct idlist **root, int id, char *name,
00051 int id2)
00052 {
00053 struct idlist *node = new(struct idlist);
00054 if (!node)
00055 out_of_memory("add_to_list");
00056 node->next = *root;
00057 node->name = name;
00058 node->id = id;
00059 node->id2 = id2;
00060 *root = node;
00061 return node;
00062 }
00063
00064
00065 static char *uid_to_name(uid_t uid)
00066 {
00067 struct passwd *pass = getpwuid(uid);
00068 if (pass)
00069 return strdup(pass->pw_name);
00070 return NULL;
00071 }
00072
00073
00074 static char *gid_to_name(gid_t gid)
00075 {
00076 struct group *grp = getgrgid(gid);
00077 if (grp)
00078 return strdup(grp->gr_name);
00079 return NULL;
00080 }
00081
00082 static int map_uid(int id, char *name)
00083 {
00084 uid_t uid;
00085 if (id != 0 && name_to_uid(name, &uid))
00086 return uid;
00087 return id;
00088 }
00089
00090 static int map_gid(int id, char *name)
00091 {
00092 gid_t gid;
00093 if (id != 0 && name_to_gid(name, &gid))
00094 return gid;
00095 return id;
00096 }
00097
00098 static int is_in_group(gid_t gid)
00099 {
00100 #ifdef HAVE_GETGROUPS
00101 static gid_t last_in = GID_NONE, last_out;
00102 static int ngroups = -2;
00103 static GETGROUPS_T *gidset;
00104 int n;
00105
00106 if (gid == last_in)
00107 return last_out;
00108 if (ngroups < -1) {
00109 gid_t mygid = MY_GID();
00110 if ((ngroups = getgroups(0, NULL)) < 0)
00111 ngroups = 0;
00112 gidset = new_array(GETGROUPS_T, ngroups+1);
00113 if (!gidset)
00114 out_of_memory("is_in_group");
00115 if (ngroups > 0)
00116 ngroups = getgroups(ngroups, gidset);
00117
00118 for (n = 0; n < ngroups; n++) {
00119 if (gidset[n] == mygid)
00120 break;
00121 }
00122 if (n == ngroups)
00123 gidset[ngroups++] = mygid;
00124 if (verbose > 3) {
00125 int pos;
00126 char *gidbuf = new_array(char, ngroups*21+32);
00127 if (!gidbuf)
00128 out_of_memory("is_in_group");
00129 sprintf(gidbuf, "process has %d gid%s: ",
00130 ngroups, ngroups == 1? "" : "s");
00131 pos = strlen(gidbuf);
00132 for (n = 0; n < ngroups; n++) {
00133 sprintf(gidbuf+pos, " %d", (int)gidset[n]);
00134 pos += strlen(gidbuf+pos);
00135 }
00136 rprintf(FINFO, "%s\n", gidbuf);
00137 free(gidbuf);
00138 }
00139 }
00140
00141 last_in = gid;
00142 for (n = 0; n < ngroups; n++) {
00143 if (gidset[n] == gid)
00144 return last_out = 1;
00145 }
00146 return last_out = 0;
00147
00148 #else
00149 static gid_t mygid = GID_NONE;
00150 if (mygid == GID_NONE) {
00151 mygid = MY_GID();
00152 if (verbose > 3)
00153 rprintf(FINFO, "process has gid %d\n", (int)mygid);
00154 }
00155 return gid == mygid;
00156 #endif
00157 }
00158
00159
00160 static struct idlist *recv_add_uid(int id, char *name)
00161 {
00162 int id2 = name ? map_uid(id, name) : id;
00163 struct idlist *node;
00164
00165 node = add_to_list(&uidlist, id, name, id2);
00166
00167 if (verbose > 3) {
00168 rprintf(FINFO, "uid %d(%s) maps to %d\n",
00169 id, name ? name : "", id2);
00170 }
00171
00172 return node;
00173 }
00174
00175
00176 static struct idlist *recv_add_gid(int id, char *name)
00177 {
00178 int id2 = name ? map_gid(id, name) : id;
00179 struct idlist *node;
00180
00181 if (!am_root && !is_in_group(id2))
00182 id2 = GID_NONE;
00183 node = add_to_list(&gidlist, id, name, id2);
00184
00185 if (verbose > 3) {
00186 rprintf(FINFO, "gid %d(%s) maps to %d\n",
00187 id, name ? name : "", id2);
00188 }
00189
00190 return node;
00191 }
00192
00193
00194 static uid_t match_uid(uid_t uid)
00195 {
00196 static uid_t last_in, last_out;
00197 struct idlist *list;
00198
00199 if (uid == 0)
00200 return 0;
00201
00202 if (uid == last_in)
00203 return last_out;
00204
00205 last_in = uid;
00206
00207 for (list = uidlist; list; list = list->next) {
00208 if (list->id == (int)uid)
00209 return last_out = (uid_t)list->id2;
00210 }
00211
00212 return last_out = uid;
00213 }
00214
00215 static gid_t match_gid(gid_t gid)
00216 {
00217 static gid_t last_in = GID_NONE, last_out = GID_NONE;
00218 struct idlist *list;
00219
00220 if (gid == GID_NONE)
00221 return GID_NONE;
00222
00223 if (gid == last_in)
00224 return last_out;
00225
00226 last_in = gid;
00227
00228 for (list = gidlist; list; list = list->next) {
00229 if (list->id == (int)gid)
00230 return last_out = (gid_t)list->id2;
00231 }
00232
00233 list = recv_add_gid(gid, NULL);
00234 return last_out = list->id2;
00235 }
00236
00237
00238 void add_uid(uid_t uid)
00239 {
00240 struct idlist *list;
00241
00242 if (uid == 0)
00243 return;
00244
00245 for (list = uidlist; list; list = list->next) {
00246 if (list->id == (int)uid)
00247 return;
00248 }
00249
00250 add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0);
00251 }
00252
00253
00254 void add_gid(gid_t gid)
00255 {
00256 struct idlist *list;
00257
00258 if (gid == 0)
00259 return;
00260
00261 for (list = gidlist; list; list = list->next) {
00262 if (list->id == (int)gid)
00263 return;
00264 }
00265
00266 add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0);
00267 }
00268
00269
00270
00271 void send_uid_list(int f)
00272 {
00273 struct idlist *list;
00274
00275 if (numeric_ids)
00276 return;
00277
00278 if (preserve_uid || preserve_acls) {
00279 int len;
00280
00281 for (list = uidlist; list; list = list->next) {
00282 if (!list->name)
00283 continue;
00284 len = strlen(list->name);
00285 write_int(f, list->id);
00286 write_byte(f, len);
00287 write_buf(f, list->name, len);
00288 }
00289
00290
00291
00292 write_int(f, 0);
00293 }
00294
00295 if (preserve_gid || preserve_acls) {
00296 int len;
00297 for (list = gidlist; list; list = list->next) {
00298 if (!list->name)
00299 continue;
00300 len = strlen(list->name);
00301 write_int(f, list->id);
00302 write_byte(f, len);
00303 write_buf(f, list->name, len);
00304 }
00305 write_int(f, 0);
00306 }
00307 }
00308
00309
00310
00311 void recv_uid_list(int f, struct file_list *flist)
00312 {
00313 int id, i;
00314 char *name;
00315
00316 if ((preserve_uid || preserve_acls) && !numeric_ids) {
00317
00318 while ((id = read_int(f)) != 0) {
00319 int len = read_byte(f);
00320 name = new_array(char, len+1);
00321 if (!name)
00322 out_of_memory("recv_uid_list");
00323 read_sbuf(f, name, len);
00324 recv_add_uid(id, name);
00325 }
00326 }
00327
00328 if ((preserve_gid || preserve_acls) && !numeric_ids) {
00329
00330 while ((id = read_int(f)) != 0) {
00331 int len = read_byte(f);
00332 name = new_array(char, len+1);
00333 if (!name)
00334 out_of_memory("recv_uid_list");
00335 read_sbuf(f, name, len);
00336 recv_add_gid(id, name);
00337 }
00338 }
00339
00340 #ifdef SUPPORT_ACLS
00341 if (preserve_acls && !numeric_ids) {
00342 id_t *id;
00343 while ((id = next_acl_uid(flist)) != NULL)
00344 *id = match_uid(*id);
00345 while ((id = next_acl_gid(flist)) != NULL)
00346 *id = match_gid(*id);
00347 }
00348 #endif
00349
00350
00351 if (am_root && preserve_uid && !numeric_ids) {
00352 for (i = 0; i < flist->count; i++)
00353 flist->files[i]->uid = match_uid(flist->files[i]->uid);
00354 }
00355 if (preserve_gid && (!am_root || !numeric_ids)) {
00356 for (i = 0; i < flist->count; i++)
00357 flist->files[i]->gid = match_gid(flist->files[i]->gid);
00358 }
00359 }