Annotation of src/usr.bin/cvs/server.c, Revision 1.60
1.60 ! ray 1: /* $OpenBSD: server.c,v 1.59 2007/05/26 23:52:04 ray Exp $ */
1.1 jfb 2: /*
1.29 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.29 joris 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.
1.1 jfb 8: *
1.29 joris 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.
1.1 jfb 16: */
17:
1.55 otto 18: #include <sys/stat.h>
19:
20: #include <errno.h>
21: #include <fcntl.h>
22: #include <libgen.h>
23: #include <stdlib.h>
24: #include <string.h>
25: #include <unistd.h>
1.1 jfb 26:
27: #include "cvs.h"
1.29 joris 28: #include "remote.h"
29:
30: struct cvs_resp cvs_responses[] = {
31: /* this is what our server uses, the client should support it */
32: { "Valid-requests", 1, cvs_client_validreq, RESP_NEEDED },
33: { "ok", 0, cvs_client_ok, RESP_NEEDED},
34: { "error", 0, cvs_client_error, RESP_NEEDED },
35: { "E", 0, cvs_client_e, RESP_NEEDED },
36: { "M", 0, cvs_client_m, RESP_NEEDED },
37: { "Checked-in", 0, cvs_client_checkedin, RESP_NEEDED },
38: { "Updated", 0, cvs_client_updated, RESP_NEEDED },
39: { "Merged", 0, cvs_client_merged, RESP_NEEDED },
40: { "Removed", 0, cvs_client_removed, RESP_NEEDED },
41: { "Remove-entry", 0, cvs_client_remove_entry, RESP_NEEDED },
1.46 xsa 42: { "Set-static-directory", 0, cvs_client_set_static_directory, RESP_NEEDED },
1.45 xsa 43: { "Clear-static-directory", 0, cvs_client_clear_static_directory, RESP_NEEDED },
44: { "Set-sticky", 0, cvs_client_set_sticky, RESP_NEEDED },
45: { "Clear-sticky", 0, cvs_client_clear_sticky, RESP_NEEDED },
1.29 joris 46:
47: /* unsupported responses until told otherwise */
48: { "New-entry", 0, NULL, 0 },
49: { "Created", 0, NULL, 0 },
50: { "Update-existing", 0, NULL, 0 },
51: { "Rcs-diff", 0, NULL, 0 },
52: { "Patched", 0, NULL, 0 },
53: { "Mode", 0, NULL, 0 },
54: { "Mod-time", 0, NULL, 0 },
55: { "Checksum", 0, NULL, 0 },
56: { "Copy-file", 0, NULL, 0 },
57: { "Template", 0, NULL, 0 },
58: { "Set-checkin-prog", 0, NULL, 0 },
59: { "Set-update-prog", 0, NULL, 0 },
60: { "Notified", 0, NULL, 0 },
61: { "Module-expansion", 0, NULL, 0 },
62: { "Wrapper-rcsOption", 0, NULL, 0 },
63: { "Mbinary", 0, NULL, 0 },
64: { "F", 0, NULL, 0 },
65: { "MT", 0, NULL, 0 },
66: { "", -1, NULL, 0 }
67: };
1.1 jfb 68:
1.29 joris 69: int cvs_server(int, char **);
70: char *cvs_server_path = NULL;
1.1 jfb 71:
1.29 joris 72: static char *server_currentdir = NULL;
73: static char *server_argv[CVS_CMD_MAXARG];
74: static int server_argc = 1;
1.8 joris 75:
1.17 jfb 76: struct cvs_cmd cvs_cmd_server = {
1.29 joris 77: CVS_OP_SERVER, 0, "server", { "", "" },
78: "server mode",
1.17 jfb 79: NULL,
1.29 joris 80: NULL,
81: NULL,
82: cvs_server
1.17 jfb 83: };
1.1 jfb 84:
1.14 joris 85:
1.1 jfb 86: int
87: cvs_server(int argc, char **argv)
88: {
1.29 joris 89: char *cmd, *data;
90: struct cvs_req *req;
91:
92: server_argv[0] = xstrdup("server");
93:
1.58 ray 94: (void)xasprintf(&cvs_server_path, "%s/cvs-serv%d", cvs_tmpdir,
95: getpid());
1.29 joris 96:
97: if (mkdir(cvs_server_path, 0700) == -1)
98: fatal("failed to create temporary server directory: %s, %s",
99: cvs_server_path, strerror(errno));
100:
101: if (chdir(cvs_server_path) == -1)
102: fatal("failed to change directory to '%s'", cvs_server_path);
103:
104: for (;;) {
105: cmd = cvs_remote_input();
106:
107: if ((data = strchr(cmd, ' ')) != NULL)
108: (*data++) = '\0';
109:
110: req = cvs_remote_get_request_info(cmd);
111: if (req == NULL)
112: fatal("request '%s' is not supported by our server",
113: cmd);
114:
115: if (req->hdlr == NULL)
116: fatal("opencvs server does not support '%s'", cmd);
117:
118: (*req->hdlr)(data);
119: xfree(cmd);
1.14 joris 120: }
121:
1.29 joris 122: return (0);
123: }
124:
125: void
126: cvs_server_send_response(char *fmt, ...)
127: {
128: va_list ap;
1.59 ray 129: char *data;
1.29 joris 130:
131: va_start(ap, fmt);
1.56 ray 132: if (vasprintf(&data, fmt, ap) == -1)
133: fatal("vasprintf: %s", strerror(errno));
1.29 joris 134: va_end(ap);
1.14 joris 135:
1.30 joris 136: cvs_log(LP_TRACE, "%s", data);
1.29 joris 137: cvs_remote_output(data);
138: xfree(data);
139: }
140:
141: void
142: cvs_server_root(char *data)
143: {
144: fatal("duplicate Root request from client, violates the protocol");
145: }
146:
147: void
148: cvs_server_validresp(char *data)
149: {
150: int i;
151: char *sp, *ep;
152: struct cvs_resp *resp;
153:
1.57 ray 154: if ((sp = data) == NULL)
155: fatal("Missing argument for Valid-responses");
156:
1.29 joris 157: do {
158: if ((ep = strchr(sp, ' ')) != NULL)
159: *ep = '\0';
160:
161: resp = cvs_remote_get_response_info(sp);
162: if (resp != NULL)
163: resp->supported = 1;
164:
165: if (ep != NULL)
166: sp = ep + 1;
167: } while (ep != NULL);
168:
169: for (i = 0; cvs_responses[i].supported != -1; i++) {
170: resp = &cvs_responses[i];
171: if ((resp->flags & RESP_NEEDED) &&
172: resp->supported != 1) {
173: fatal("client does not support required '%s'",
174: resp->name);
1.1 jfb 175: }
1.29 joris 176: }
177: }
1.1 jfb 178:
1.29 joris 179: void
180: cvs_server_validreq(char *data)
181: {
182: BUF *bp;
183: char *d;
184: int i, first;
185:
186: first = 0;
187: bp = cvs_buf_alloc(512, BUF_AUTOEXT);
188: for (i = 0; cvs_requests[i].supported != -1; i++) {
189: if (cvs_requests[i].hdlr == NULL)
1.5 jfb 190: continue;
1.1 jfb 191:
1.29 joris 192: if (first != 0)
193: cvs_buf_append(bp, " ", 1);
194: else
195: first++;
196:
197: cvs_buf_append(bp, cvs_requests[i].name,
198: strlen(cvs_requests[i].name));
199: }
200:
201: cvs_buf_putc(bp, '\0');
202: d = cvs_buf_release(bp);
203:
204: cvs_server_send_response("Valid-requests %s", d);
205: cvs_server_send_response("ok");
206: xfree(d);
1.42 xsa 207: }
208:
209: void
1.43 xsa 210: cvs_server_static_directory(char *data)
211: {
212: FILE *fp;
1.51 otto 213: char fpath[MAXPATHLEN];
1.43 xsa 214:
1.54 xsa 215: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
216: server_currentdir, CVS_PATH_STATICENTRIES);
1.43 xsa 217:
218: if ((fp = fopen(fpath, "w+")) == NULL) {
219: cvs_log(LP_ERRNO, "%s", fpath);
1.51 otto 220: return;
1.43 xsa 221: }
222: (void)fclose(fp);
223: }
224:
225: void
1.42 xsa 226: cvs_server_sticky(char *data)
227: {
228: FILE *fp;
1.51 otto 229: char tagpath[MAXPATHLEN];
1.42 xsa 230:
1.57 ray 231: if (data == NULL)
232: fatal("Missing argument for Sticky");
233:
1.54 xsa 234: (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s",
235: server_currentdir, CVS_PATH_TAG);
1.42 xsa 236:
1.43 xsa 237: if ((fp = fopen(tagpath, "w+")) == NULL) {
1.42 xsa 238: cvs_log(LP_ERRNO, "%s", tagpath);
1.51 otto 239: return;
1.42 xsa 240: }
241:
242: (void)fprintf(fp, "%s\n", data);
243: (void)fclose(fp);
1.29 joris 244: }
245:
246: void
247: cvs_server_globalopt(char *data)
248: {
1.57 ray 249: if (data == NULL)
250: fatal("Missing argument for Global_option");
251:
1.38 xsa 252: if (!strcmp(data, "-l"))
253: cvs_nolog = 1;
1.29 joris 254:
255: if (!strcmp(data, "-n"))
256: cvs_noexec = 1;
1.38 xsa 257:
258: if (!strcmp(data, "-Q"))
259: verbosity = 0;
260:
261: if (!strcmp(data, "-r"))
262: cvs_readonly = 1;
263:
264: if (!strcmp(data, "-t"))
265: cvs_trace = 1;
1.29 joris 266:
267: if (!strcmp(data, "-V"))
268: verbosity = 2;
1.39 xsa 269: }
270:
271: void
272: cvs_server_set(char *data)
273: {
274: char *ep;
275:
1.57 ray 276: if (data == NULL)
277: fatal("Missing argument for Set");
278:
1.39 xsa 279: ep = strchr(data, '=');
280: if (ep == NULL)
281: fatal("no = in variable assignment");
282:
283: *(ep++) = '\0';
284: if (cvs_var_set(data, ep) < 0)
285: fatal("cvs_server_set: cvs_var_set failed");
1.29 joris 286: }
1.1 jfb 287:
1.29 joris 288: void
289: cvs_server_directory(char *data)
290: {
291: CVSENTRIES *entlist;
1.51 otto 292: char *dir, *repo, *parent, entry[CVS_ENT_MAXLINELEN], *dirn, *p;
1.29 joris 293:
294: dir = cvs_remote_input();
1.49 joris 295: STRIP_SLASH(dir);
296:
297: if (strlen(dir) < strlen(current_cvsroot->cr_dir))
1.29 joris 298: fatal("cvs_server_directory: bad Directory request");
299:
1.49 joris 300: repo = dir + strlen(current_cvsroot->cr_dir);
301:
302: /*
303: * This is somewhat required for checkout, as the
304: * directory request will be:
305: *
306: * Directory .
307: * /path/to/cvs/root
308: */
309: if (repo[0] == '\0')
310: p = xstrdup(".");
311: else
312: p = xstrdup(repo + 1);
313:
314: cvs_mkpath(p);
1.29 joris 315:
1.49 joris 316: if ((dirn = basename(p)) == NULL)
1.29 joris 317: fatal("cvs_server_directory: %s", strerror(errno));
318:
1.49 joris 319: if ((parent = dirname(p)) == NULL)
1.29 joris 320: fatal("cvs_server_directory: %s", strerror(errno));
321:
322: if (strcmp(parent, ".")) {
323: entlist = cvs_ent_open(parent);
1.53 xsa 324: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////", dirn);
1.29 joris 325:
326: cvs_ent_add(entlist, entry);
327: cvs_ent_close(entlist, ENT_SYNC);
1.1 jfb 328: }
329:
1.29 joris 330: if (server_currentdir != NULL)
331: xfree(server_currentdir);
1.49 joris 332: server_currentdir = xstrdup(p);
1.29 joris 333:
1.49 joris 334: xfree(p);
1.29 joris 335: xfree(dir);
336: }
337:
338: void
339: cvs_server_entry(char *data)
340: {
341: CVSENTRIES *entlist;
342:
1.57 ray 343: if (data == NULL)
344: fatal("Missing argument for Entry");
345:
1.29 joris 346: entlist = cvs_ent_open(server_currentdir);
347: cvs_ent_add(entlist, data);
348: cvs_ent_close(entlist, ENT_SYNC);
349: }
350:
351: void
352: cvs_server_modified(char *data)
353: {
1.41 xsa 354: int fd;
1.29 joris 355: size_t flen;
356: mode_t fmode;
357: const char *errstr;
1.51 otto 358: char *mode, *len, fpath[MAXPATHLEN];
1.29 joris 359:
1.57 ray 360: if (data == NULL)
361: fatal("Missing argument for Modified");
362:
1.29 joris 363: mode = cvs_remote_input();
364: len = cvs_remote_input();
365:
366: cvs_strtomode(mode, &fmode);
367: xfree(mode);
368:
369: flen = strtonum(len, 0, INT_MAX, &errstr);
370: if (errstr != NULL)
371: fatal("cvs_server_modified: %s", errstr);
372: xfree(len);
373:
1.54 xsa 374: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", server_currentdir, data);
1.29 joris 375:
376: if ((fd = open(fpath, O_WRONLY | O_CREAT | O_TRUNC)) == -1)
377: fatal("cvs_server_modified: %s: %s", fpath, strerror(errno));
378:
1.48 joris 379: cvs_remote_receive_file(fd, flen);
1.29 joris 380:
381: if (fchmod(fd, 0600) == -1)
382: fatal("cvs_server_modified: failed to set file mode");
383:
384: (void)close(fd);
385: }
386:
387: void
388: cvs_server_useunchanged(char *data)
389: {
390: }
391:
392: void
393: cvs_server_unchanged(char *data)
394: {
1.41 xsa 395: int fd;
1.51 otto 396: char fpath[MAXPATHLEN];
1.29 joris 397: CVSENTRIES *entlist;
398: struct cvs_ent *ent;
399: struct timeval tv[2];
400:
1.57 ray 401: if (data == NULL)
402: fatal("Missing argument for Unchanged");
403:
1.54 xsa 404: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", server_currentdir, data);
1.29 joris 405:
406: if ((fd = open(fpath, O_RDWR | O_CREAT | O_TRUNC)) == -1)
407: fatal("cvs_server_unchanged: %s: %s", fpath, strerror(errno));
408:
409: entlist = cvs_ent_open(server_currentdir);
410: ent = cvs_ent_get(entlist, data);
411: if (ent == NULL)
412: fatal("received Unchanged request for non-existing file");
413: cvs_ent_close(entlist, ENT_NOSYNC);
414:
1.52 joris 415: tv[0].tv_sec = ent->ce_mtime;
1.29 joris 416: tv[0].tv_usec = 0;
417: tv[1] = tv[0];
418: if (futimes(fd, tv) == -1)
419: fatal("cvs_server_unchanged: failed to set modified time");
420:
421: if (fchmod(fd, 0600) == -1)
422: fatal("cvs_server_unchanged: failed to set mode");
423:
424: cvs_ent_free(ent);
425: (void)close(fd);
426: }
427:
428: void
429: cvs_server_questionable(char *data)
430: {
431: }
432:
433: void
434: cvs_server_argument(char *data)
435: {
1.60 ! ray 436: if (server_argc >= CVS_CMD_MAXARG)
1.29 joris 437: fatal("cvs_server_argument: too many arguments sent");
1.57 ray 438:
439: if (data == NULL)
440: fatal("Missing argument for Argument");
1.29 joris 441:
442: server_argv[server_argc++] = xstrdup(data);
1.37 xsa 443: }
444:
445: void
446: cvs_server_argumentx(char *data)
447: {
1.44 xsa 448: }
449:
450: void
451: cvs_server_update_patches(char *data)
452: {
453: /*
454: * This does not actually do anything.
455: * It is used to tell that the server is able to
456: * generate patches when given an `update' request.
457: * The client must issue the -u argument to `update'
458: * to receive patches.
459: */
1.29 joris 460: }
461:
462: void
1.31 xsa 463: cvs_server_add(char *data)
464: {
465: if (chdir(server_currentdir) == -1)
466: fatal("cvs_server_add: %s", strerror(errno));
467:
468: cvs_cmdop = CVS_OP_ADD;
469: cvs_add(server_argc, server_argv);
1.50 joris 470: cvs_server_send_response("ok");
471: }
472:
473: void
474: cvs_server_import(char *data)
475: {
476: if (chdir(server_currentdir) == -1)
477: fatal("cvs_server_import: %s", strerror(errno));
478:
479: cvs_cmdop = CVS_OP_IMPORT;
480: cvs_import(server_argc, server_argv);
1.31 xsa 481: cvs_server_send_response("ok");
482: }
1.35 xsa 483:
484: void
485: cvs_server_admin(char *data)
486: {
487: if (chdir(server_currentdir) == -1)
488: fatal("cvs_server_admin: %s", strerror(errno));
489:
490: cvs_cmdop = CVS_OP_ADMIN;
491: cvs_admin(server_argc, server_argv);
492: cvs_server_send_response("ok");
493: }
494:
1.40 xsa 495: void
496: cvs_server_annotate(char *data)
497: {
498: if (chdir(server_currentdir) == -1)
499: fatal("cvs_server_annotate: %s", strerror(errno));
500:
501: cvs_cmdop = CVS_OP_ANNOTATE;
502: cvs_annotate(server_argc, server_argv);
503: cvs_server_send_response("ok");
504: }
1.31 xsa 505:
506: void
1.29 joris 507: cvs_server_commit(char *data)
508: {
509: if (chdir(server_currentdir) == -1)
510: fatal("cvs_server_commit: %s", strerror(errno));
511:
512: cvs_cmdop = CVS_OP_COMMIT;
513: cvs_commit(server_argc, server_argv);
1.49 joris 514: cvs_server_send_response("ok");
515: }
516:
517: void
518: cvs_server_checkout(char *data)
519: { if (chdir(server_currentdir) == -1)
520: fatal("cvs_server_checkout: %s", strerror(errno));
521:
522: cvs_cmdop = CVS_OP_CHECKOUT;
523: cvs_checkout(server_argc, server_argv);
1.29 joris 524: cvs_server_send_response("ok");
525: }
526:
527: void
528: cvs_server_diff(char *data)
529: {
530: if (chdir(server_currentdir) == -1)
531: fatal("cvs_server_diff: %s", strerror(errno));
532:
533: cvs_cmdop = CVS_OP_DIFF;
534: cvs_diff(server_argc, server_argv);
1.34 xsa 535: cvs_server_send_response("ok");
536: }
537:
538: void
539: cvs_server_init(char *data)
540: {
541: if (chdir(server_currentdir) == -1)
542: fatal("cvs_server_init: %s", strerror(errno));
543:
544: cvs_cmdop = CVS_OP_INIT;
545: cvs_init(server_argc, server_argv);
1.31 xsa 546: cvs_server_send_response("ok");
547: }
548:
549: void
550: cvs_server_remove(char *data)
551: {
552: if (chdir(server_currentdir) == -1)
553: fatal("cvs_server_remove: %s", strerror(errno));
554:
555: cvs_cmdop = CVS_OP_REMOVE;
556: cvs_remove(server_argc, server_argv);
1.29 joris 557: cvs_server_send_response("ok");
558: }
559:
560: void
561: cvs_server_status(char *data)
562: {
563: if (chdir(server_currentdir) == -1)
564: fatal("cvs_server_status: %s", strerror(errno));
565:
566: cvs_cmdop = CVS_OP_STATUS;
567: cvs_status(server_argc, server_argv);
568: cvs_server_send_response("ok");
569: }
570:
571: void
572: cvs_server_log(char *data)
573: {
574: if (chdir(server_currentdir) == -1)
575: fatal("cvs_server_log: %s", strerror(errno));
576:
577: cvs_cmdop = CVS_OP_LOG;
1.32 xsa 578: cvs_getlog(server_argc, server_argv);
579: cvs_server_send_response("ok");
580: }
581:
582: void
583: cvs_server_tag(char *data)
584: {
585: if (chdir(server_currentdir) == -1)
586: fatal("cvs_server_tag: %s", strerror(errno));
587:
588: cvs_cmdop = CVS_OP_TAG;
1.33 xsa 589: cvs_tag(server_argc, server_argv);
1.29 joris 590: cvs_server_send_response("ok");
591: }
592:
593: void
594: cvs_server_update(char *data)
595: {
596: if (chdir(server_currentdir) == -1)
597: fatal("cvs_server_update: %s", strerror(errno));
1.14 joris 598:
1.29 joris 599: cvs_cmdop = CVS_OP_UPDATE;
600: cvs_update(server_argc, server_argv);
1.36 xsa 601: cvs_server_send_response("ok");
602: }
603:
604: void
605: cvs_server_version(char *data)
606: {
607: cvs_cmdop = CVS_OP_VERSION;
608: cvs_version(server_argc, server_argv);
1.29 joris 609: cvs_server_send_response("ok");
1.47 joris 610: }
611:
612: void
613: cvs_server_update_entry(const char *resp, struct cvs_file *cf)
614: {
1.58 ray 615: char *p;
1.47 joris 616:
617: if ((p = strrchr(cf->file_rpath, ',')) != NULL)
618: *p = '\0';
619:
1.58 ray 620: cvs_server_send_response("%s %s/", resp, cf->file_wd);
1.47 joris 621: cvs_remote_output(cf->file_rpath);
622:
623: if (p != NULL)
624: *p = ',';
1.1 jfb 625: }