2 * Copyright (c) 2003-2006, Maxime Henrion <mux@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/select.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
70 static void killer_start(struct killer *, struct mux *);
71 static void *killer_run(void *);
72 static void killer_stop(struct killer *);
74 static int proto_waitconnect(int);
75 static int proto_greet(struct config *);
76 static int proto_negproto(struct config *);
77 static int proto_login(struct config *);
78 static int proto_fileattr(struct config *);
79 static int proto_xchgcoll(struct config *);
80 static struct mux *proto_mux(struct config *);
82 static int proto_escape(struct stream *, const char *);
83 static void proto_unescape(char *);
86 proto_waitconnect(int s)
90 int error, rv, soerror;
96 rv = select(s + 1, &readfd, NULL, NULL, NULL);
97 } while (rv == -1 && errno == EINTR);
100 /* Check that the connection was really successful. */
101 len = sizeof(soerror);
102 error = getsockopt(s, SOL_SOCKET, SO_ERROR, &soerror, &len);
104 /* We have no choice but faking an error here. */
105 errno = ECONNREFUSED;
115 /* Connect to the CVSup server. */
117 proto_connect(struct config *config, int family, uint16_t port)
119 char addrbuf[NI_MAXHOST];
120 /* Enough to hold sizeof("cvsup") or any port number. */
122 struct addrinfo *res, *ai, hints;
127 snprintf(servname, sizeof(servname), "%d", port);
129 strncpy(servname, "cvsup", sizeof(servname) - 1);
130 servname[sizeof(servname) - 1] = '\0';
132 memset(&hints, 0, sizeof(hints));
133 hints.ai_family = family;
134 hints.ai_socktype = SOCK_STREAM;
135 error = getaddrinfo(config->host, servname, &hints, &res);
137 * Try with the hardcoded port number for OSes that don't
138 * have cvsup defined in the /etc/services file.
140 if (error == EAI_SERVICE) {
141 strncpy(servname, "5999", sizeof(servname) - 1);
142 servname[sizeof(servname) - 1] = '\0';
143 error = getaddrinfo(config->host, servname, &hints, &res);
146 lprintf(0, "Name lookup failure for \"%s\": %s\n", config->host,
147 gai_strerror(error));
148 return (STATUS_TRANSIENTFAILURE);
150 for (ai = res; ai != NULL; ai = ai->ai_next) {
151 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
154 if (config->laddr != NULL) {
156 (void)setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
158 error = bind(s, config->laddr,
162 error = connect(s, ai->ai_addr, ai->ai_addrlen);
163 if (error && errno == EINTR)
164 error = proto_waitconnect(s);
169 (void)getnameinfo(ai->ai_addr, ai->ai_addrlen, addrbuf,
170 sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
171 if (s == -1 || error) {
172 lprintf(0, "Cannot connect to %s: %s\n", addrbuf,
176 lprintf(1, "Connected to %s\n", addrbuf);
179 return (STATUS_SUCCESS);
182 return (STATUS_TRANSIENTFAILURE);
185 /* Greet the server. */
187 proto_greet(struct config *config)
189 char *line, *cmd, *msg, *swver;
193 line = stream_getln(s, NULL);
194 cmd = proto_get_ascii(&line);
197 if (strcmp(cmd, "OK") == 0) {
198 (void)proto_get_ascii(&line); /* major number */
199 (void)proto_get_ascii(&line); /* minor number */
200 swver = proto_get_ascii(&line);
201 } else if (strcmp(cmd, "!") == 0) {
202 msg = proto_get_rest(&line);
205 lprintf(-1, "Rejected by server: %s\n", msg);
206 return (STATUS_TRANSIENTFAILURE);
209 lprintf(2, "Server software version: %s\n",
210 swver != NULL ? swver : ".");
211 return (STATUS_SUCCESS);
213 lprintf(-1, "Invalid greeting from server\n");
214 return (STATUS_FAILURE);
217 /* Negotiate protocol version with the server. */
219 proto_negproto(struct config *config)
222 char *cmd, *line, *msg;
226 proto_printf(s, "PROTO %d %d %s\n", PROTO_MAJ, PROTO_MIN, PROTO_SWVER);
228 line = stream_getln(s, NULL);
229 cmd = proto_get_ascii(&line);
230 if (cmd == NULL || line == NULL)
232 if (strcmp(cmd, "!") == 0) {
233 msg = proto_get_rest(&line);
234 lprintf(-1, "Protocol negotiation failed: %s\n", msg);
236 } else if (strcmp(cmd, "PROTO") != 0)
238 error = proto_get_int(&line, &maj, 10);
240 error = proto_get_int(&line, &min, 10);
243 if (maj != PROTO_MAJ || min != PROTO_MIN) {
244 lprintf(-1, "Server protocol version %d.%d not supported "
245 "by client\n", maj, min);
246 return (STATUS_FAILURE);
248 return (STATUS_SUCCESS);
250 lprintf(-1, "Invalid PROTO command from server\n");
251 return (STATUS_FAILURE);
255 proto_login(struct config *config)
258 char hostbuf[MAXHOSTNAMELEN];
259 char *line, *login, *host, *cmd, *realm, *challenge, *msg;
263 error = gethostname(hostbuf, sizeof(hostbuf));
264 hostbuf[sizeof(hostbuf) - 1] = '\0';
270 proto_printf(s, "USER %s %s\n", login != NULL ? login : "?",
271 host != NULL ? host : "?");
273 line = stream_getln(s, NULL);
274 cmd = proto_get_ascii(&line);
275 realm = proto_get_ascii(&line);
276 challenge = proto_get_ascii(&line);
277 if (challenge == NULL || line != NULL)
279 if (strcmp(realm, ".") != 0 || strcmp(challenge, ".") != 0) {
280 lprintf(-1, "Authentication required by the server and not "
281 "supported by client\n");
282 return (STATUS_FAILURE);
284 proto_printf(s, "AUTHMD5 . . .\n");
286 line = stream_getln(s, NULL);
287 cmd = proto_get_ascii(&line);
288 if (cmd == NULL || line == NULL)
290 if (strcmp(cmd, "OK") == 0)
291 return (STATUS_SUCCESS);
292 if (strcmp(cmd, "!") == 0) {
293 msg = proto_get_rest(&line);
296 lprintf(-1, "Server error: %s\n", msg);
297 return (STATUS_FAILURE);
300 lprintf(-1, "Invalid server reply to AUTHMD5\n");
301 return (STATUS_FAILURE);
305 * File attribute support negotiation.
308 proto_fileattr(struct config *config)
310 fattr_support_t support;
313 int error, i, n, attr;
316 lprintf(2, "Negotiating file attribute support\n");
317 proto_printf(s, "ATTR %d\n", FT_NUMBER);
318 for (i = 0; i < FT_NUMBER; i++)
319 proto_printf(s, "%x\n", fattr_supported(i));
320 proto_printf(s, ".\n");
322 line = stream_getln(s, NULL);
325 cmd = proto_get_ascii(&line);
326 error = proto_get_int(&line, &n, 10);
327 if (error || line != NULL || strcmp(cmd, "ATTR") != 0 || n > FT_NUMBER)
329 for (i = 0; i < n; i++) {
330 line = stream_getln(s, NULL);
333 error = proto_get_int(&line, &attr, 16);
336 support[i] = fattr_supported(i) & attr;
338 for (i = n; i < FT_NUMBER; i++)
340 line = stream_getln(s, NULL);
341 if (line == NULL || strcmp(line, ".") != 0)
343 memcpy(config->fasupport, support, sizeof(config->fasupport));
344 return (STATUS_SUCCESS);
346 lprintf(-1, "Protocol error negotiating attribute support\n");
347 return (STATUS_FAILURE);
351 * Exchange collection information.
354 proto_xchgcoll(struct config *config)
358 struct globtree *diraccept, *dirrefuse;
359 struct globtree *fileaccept, *filerefuse;
360 char *line, *cmd, *collname, *pat;
361 char *msg, *release, *ident, *rcskey, *prefix;
363 int error, flags, options;
366 lprintf(2, "Exchanging collection information\n");
367 STAILQ_FOREACH(coll, &config->colls, co_next) {
368 proto_printf(s, "COLL %s %s %o %d\n", coll->co_name,
369 coll->co_release, coll->co_umask, coll->co_options);
370 for (i = 0; i < pattlist_size(coll->co_accepts); i++) {
371 proto_printf(s, "ACC %s\n",
372 pattlist_get(coll->co_accepts, i));
374 for (i = 0; i < pattlist_size(coll->co_refusals); i++) {
375 proto_printf(s, "REF %s\n",
376 pattlist_get(coll->co_refusals, i));
378 proto_printf(s, ".\n");
380 proto_printf(s, ".\n");
383 STAILQ_FOREACH(coll, &config->colls, co_next) {
384 if (coll->co_options & CO_SKIP)
386 coll->co_norsync = globtree_false();
387 line = stream_getln(s, NULL);
390 cmd = proto_get_ascii(&line);
391 collname = proto_get_ascii(&line);
392 release = proto_get_ascii(&line);
393 error = proto_get_int(&line, &options, 10);
394 if (error || line != NULL)
396 if (strcmp(cmd, "COLL") != 0 ||
397 strcmp(collname, coll->co_name) != 0 ||
398 strcmp(release, coll->co_release) != 0)
401 (coll->co_options | (options & CO_SERVMAYSET)) &
402 ~(~options & CO_SERVMAYCLEAR);
403 while ((line = stream_getln(s, NULL)) != NULL) {
404 if (strcmp(line, ".") == 0)
406 cmd = proto_get_ascii(&line);
409 if (strcmp(cmd, "!") == 0) {
410 msg = proto_get_rest(&line);
413 lprintf(-1, "Server message: %s\n", msg);
414 } else if (strcmp(cmd, "PRFX") == 0) {
415 prefix = proto_get_ascii(&line);
416 if (prefix == NULL || line != NULL)
418 coll->co_cvsroot = xstrdup(prefix);
419 } else if (strcmp(cmd, "KEYALIAS") == 0) {
420 ident = proto_get_ascii(&line);
421 rcskey = proto_get_ascii(&line);
422 if (rcskey == NULL || line != NULL)
424 error = keyword_alias(coll->co_keyword, ident,
428 } else if (strcmp(cmd, "KEYON") == 0) {
429 ident = proto_get_ascii(&line);
430 if (ident == NULL || line != NULL)
432 error = keyword_enable(coll->co_keyword, ident);
435 } else if (strcmp(cmd, "KEYOFF") == 0) {
436 ident = proto_get_ascii(&line);
437 if (ident == NULL || line != NULL)
439 error = keyword_disable(coll->co_keyword,
443 } else if (strcmp(cmd, "NORS") == 0) {
444 pat = proto_get_ascii(&line);
445 if (pat == NULL || line != NULL)
447 coll->co_norsync = globtree_or(coll->co_norsync,
448 globtree_match(pat, FNM_PATHNAME));
449 } else if (strcmp(cmd, "RNORS") == 0) {
450 pat = proto_get_ascii(&line);
451 if (pat == NULL || line != NULL)
453 coll->co_norsync = globtree_or(coll->co_norsync,
454 globtree_match(pat, FNM_PATHNAME |
461 keyword_prepare(coll->co_keyword);
463 diraccept = globtree_true();
464 fileaccept = globtree_true();
465 dirrefuse = globtree_false();
466 filerefuse = globtree_false();
468 if (pattlist_size(coll->co_accepts) > 0) {
469 globtree_free(diraccept);
470 globtree_free(fileaccept);
471 diraccept = globtree_false();
472 fileaccept = globtree_false();
473 flags = FNM_PATHNAME | FNM_LEADING_DIR |
475 for (i = 0; i < pattlist_size(coll->co_accepts); i++) {
476 pat = pattlist_get(coll->co_accepts, i);
477 diraccept = globtree_or(diraccept,
478 globtree_match(pat, flags));
481 if (coll->co_options & CO_CHECKOUTMODE &&
482 (len == 0 || pat[len - 1] != '*')) {
483 /* We must modify the pattern so that it
484 refers to the RCS file, rather than
485 the checked-out file. */
486 xasprintf(&pat, "%s,v", pat);
487 fileaccept = globtree_or(fileaccept,
488 globtree_match(pat, flags));
491 fileaccept = globtree_or(fileaccept,
492 globtree_match(pat, flags));
497 for (i = 0; i < pattlist_size(coll->co_refusals); i++) {
498 pat = pattlist_get(coll->co_refusals, i);
499 dirrefuse = globtree_or(dirrefuse,
500 globtree_match(pat, 0));
502 if (coll->co_options & CO_CHECKOUTMODE &&
503 (len == 0 || pat[len - 1] != '*')) {
504 /* We must modify the pattern so that it refers
505 to the RCS file, rather than the checked-out
507 xasprintf(&pat, "%s,v", pat);
508 filerefuse = globtree_or(filerefuse,
509 globtree_match(pat, 0));
512 filerefuse = globtree_or(filerefuse,
513 globtree_match(pat, 0));
517 coll->co_dirfilter = globtree_and(diraccept,
518 globtree_not(dirrefuse));
519 coll->co_filefilter = globtree_and(fileaccept,
520 globtree_not(filerefuse));
522 /* At this point we don't need the pattern lists anymore. */
523 pattlist_free(coll->co_accepts);
524 pattlist_free(coll->co_refusals);
525 coll->co_accepts = NULL;
526 coll->co_refusals = NULL;
528 /* Set up a mask of file attributes that we don't want to sync
530 if (!(coll->co_options & CO_SETOWNER))
531 coll->co_attrignore |= FA_OWNER | FA_GROUP;
532 if (!(coll->co_options & CO_SETMODE))
533 coll->co_attrignore |= FA_MODE;
534 if (!(coll->co_options & CO_SETFLAGS))
535 coll->co_attrignore |= FA_FLAGS;
537 return (STATUS_SUCCESS);
539 lprintf(-1, "Protocol error during collection exchange\n");
540 return (STATUS_FAILURE);
544 proto_mux(struct config *config)
547 struct stream *s, *wr;
548 struct chan *chan0, *chan1;
552 lprintf(2, "Establishing multiplexed-mode data connection\n");
553 proto_printf(s, "MUX\n");
555 m = mux_open(config->socket, &chan0);
557 lprintf(-1, "Cannot open the multiplexer\n");
562 lprintf(-1, "ChannelMux.Listen failed: %s\n", strerror(errno));
566 wr = stream_open(chan0, NULL, (stream_writefn_t *)chan_write, NULL);
567 proto_printf(wr, "CHAN %d\n", id);
569 chan1 = chan_accept(m, id);
571 lprintf(-1, "ChannelMux.Accept failed: %s\n", strerror(errno));
575 config->chan0 = chan0;
576 config->chan1 = chan1;
581 * Initializes the connection to the CVSup server, that is handle
582 * the protocol negotiation, logging in, exchanging file attributes
583 * support and collections information, and finally run the update
587 proto_run(struct config *config)
589 struct thread_args lister_args;
590 struct thread_args detailer_args;
591 struct thread_args updater_args;
592 struct thread_args *args;
593 struct killer killer;
594 struct threads *workers;
599 * We pass NULL for the close() function because we'll reuse
600 * the socket after the stream is closed.
602 config->server = stream_open_fd(config->socket, stream_read_fd,
603 stream_write_fd, NULL);
604 status = proto_greet(config);
605 if (status == STATUS_SUCCESS)
606 status = proto_negproto(config);
607 if (status == STATUS_SUCCESS)
608 status = proto_login(config);
609 if (status == STATUS_SUCCESS)
610 status = proto_fileattr(config);
611 if (status == STATUS_SUCCESS)
612 status = proto_xchgcoll(config);
613 if (status != STATUS_SUCCESS)
616 /* Multi-threaded action starts here. */
617 m = proto_mux(config);
619 return (STATUS_FAILURE);
621 stream_close(config->server);
622 config->server = NULL;
623 config->fixups = fixups_new();
624 killer_start(&killer, m);
626 /* Start the worker threads. */
627 workers = threads_new();
629 args->config = config;
633 args->wr = stream_open(config->chan0,
634 NULL, (stream_writefn_t *)chan_write, NULL);
635 threads_create(workers, lister, args);
637 args = &detailer_args;
638 args->config = config;
641 args->rd = stream_open(config->chan0,
642 (stream_readfn_t *)chan_read, NULL, NULL);
643 args->wr = stream_open(config->chan1,
644 NULL, (stream_writefn_t *)chan_write, NULL);
645 threads_create(workers, detailer, args);
647 args = &updater_args;
648 args->config = config;
651 args->rd = stream_open(config->chan1,
652 (stream_readfn_t *)chan_read, NULL, NULL);
654 threads_create(workers, updater, args);
656 lprintf(2, "Running\n");
657 /* Wait for all the worker threads to finish. */
658 status = STATUS_SUCCESS;
659 for (i = 0; i < 3; i++) {
660 args = threads_wait(workers);
661 if (args->rd != NULL)
662 stream_close(args->rd);
663 if (args->wr != NULL)
664 stream_close(args->wr);
665 if (args->status != STATUS_SUCCESS) {
666 assert(args->errmsg != NULL);
667 if (status == STATUS_SUCCESS) {
668 status = args->status;
669 /* Shutdown the multiplexer to wake up all
670 the other threads. */
671 mux_shutdown(m, args->errmsg, status);
676 threads_free(workers);
677 if (status == STATUS_SUCCESS) {
678 lprintf(2, "Shutting down connection to server\n");
679 chan_close(config->chan0);
680 chan_close(config->chan1);
681 chan_wait(config->chan0);
682 chan_wait(config->chan1);
683 mux_shutdown(m, NULL, STATUS_SUCCESS);
685 killer_stop(&killer);
686 fixups_free(config->fixups);
687 status = mux_close(m);
688 if (status == STATUS_SUCCESS) {
689 lprintf(1, "Finished successfully\n");
690 } else if (status == STATUS_INTERRUPTED) {
691 lprintf(-1, "Interrupted\n");
692 if (killer.killedby != -1)
693 kill(getpid(), killer.killedby);
699 * Write a string into the stream, escaping characters as needed.
700 * Characters escaped:
709 proto_escape(struct stream *wr, const char *s)
715 /* Handle characters that need escaping. */
717 len = strcspn(s, " \t\r\n\\");
718 n = stream_write(wr, s, len);
724 n = stream_write(wr, "\\_", 2);
727 n = stream_write(wr, "\\t", 2);
730 n = stream_write(wr, "\\r", 2);
733 n = stream_write(wr, "\\n", 2);
736 n = stream_write(wr, "\\\\", 2);
747 * A simple printf() implementation specifically tailored for csup.
748 * List of the supported formats:
751 * %d or %i Print an int as decimal.
752 * %x Print an int as hexadecimal.
753 * %o Print an int as octal.
754 * %t Print a time_t as decimal.
755 * %s Print a char * escaping some characters as needed.
756 * %S Print a char * without escaping.
757 * %f Print an encoded struct fattr *.
758 * %F Print an encoded struct fattr *, specifying the supported
762 proto_printf(struct stream *wr, const char *format, ...)
764 fattr_support_t *support;
777 va_start(ap, format);
778 while ((cp = strchr(fmt, '%')) != NULL) {
780 n = stream_write(wr, fmt, cp - fmt);
789 rv = stream_printf(wr, "%c", c);
793 val = va_arg(ap, int);
794 rv = stream_printf(wr, "%d", val);
797 val = va_arg(ap, int);
798 rv = stream_printf(wr, "%x", val);
801 val = va_arg(ap, int);
802 rv = stream_printf(wr, "%o", val);
805 s = va_arg(ap, char *);
807 rv = stream_printf(wr, "%s", s);
810 s = va_arg(ap, char *);
812 rv = proto_escape(wr, s);
815 longval = (long long)va_arg(ap, time_t);
816 rv = stream_printf(wr, "%lld", longval);
819 fa = va_arg(ap, struct fattr *);
820 attr = fattr_encode(fa, NULL, 0);
821 rv = proto_escape(wr, attr);
825 fa = va_arg(ap, struct fattr *);
826 support = va_arg(ap, fattr_support_t *);
827 ignore = va_arg(ap, int);
828 attr = fattr_encode(fa, *support, ignore);
829 rv = proto_escape(wr, attr);
833 n = stream_write(wr, "%", 1);
843 rv = stream_printf(wr, "%s", fmt);
853 * Unescape the string, see proto_escape().
856 proto_unescape(char *s)
861 while ((cp = strchr(cp, '\\')) != NULL) {
882 while (*cp2 != '\0') {
890 * Get an ascii token in the string.
893 proto_get_ascii(char **s)
897 ret = strsep(s, " ");
900 /* Make sure we disallow 0-length fields. */
910 * Get the rest of the string.
913 proto_get_rest(char **s)
929 proto_get_int(char **s, int *val, int base)
934 cp = proto_get_ascii(s);
937 error = asciitoint(cp, val, base);
942 * Get a time_t token.
944 * Ideally, we would use an intmax_t and strtoimax() here, but strtoll()
945 * is more portable and 64bits should be enough for a timestamp.
948 proto_get_time(char **s, time_t *val)
953 cp = proto_get_ascii(s);
957 tmp = strtoll(cp, &end, 10);
958 if (errno || *end != '\0')
964 /* Start the killer thread. It is used to protect against some signals
965 during the multi-threaded run so that we can gracefully fail. */
967 killer_start(struct killer *k, struct mux *m)
973 sigemptyset(&k->sigset);
974 sigaddset(&k->sigset, SIGINT);
975 sigaddset(&k->sigset, SIGHUP);
976 sigaddset(&k->sigset, SIGTERM);
977 sigaddset(&k->sigset, SIGPIPE);
978 pthread_sigmask(SIG_BLOCK, &k->sigset, NULL);
979 error = pthread_create(&k->thread, NULL, killer_run, k);
981 err(1, "pthread_create");
984 /* The main loop of the killer thread. */
986 killer_run(void *arg)
993 error = sigwait(&k->sigset, &sig);
995 if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM) {
996 if (k->killedby == -1) {
998 /* Ensure we don't get canceled during the shutdown. */
999 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
1000 mux_shutdown(k->mux, "Cleaning up ...",
1001 STATUS_INTERRUPTED);
1002 pthread_setcancelstate(old, NULL);
1008 /* Stop the killer thread. */
1010 killer_stop(struct killer *k)
1015 error = pthread_cancel(k->thread);
1017 pthread_join(k->thread, &val);
1018 assert(val == PTHREAD_CANCELED);
1019 pthread_sigmask(SIG_UNBLOCK, &k->sigset, NULL);