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