1 /* $OpenBSD: client.c,v 1.125 2015/11/05 09:48:21 nicm Exp $ */ 2 /* 3 * Copyright (c) 2006 Joris Vink <joris@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/types.h> 19 #include <sys/dirent.h> 20 #include <sys/stat.h> 21 #include <sys/time.h> 22 23 #include <errno.h> 24 #include <fcntl.h> 25 #include <libgen.h> 26 #include <limits.h> 27 #include <pwd.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <time.h> 31 #include <unistd.h> 32 33 #include "cvs.h" 34 #include "remote.h" 35 36 struct cvs_req cvs_requests[] = { 37 /* this is what our client will use, the server should support it */ 38 { "Root", 1, cvs_server_root, REQ_NEEDED }, 39 { "Valid-responses", 1, cvs_server_validresp, REQ_NEEDED }, 40 { "valid-requests", 1, cvs_server_validreq, REQ_NEEDED }, 41 { "Directory", 0, cvs_server_directory, REQ_NEEDED }, 42 { "Static-directory", 0, cvs_server_static_directory, 43 REQ_NEEDED | REQ_NEEDDIR }, 44 { "Sticky", 0, cvs_server_sticky, 45 REQ_NEEDED | REQ_NEEDDIR }, 46 { "Entry", 0, cvs_server_entry, 47 REQ_NEEDED | REQ_NEEDDIR }, 48 { "Modified", 0, cvs_server_modified, 49 REQ_NEEDED | REQ_NEEDDIR }, 50 { "UseUnchanged", 0, cvs_server_useunchanged, REQ_NEEDED }, 51 { "Unchanged", 0, cvs_server_unchanged, 52 REQ_NEEDED | REQ_NEEDDIR }, 53 { "Questionable", 0, cvs_server_questionable, REQ_NEEDED }, 54 { "Argument", 0, cvs_server_argument, REQ_NEEDED }, 55 { "Argumentx", 0, cvs_server_argumentx, REQ_NEEDED }, 56 { "Global_option", 0, cvs_server_globalopt, REQ_NEEDED }, 57 { "Set", 0, cvs_server_set, REQ_NEEDED }, 58 { "expand-modules", 0, cvs_server_exp_modules, 0 }, 59 60 /* 61 * used to tell the server what is going on in our 62 * working copy, unsupported until we are told otherwise 63 */ 64 { "Max-dotdot", 0, NULL, 0 }, 65 { "Checkin-prog", 0, NULL, 0 }, 66 { "Update-prog", 0, NULL, 0 }, 67 { "Kopt", 0, NULL, 0 }, 68 { "Checkin-time", 0, NULL, 0 }, 69 { "Is-modified", 0, NULL, 0 }, 70 { "Notify", 0, NULL, 0 }, 71 { "Case", 0, NULL, 0 }, 72 { "Gzip-stream", 0, NULL, 0 }, 73 { "wrapper-sendme-rcsOptions", 0, NULL, 0 }, 74 { "Kerberos-encrypt", 0, NULL, 0 }, 75 { "Gssapi-encrypt", 0, NULL, 0 }, 76 { "Gssapi-authenticate", 0, NULL, 0 }, 77 78 /* commands that might be supported */ 79 { "ci", 0, cvs_server_commit, REQ_NEEDDIR }, 80 { "co", 0, cvs_server_checkout, REQ_NEEDDIR }, 81 { "update", 0, cvs_server_update, REQ_NEEDDIR }, 82 { "diff", 0, cvs_server_diff, REQ_NEEDDIR }, 83 { "log", 0, cvs_server_log, REQ_NEEDDIR }, 84 { "rlog", 0, cvs_server_rlog, 0 }, 85 { "add", 0, cvs_server_add, REQ_NEEDDIR }, 86 { "remove", 0, cvs_server_remove, REQ_NEEDDIR }, 87 { "update-patches", 0, cvs_server_update_patches, 0 }, 88 { "gzip-file-contents", 0, NULL, 0 }, 89 { "status", 0, cvs_server_status, REQ_NEEDDIR }, 90 { "rdiff", 0, cvs_server_rdiff, 0 }, 91 { "tag", 0, cvs_server_tag, REQ_NEEDDIR }, 92 { "rtag", 0, cvs_server_rtag, 0 }, 93 { "import", 0, cvs_server_import, REQ_NEEDDIR }, 94 { "admin", 0, cvs_server_admin, REQ_NEEDDIR }, 95 { "export", 0, cvs_server_export, REQ_NEEDDIR }, 96 { "history", 0, NULL, 0 }, 97 { "release", 0, cvs_server_release, REQ_NEEDDIR }, 98 { "watch-on", 0, NULL, 0 }, 99 { "watch-off", 0, NULL, 0 }, 100 { "watch-add", 0, NULL, 0 }, 101 { "watch-remove", 0, NULL, 0 }, 102 { "watchers", 0, NULL, 0 }, 103 { "editors", 0, NULL, 0 }, 104 { "init", 0, cvs_server_init, 0 }, 105 { "annotate", 0, cvs_server_annotate, REQ_NEEDDIR }, 106 { "rannotate", 0, cvs_server_rannotate, 0 }, 107 { "noop", 0, NULL, 0 }, 108 { "version", 0, cvs_server_version, 0 }, 109 { "", -1, NULL, 0 } 110 }; 111 112 static void client_check_directory(char *, char *); 113 static char *client_get_supported_responses(void); 114 static char *lastdir = NULL; 115 static int end_of_response = 0; 116 117 static void cvs_client_initlog(void); 118 119 /* 120 * File descriptors for protocol logging when the CVS_CLIENT_LOG environment 121 * variable is set. 122 */ 123 static int cvs_client_logon = 0; 124 int cvs_client_inlog_fd = -1; 125 int cvs_client_outlog_fd = -1; 126 127 128 int server_response = SERVER_OK; 129 130 static char * 131 client_get_supported_responses(void) 132 { 133 BUF *bp; 134 char *d; 135 int i, first; 136 137 first = 0; 138 bp = buf_alloc(512); 139 for (i = 0; cvs_responses[i].supported != -1; i++) { 140 if (cvs_responses[i].hdlr == NULL) 141 continue; 142 143 if (first != 0) 144 buf_putc(bp, ' '); 145 else 146 first++; 147 buf_puts(bp, cvs_responses[i].name); 148 } 149 150 buf_putc(bp, '\0'); 151 d = buf_release(bp); 152 return (d); 153 } 154 155 static void 156 client_check_directory(char *data, char *repository) 157 { 158 CVSENTRIES *entlist; 159 char *entry, *parent, *base, *p; 160 161 STRIP_SLASH(data); 162 163 /* first directory we get is our module root */ 164 if (module_repo_root == NULL && checkout_target_dir != NULL) { 165 p = repository + strlen(current_cvsroot->cr_dir) + 1; 166 module_repo_root = xstrdup(p); 167 p = strrchr(module_repo_root, '/'); 168 if (p != NULL) 169 *p = '\0'; 170 } 171 172 cvs_mkpath(data, NULL); 173 174 if (cvs_cmdop == CVS_OP_EXPORT) 175 return; 176 177 if ((base = basename(data)) == NULL) 178 fatal("client_check_directory: overflow"); 179 180 if ((parent = dirname(data)) == NULL) 181 fatal("client_check_directory: overflow"); 182 183 if (!strcmp(parent, ".")) 184 return; 185 186 entry = xmalloc(CVS_ENT_MAXLINELEN); 187 cvs_ent_line_str(base, NULL, NULL, NULL, NULL, 1, 0, entry, 188 CVS_ENT_MAXLINELEN); 189 190 entlist = cvs_ent_open(parent); 191 cvs_ent_add(entlist, entry); 192 193 free(entry); 194 } 195 196 void 197 cvs_client_connect_to_server(void) 198 { 199 struct cvs_var *vp; 200 char *cmd, *argv[9], *resp; 201 int ifd[2], ofd[2], argc; 202 203 if (cvs_server_active == 1) 204 fatal("cvs_client_connect: I was already connected to server"); 205 206 switch (current_cvsroot->cr_method) { 207 case CVS_METHOD_PSERVER: 208 case CVS_METHOD_KSERVER: 209 case CVS_METHOD_GSERVER: 210 case CVS_METHOD_FORK: 211 fatal("the specified connection method is not supported"); 212 default: 213 break; 214 } 215 216 if (pipe(ifd) == -1) 217 fatal("cvs_client_connect: %s", strerror(errno)); 218 if (pipe(ofd) == -1) 219 fatal("cvs_client_connect: %s", strerror(errno)); 220 221 switch (fork()) { 222 case -1: 223 fatal("cvs_client_connect: fork failed: %s", strerror(errno)); 224 case 0: 225 if (dup2(ifd[0], STDIN_FILENO) == -1) 226 fatal("cvs_client_connect: %s", strerror(errno)); 227 if (dup2(ofd[1], STDOUT_FILENO) == -1) 228 fatal("cvs_client_connect: %s", strerror(errno)); 229 230 close(ifd[1]); 231 close(ofd[0]); 232 233 if ((cmd = getenv("CVS_SERVER")) == NULL) 234 cmd = CVS_SERVER_DEFAULT; 235 236 argc = 0; 237 argv[argc++] = cvs_rsh; 238 239 if (current_cvsroot->cr_user != NULL) { 240 argv[argc++] = "-l"; 241 argv[argc++] = current_cvsroot->cr_user; 242 } 243 244 argv[argc++] = current_cvsroot->cr_host; 245 argv[argc++] = cmd; 246 argv[argc++] = "server"; 247 argv[argc] = NULL; 248 249 cvs_log(LP_TRACE, "connecting to server %s", 250 current_cvsroot->cr_host); 251 252 execvp(argv[0], argv); 253 fatal("cvs_client_connect: failed to execute cvs server"); 254 default: 255 break; 256 } 257 258 close(ifd[0]); 259 close(ofd[1]); 260 261 if ((current_cvsroot->cr_srvin = fdopen(ifd[1], "w")) == NULL) 262 fatal("cvs_client_connect: %s", strerror(errno)); 263 if ((current_cvsroot->cr_srvout = fdopen(ofd[0], "r")) == NULL) 264 fatal("cvs_client_connect: %s", strerror(errno)); 265 266 setvbuf(current_cvsroot->cr_srvin, NULL,_IOLBF, 0); 267 setvbuf(current_cvsroot->cr_srvout, NULL, _IOLBF, 0); 268 269 cvs_client_initlog(); 270 271 if (cvs_cmdop != CVS_OP_INIT) 272 cvs_client_send_request("Root %s", current_cvsroot->cr_dir); 273 274 resp = client_get_supported_responses(); 275 cvs_client_send_request("Valid-responses %s", resp); 276 free(resp); 277 278 cvs_client_send_request("valid-requests"); 279 cvs_client_get_responses(); 280 281 cvs_client_send_request("UseUnchanged"); 282 283 if (cvs_nolog == 1) 284 cvs_client_send_request("Global_option -l"); 285 286 if (cvs_noexec == 1) 287 cvs_client_send_request("Global_option -n"); 288 289 switch (verbosity) { 290 case 0: 291 cvs_client_send_request("Global_option -Q"); 292 break; 293 case 1: 294 /* Be quiet. This is the default in OpenCVS. */ 295 cvs_client_send_request("Global_option -q"); 296 break; 297 default: 298 break; 299 } 300 301 if (cvs_readonly == 1) 302 cvs_client_send_request("Global_option -r"); 303 304 if (cvs_trace == 1) 305 cvs_client_send_request("Global_option -t"); 306 307 /* XXX: If 'Set' is supported? */ 308 TAILQ_FOREACH(vp, &cvs_variables, cv_link) 309 cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val); 310 } 311 312 void 313 cvs_client_send_request(char *fmt, ...) 314 { 315 int i; 316 va_list ap; 317 char *data, *s; 318 struct cvs_req *req; 319 320 va_start(ap, fmt); 321 i = vasprintf(&data, fmt, ap); 322 va_end(ap); 323 if (i == -1) 324 fatal("cvs_client_send_request: could not allocate memory"); 325 326 if ((s = strchr(data, ' ')) != NULL) 327 *s = '\0'; 328 329 req = cvs_remote_get_request_info(data); 330 if (req == NULL) 331 fatal("'%s' is an unknown request", data); 332 333 if (req->supported != 1) 334 fatal("remote cvs server does not support '%s'", data); 335 336 if (s != NULL) 337 *s = ' '; 338 339 cvs_log(LP_TRACE, "%s", data); 340 341 cvs_remote_output(data); 342 free(data); 343 } 344 345 void 346 cvs_client_read_response(void) 347 { 348 char *cmd, *data; 349 struct cvs_resp *resp; 350 351 cmd = cvs_remote_input(); 352 if ((data = strchr(cmd, ' ')) != NULL) 353 (*data++) = '\0'; 354 355 resp = cvs_remote_get_response_info(cmd); 356 if (resp == NULL) 357 fatal("response '%s' is not supported by our client", cmd); 358 359 if (resp->hdlr == NULL) 360 fatal("opencvs client does not support '%s'", cmd); 361 362 (*resp->hdlr)(data); 363 364 free(cmd); 365 } 366 367 void 368 cvs_client_get_responses(void) 369 { 370 while (end_of_response != 1) 371 cvs_client_read_response(); 372 373 end_of_response = 0; 374 } 375 376 void 377 cvs_client_send_logmsg(char *msg) 378 { 379 char *buf, *p, *q; 380 381 (void)xasprintf(&buf, "%s\n", msg); 382 383 cvs_client_send_request("Argument -m"); 384 if ((p = strchr(buf, '\n')) != NULL) 385 *p++ = '\0'; 386 cvs_client_send_request("Argument %s", buf); 387 for (q = p; p != NULL; q = p) { 388 if ((p = strchr(q, '\n')) != NULL) { 389 *p++ = '\0'; 390 cvs_client_send_request("Argumentx %s", q); 391 } 392 } 393 394 free(buf); 395 } 396 397 void 398 cvs_client_senddir(const char *dir) 399 { 400 struct stat st; 401 int nb; 402 char *d, *date, fpath[PATH_MAX], repo[PATH_MAX], *tag; 403 404 d = NULL; 405 406 if (lastdir != NULL && !strcmp(dir, lastdir)) 407 return; 408 409 cvs_get_repository_path(dir, repo, PATH_MAX); 410 411 if (cvs_cmdop != CVS_OP_RLOG) 412 cvs_client_send_request("Directory %s\n%s", dir, repo); 413 414 (void)xsnprintf(fpath, PATH_MAX, "%s/%s", 415 dir, CVS_PATH_STATICENTRIES); 416 417 if (stat(fpath, &st) == 0 && (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH))) 418 cvs_client_send_request("Static-directory"); 419 420 d = xstrdup(dir); 421 cvs_parse_tagfile(d, &tag, &date, &nb); 422 423 if (tag != NULL || date != NULL) { 424 char buf[128]; 425 426 if (tag != NULL && nb != 0) { 427 if (strlcpy(buf, "N", sizeof(buf)) >= sizeof(buf)) 428 fatal("cvs_client_senddir: truncation"); 429 } else if (tag != NULL) { 430 if (strlcpy(buf, "T", sizeof(buf)) >= sizeof(buf)) 431 fatal("cvs_client_senddir: truncation"); 432 } else { 433 if (strlcpy(buf, "D", sizeof(buf)) >= sizeof(buf)) 434 fatal("cvs_client_senddir: truncation"); 435 } 436 437 if (strlcat(buf, tag ? tag : date, sizeof(buf)) >= sizeof(buf)) 438 fatal("cvs_client_senddir: truncation"); 439 440 cvs_client_send_request("Sticky %s", buf); 441 442 free(tag); 443 free(date); 444 } 445 free(d); 446 free(lastdir); 447 lastdir = xstrdup(dir); 448 } 449 450 void 451 cvs_client_sendfile(struct cvs_file *cf) 452 { 453 size_t len; 454 struct tm datetm; 455 char rev[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ], sticky[CVS_REV_BUFSZ]; 456 457 if (cf->file_type != CVS_FILE) 458 return; 459 460 cvs_client_senddir(cf->file_wd); 461 cvs_remote_classify_file(cf); 462 463 if (cf->file_type == CVS_DIR) 464 return; 465 466 if (cf->file_ent != NULL && cvs_cmdop != CVS_OP_IMPORT) { 467 if (cf->file_status == FILE_ADDED) { 468 len = strlcpy(rev, "0", sizeof(rev)); 469 if (len >= sizeof(rev)) 470 fatal("cvs_client_sendfile: truncation"); 471 472 len = strlcpy(timebuf, "Initial ", sizeof(timebuf)); 473 if (len >= sizeof(timebuf)) 474 fatal("cvs_client_sendfile: truncation"); 475 476 len = strlcat(timebuf, cf->file_name, sizeof(timebuf)); 477 if (len >= sizeof(timebuf)) 478 fatal("cvs_client_sendfile: truncation"); 479 } else { 480 rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev)); 481 ctime_r(&cf->file_ent->ce_mtime, timebuf); 482 } 483 484 if (cf->file_ent->ce_conflict == NULL) { 485 timebuf[strcspn(timebuf, "\n")] = '\0'; 486 } else { 487 len = strlcpy(timebuf, cf->file_ent->ce_conflict, 488 sizeof(timebuf)); 489 if (len >= sizeof(timebuf)) 490 fatal("cvs_client_sendfile: truncation"); 491 len = strlcat(timebuf, "+=", sizeof(timebuf)); 492 if (len >= sizeof(timebuf)) 493 fatal("cvs_client_sendfile: truncation"); 494 } 495 496 sticky[0] = '\0'; 497 if (cf->file_ent->ce_tag != NULL) { 498 (void)xsnprintf(sticky, sizeof(sticky), "T%s", 499 cf->file_ent->ce_tag); 500 } else if (cf->file_ent->ce_date != -1) { 501 gmtime_r(&(cf->file_ent->ce_date), &datetm); 502 (void)strftime(sticky, sizeof(sticky), 503 "D"CVS_DATE_FMT, &datetm); 504 } 505 506 cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s", 507 cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "", 508 rev, timebuf, cf->file_ent->ce_opts ? 509 cf->file_ent->ce_opts : "", sticky); 510 } 511 512 if (cvs_cmdop == CVS_OP_ADD) 513 cf->file_status = FILE_MODIFIED; 514 515 switch (cf->file_status) { 516 case FILE_UNKNOWN: 517 if (cf->file_flags & FILE_ON_DISK) 518 cvs_client_send_request("Questionable %s", 519 cf->file_name); 520 break; 521 case FILE_ADDED: 522 if (backup_local_changes) /* for update -C */ 523 cvs_backup_file(cf); 524 525 cvs_client_send_request("Modified %s", cf->file_name); 526 cvs_remote_send_file(cf->file_path, cf->fd); 527 break; 528 case FILE_MODIFIED: 529 if (backup_local_changes) { /* for update -C */ 530 cvs_backup_file(cf); 531 cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s", 532 cf->file_name, "", rev, timebuf, 533 cf->file_ent->ce_opts ? cf->file_ent->ce_opts : "", 534 sticky); 535 break; 536 } 537 538 cvs_client_send_request("Modified %s", cf->file_name); 539 cvs_remote_send_file(cf->file_path, cf->fd); 540 break; 541 case FILE_UPTODATE: 542 cvs_client_send_request("Unchanged %s", cf->file_name); 543 break; 544 } 545 } 546 547 void 548 cvs_client_send_files(char **argv, int argc) 549 { 550 int i; 551 552 for (i = 0; i < argc; i++) 553 cvs_client_send_request("Argument %s", argv[i]); 554 } 555 556 void 557 cvs_client_ok(char *data) 558 { 559 end_of_response = 1; 560 server_response = SERVER_OK; 561 } 562 563 void 564 cvs_client_error(char *data) 565 { 566 end_of_response = 1; 567 server_response = SERVER_ERROR; 568 } 569 570 void 571 cvs_client_validreq(char *data) 572 { 573 int i; 574 char *sp, *ep; 575 struct cvs_req *req; 576 577 if ((sp = data) == NULL) 578 fatal("Missing argument for Valid-requests"); 579 580 do { 581 if ((ep = strchr(sp, ' ')) != NULL) 582 *ep = '\0'; 583 584 req = cvs_remote_get_request_info(sp); 585 if (req != NULL) 586 req->supported = 1; 587 588 if (ep != NULL) 589 sp = ep + 1; 590 } while (ep != NULL); 591 592 for (i = 0; cvs_requests[i].supported != -1; i++) { 593 req = &cvs_requests[i]; 594 if ((req->flags & REQ_NEEDED) && 595 req->supported != 1) { 596 fatal("server does not support required '%s'", 597 req->name); 598 } 599 } 600 } 601 602 void 603 cvs_client_e(char *data) 604 { 605 if (data == NULL) 606 fatal("Missing argument for E"); 607 608 fprintf(stderr, "%s\n", data); 609 } 610 611 void 612 cvs_client_m(char *data) 613 { 614 if (data == NULL) 615 fatal("Missing argument for M"); 616 617 puts(data); 618 } 619 620 void 621 cvs_client_checkedin(char *data) 622 { 623 CVSENTRIES *entlist; 624 struct cvs_ent *ent, *newent; 625 size_t len; 626 struct tm datetm; 627 char *dir, *e, *entry, rev[CVS_REV_BUFSZ]; 628 char sticky[CVS_ENT_MAXLINELEN], timebuf[CVS_TIME_BUFSZ]; 629 630 if (data == NULL) 631 fatal("Missing argument for Checked-in"); 632 633 dir = cvs_remote_input(); 634 e = cvs_remote_input(); 635 free(dir); 636 637 entlist = cvs_ent_open(data); 638 newent = cvs_ent_parse(e); 639 ent = cvs_ent_get(entlist, newent->ce_name); 640 free(e); 641 642 rcsnum_tostr(newent->ce_rev, rev, sizeof(rev)); 643 644 sticky[0] = '\0'; 645 if (ent == NULL) { 646 len = strlcpy(rev, "0", sizeof(rev)); 647 if (len >= sizeof(rev)) 648 fatal("cvs_client_sendfile: truncation"); 649 650 len = strlcpy(timebuf, "Initial ", sizeof(timebuf)); 651 if (len >= sizeof(timebuf)) 652 fatal("cvs_client_sendfile: truncation"); 653 654 len = strlcat(timebuf, newent->ce_name, sizeof(timebuf)); 655 if (len >= sizeof(timebuf)) 656 fatal("cvs_client_sendfile: truncation"); 657 } else { 658 gmtime_r(&ent->ce_mtime, &datetm); 659 asctime_r(&datetm, timebuf); 660 timebuf[strcspn(timebuf, "\n")] = '\0'; 661 662 if (newent->ce_tag != NULL) { 663 (void)xsnprintf(sticky, sizeof(sticky), "T%s", 664 newent->ce_tag); 665 } else if (newent->ce_date != -1) { 666 gmtime_r(&(newent->ce_date), &datetm); 667 (void)strftime(sticky, sizeof(sticky), 668 "D"CVS_DATE_FMT, &datetm); 669 } 670 671 cvs_ent_free(ent); 672 } 673 674 entry = xmalloc(CVS_ENT_MAXLINELEN); 675 cvs_ent_line_str(newent->ce_name, rev, timebuf, 676 newent->ce_opts ? newent->ce_opts : "", sticky, 0, 677 newent->ce_status == CVS_ENT_REMOVED ? 1 : 0, 678 entry, CVS_ENT_MAXLINELEN); 679 680 cvs_ent_free(newent); 681 cvs_ent_add(entlist, entry); 682 683 free(entry); 684 } 685 686 void 687 cvs_client_updated(char *data) 688 { 689 int fd; 690 time_t now; 691 mode_t fmode; 692 size_t flen; 693 CVSENTRIES *ent; 694 struct cvs_ent *e; 695 const char *errstr; 696 struct tm datetm; 697 struct timeval tv[2]; 698 char repo[PATH_MAX], *entry; 699 char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ]; 700 char *en, *mode, *len, *rpath, *p; 701 char sticky[CVS_ENT_MAXLINELEN], fpath[PATH_MAX]; 702 703 if (data == NULL) 704 fatal("Missing argument for Updated"); 705 706 rpath = cvs_remote_input(); 707 en = cvs_remote_input(); 708 mode = cvs_remote_input(); 709 len = cvs_remote_input(); 710 711 client_check_directory(data, rpath); 712 cvs_get_repository_path(".", repo, PATH_MAX); 713 714 STRIP_SLASH(repo); 715 716 if (strlen(repo) + 1 > strlen(rpath)) 717 fatal("received a repository path that is too short"); 718 719 p = strrchr(rpath, '/'); 720 if (p == NULL) 721 fatal("malicious repository path from server"); 722 723 (void)xsnprintf(fpath, sizeof(fpath), "%s/%s", data, p); 724 725 flen = strtonum(len, 0, INT_MAX, &errstr); 726 if (errstr != NULL) 727 fatal("cvs_client_updated: %s: %s", len, errstr); 728 free(len); 729 730 cvs_strtomode(mode, &fmode); 731 free(mode); 732 fmode &= ~cvs_umask; 733 734 time(&now); 735 gmtime_r(&now, &datetm); 736 asctime_r(&datetm, timebuf); 737 timebuf[strcspn(timebuf, "\n")] = '\0'; 738 739 e = cvs_ent_parse(en); 740 free(en); 741 742 sticky[0] = '\0'; 743 if (e->ce_tag != NULL) { 744 (void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag); 745 } else if (e->ce_date != -1) { 746 gmtime_r(&(e->ce_date), &datetm); 747 (void)strftime(sticky, sizeof(sticky), 748 "D"CVS_DATE_FMT, &datetm); 749 } 750 751 rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf)); 752 753 entry = xmalloc(CVS_ENT_MAXLINELEN); 754 cvs_ent_line_str(e->ce_name, revbuf, timebuf, 755 e->ce_opts ? e->ce_opts : "", sticky, 0, 0, 756 entry, CVS_ENT_MAXLINELEN); 757 758 cvs_ent_free(e); 759 760 if (cvs_cmdop != CVS_OP_EXPORT) { 761 ent = cvs_ent_open(data); 762 cvs_ent_add(ent, entry); 763 } 764 765 free(entry); 766 767 (void)unlink(fpath); 768 if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1) 769 fatal("cvs_client_updated: open: %s: %s", 770 fpath, strerror(errno)); 771 772 cvs_remote_receive_file(fd, flen); 773 774 tv[0].tv_sec = now; 775 tv[0].tv_usec = 0; 776 tv[1] = tv[0]; 777 778 if (futimes(fd, tv) == -1) 779 fatal("cvs_client_updated: futimes: %s", strerror(errno)); 780 781 if (fchmod(fd, fmode) == -1) 782 fatal("cvs_client_updated: fchmod: %s", strerror(errno)); 783 784 (void)close(fd); 785 786 free(rpath); 787 } 788 789 void 790 cvs_client_merged(char *data) 791 { 792 int fd; 793 time_t now; 794 mode_t fmode; 795 size_t flen; 796 CVSENTRIES *ent; 797 const char *errstr; 798 struct timeval tv[2]; 799 struct tm datetm; 800 char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode; 801 char *len, *fpath, *wdir; 802 803 if (data == NULL) 804 fatal("Missing argument for Merged"); 805 806 rpath = cvs_remote_input(); 807 entry = cvs_remote_input(); 808 mode = cvs_remote_input(); 809 len = cvs_remote_input(); 810 811 client_check_directory(data, rpath); 812 813 repo = xmalloc(PATH_MAX); 814 cvs_get_repository_path(".", repo, PATH_MAX); 815 816 STRIP_SLASH(repo); 817 818 if (strlen(repo) + 1 > strlen(rpath)) 819 fatal("received a repository path that is too short"); 820 821 fpath = rpath + strlen(repo) + 1; 822 if ((wdir = dirname(fpath)) == NULL) 823 fatal("cvs_client_merged: dirname: %s", strerror(errno)); 824 free(repo); 825 826 flen = strtonum(len, 0, INT_MAX, &errstr); 827 if (errstr != NULL) 828 fatal("cvs_client_merged: %s: %s", len, errstr); 829 free(len); 830 831 cvs_strtomode(mode, &fmode); 832 free(mode); 833 fmode &= ~cvs_umask; 834 835 time(&now); 836 gmtime_r(&now, &datetm); 837 asctime_r(&datetm, timebuf); 838 timebuf[strcspn(timebuf, "\n")] = '\0'; 839 840 ent = cvs_ent_open(wdir); 841 cvs_ent_add(ent, entry); 842 free(entry); 843 844 (void)unlink(fpath); 845 if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1) 846 fatal("cvs_client_merged: open: %s: %s", 847 fpath, strerror(errno)); 848 849 cvs_remote_receive_file(fd, flen); 850 851 tv[0].tv_sec = now; 852 tv[0].tv_usec = 0; 853 tv[1] = tv[0]; 854 855 if (futimes(fd, tv) == -1) 856 fatal("cvs_client_merged: futimes: %s", strerror(errno)); 857 858 if (fchmod(fd, fmode) == -1) 859 fatal("cvs_client_merged: fchmod: %s", strerror(errno)); 860 861 (void)close(fd); 862 863 free(rpath); 864 } 865 866 void 867 cvs_client_removed(char *data) 868 { 869 CVSENTRIES *entlist; 870 char *rpath, *filename, fpath[PATH_MAX]; 871 872 if (data == NULL) 873 fatal("Missing argument for Removed"); 874 875 rpath = cvs_remote_input(); 876 if ((filename = strrchr(rpath, '/')) == NULL) 877 fatal("bad rpath in cvs_client_removed: %s", rpath); 878 filename++; 879 880 entlist = cvs_ent_open(data); 881 cvs_ent_remove(entlist, filename); 882 883 (void)xsnprintf(fpath, PATH_MAX, "%s/%s", data, filename); 884 (void)unlink(fpath); 885 886 free(rpath); 887 } 888 889 void 890 cvs_client_remove_entry(char *data) 891 { 892 CVSENTRIES *entlist; 893 char *filename, *rpath; 894 895 if (data == NULL) 896 fatal("Missing argument for Remove-entry"); 897 898 rpath = cvs_remote_input(); 899 if ((filename = strrchr(rpath, '/')) == NULL) 900 fatal("bad rpath in cvs_client_remove_entry: %s", rpath); 901 filename++; 902 903 entlist = cvs_ent_open(data); 904 cvs_ent_remove(entlist, filename); 905 906 free(rpath); 907 } 908 909 void 910 cvs_client_set_static_directory(char *data) 911 { 912 FILE *fp; 913 char *dir, fpath[PATH_MAX]; 914 915 if (data == NULL) 916 fatal("Missing argument for Set-static-directory"); 917 918 STRIP_SLASH(data); 919 920 dir = cvs_remote_input(); 921 free(dir); 922 923 if (cvs_cmdop == CVS_OP_EXPORT) 924 return; 925 926 (void)xsnprintf(fpath, PATH_MAX, "%s/%s", 927 data, CVS_PATH_STATICENTRIES); 928 929 if ((fp = fopen(fpath, "w+")) == NULL) { 930 cvs_log(LP_ERRNO, "%s", fpath); 931 return; 932 } 933 (void)fclose(fp); 934 } 935 936 void 937 cvs_client_clear_static_directory(char *data) 938 { 939 char *dir, fpath[PATH_MAX]; 940 941 if (data == NULL) 942 fatal("Missing argument for Clear-static-directory"); 943 944 STRIP_SLASH(data); 945 946 dir = cvs_remote_input(); 947 free(dir); 948 949 if (cvs_cmdop == CVS_OP_EXPORT) 950 return; 951 952 (void)xsnprintf(fpath, PATH_MAX, "%s/%s", 953 data, CVS_PATH_STATICENTRIES); 954 955 (void)cvs_unlink(fpath); 956 } 957 958 void 959 cvs_client_set_sticky(char *data) 960 { 961 FILE *fp; 962 char *dir, *tag, tagpath[PATH_MAX]; 963 964 if (data == NULL) 965 fatal("Missing argument for Set-sticky"); 966 967 STRIP_SLASH(data); 968 969 dir = cvs_remote_input(); 970 tag = cvs_remote_input(); 971 972 if (cvs_cmdop == CVS_OP_EXPORT) 973 goto out; 974 975 client_check_directory(data, dir); 976 977 (void)xsnprintf(tagpath, PATH_MAX, "%s/%s", data, CVS_PATH_TAG); 978 979 if ((fp = fopen(tagpath, "w+")) == NULL) { 980 cvs_log(LP_ERRNO, "%s", tagpath); 981 goto out; 982 } 983 984 (void)fprintf(fp, "%s\n", tag); 985 (void)fclose(fp); 986 out: 987 free(tag); 988 free(dir); 989 } 990 991 void 992 cvs_client_clear_sticky(char *data) 993 { 994 char *dir, tagpath[PATH_MAX]; 995 996 if (data == NULL) 997 fatal("Missing argument for Clear-sticky"); 998 999 STRIP_SLASH(data); 1000 1001 dir = cvs_remote_input(); 1002 1003 if (cvs_cmdop == CVS_OP_EXPORT) { 1004 free(dir); 1005 return; 1006 } 1007 1008 client_check_directory(data, dir); 1009 1010 (void)xsnprintf(tagpath, PATH_MAX, "%s/%s", data, CVS_PATH_TAG); 1011 (void)unlink(tagpath); 1012 1013 free(dir); 1014 } 1015 1016 1017 /* 1018 * cvs_client_initlog() 1019 * 1020 * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is 1021 * set. In this case, the variable's value is used as a path to which the 1022 * appropriate suffix is added (".in" for client input and ".out" for server 1023 * output). 1024 */ 1025 static void 1026 cvs_client_initlog(void) 1027 { 1028 u_int i; 1029 char *env, *envdup, buf[PATH_MAX], fpath[PATH_MAX]; 1030 char rpath[PATH_MAX], timebuf[CVS_TIME_BUFSZ], *s; 1031 struct stat st; 1032 time_t now; 1033 struct passwd *pwd; 1034 1035 /* avoid doing it more than once */ 1036 if (cvs_client_logon) 1037 return; 1038 1039 if ((env = getenv("CVS_CLIENT_LOG")) == NULL) 1040 return; 1041 1042 envdup = xstrdup(env); 1043 if ((s = strchr(envdup, '%')) != NULL) 1044 *s = '\0'; 1045 1046 if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf)) 1047 fatal("cvs_client_initlog: truncation"); 1048 1049 if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath)) 1050 fatal("cvs_client_initlog: truncation"); 1051 1052 free(envdup); 1053 1054 s = buf; 1055 while ((s = strchr(s, '%')) != NULL) { 1056 s++; 1057 switch (*s) { 1058 case 'c': 1059 if (strlcpy(fpath, cmdp->cmd_name, sizeof(fpath)) >= 1060 sizeof(fpath)) 1061 fatal("cvs_client_initlog: truncation"); 1062 break; 1063 case 'd': 1064 time(&now); 1065 ctime_r(&now, timebuf); 1066 timebuf[strcspn(timebuf, "\n")] = '\0'; 1067 if (strlcpy(fpath, timebuf, sizeof(fpath)) >= 1068 sizeof(fpath)) 1069 fatal("cvs_client_initlog: truncation"); 1070 break; 1071 case 'p': 1072 (void)xsnprintf(fpath, sizeof(fpath), "%d", getpid()); 1073 break; 1074 case 'u': 1075 if ((pwd = getpwuid(getuid())) != NULL) { 1076 if (strlcpy(fpath, pwd->pw_name, 1077 sizeof(fpath)) >= sizeof(fpath)) 1078 fatal("cvs_client_initlog: truncation"); 1079 } else { 1080 fpath[0] = '\0'; 1081 } 1082 endpwent(); 1083 break; 1084 default: 1085 fpath[0] = '\0'; 1086 break; 1087 } 1088 1089 if (fpath[0] != '\0') { 1090 if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath)) 1091 fatal("cvs_client_initlog: truncation"); 1092 1093 if (strlcat(rpath, fpath, sizeof(rpath)) 1094 >= sizeof(rpath)) 1095 fatal("cvs_client_initlog: truncation"); 1096 } 1097 } 1098 1099 for (i = 0; i < UINT_MAX; i++) { 1100 (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i); 1101 1102 if (stat(fpath, &st) != -1) 1103 continue; 1104 1105 if (errno != ENOENT) 1106 fatal("cvs_client_initlog() stat failed '%s'", 1107 strerror(errno)); 1108 1109 break; 1110 } 1111 1112 if ((cvs_client_inlog_fd = open(fpath, 1113 O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) { 1114 fatal("cvs_client_initlog: open `%s': %s", 1115 fpath, strerror(errno)); 1116 } 1117 1118 for (i = 0; i < UINT_MAX; i++) { 1119 (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i); 1120 1121 if (stat(fpath, &st) != -1) 1122 continue; 1123 1124 if (errno != ENOENT) 1125 fatal("cvs_client_initlog() stat failed '%s'", 1126 strerror(errno)); 1127 1128 break; 1129 } 1130 1131 if ((cvs_client_outlog_fd = open(fpath, 1132 O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) { 1133 fatal("cvs_client_initlog: open `%s': %s", 1134 fpath, strerror(errno)); 1135 } 1136 1137 cvs_client_logon = 1; 1138 } 1139