1 This patch adds a "name converter" daemon option that allows you
2 to specify a user-/group- name converter program that converts
3 between ID numbers and names. This only works in daemon mode,
4 and is useful for both chroot use (since the converter runs
5 outside the chroot) or to specify a converter that doesn't use
6 the normal passwd/group setup.
8 The converter must use a null char ('\0') as the line terminator
9 for input/output on stdin/stdout. A sample converter written in
10 perl is supplied in the support dir: nameconvert. To use it,
11 specify this daemon option:
13 name converter = /path/nameconvert
15 If /path/ is omitted, the script will be found on the $PATH.
17 To use this patch, run these commands for a successful build:
19 patch -p1 <patches/nameconverter.diff
20 ./configure (optional if already run)
23 based-on: 4c8eb5f9511dd1393b147c4e993ee15448faedf5
24 diff --git a/authenticate.c b/authenticate.c
27 @@ -226,7 +226,7 @@ char *auth_server(int f_in, int f_out, int module, const char *host,
28 char *users = lp_auth_users(module);
29 char challenge[MAX_DIGEST_LEN*2];
30 char line[BIGPATHBUFLEN];
31 - char **auth_uid_groups = NULL;
32 + const char **auth_uid_groups = NULL;
33 int auth_uid_groups_cnt = -1;
34 const char *err = NULL;
36 @@ -286,7 +286,7 @@ char *auth_server(int f_in, int f_out, int module, const char *host,
37 || getallgroups(auth_uid, gid_list, &auth_uid_groups_cnt) != NULL)
38 auth_uid_groups_cnt = 0;
40 - if ((auth_uid_groups = new_array(char *, auth_uid_groups_cnt)) == NULL)
41 + if ((auth_uid_groups = new_array(const char *, auth_uid_groups_cnt)) == NULL)
42 out_of_memory("auth_server");
43 for (j = 0; j < auth_uid_groups_cnt; j++)
44 auth_uid_groups[j] = gid_to_group(gid_list[j]);
45 @@ -313,7 +313,7 @@ char *auth_server(int f_in, int f_out, int module, const char *host,
46 else if (opt_ch == 'd')
47 err = "denied by rule";
49 - char *group = group_match >= 0 ? auth_uid_groups[group_match] : NULL;
50 + const char *group = group_match >= 0 ? auth_uid_groups[group_match] : NULL;
51 err = check_secret(module, line, group, challenge, pass);
54 @@ -324,7 +324,7 @@ char *auth_server(int f_in, int f_out, int module, const char *host,
56 for (j = 0; j < auth_uid_groups_cnt; j++) {
57 if (auth_uid_groups[j])
58 - free(auth_uid_groups[j]);
59 + free((char*)auth_uid_groups[j]);
61 free(auth_uid_groups);
63 diff --git a/clientserver.c b/clientserver.c
66 @@ -65,6 +65,7 @@ extern iconv_t ic_send, ic_recv;
70 +pid_t namecvt_pid = 0;
71 struct chmod_mode_struct *daemon_chmod_modes;
73 /* module_dirlen is the length of the module_dir string when in daemon
74 @@ -76,6 +77,7 @@ unsigned int module_dirlen = 0;
75 char *full_module_path;
77 static int rl_nulls = 0;
78 +static int namecvt_fd_req = -1, namecvt_fd_ans = -1;
81 static struct sigaction sigact;
82 @@ -693,7 +695,7 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
86 - if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) {
87 + if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i) || *lp_name_converter(i)) {
90 /* For post-xfer exec, fork a new process to run the rsync
91 @@ -775,6 +777,44 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
92 set_blocking(pre_exec_arg_fd);
93 set_blocking(pre_exec_error_fd);
95 + if (*lp_name_converter(i)) {
96 + int fds_to[2], fds_from[2];
97 + if (pipe(fds_to) < 0 || pipe(fds_from) < 0
98 + || (namecvt_pid = fork()) < 0) {
99 + rsyserr(FLOG, errno, "name-converter exec preparation failed");
100 + io_printf(f_out, "@ERROR: name-converter exec preparation failed\n");
103 + if (namecvt_pid == 0) {
104 + char *args[100], *run = lp_name_converter(i);
107 + close(fds_from[0]);
108 + set_blocking(fds_to[0]);
109 + set_blocking(fds_from[1]);
110 + close(STDIN_FILENO);
111 + close(STDOUT_FILENO);
112 + dup2(fds_to[0], STDIN_FILENO);
113 + dup2(fds_from[1], STDOUT_FILENO);
114 + while (cnt+1 < (int)(sizeof args / sizeof (char *))) {
115 + char *space = strchr(run, ' ');
123 + execvp(args[0], args);
127 + close(fds_from[1]);
128 + set_blocking(fds_to[1]);
129 + set_blocking(fds_from[0]);
130 + namecvt_fd_req = fds_to[1];
131 + namecvt_fd_ans = fds_from[0];
136 @@ -1011,6 +1051,44 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
140 +int namecvt_name(const char *cmd, const char *name)
143 + int got, len = snprintf(buf, sizeof buf, "%s %s", cmd, name);
144 + if (len >= (int)sizeof buf) {
145 + rprintf(FERROR, "namecvt_name() request was too large.\n");
146 + exit_cleanup(RERR_UNSUPPORTED);
148 + while ((got = write(namecvt_fd_req, buf, len + 1)) != len + 1) {
149 + if (got < 0 && errno == EINTR)
151 + rprintf(FERROR, "Connection to name-converter failed.\n");
152 + exit_cleanup(RERR_SOCKETIO);
154 + if (!(len = read_arg_from_pipe(namecvt_fd_ans, buf, sizeof buf)))
159 +const char *namecvt_id(const char *cmd, int id)
162 + int got, len = snprintf(buf, sizeof buf, "%s %d", cmd, id);
163 + if (len >= (int)sizeof buf) {
164 + rprintf(FERROR, "namecvt_id() request was too large.\n");
165 + exit_cleanup(RERR_UNSUPPORTED);
167 + while ((got = write(namecvt_fd_req, buf, len + 1)) != len + 1) {
168 + if (got < 0 && errno == EINTR)
170 + rprintf(FERROR, "Connection to name-converter failed.\n");
171 + exit_cleanup(RERR_SOCKETIO);
173 + if (!(len = read_arg_from_pipe(namecvt_fd_ans, buf, sizeof buf)))
175 + return strdup(buf);
178 /* send a list of available modules to the client. Don't list those
179 with "list = False". */
180 static void send_listing(int fd)
181 diff --git a/loadparm.c b/loadparm.c
184 @@ -123,6 +123,7 @@ typedef struct {
188 + char *name_converter;
189 char *outgoing_chmod;
192 @@ -199,6 +200,7 @@ static const all_vars Defaults = {
193 /* log_file; */ NULL,
194 /* log_format; */ "%o %h [%a] %m (%u) %f %l",
196 + /* name_converter; */ NULL,
197 /* outgoing_chmod; */ NULL,
199 /* postxfer_exec; */ NULL,
200 @@ -344,6 +346,7 @@ static struct parm_struct parm_table[] =
201 {"max verbosity", P_INTEGER,P_LOCAL, &Vars.l.max_verbosity, NULL,0},
202 {"munge symlinks", P_BOOL, P_LOCAL, &Vars.l.munge_symlinks, NULL,0},
203 {"name", P_STRING, P_LOCAL, &Vars.l.name, NULL,0},
204 + {"name converter", P_STRING, P_LOCAL, &Vars.l.name_converter, NULL,0},
205 {"numeric ids", P_BOOL, P_LOCAL, &Vars.l.numeric_ids, NULL,0},
206 {"outgoing chmod", P_STRING, P_LOCAL, &Vars.l.outgoing_chmod, NULL,0},
207 {"path", P_PATH, P_LOCAL, &Vars.l.path, NULL,0},
208 @@ -472,6 +475,7 @@ FN_LOCAL_STRING(lp_outgoing_chmod, outgoing_chmod)
209 FN_LOCAL_STRING(lp_path, path)
210 FN_LOCAL_STRING(lp_postxfer_exec, postxfer_exec)
211 FN_LOCAL_STRING(lp_prexfer_exec, prexfer_exec)
212 +FN_LOCAL_STRING(lp_name_converter, name_converter)
213 FN_LOCAL_STRING(lp_refuse_options, refuse_options)
214 FN_LOCAL_STRING(lp_secrets_file, secrets_file)
215 FN_LOCAL_STRING(lp_temp_dir, temp_dir)
216 diff --git a/rsyncd.conf.yo b/rsyncd.conf.yo
219 @@ -195,10 +195,11 @@ if the module is not read-only).
221 When this parameter is enabled, rsync will not attempt to map users and groups
222 by name (by default), but instead copy IDs as though bf(--numeric-ids) had
223 -been specified. In order to enable name-mapping, rsync needs to be able to
224 +been specified. In order to enable name-mapping, rsync needs either the
225 +bf(name converter) parameter to specify a conversion program, or it needs to
226 use the standard library functions for looking up names and IDs (i.e.
227 code(getpwuid()), code(getgrgid()), code(getpwname()), and code(getgrnam())).
228 -This means the rsync
229 +The latter choice means the rsync
230 process in the chroot hierarchy will need to have access to the resources
231 used by these library functions (traditionally /etc/passwd and
232 /etc/group, but perhaps additional dynamic libraries as well).
233 @@ -264,6 +265,27 @@ path elements that rsync believes will allow a symlink to escape the module's
234 hierarchy. There are tricky ways to work around this, though, so you had
235 better trust your users if you choose this combination of parameters.
237 +dit(bf(name converter)) This parameter lets you specify a
238 +program that will be run by the rsync daemon (prior to bf(use chroot), if
239 +that parameter is enabled) to convert user/group names into numbers or visa
240 +versa. There is a sample perl script in the support directory named
241 +"nameconvert" that you can use to enable the use of the normal passwd/group
242 +lookup calls in a chroot daemon (which does not require any extra files
243 +be placed in the chroot area). This use is configured as follows:
245 +verb( name converter = /path/nameconvert)
247 +You could alternately specify a program that responds to each request using
248 +a lookup table to find the names and numbers, this allows you to configure
249 +per-module name conversion. See the support/nameconvert script for the
250 +details of what requests can be sent to the program.
252 +The program will have access to some of the environment variables that are
253 +described in the section on bf(pre-xfer exec): bf(RSYNC_MODULE_NAME),
254 +bf(RSYNC_MODULE_PATH), bf(RSYNC_HOST_ADDR), bf(RSYNC_HOST_NAME), and
255 +bf(RSYNC_USER_NAME). This is useful if you want to customize the
256 +conversion using a single program invocation.
258 dit(bf(charset)) This specifies the name of the character set in which the
259 module's filenames are stored. If the client uses an bf(--iconv) option,
260 the daemon will use the value of the "charset" parameter regardless of the
261 diff --git a/support/nameconvert b/support/nameconvert
264 +++ b/support/nameconvert
267 +# This implements a simple protocol to do {user,group}-{name,id}
268 +# conversions. All input and output consists of simple strings
269 +# with a terminating null char (or newline for debugging). If
270 +# the conversion fails, an empty string is returned.
272 +# The requests can be:
274 +# uid ID_NUM\0 -> NAME\0
275 +# gid ID_NUM\0 -> NAME\0
276 +# usr NAME\0 -> ID_NUM\0
277 +# grp NAME\0 -> ID_NUM\0
279 +# An unknown ID_NUM or NAME results in an empty return value.
281 +# This is used by an rsync daemon when configured with the
282 +# "name converter" setting.
286 +my $eol = grep(/^--debug$/, @ARGV) ? "\n" : "\0";
294 + if (/^uid (\d+)$/) {
295 + $ans = getpwuid($1);
296 + } elsif (/^gid (\d+)$/) {
297 + $ans = getgrgid($1);
298 + } elsif (/^usr (\S+)$/) {
299 + $ans = getpwnam($1);
300 + } elsif (/^grp (\S+)$/) {
301 + $ans = getgrnam($1);
303 + die "Invalid request: $_";
305 + $ans = '' unless defined $ans;
308 diff --git a/t_stub.c b/t_stub.c
311 @@ -33,6 +33,7 @@ int preserve_times = 0;
312 int preserve_xattrs = 0;
316 filter_rule_list daemon_filter_list;
318 void rprintf(UNUSED(enum logcode code), const char *format, ...)
319 @@ -83,6 +84,11 @@ filter_rule_list daemon_filter_list;
323 + int namecvt_name(UNUSED(const char *cmd), UNUSED(const char *name))
328 char *lp_name(UNUSED(int mod))
331 diff --git a/uidlist.c b/uidlist.c
334 @@ -33,6 +33,7 @@ extern int preserve_uid;
335 extern int preserve_gid;
336 extern int preserve_acls;
337 extern int numeric_ids;
338 +extern pid_t namecvt_pid;
339 extern gid_t our_gid;
340 extern char *usermap;
341 extern char *groupmap;
342 @@ -97,19 +98,27 @@ static struct idlist *add_to_list(struct idlist **root, id_t id, union name_or_i
345 /* turn a uid into a user name */
346 -char *uid_to_user(uid_t uid)
347 +const char *uid_to_user(uid_t uid)
349 - struct passwd *pass = getpwuid(uid);
351 + struct passwd *pass;
354 + return namecvt_id("uid", (int)uid);
356 + if ((pass = getpwuid(uid)) != NULL)
357 return strdup(pass->pw_name);
361 /* turn a gid into a group name */
362 -char *gid_to_group(gid_t gid)
363 +const char *gid_to_group(gid_t gid)
365 - struct group *grp = getgrgid(gid);
370 + return namecvt_id("gid", (int)gid);
372 + if ((grp = getgrgid(gid)) != NULL)
373 return strdup(grp->gr_name);
376 @@ -117,32 +126,54 @@ char *gid_to_group(gid_t gid)
377 /* Parse a user name or (optionally) a number into a uid */
378 int user_to_uid(const char *name, uid_t *uid_p, BOOL num_ok)
380 - struct passwd *pass;
386 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
387 *uid_p = id_parse(name);
390 - if (!(pass = getpwnam(name)))
392 - *uid_p = pass->pw_uid;
395 + if (!(uid = namecvt_name("usr", name)))
398 + struct passwd *pass;
399 + if (!(pass = getpwnam(name)))
401 + uid = pass->pw_uid;
408 /* Parse a group name or (optionally) a number into a gid */
409 int group_to_gid(const char *name, gid_t *gid_p, BOOL num_ok)
417 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
418 *gid_p = id_parse(name);
421 - if (!(grp = getgrnam(name)))
423 - *gid_p = grp->gr_gid;
426 + if (!(gid = namecvt_name("grp", name)))
430 + if (!(grp = getgrnam(name)))
439 diff --git a/util.c b/util.c
442 @@ -36,6 +36,8 @@ extern int preallocate_files;
443 extern char *module_dir;
444 extern unsigned int module_dirlen;
445 extern char *partial_dir;
446 +extern pid_t namecvt_pid;
447 +extern unsigned int module_dirlen;
448 extern filter_rule_list daemon_filter_list;
450 int sanitize_paths = 0;