The patches for 3.2.3pre1.
[rsync.git/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: 8695bcc2b140b2518254234659a03b96f04055fc
14 diff --git a/Makefile.in b/Makefile.in
15 --- a/Makefile.in
16 +++ b/Makefile.in
17 @@ -16,6 +16,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 @@ -45,7 +47,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         fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
29  OBJS3=progress.o pipe.o @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) @SIMD@ $(DAEMON_OBJ) $(LIBOBJ) @BUILD_ZLIB@ @BUILD_POPT@
35 @@ -94,7 +96,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  $(OBJS): $(HEADERS)
43  $(CHECK_OBJS): $(HEADERS)
44 diff --git a/clientserver.c b/clientserver.c
45 --- a/clientserver.c
46 +++ b/clientserver.c
47 @@ -1456,6 +1456,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 @@ -994,6 +994,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  #include <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 @@ -1405,6 +1405,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/options.c b/options.c
135 --- a/options.c
136 +++ b/options.c
137 @@ -681,6 +681,11 @@ static void print_info_flags(enum logcode f)
138  #endif
139                         "crtimes",
140  
141 +#ifndef HAVE_LIBSLP
142 +               "no "
143 +#endif
144 +                       "SLP",
145 +
146         "*Optimizations",
147  
148  #ifndef HAVE_SIMD
149 diff --git a/rsync.1.md b/rsync.1.md
150 --- a/rsync.1.md
151 +++ b/rsync.1.md
152 @@ -149,7 +149,19 @@ rsync daemon by leaving off the module name:
153  
154  >     rsync somehost.mydomain.com::
155  
156 -See the following section for more details.
157 +And, if Service Location Protocol is available, the following will list the
158 +available rsync servers:
159 +
160 +>     rsync rsync://
161 +
162 +See the following section for even more usage details.
163 +
164 +One more thing, if Service Location Protocol is available, the following will
165 +list the available rsync servers:
166 +
167 +>     rsync rsync://
168 +
169 +See the following section for even more usage details.
170  
171  # ADVANCED USAGE
172  
173 diff --git a/rsync.h b/rsync.h
174 --- a/rsync.h
175 +++ b/rsync.h
176 @@ -224,6 +224,10 @@
177  #define SIGNIFICANT_ITEM_FLAGS (~(\
178         ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
179  
180 +/* this is the minimum we'll use, irrespective of config setting */
181 +/* definitely don't set to less than about 30 seconds */
182 +#define SLP_MIN_TIMEOUT 120
183 +
184  #define CFN_KEEP_DOT_DIRS (1<<0)
185  #define CFN_KEEP_TRAILING_SLASH (1<<1)
186  #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
187 diff --git a/rsyncd.conf b/rsyncd.conf
188 new file mode 100644
189 --- /dev/null
190 +++ b/rsyncd.conf
191 @@ -0,0 +1 @@
192 +slp refresh = 300
193 diff --git a/rsyncd.conf.5.md b/rsyncd.conf.5.md
194 --- a/rsyncd.conf.5.md
195 +++ b/rsyncd.conf.5.md
196 @@ -136,6 +136,21 @@ a literal % into a value is to use %%.
197      You can override the default backlog value when the daemon listens for
198      connections.  It defaults to 5.
199  
200 +0.  `use slp`
201 +
202 +    You can enable Service Location Protocol support by enabling this global
203 +    parameter.  The default is "false".
204 +
205 +0.  `slp refresh`
206 +
207 +    This parameter is used to determine how long service advertisements are
208 +    valid (measured in seconds), and is only applicable if you have Service
209 +    Location Protocol support compiled in. If this is not set or is set to
210 +    zero, then service advertisements never time out. If this is set to less
211 +    than 120 seconds, then 120 seconds is used. If it is set to more than
212 +    65535, then 65535 is used (which is a limitation of SLP).  Using 3600
213 +    (one hour) is a good number if you tend to change your configuration.
214 +
215  # MODULE PARAMETERS
216  
217  After the global parameters you should define a number of modules, each module
218 @@ -1168,6 +1183,7 @@ A more sophisticated example would be:
219  > max connections = 4
220  > syslog facility = local5
221  > pid file = /var/run/rsyncd.pid
222 +> slp refresh = 3600
223  >
224  > [ftp]
225  >         path = /var/ftp/./pub
226 diff --git a/socket.c b/socket.c
227 --- a/socket.c
228 +++ b/socket.c
229 @@ -534,6 +534,16 @@ void start_accept_loop(int port, int (*fn)(int, int))
230  {
231         fd_set deffds;
232         int *sp, maxfd, i;
233 +#ifdef HAVE_LIBSLP
234 +       time_t next_slp_refresh;
235 +       short slp_timeout = lp_use_slp() ? lp_slp_refresh() : 0;
236 +       if (slp_timeout) {
237 +               if (slp_timeout < SLP_MIN_TIMEOUT)
238 +                       slp_timeout = SLP_MIN_TIMEOUT;
239 +               /* re-register before slp times out */
240 +               slp_timeout -= 15;
241 +       }
242 +#endif
243  
244  #ifdef HAVE_SIGACTION
245         sigact.sa_flags = SA_NOCLDSTOP;
246 @@ -561,14 +571,25 @@ void start_accept_loop(int port, int (*fn)(int, int))
247                         maxfd = sp[i];
248         }
249  
250 +#ifdef HAVE_LIBSLP
251 +       next_slp_refresh = time(NULL) + slp_timeout;
252 +#endif
253 +
254         /* now accept incoming connections - forking a new process
255          * for each incoming connection */
256         while (1) {
257                 fd_set fds;
258                 pid_t pid;
259                 int fd;
260 +               int sel_ret;
261                 struct sockaddr_storage addr;
262                 socklen_t addrlen = sizeof addr;
263 +#ifdef HAVE_LIBSLP
264 +               struct timeval slp_tv;
265 +
266 +               slp_tv.tv_sec = 10;
267 +               slp_tv.tv_usec = 0;
268 +#endif
269  
270                 /* close log file before the potentially very long select so
271                  * file can be trimmed by another process instead of growing
272 @@ -581,7 +602,18 @@ void start_accept_loop(int port, int (*fn)(int, int))
273                 fds = deffds;
274  #endif
275  
276 -               if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 1)
277 +#ifdef HAVE_LIBSLP
278 +               sel_ret = select(maxfd + 1, &fds, NULL, NULL,
279 +                                slp_timeout ? &slp_tv : NULL);
280 +               if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
281 +                       rprintf(FINFO, "Service registration expired, refreshing it\n");
282 +                       register_services();
283 +                       next_slp_refresh = time(NULL) + slp_timeout;
284 +               }
285 +#else
286 +               sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
287 +#endif
288 +               if (sel_ret < 1)
289                         continue;
290  
291                 for (i = 0, fd = -1; sp[i] >= 0; i++) {
292 diff --git a/srvloc.c b/srvloc.c
293 new file mode 100644
294 --- /dev/null
295 +++ b/srvloc.c
296 @@ -0,0 +1,103 @@
297 +/* -*- c-file-style: "linux"; -*-
298 +
299 +   Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
300 +
301 +   This program is free software; you can redistribute it and/or modify
302 +   it under the terms of the GNU General Public License as published by
303 +   the Free Software Foundation; either version 2 of the License, or
304 +   (at your option) any later version.
305 +
306 +   This program is distributed in the hope that it will be useful,
307 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
308 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
309 +   GNU General Public License for more details.
310 +
311 +   You should have received a copy of the GNU General Public License
312 +   along with this program; if not, write to the Free Software
313 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
314 +*/
315 +
316 +/* This file implements the service location functionality */
317 +/* Basically, it uses normal Service Location Protocol API */
318 +
319 +/* It is really a cheap hack - just to show how it might work
320 +   in a real application.
321 +*/
322 +
323 +#include "rsync.h"
324 +
325 +#include <slp.h>
326 +#include <stdio.h>
327 +#include <string.h>
328 +
329 +/* This one just prints out the attributes */
330 +static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
331 +                                 SLPError errcode, UNUSED(void *cookie))
332 +{
333 +       char *cleanstr;
334 +
335 +       if (errcode == SLP_OK) {
336 +               if (!strcmp(attrlist, "(comment=)"))
337 +                       rprintf(FINFO, "\t(No description)\n");
338 +               else {
339 +                       cleanstr = strrchr(attrlist, ')') ;
340 +                       *cleanstr = ' '; /* remove last ')' */
341 +                       rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
342 +               }
343 +       }
344 +       return SLP_FALSE;
345 +}
346 +
347 +static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
348 +                       const char *srvurl, UNUSED(unsigned short lifetime),
349 +                       SLPError errcode, void *cookie)
350 +{
351 +       SLPError    result;
352 +       SLPHandle   attrhslp;
353 +
354 +       if (errcode == SLP_OK) {
355 +               /* chop service: off the front */
356 +               rprintf(FINFO, "  %s  ", (strchr(srvurl, ':') + 1));
357 +               /* check for any attributes */
358 +               if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
359 +                       result = SLPFindAttrs(attrhslp, srvurl,
360 +                                             "", /* return all attributes */
361 +                                             "", /* use configured scopes */
362 +                                             getAttrCallback, NULL);
363 +                       if (result != SLP_OK) {
364 +                               rprintf(FERROR, "errorcode: %i\n",result);
365 +                       }
366 +                       SLPClose(attrhslp);
367 +               }
368 +               *(SLPError*)cookie = SLP_OK;
369 +       } else
370 +               *(SLPError*)cookie = errcode;
371 +
372 +       /* Return SLP_TRUE because we want to be called again
373 +        * if more services were found. */
374 +
375 +       return SLP_TRUE;
376 +}
377 +
378 +int print_service_list(void)
379 +{
380 +       SLPError err;
381 +       SLPError callbackerr;
382 +       SLPHandle hslp;
383 +
384 +       err = SLPOpen("en",SLP_FALSE,&hslp);
385 +       if (err != SLP_OK) {
386 +               rprintf(FERROR, "Error opening slp handle %i\n", err);
387 +               return err;
388 +       }
389 +
390 +       SLPFindSrvs(hslp, "rsync",
391 +                   0, /* use configured scopes */
392 +                   0, /* no attr filter        */
393 +                   getSLPSrvURLCallback, &callbackerr);
394 +
395 +       /* Now that we're done using slp, close the slp handle */
396 +       SLPClose(hslp);
397 +
398 +       return 0;
399 +}
400 diff --git a/srvreg.c b/srvreg.c
401 new file mode 100644
402 --- /dev/null
403 +++ b/srvreg.c
404 @@ -0,0 +1,128 @@
405 +/* -*- c-file-style: "linux"; -*-
406 +
407 +   Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
408 +
409 +   This program is free software; you can redistribute it and/or modify
410 +   it under the terms of the GNU General Public License as published by
411 +   the Free Software Foundation; either version 2 of the License, or
412 +   (at your option) any later version.
413 +
414 +   This program is distributed in the hope that it will be useful,
415 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
416 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
417 +   GNU General Public License for more details.
418 +
419 +   You should have received a copy of the GNU General Public License
420 +   along with this program; if not, write to the Free Software
421 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
422 +*/
423 +
424 +/* This file implements the service registration functionality */
425 +
426 +/* Basically, it uses normal Service Location Protocol API */
427 +
428 +#include "rsync.h"
429 +#include "slp.h"
430 +#include "netdb.h"
431 +
432 +extern int rsync_port;
433 +
434 +static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
435 +{
436 +       /* return the error code in the cookie */
437 +       *(SLPError*)cookie = errcode;
438 +
439 +       /* You could do something else here like print out
440 +        * the errcode, etc.  Remember, as a general rule,
441 +        * do not try to do too much in a callback because
442 +        * it is being executed by the same thread that is
443 +        * reading slp packets from the wire. */
444 +}
445 +
446 +int register_services(void)
447 +{
448 +       SLPError err, callbackerr;
449 +       SLPHandle hslp;
450 +       int n;
451 +       int i;
452 +       char srv[120];
453 +       char attr[120];
454 +       char localhost[256];
455 +       extern char *config_file;
456 +       short timeout;
457 +       struct addrinfo aih, *ai = 0;
458 +
459 +       if (!lp_load(config_file, 0)) {
460 +               exit_cleanup(RERR_SYNTAX);
461 +       }
462 +
463 +       n = lp_num_modules();
464 +
465 +       if (0 == lp_slp_refresh())
466 +               timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
467 +       else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
468 +               timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
469 +       else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
470 +               timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
471 +       else
472 +               timeout = lp_slp_refresh();
473 +
474 +       rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
475 +       err = SLPOpen("en",SLP_FALSE,&hslp);
476 +       if (err != SLP_OK) {
477 +               rprintf(FINFO, "Error opening slp handle %i\n",err);
478 +               return err;
479 +       }
480 +       if (gethostname(localhost, sizeof localhost)) {
481 +              rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
482 +              return err;
483 +       }
484 +       memset(&aih, 0, sizeof aih);
485 +       aih.ai_family = PF_UNSPEC;
486 +       aih.ai_flags = AI_CANONNAME;
487 +       if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
488 +              rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
489 +              return err;
490 +       }
491 +       /* Register each service with SLP */
492 +       for (i = 0; i < n; i++) {
493 +               if (!lp_list(i))
494 +                       continue;
495 +
496 +               snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
497 +                        ai->ai_canonname,
498 +                        rsync_port,
499 +                        lp_name(i));
500 +               rprintf(FINFO, "    %s\n", srv);
501 +               if (lp_comment(i)) {
502 +                       snprintf(attr, sizeof attr, "(comment=%s)",
503 +                                lp_comment(i));
504 +               }
505 +               err = SLPReg(hslp,
506 +                            srv, /* service to register */
507 +                            timeout,
508 +                            0,  /* this is ignored */
509 +                            attr, /* attributes */
510 +                            SLP_TRUE, /* new registration - don't change this */
511 +                            slp_callback, /* callback */
512 +                            &callbackerr);
513 +
514 +               /* err may contain an error code that occurred as the slp library
515 +                * _prepared_ to make the call. */
516 +               if (err != SLP_OK || callbackerr != SLP_OK)
517 +                       rprintf(FINFO, "Error registering service with slp %i\n", err);
518 +
519 +               /* callbackerr may contain an error code (that was assigned through
520 +                * the callback cookie) that occurred as slp packets were sent on
521 +                * the wire. */
522 +               if (callbackerr != SLP_OK)
523 +                       rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
524 +       }
525 +
526 +       /* Now that we're done using slp, close the slp handle */
527 +       freeaddrinfo(ai);
528 +       SLPClose(hslp);
529 +
530 +       /* refresh is done in main select loop */
531 +       return 0;
532 +}