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