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