The patches for 3.3.0.
[rsync-patches.git] / slp.diff
1 This adds Service Location Protocol support.
2
3 To use this patch, run these commands for a successful build:
4
5     patch -p1 <patches/slp.diff
6     ./prepare-source
7     ./configure --enable-slp
8     make
9
10 TODO: the configure changes should abort if the user requests --enable-slp
11 and we can't honor that request.
12
13 based-on: 6c8ca91c731b7bf2b081694bda85b7dadc2b7aff
14 diff --git a/Makefile.in b/Makefile.in
15 --- a/Makefile.in
16 +++ b/Makefile.in
17 @@ -17,6 +17,8 @@ CXX=@CXX@
18  CXXFLAGS=@CXXFLAGS@
19  EXEEXT=@EXEEXT@
20  LDFLAGS=@LDFLAGS@
21 +LIBSLP=@LIBSLP@
22 +SLPOBJ=@SLPOBJ@
23  LIBOBJDIR=lib/
24  
25  INSTALLCMD=@INSTALL@
26 @@ -48,7 +50,7 @@ OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
27  OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o hashtable.o \
28         usage.o fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
29  OBJS3=progress.o pipe.o @MD5_ASM@ @ROLL_SIMD@ @ROLL_ASM@
30 -DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
31 +DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o $(SLPOBJ)
32  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
33         popt/popthelp.o popt/poptparse.o
34  OBJS=$(OBJS1) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) @BUILD_ZLIB@ @BUILD_POPT@
35 @@ -103,7 +105,7 @@ install-strip:
36         $(MAKE) INSTALL_STRIP='-s' install
37  
38  rsync$(EXEEXT): $(OBJS)
39 -       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
40 +       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(LIBSLP)
41  
42  rrsync: support/rrsync
43         cp -p $(srcdir)/support/rrsync rrsync
44 diff --git a/clientserver.c b/clientserver.c
45 --- a/clientserver.c
46 +++ b/clientserver.c
47 @@ -1533,6 +1533,13 @@ int daemon_main(void)
48          * address too.  In fact, why not just do getnameinfo on the
49          * local address??? */
50  
51 +#ifdef HAVE_LIBSLP
52 +       if (lp_use_slp() && register_services()) {
53 +               rprintf(FINFO,
54 +                   "Couldn't register with service discovery protocol, continuing anyway\n");
55 +       }
56 +#endif
57 +
58         start_accept_loop(rsync_port, start_daemon);
59         return -1;
60  }
61 diff --git a/configure.ac b/configure.ac
62 --- a/configure.ac
63 +++ b/configure.ac
64 @@ -1047,6 +1047,29 @@ if test $rsync_cv_can_hardlink_special = yes; then
65      AC_DEFINE(CAN_HARDLINK_SPECIAL, 1, [Define to 1 if link() can hard-link special files.])
66  fi
67  
68 +AC_ARG_ENABLE(slp, [  --disable-slp           turn off SLP support, defaults to on])
69 +AC_ARG_WITH(openslp-libs, [  --with-openslp-libs     set directory for OpenSLP library],
70 +    LDFLAGS="-L$withval $LDFLAGS"
71 +    DSOFLAGS="-L$withval $DSOFLAGS",)
72 +AC_ARG_WITH(openslp-includes, [  --with-openslp-includes set directory for OpenSLP includes],
73 +    CFLAGS="-I$withval $CFLAGS"
74 +    CXXFLAGS="-I$withval $CXXFLAGS"
75 +    CPPFLAGS="-I$withval $CPPFLAGS",)
76 +
77 +LIBSLP=""
78 +SLPOBJ=""
79 +
80 +if test x$enable_slp != xno; then
81 +    AC_CHECK_HEADER(slp.h,
82 +        AC_CHECK_LIB(slp, SLPOpen,
83 +           AC_DEFINE(HAVE_LIBSLP, 1, [Define to 1 for SLP support])
84 +           SLPOBJ="srvreg.o srvloc.o"
85 +            LIBSLP="-lslp"))
86 +fi
87 +
88 +AC_SUBST(LIBSLP)
89 +AC_SUBST(SLPOBJ)
90 +
91  AC_CACHE_CHECK([for working socketpair],rsync_cv_HAVE_SOCKETPAIR,[
92  AC_RUN_IFELSE([AC_LANG_SOURCE([[
93  #ifdef HAVE_SYS_TYPES_H
94 diff --git a/daemon-parm.txt b/daemon-parm.txt
95 --- a/daemon-parm.txt
96 +++ b/daemon-parm.txt
97 @@ -10,8 +10,10 @@ STRING       socket_options          NULL
98  
99  INTEGER        listen_backlog          5
100  INTEGER        rsync_port|port         0
101 +INTEGER        slp_refresh             0
102  
103  BOOL   proxy_protocol          False
104 +BOOL   use_slp                 False
105  
106  Locals: =================================================================
107  
108 diff --git a/main.c b/main.c
109 --- a/main.c
110 +++ b/main.c
111 @@ -1400,6 +1400,22 @@ static int start_client(int argc, char *argv[])
112  
113         if (!read_batch) { /* for read_batch, NO source is specified */
114                 char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
115 +
116 +               if (shell_machine && !shell_machine[0]) {
117 +#ifdef HAVE_LIBSLP
118 +                       /* User entered just rsync:// URI */
119 +                       if (lp_use_slp()) {
120 +                               print_service_list();
121 +                               exit_cleanup(0);
122 +                       }
123 +                       rprintf(FINFO, "SLP is disabled, cannot browse\n");
124 +                       exit_cleanup(RERR_SYNTAX);
125 +#else /* No SLP, die here */
126 +                       rprintf(FINFO, "No SLP support, cannot browse\n");
127 +                       exit_cleanup(RERR_SYNTAX);
128 +#endif
129 +               }
130 +
131                 if (path) { /* source is remote */
132                         char *dummy_host;
133                         int dummy_port = 0;
134 diff --git a/rsync.1.md b/rsync.1.md
135 --- a/rsync.1.md
136 +++ b/rsync.1.md
137 @@ -152,6 +152,18 @@ rsync daemon by leaving off the module name:
138  
139  >     rsync somehost.mydomain.com::
140  
141 +And, if Service Location Protocol is available, the following will list the
142 +available rsync servers:
143 +
144 +>     rsync rsync://
145 +
146 +See the following section for even more usage details.
147 +
148 +One more thing, if Service Location Protocol is available, the following will
149 +list the available rsync servers:
150 +
151 +>     rsync rsync://
152 +
153  ## COPYING TO A DIFFERENT NAME
154  
155  When you want to copy a directory to a different name, use a trailing slash on
156 diff --git a/rsync.h b/rsync.h
157 --- a/rsync.h
158 +++ b/rsync.h
159 @@ -234,6 +234,10 @@
160  #define SIGNIFICANT_ITEM_FLAGS (~(\
161         ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
162  
163 +/* this is the minimum we'll use, irrespective of config setting */
164 +/* definitely don't set to less than about 30 seconds */
165 +#define SLP_MIN_TIMEOUT 120
166 +
167  #define CFN_KEEP_DOT_DIRS (1<<0)
168  #define CFN_KEEP_TRAILING_SLASH (1<<1)
169  #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
170 diff --git a/rsyncd.conf b/rsyncd.conf
171 new file mode 100644
172 --- /dev/null
173 +++ b/rsyncd.conf
174 @@ -0,0 +1 @@
175 +slp refresh = 300
176 diff --git a/rsyncd.conf.5.md b/rsyncd.conf.5.md
177 --- a/rsyncd.conf.5.md
178 +++ b/rsyncd.conf.5.md
179 @@ -120,6 +120,21 @@ parameters:
180      You can override the default backlog value when the daemon listens for
181      connections.  It defaults to 5.
182  
183 +0.  `use slp`
184 +
185 +    You can enable Service Location Protocol support by enabling this global
186 +    parameter.  The default is "false".
187 +
188 +0.  `slp refresh`
189 +
190 +    This parameter is used to determine how long service advertisements are
191 +    valid (measured in seconds), and is only applicable if you have Service
192 +    Location Protocol support compiled in. If this is not set or is set to
193 +    zero, then service advertisements never time out. If this is set to less
194 +    than 120 seconds, then 120 seconds is used. If it is set to more than
195 +    65535, then 65535 is used (which is a limitation of SLP).  Using 3600
196 +    (one hour) is a good number if you tend to change your configuration.
197 +
198  You may also include any [MODULE PARAMETERS](#) in the global part of the
199  config file, in which case the supplied value will override the default for
200  that parameter.
201 @@ -1208,6 +1223,7 @@ A more sophisticated example would be:
202  > max connections = 4
203  > syslog facility = local5
204  > pid file = /var/run/rsyncd.pid
205 +> slp refresh = 3600
206  >
207  > [ftp]
208  >         path = /var/ftp/./pub
209 diff --git a/socket.c b/socket.c
210 --- a/socket.c
211 +++ b/socket.c
212 @@ -534,6 +534,16 @@ void start_accept_loop(int port, int (*fn)(int, int))
213  {
214         fd_set deffds;
215         int *sp, maxfd, i;
216 +#ifdef HAVE_LIBSLP
217 +       time_t next_slp_refresh;
218 +       short slp_timeout = lp_use_slp() ? lp_slp_refresh() : 0;
219 +       if (slp_timeout) {
220 +               if (slp_timeout < SLP_MIN_TIMEOUT)
221 +                       slp_timeout = SLP_MIN_TIMEOUT;
222 +               /* re-register before slp times out */
223 +               slp_timeout -= 15;
224 +       }
225 +#endif
226  
227  #ifdef HAVE_SIGACTION
228         sigact.sa_flags = SA_NOCLDSTOP;
229 @@ -561,14 +571,25 @@ void start_accept_loop(int port, int (*fn)(int, int))
230                         maxfd = sp[i];
231         }
232  
233 +#ifdef HAVE_LIBSLP
234 +       next_slp_refresh = time(NULL) + slp_timeout;
235 +#endif
236 +
237         /* now accept incoming connections - forking a new process
238          * for each incoming connection */
239         while (1) {
240                 fd_set fds;
241                 pid_t pid;
242                 int fd;
243 +               int sel_ret;
244                 struct sockaddr_storage addr;
245                 socklen_t addrlen = sizeof addr;
246 +#ifdef HAVE_LIBSLP
247 +               struct timeval slp_tv;
248 +
249 +               slp_tv.tv_sec = 10;
250 +               slp_tv.tv_usec = 0;
251 +#endif
252  
253                 /* close log file before the potentially very long select so
254                  * file can be trimmed by another process instead of growing
255 @@ -581,7 +602,18 @@ void start_accept_loop(int port, int (*fn)(int, int))
256                 fds = deffds;
257  #endif
258  
259 -               if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 1)
260 +#ifdef HAVE_LIBSLP
261 +               sel_ret = select(maxfd + 1, &fds, NULL, NULL,
262 +                                slp_timeout ? &slp_tv : NULL);
263 +               if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
264 +                       rprintf(FINFO, "Service registration expired, refreshing it\n");
265 +                       register_services();
266 +                       next_slp_refresh = time(NULL) + slp_timeout;
267 +               }
268 +#else
269 +               sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
270 +#endif
271 +               if (sel_ret < 1)
272                         continue;
273  
274                 for (i = 0, fd = -1; sp[i] >= 0; i++) {
275 diff --git a/srvloc.c b/srvloc.c
276 new file mode 100644
277 --- /dev/null
278 +++ b/srvloc.c
279 @@ -0,0 +1,103 @@
280 +/* -*- c-file-style: "linux"; -*-
281 +
282 +   Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
283 +
284 +   This program is free software; you can redistribute it and/or modify
285 +   it under the terms of the GNU General Public License as published by
286 +   the Free Software Foundation; either version 2 of the License, or
287 +   (at your option) any later version.
288 +
289 +   This program is distributed in the hope that it will be useful,
290 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
291 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
292 +   GNU General Public License for more details.
293 +
294 +   You should have received a copy of the GNU General Public License
295 +   along with this program; if not, write to the Free Software
296 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
297 +*/
298 +
299 +/* This file implements the service location functionality */
300 +/* Basically, it uses normal Service Location Protocol API */
301 +
302 +/* It is really a cheap hack - just to show how it might work
303 +   in a real application.
304 +*/
305 +
306 +#include "rsync.h"
307 +
308 +#include <slp.h>
309 +#include <stdio.h>
310 +#include <string.h>
311 +
312 +/* This one just prints out the attributes */
313 +static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
314 +                                 SLPError errcode, UNUSED(void *cookie))
315 +{
316 +       char *cleanstr;
317 +
318 +       if (errcode == SLP_OK) {
319 +               if (!strcmp(attrlist, "(comment=)"))
320 +                       rprintf(FINFO, "\t(No description)\n");
321 +               else {
322 +                       cleanstr = strrchr(attrlist, ')') ;
323 +                       *cleanstr = ' '; /* remove last ')' */
324 +                       rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
325 +               }
326 +       }
327 +       return SLP_FALSE;
328 +}
329 +
330 +static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
331 +                       const char *srvurl, UNUSED(unsigned short lifetime),
332 +                       SLPError errcode, void *cookie)
333 +{
334 +       SLPError    result;
335 +       SLPHandle   attrhslp;
336 +
337 +       if (errcode == SLP_OK) {
338 +               /* chop service: off the front */
339 +               rprintf(FINFO, "  %s  ", (strchr(srvurl, ':') + 1));
340 +               /* check for any attributes */
341 +               if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
342 +                       result = SLPFindAttrs(attrhslp, srvurl,
343 +                                             "", /* return all attributes */
344 +                                             "", /* use configured scopes */
345 +                                             getAttrCallback, NULL);
346 +                       if (result != SLP_OK) {
347 +                               rprintf(FERROR, "errorcode: %i\n",result);
348 +                       }
349 +                       SLPClose(attrhslp);
350 +               }
351 +               *(SLPError*)cookie = SLP_OK;
352 +       } else
353 +               *(SLPError*)cookie = errcode;
354 +
355 +       /* Return SLP_TRUE because we want to be called again
356 +        * if more services were found. */
357 +
358 +       return SLP_TRUE;
359 +}
360 +
361 +int print_service_list(void)
362 +{
363 +       SLPError err;
364 +       SLPError callbackerr;
365 +       SLPHandle hslp;
366 +
367 +       err = SLPOpen("en",SLP_FALSE,&hslp);
368 +       if (err != SLP_OK) {
369 +               rprintf(FERROR, "Error opening slp handle %i\n", err);
370 +               return err;
371 +       }
372 +
373 +       SLPFindSrvs(hslp, "rsync",
374 +                   0, /* use configured scopes */
375 +                   0, /* no attr filter        */
376 +                   getSLPSrvURLCallback, &callbackerr);
377 +
378 +       /* Now that we're done using slp, close the slp handle */
379 +       SLPClose(hslp);
380 +
381 +       return 0;
382 +}
383 diff --git a/srvreg.c b/srvreg.c
384 new file mode 100644
385 --- /dev/null
386 +++ b/srvreg.c
387 @@ -0,0 +1,128 @@
388 +/* -*- c-file-style: "linux"; -*-
389 +
390 +   Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
391 +
392 +   This program is free software; you can redistribute it and/or modify
393 +   it under the terms of the GNU General Public License as published by
394 +   the Free Software Foundation; either version 2 of the License, or
395 +   (at your option) any later version.
396 +
397 +   This program is distributed in the hope that it will be useful,
398 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
399 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
400 +   GNU General Public License for more details.
401 +
402 +   You should have received a copy of the GNU General Public License
403 +   along with this program; if not, write to the Free Software
404 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
405 +*/
406 +
407 +/* This file implements the service registration functionality */
408 +
409 +/* Basically, it uses normal Service Location Protocol API */
410 +
411 +#include "rsync.h"
412 +#include "slp.h"
413 +#include "netdb.h"
414 +
415 +extern int rsync_port;
416 +
417 +static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
418 +{
419 +       /* return the error code in the cookie */
420 +       *(SLPError*)cookie = errcode;
421 +
422 +       /* You could do something else here like print out
423 +        * the errcode, etc.  Remember, as a general rule,
424 +        * do not try to do too much in a callback because
425 +        * it is being executed by the same thread that is
426 +        * reading slp packets from the wire. */
427 +}
428 +
429 +int register_services(void)
430 +{
431 +       SLPError err, callbackerr;
432 +       SLPHandle hslp;
433 +       int n;
434 +       int i;
435 +       char srv[120];
436 +       char attr[120];
437 +       char localhost[256];
438 +       extern char *config_file;
439 +       short timeout;
440 +       struct addrinfo aih, *ai = 0;
441 +
442 +       if (!lp_load(config_file, 0)) {
443 +               exit_cleanup(RERR_SYNTAX);
444 +       }
445 +
446 +       n = lp_num_modules();
447 +
448 +       if (0 == lp_slp_refresh())
449 +               timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
450 +       else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
451 +               timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
452 +       else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
453 +               timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
454 +       else
455 +               timeout = lp_slp_refresh();
456 +
457 +       rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
458 +       err = SLPOpen("en",SLP_FALSE,&hslp);
459 +       if (err != SLP_OK) {
460 +               rprintf(FINFO, "Error opening slp handle %i\n",err);
461 +               return err;
462 +       }
463 +       if (gethostname(localhost, sizeof localhost)) {
464 +              rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
465 +              return err;
466 +       }
467 +       memset(&aih, 0, sizeof aih);
468 +       aih.ai_family = PF_UNSPEC;
469 +       aih.ai_flags = AI_CANONNAME;
470 +       if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
471 +              rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
472 +              return err;
473 +       }
474 +       /* Register each service with SLP */
475 +       for (i = 0; i < n; i++) {
476 +               if (!lp_list(i))
477 +                       continue;
478 +
479 +               snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
480 +                        ai->ai_canonname,
481 +                        rsync_port,
482 +                        lp_name(i));
483 +               rprintf(FINFO, "    %s\n", srv);
484 +               if (lp_comment(i)) {
485 +                       snprintf(attr, sizeof attr, "(comment=%s)",
486 +                                lp_comment(i));
487 +               }
488 +               err = SLPReg(hslp,
489 +                            srv, /* service to register */
490 +                            timeout,
491 +                            0,  /* this is ignored */
492 +                            attr, /* attributes */
493 +                            SLP_TRUE, /* new registration - don't change this */
494 +                            slp_callback, /* callback */
495 +                            &callbackerr);
496 +
497 +               /* err may contain an error code that occurred as the slp library
498 +                * _prepared_ to make the call. */
499 +               if (err != SLP_OK || callbackerr != SLP_OK)
500 +                       rprintf(FINFO, "Error registering service with slp %i\n", err);
501 +
502 +               /* callbackerr may contain an error code (that was assigned through
503 +                * the callback cookie) that occurred as slp packets were sent on
504 +                * the wire. */
505 +               if (callbackerr != SLP_OK)
506 +                       rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
507 +       }
508 +
509 +       /* Now that we're done using slp, close the slp handle */
510 +       freeaddrinfo(ai);
511 +       SLPClose(hslp);
512 +
513 +       /* refresh is done in main select loop */
514 +       return 0;
515 +}
516 diff --git a/usage.c b/usage.c
517 --- a/usage.c
518 +++ b/usage.c
519 @@ -138,6 +138,11 @@ static void print_info_flags(enum logcode f)
520  #endif
521                         "crtimes",
522  
523 +#ifndef HAVE_LIBSLP
524 +               "no "
525 +#endif
526 +                       "SLP",
527 +
528         "*Optimizations",
529  
530  #ifndef USE_ROLL_SIMD