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