Matt's transliterate patch.
[rsync/rsync-patches.git] / slp.diff
CommitLineData
9978080e
WD
1This adds Service Location Protocol support.
2
03019e41 3To use this patch, run these commands for a successful build:
9978080e 4
03019e41 5 patch -p1 <patches/slp.diff
27e96866 6 ./prepare-source
9978080e 7 ./configure --enable-slp
9978080e
WD
8 make
9
6bc8e4ec
WD
10TODO: the configure changes should abort if the user requests --enable-slp
11and we can't honor that request.
12
9a7eef96
WD
13--- old/Makefile.in
14+++ new/Makefile.in
03019e41 15@@ -13,6 +13,8 @@ CFLAGS=@CFLAGS@
9978080e
WD
16 CPPFLAGS=@CPPFLAGS@
17 EXEEXT=@EXEEXT@
18 LDFLAGS=@LDFLAGS@
19+LIBSLP=@LIBSLP@
20+SLPOBJ=@SLPOBJ@
21
22 INSTALLCMD=@INSTALL@
23 INSTALLMAN=@INSTALL@
f2376a08 24@@ -36,7 +38,7 @@ OBJS1=flist.o rsync.o generator.o receiv
790ba11a 25 OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o hashtable.o \
5795bf59 26 fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
9978080e
WD
27 OBJS3=progress.o pipe.o
28-DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
29+DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o $(SLPOBJ)
30 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
31 popt/popthelp.o popt/poptparse.o
32 OBJS=$(OBJS1) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) $(ZLIBOBJ) @BUILD_POPT@
58b399b9 33@@ -72,7 +74,7 @@ install-strip:
9978080e
WD
34 $(MAKE) INSTALL_STRIP='-s' install
35
36 rsync$(EXEEXT): $(OBJS)
37- $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
38+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(LIBSLP)
39
40 $(OBJS): $(HEADERS)
41
9a7eef96
WD
42--- old/clientserver.c
43+++ new/clientserver.c
76c553f7 44@@ -952,6 +952,13 @@ int daemon_main(void)
9978080e
WD
45 * address too. In fact, why not just do inet_ntop on the
46 * local address??? */
47
48+#ifdef HAVE_LIBSLP
49+ if (register_services()) {
50+ rprintf(FINFO,
51+ "Couldn't register with service discovery protocol, continuing anyway\n");
52+ }
53+#endif
54+
9f085fa3
WD
55 start_accept_loop(rsync_port, start_daemon);
56 return -1;
57 }
9a7eef96
WD
58--- old/configure.in
59+++ new/configure.in
76c553f7 60@@ -625,6 +625,29 @@ if test $rsync_cv_can_hardlink_special =
1898c899 61 AC_DEFINE(CAN_HARDLINK_SPECIAL, 1, [Define to 1 if link() can hard-link special files.])
9978080e
WD
62 fi
63
64+AC_ARG_ENABLE(slp, [ --disable-slp turn off SLP support, defaults to on])
65+AC_ARG_WITH(openslp-libs, [ --with-openslp-libs set directory for OpenSLP library],
66+ LDFLAGS="-L$withval $LDFLAGS"
67+ DSOFLAGS="-L$withval $DSOFLAGS",)
68+AC_ARG_WITH(openslp-includes, [ --with-openslp-includes set directory for OpenSLP includes],
69+ CFLAGS="-I$withval $CFLAGS"
70+ CXXFLAGS="-I$withval $CXXFLAGS"
71+ CPPFLAGS="-I$withval $CPPFLAGS",)
72+
73+LIBSLP=""
74+SLPOBJ=""
75+
76+if test x$enable_slp != xno; then
77+ AC_CHECK_HEADER(slp.h,
78+ AC_CHECK_LIB(slp, SLPOpen,
79+ AC_DEFINE(HAVE_LIBSLP, 1, [Define to 1 for SLP support])
80+ SLPOBJ="srvreg.o srvloc.o"
81+ LIBSLP="-lslp"))
82+fi
83+
84+AC_SUBST(LIBSLP)
85+AC_SUBST(SLPOBJ)
86+
87 AC_CACHE_CHECK([for working socketpair],rsync_cv_HAVE_SOCKETPAIR,[
88 AC_TRY_RUN([
89 #include <sys/types.h>
9a7eef96
WD
90--- old/loadparm.c
91+++ new/loadparm.c
9f085fa3 92@@ -111,6 +111,9 @@ typedef struct
9978080e
WD
93 char *socket_options;
94
95 int rsync_port;
6bc8e4ec 96+#ifdef HAVE_LIBSLP
9978080e 97+ int slp_refresh;
6bc8e4ec 98+#endif
9978080e
WD
99 } global;
100
0ffcefad 101 static global Globals;
9f085fa3 102@@ -296,6 +299,9 @@ static struct parm_struct parm_table[] =
9978080e
WD
103 {"motd file", P_STRING, P_GLOBAL,&Globals.motd_file, NULL,0},
104 {"pid file", P_STRING, P_GLOBAL,&Globals.pid_file, NULL,0},
105 {"port", P_INTEGER,P_GLOBAL,&Globals.rsync_port, NULL,0},
6bc8e4ec 106+#ifdef HAVE_LIBSLP
9978080e 107+ {"slp refresh", P_INTEGER,P_GLOBAL,&Globals.slp_refresh, NULL,0},
6bc8e4ec 108+#endif
9978080e 109 {"socket options", P_STRING, P_GLOBAL,&Globals.socket_options, NULL,0},
9978080e 110
0ffcefad 111 {"auth users", P_STRING, P_LOCAL, &sDefault.auth_users, NULL,0},
9f085fa3 112@@ -387,6 +393,9 @@ FN_GLOBAL_STRING(lp_pid_file, &Globals.p
9978080e
WD
113 FN_GLOBAL_STRING(lp_socket_options, &Globals.socket_options)
114
115 FN_GLOBAL_INTEGER(lp_rsync_port, &Globals.rsync_port)
6bc8e4ec 116+#ifdef HAVE_LIBSLP
9978080e 117+FN_GLOBAL_INTEGER(lp_slp_refresh, &Globals.slp_refresh)
6bc8e4ec 118+#endif
9978080e
WD
119
120 FN_LOCAL_STRING(lp_auth_users, auth_users)
0ffcefad 121 FN_LOCAL_STRING(lp_comment, comment)
9a7eef96
WD
122--- old/main.c
123+++ new/main.c
76c553f7 124@@ -1139,6 +1139,18 @@ static int start_client(int argc, char *
3731f97b 125
9978080e 126 if (!read_batch) { /* for read_batch, NO source is specified */
790ba11a 127 char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
9978080e
WD
128+
129+ if (shell_machine && !shell_machine[0]) {
130+#ifdef HAVE_LIBSLP
131+ /* User entered just rsync:// URI */
132+ print_service_list();
133+ exit_cleanup(0);
134+#else /* No SLP, die here */
135+ rprintf(FINFO, "No SLP support, cannot browse\n");
136+ exit_cleanup(RERR_SYNTAX);
137+#endif
138+ }
139+
790ba11a 140 if (path) { /* source is remote */
9f085fa3
WD
141 char *dummy_host;
142 int dummy_port = 0;
9a7eef96
WD
143--- old/options.c
144+++ new/options.c
9f085fa3 145@@ -221,6 +221,7 @@ static void print_rsync_version(enum log
9978080e 146 char const *links = "no ";
58b399b9 147 char const *iconv = "no ";
9978080e
WD
148 char const *ipv6 = "no ";
149+ char const *slp = "no ";
150 STRUCT_STAT *dumstat;
151
ac2da598 152 #if SUBPROTOCOL_VERSION != 0
9f085fa3 153@@ -250,6 +251,9 @@ static void print_rsync_version(enum log
58b399b9
WD
154 #ifdef ICONV_OPTION
155 iconv = "";
9978080e 156 #endif
9978080e
WD
157+#if HAVE_LIBSLP
158+ slp = "";
159+#endif
ac2da598
WD
160
161 rprintf(f, "%s version %s protocol version %d%s\n",
162 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
9f085fa3 163@@ -263,8 +267,8 @@ static void print_rsync_version(enum log
5e3c6c93
WD
164 (int)(sizeof (int64) * 8));
165 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
166 got_socketpair, hardlinks, links, ipv6, have_inplace);
58b399b9
WD
167- rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv\n",
168- have_inplace, acls, xattrs, iconv);
169+ rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %sSLP\n",
170+ have_inplace, acls, xattrs, iconv, slp);
5e3c6c93 171
9978080e 172 #ifdef MAINTAINER_MODE
5e3c6c93 173 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
9a7eef96
WD
174--- old/rsync.h
175+++ new/rsync.h
76c553f7 176@@ -183,6 +183,10 @@
9978080e
WD
177 #define SIGNIFICANT_ITEM_FLAGS (~(\
178 ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
179
180+/* this is the minimum we'll use, irrespective of config setting */
181+/* definately don't set to less than about 30 seconds */
182+#define SLP_MIN_TIMEOUT 120
76c553f7
WD
183+
184 #define CFN_KEEP_LEADING_DOT_DIR (1<<0)
185 #define CFN_KEEP_TRAILING_SLASH (1<<1)
186 #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
9a7eef96
WD
187--- old/rsync.yo
188+++ new/rsync.yo
76c553f7 189@@ -144,7 +144,12 @@ particular rsync daemon by leaving off t
9978080e
WD
190
191 quote(tt(rsync somehost.mydomain.com::))
192
193-See the following section for more details.
194+And, if Service Location Protocol is available, the following will list the
195+available rsync servers:
196+
197+quote(tt(rsync rsync://))
198+
199+See the following section for even more usage details.
200
201 manpagesection(ADVANCED USAGE)
202
9a7eef96
WD
203--- old/rsyncd.conf
204+++ new/rsyncd.conf
9978080e
WD
205@@ -0,0 +1,3 @@
206+
207+slp refresh = 300
208+
9a7eef96
WD
209--- old/rsyncd.conf.yo
210+++ new/rsyncd.conf.yo
76c553f7 211@@ -104,6 +104,15 @@ details on some of the options you may b
9978080e
WD
212 special socket options are set. These settings are superseded by the
213 bf(--sockopts) command-line option.
214
215+dit(bf(slp refresh)) This option is used to determine how long service
216+advertisements are valid (measured in seconds), and is only applicable if
217+you have Service Location Protocol support compiled in. If this option is
218+not set or is set to zero, then service advertisements never time out. If
219+this is set to less than 120 seconds, then 120 seconds is used. If it is
220+set to more than 65535, then 65535 is used (which is a limitation of SLP).
221+Using 3600 (one hour) is a good number if you tend to change your
222+configuration.
223+
224 enddit()
225
226
76c553f7 227@@ -566,6 +575,7 @@ use chroot = no
9978080e
WD
228 max connections = 4
229 syslog facility = local5
230 pid file = /var/run/rsyncd.pid
231+slp refresh = 3600
232
233 [ftp]
234 path = /var/ftp/pub
9a7eef96
WD
235--- old/socket.c
236+++ new/socket.c
9f085fa3 237@@ -502,6 +502,16 @@ void start_accept_loop(int port, int (*f
9978080e
WD
238 {
239 fd_set deffds;
240 int *sp, maxfd, i;
6bc8e4ec 241+#ifdef HAVE_LIBSLP
9978080e
WD
242+ time_t next_slp_refresh;
243+ short slp_timeout = lp_slp_refresh();
244+ if (slp_timeout) {
245+ if (slp_timeout < SLP_MIN_TIMEOUT)
246+ slp_timeout = SLP_MIN_TIMEOUT;
247+ /* re-register before slp times out */
248+ slp_timeout -= 15;
249+ }
6bc8e4ec 250+#endif
9978080e 251
27e96866 252 #ifdef HAVE_SIGACTION
9978080e 253 sigact.sa_flags = SA_NOCLDSTOP;
9f085fa3 254@@ -530,14 +540,25 @@ void start_accept_loop(int port, int (*f
9978080e
WD
255 maxfd = sp[i];
256 }
257
6bc8e4ec 258+#ifdef HAVE_LIBSLP
9978080e 259+ next_slp_refresh = time(NULL) + slp_timeout;
6bc8e4ec 260+#endif
9978080e
WD
261+
262 /* now accept incoming connections - forking a new process
263 * for each incoming connection */
264 while (1) {
265 fd_set fds;
266 pid_t pid;
267 int fd;
268+ int sel_ret;
9978080e
WD
269 struct sockaddr_storage addr;
270 socklen_t addrlen = sizeof addr;
6bc8e4ec
WD
271+#ifdef HAVE_LIBSLP
272+ struct timeval slp_tv;
273+
9978080e
WD
274+ slp_tv.tv_sec = 10;
275+ slp_tv.tv_usec = 0;
6bc8e4ec 276+#endif
9978080e
WD
277
278 /* close log file before the potentially very long select so
279 * file can be trimmed by another process instead of growing
9f085fa3 280@@ -549,8 +570,18 @@ void start_accept_loop(int port, int (*f
9978080e
WD
281 #else
282 fds = deffds;
283 #endif
284-
285- if (select(maxfd + 1, &fds, NULL, NULL, NULL) != 1)
6bc8e4ec
WD
286+#ifdef HAVE_LIBSLP
287+ sel_ret = select(maxfd + 1, &fds, NULL, NULL,
288+ slp_timeout ? &slp_tv : NULL);
9978080e
WD
289+ if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
290+ rprintf(FINFO, "Service registration expired, refreshing it\n");
291+ register_services();
292+ next_slp_refresh = time(NULL) + slp_timeout;
293+ }
6bc8e4ec
WD
294+#else
295+ sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
296+#endif
9978080e
WD
297+ if (sel_ret != 1)
298 continue;
299
300 for (i = 0, fd = -1; sp[i] >= 0; i++) {
9a7eef96
WD
301--- old/srvloc.c
302+++ new/srvloc.c
6bc8e4ec 303@@ -0,0 +1,103 @@
9978080e
WD
304+/* -*- c-file-style: "linux"; -*-
305+
306+ Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
307+
308+ This program is free software; you can redistribute it and/or modify
309+ it under the terms of the GNU General Public License as published by
310+ the Free Software Foundation; either version 2 of the License, or
311+ (at your option) any later version.
312+
313+ This program is distributed in the hope that it will be useful,
314+ but WITHOUT ANY WARRANTY; without even the implied warranty of
315+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
316+ GNU General Public License for more details.
317+
318+ You should have received a copy of the GNU General Public License
319+ along with this program; if not, write to the Free Software
320+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
321+*/
322+
323+/* This file implements the service location functionality */
324+/* Basically, it uses normal Service Location Protocol API */
325+
326+/* It is really a cheap hack - just to show how it might work
327+ in a real application.
328+*/
329+
330+#include "rsync.h"
331+
332+#include <slp.h>
333+#include <stdio.h>
334+#include <string.h>
335+
336+/* This one just prints out the attributes */
337+static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
338+ SLPError errcode, UNUSED(void *cookie))
339+{
340+ char *cleanstr;
341+
342+ if (errcode == SLP_OK) {
6bc8e4ec 343+ if (!strcmp(attrlist, "(comment=)"))
9978080e 344+ rprintf(FINFO, "\t(No description)\n");
6bc8e4ec 345+ else {
9978080e
WD
346+ cleanstr = strrchr(attrlist, ')') ;
347+ *cleanstr = ' '; /* remove last ')' */
348+ rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
349+ }
350+ }
351+ return SLP_FALSE;
352+}
353+
6bc8e4ec
WD
354+static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
355+ const char *srvurl, UNUSED(unsigned short lifetime),
356+ SLPError errcode, void *cookie)
9978080e
WD
357+{
358+ SLPError result;
359+ SLPHandle attrhslp;
360+
361+ if (errcode == SLP_OK) {
362+ /* chop service: off the front */
363+ rprintf(FINFO, " %s ", (strchr(srvurl, ':') + 1));
364+ /* check for any attributes */
365+ if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
366+ result = SLPFindAttrs(attrhslp, srvurl,
367+ "", /* return all attributes */
368+ "", /* use configured scopes */
369+ getAttrCallback, NULL);
370+ if (result != SLP_OK) {
371+ rprintf(FERROR, "errorcode: %i\n",result);
372+ }
373+ SLPClose(attrhslp);
374+ }
375+ *(SLPError*)cookie = SLP_OK;
6bc8e4ec 376+ } else
9978080e 377+ *(SLPError*)cookie = errcode;
9978080e
WD
378+
379+ /* Return SLP_TRUE because we want to be called again
380+ * if more services were found. */
381+
382+ return SLP_TRUE;
383+}
384+
385+int print_service_list(void)
386+{
387+ SLPError err;
388+ SLPError callbackerr;
389+ SLPHandle hslp;
390+
391+ err = SLPOpen("en",SLP_FALSE,&hslp);
392+ if (err != SLP_OK) {
393+ rprintf(FERROR, "Error opening slp handle %i\n", err);
394+ return err;
395+ }
396+
397+ SLPFindSrvs(hslp, "rsync",
398+ 0, /* use configured scopes */
399+ 0, /* no attr filter */
400+ getSLPSrvURLCallback, &callbackerr);
401+
402+ /* Now that we're done using slp, close the slp handle */
403+ SLPClose(hslp);
404+
405+ return 0;
406+}
9a7eef96
WD
407--- old/srvreg.c
408+++ new/srvreg.c
9978080e
WD
409@@ -0,0 +1,128 @@
410+/* -*- c-file-style: "linux"; -*-
411+
412+ Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
413+
414+ This program is free software; you can redistribute it and/or modify
415+ it under the terms of the GNU General Public License as published by
416+ the Free Software Foundation; either version 2 of the License, or
417+ (at your option) any later version.
418+
419+ This program is distributed in the hope that it will be useful,
420+ but WITHOUT ANY WARRANTY; without even the implied warranty of
421+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
422+ GNU General Public License for more details.
423+
424+ You should have received a copy of the GNU General Public License
425+ along with this program; if not, write to the Free Software
426+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
427+*/
428+
429+/* This file implements the service registration functionality */
430+
431+/* Basically, it uses normal Service Location Protocol API */
432+
433+#include "rsync.h"
434+#include "slp.h"
435+#include "netdb.h"
436+
437+extern int rsync_port;
438+
439+static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
440+{
441+ /* return the error code in the cookie */
442+ *(SLPError*)cookie = errcode;
443+
444+ /* You could do something else here like print out
445+ * the errcode, etc. Remember, as a general rule,
446+ * do not try to do too much in a callback because
447+ * it is being executed by the same thread that is
448+ * reading slp packets from the wire. */
449+}
450+
451+int register_services(void)
452+{
453+ SLPError err, callbackerr;
454+ SLPHandle hslp;
455+ int n;
456+ int i;
457+ char srv[120];
458+ char attr[120];
459+ char localhost[256];
460+ extern char *config_file;
461+ short timeout;
462+ struct addrinfo aih, *ai = 0;
463+
464+ if (!lp_load(config_file, 0)) {
465+ exit_cleanup(RERR_SYNTAX);
466+ }
467+
468+ n = lp_numservices();
469+
470+ if (0 == lp_slp_refresh())
471+ timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
472+ else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
473+ timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
474+ else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
475+ timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
476+ else
477+ timeout = lp_slp_refresh();
478+
479+ rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
480+ err = SLPOpen("en",SLP_FALSE,&hslp);
481+ if (err != SLP_OK) {
482+ rprintf(FINFO, "Error opening slp handle %i\n",err);
483+ return err;
484+ }
485+ if (gethostname(localhost, sizeof localhost)) {
486+ rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
487+ return err;
488+ }
489+ memset(&aih, 0, sizeof aih);
490+ aih.ai_family = PF_UNSPEC;
491+ aih.ai_flags = AI_CANONNAME;
492+ if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
493+ rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
494+ return err;
495+ }
496+ /* Register each service with SLP */
497+ for (i = 0; i < n; i++) {
498+ if (!lp_list(i))
499+ continue;
500+
501+ snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
502+ ai->ai_canonname,
503+ rsync_port,
504+ lp_name(i));
505+ rprintf(FINFO, " %s\n", srv);
506+ if (lp_comment(i)) {
507+ snprintf(attr, sizeof attr, "(comment=%s)",
508+ lp_comment(i));
509+ }
510+ err = SLPReg(hslp,
511+ srv, /* service to register */
512+ timeout,
513+ 0, /* this is ignored */
514+ attr, /* attributes */
515+ SLP_TRUE, /* new registration - don't change this */
516+ slp_callback, /* callback */
517+ &callbackerr);
518+
519+ /* err may contain an error code that occurred as the slp library
520+ * _prepared_ to make the call. */
521+ if (err != SLP_OK || callbackerr != SLP_OK)
522+ rprintf(FINFO, "Error registering service with slp %i\n", err);
523+
524+ /* callbackerr may contain an error code (that was assigned through
525+ * the callback cookie) that occurred as slp packets were sent on
526+ * the wire. */
527+ if (callbackerr != SLP_OK)
528+ rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
529+ }
530+
531+ /* Now that we're done using slp, close the slp handle */
532+ freeaddrinfo(ai);
533+ SLPClose(hslp);
534+
535+ /* refresh is done in main select loop */
536+ return 0;
537+}