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