0ebbb50285e741b635a2860b2b86efd4f762dcaf
[rsync/rsync-patches.git] / pre-post-exec.diff
1 A fairly decent attempt to add two new daemon module options:
2
3     prexfer-exec = COMMAND
4     postxfer-exec = COMMAND
5
6 The prexfer-exec command runs before the transfer happens, while the
7 postxfer-exec command runs after the transfer completes, even if the
8 transfer failed.  The following environment variables will be set in
9 both cases:
10
11     RSYNC_MODULE_NAME  --  The name of the module being accessed
12     RSYNC_MODULE_PATH  --  The path configured for the module
13
14 These environment variables will also be set for the postxfer-exec
15 command:
16
17     RSYNC_EXIT_STATUS -- rsync's exit value.  This will be 0 for a
18                          successful run, a positive value for an error
19                          that rsync returned (e.g. 23=partial xfer),
20                          or a -1 if rsync failed to exit properly.
21     RSYNC_RAW_STATUS -- the raw exit value from waitpid()
22
23 Both commands are run by the user that started the daemon (not the
24 module's uid/gid setting) without any chroot() restrictions (even if
25 the module will/has run chroot()ed).
26
27 BUILD NOTE:  You'll want to run "make proto" after applying this patch.
28
29 --- orig/clientserver.c 2005-06-10 21:33:27
30 +++ clientserver.c      2005-07-19 20:46:59
31 @@ -226,7 +226,7 @@ static int rsync_module(int f_in, int f_
32         char line[MAXPATHLEN];
33         uid_t uid = (uid_t)-2;  /* canonically "nobody" */
34         gid_t gid = (gid_t)-2;
35 -       char *p;
36 +       char *p, *s;
37         char *addr = client_addr(f_in);
38         char *host = client_name(f_in);
39         char *name = lp_name(i);
40 @@ -347,6 +347,49 @@ static int rsync_module(int f_in, int f_
41  
42         log_init();
43  
44 +       s = lp_prexfer_exec(i);
45 +       p = lp_postxfer_exec(i);
46 +       if ((s && *s) || (p && *p)) {
47 +               char *modname, *modpath;
48 +               int status;
49 +               if (asprintf(&modname, "RSYNC_MODULE_NAME=%s", name) < 0)
50 +                       out_of_memory("rsync_module");
51 +               putenv(modname);
52 +               if (asprintf(&modpath, "RSYNC_MODULE_PATH=%s", lp_path(i)) < 0)
53 +                       out_of_memory("rsync_module");
54 +               putenv(modpath);
55 +               if (s && *s) {
56 +                       status = system(s);
57 +                       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
58 +                               rprintf(FLOG, "prexfer-exec failed\n");
59 +                               io_printf(f_out, "@ERROR: prexfer-exec failed\n");
60 +                               return -1;
61 +                       }
62 +               }
63 +               if (p && *p) {
64 +                       pid_t pid = fork();
65 +                       if (pid < 0) {
66 +                               rsyserr(FLOG, errno, "fork failed");
67 +                               io_printf(f_out, "@ERROR: fork failed\n");
68 +                               return -1;
69 +                       }
70 +                       if (pid) {
71 +                               char *ret1, *ret2;
72 +                               waitpid(pid, &status, 0);
73 +                               if (asprintf(&ret1, "RSYNC_RAW_STATUS=%d", status) > 0)
74 +                                       putenv(ret1);
75 +                               if (WIFEXITED(status))
76 +                                       status = WEXITSTATUS(status);
77 +                               else
78 +                                       status = -1;
79 +                               if (asprintf(&ret2, "RSYNC_EXIT_STATUS=%d", status) > 0)
80 +                                       putenv(ret2);
81 +                               system(p);
82 +                               _exit(0);
83 +                       }
84 +               }
85 +       }
86 +
87         if (use_chroot) {
88                 /*
89                  * XXX: The 'use chroot' flag is a fairly reliable
90 --- orig/loadparm.c     2005-06-10 21:33:28
91 +++ loadparm.c  2005-07-19 20:05:26
92 @@ -140,6 +140,8 @@ typedef struct
93         char *log_format;
94         char *refuse_options;
95         char *dont_compress;
96 +       char *prexfer_exec;
97 +       char *postxfer_exec;
98         int timeout;
99         int max_connections;
100         int max_verbosity;
101 @@ -175,6 +177,8 @@ static service sDefault =
102         "%o %h [%a] %m (%u) %f %l",    /* log format */
103         NULL,    /* refuse options */
104         "*.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz",    /* dont compress */
105 +       NULL,     /* prexfer_exec */
106 +       NULL,     /* postxfer_exec */
107         0,        /* timeout */
108         0,        /* max connections */
109         1,        /* max verbosity */
110 @@ -298,6 +302,8 @@ static struct parm_struct parm_table[] =
111    {"log format",       P_STRING,  P_LOCAL,  &sDefault.log_format,  NULL,   0},
112    {"refuse options",   P_STRING,  P_LOCAL,  &sDefault.refuse_options,NULL, 0},
113    {"dont compress",    P_STRING,  P_LOCAL,  &sDefault.dont_compress,NULL,  0},
114 +  {"prexfer-exec",     P_STRING,  P_LOCAL,  &sDefault.prexfer_exec, NULL,  0},
115 +  {"postxfer-exec",    P_STRING,  P_LOCAL,  &sDefault.postxfer_exec,NULL,  0},
116    {NULL,               P_BOOL,    P_NONE,   NULL,                  NULL,   0}
117  };
118  
119 @@ -379,6 +385,8 @@ FN_LOCAL_STRING(lp_include_from, include
120  FN_LOCAL_STRING(lp_log_format, log_format)
121  FN_LOCAL_STRING(lp_refuse_options, refuse_options)
122  FN_LOCAL_STRING(lp_dont_compress, dont_compress)
123 +FN_LOCAL_STRING(lp_prexfer_exec, prexfer_exec)
124 +FN_LOCAL_STRING(lp_postxfer_exec, postxfer_exec)
125  FN_LOCAL_INTEGER(lp_timeout, timeout)
126  FN_LOCAL_INTEGER(lp_max_connections, max_connections)
127  FN_LOCAL_INTEGER(lp_max_verbosity, max_verbosity)