[BACK]Return to proto.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/proto.c, Revision 1.69

1.69    ! moritz      1: /*     $OpenBSD: proto.c,v 1.68 2005/07/25 12:13:08 xsa Exp $  */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26: /*
                     27:  * CVS client/server protocol
                     28:  * ==========================
                     29:  *
                     30:  * The following code implements the CVS client/server protocol, which is
                     31:  * documented at the following URL:
                     32:  *     http://www.loria.fr/~molli/cvs/doc/cvsclient_toc.html
                     33:  *
                     34:  * The protocol is split up into two parts; the first part is the client side
                     35:  * of things and is composed of all the response handlers, which are all named
                     36:  * with a prefix of "cvs_resp_".  The second part is the set of request
                     37:  * handlers used by the server.  These handlers process the request and
                     38:  * generate the appropriate response to send back.  The prefix for request
                     39:  * handlers is "cvs_req_".
                     40:  *
                     41:  */
                     42:
                     43: #include <sys/types.h>
                     44: #include <sys/stat.h>
                     45:
1.56      xsa        46: #include <errno.h>
1.1       jfb        47: #include <fcntl.h>
1.56      xsa        48: #include <libgen.h>
                     49: #include <pwd.h>
1.1       jfb        50: #include <stdio.h>
                     51: #include <stdlib.h>
1.56      xsa        52: #include <string.h>
1.1       jfb        53: #include <unistd.h>
                     54:
                     55: #include "cvs.h"
                     56: #include "log.h"
1.13      jfb        57: #include "proto.h"
1.1       jfb        58:
                     59:
1.11      jfb        60: /* request flags */
1.68      xsa        61: #define CVS_REQF_RESP  0x01
1.11      jfb        62:
                     63:
1.68      xsa        64: static int     cvs_initlog(void);
1.1       jfb        65:
1.18      jfb        66: struct cvs_req cvs_requests[] = {
                     67:        { CVS_REQ_DIRECTORY,     "Directory",         0             },
                     68:        { CVS_REQ_MAXDOTDOT,     "Max-dotdot",        0             },
                     69:        { CVS_REQ_STATICDIR,     "Static-directory",  0             },
                     70:        { CVS_REQ_STICKY,        "Sticky",            0             },
                     71:        { CVS_REQ_ENTRY,         "Entry",             0             },
                     72:        { CVS_REQ_ENTRYEXTRA,    "EntryExtra",        0             },
                     73:        { CVS_REQ_CHECKINTIME,   "Checkin-time",      0             },
                     74:        { CVS_REQ_MODIFIED,      "Modified",          0             },
                     75:        { CVS_REQ_ISMODIFIED,    "Is-modified",       0             },
                     76:        { CVS_REQ_UNCHANGED,     "Unchanged",         0             },
                     77:        { CVS_REQ_USEUNCHANGED,  "UseUnchanged",      0             },
                     78:        { CVS_REQ_NOTIFY,        "Notify",            0             },
                     79:        { CVS_REQ_NOTIFYUSER,    "NotifyUser",        0             },
                     80:        { CVS_REQ_QUESTIONABLE,  "Questionable",      0             },
                     81:        { CVS_REQ_CASE,          "Case",              0             },
                     82:        { CVS_REQ_UTF8,          "Utf8",              0             },
                     83:        { CVS_REQ_ARGUMENT,      "Argument",          0             },
                     84:        { CVS_REQ_ARGUMENTX,     "Argumentx",         0             },
                     85:        { CVS_REQ_GLOBALOPT,     "Global_option",     0             },
                     86:        { CVS_REQ_GZIPSTREAM,    "Gzip-stream",       0             },
                     87:        { CVS_REQ_READCVSRC2,    "read-cvsrc2",       0             },
                     88:        { CVS_REQ_READWRAP,      "read-cvswrappers",  0             },
                     89:        { CVS_REQ_READIGNORE,    "read-cvsignore",    0             },
                     90:        { CVS_REQ_ERRIFREADER,   "Error-If-Reader",   0             },
                     91:        { CVS_REQ_VALIDRCSOPT,   "Valid-RcsOptions",  0             },
                     92:        { CVS_REQ_SET,           "Set",               0             },
                     93:        { CVS_REQ_XPANDMOD,      "expand-modules",    CVS_REQF_RESP },
1.26      jfb        94:        { CVS_REQ_LOG,           "log",               CVS_REQF_RESP },
1.18      jfb        95:        { CVS_REQ_CO,            "co",                CVS_REQF_RESP },
                     96:        { CVS_REQ_EXPORT,        "export",            CVS_REQF_RESP },
1.26      jfb        97:        { CVS_REQ_ANNOTATE,      "annotate",          CVS_REQF_RESP },
                     98:        { CVS_REQ_RDIFF,         "rdiff",             CVS_REQF_RESP },
1.18      jfb        99:        { CVS_REQ_RTAG,          "rtag",              CVS_REQF_RESP },
                    100:        { CVS_REQ_INIT,          "init",              CVS_REQF_RESP },
                    101:        { CVS_REQ_STATUS,        "status",            CVS_REQF_RESP },
                    102:        { CVS_REQ_UPDATE,        "update",            CVS_REQF_RESP },
1.26      jfb       103:        { CVS_REQ_HISTORY,       "history",           CVS_REQF_RESP },
1.18      jfb       104:        { CVS_REQ_IMPORT,        "import",            CVS_REQF_RESP },
                    105:        { CVS_REQ_ADD,           "add",               CVS_REQF_RESP },
                    106:        { CVS_REQ_REMOVE,        "remove",            CVS_REQF_RESP },
                    107:        { CVS_REQ_RELEASE,       "release",           CVS_REQF_RESP },
                    108:        { CVS_REQ_ROOT,          "Root",              0             },
                    109:        { CVS_REQ_VALIDRESP,     "Valid-responses",   0             },
                    110:        { CVS_REQ_VALIDREQ,      "valid-requests",    CVS_REQF_RESP },
                    111:        { CVS_REQ_VERSION,       "version",           CVS_REQF_RESP },
                    112:        { CVS_REQ_NOOP,          "noop",              CVS_REQF_RESP },
                    113:        { CVS_REQ_DIFF,          "diff",              CVS_REQF_RESP },
1.29      jfb       114:        { CVS_REQ_CI,            "ci",                CVS_REQF_RESP },
1.35      jfb       115:        { CVS_REQ_TAG,           "tag",               CVS_REQF_RESP },
1.41      joris     116:        { CVS_REQ_ADMIN,         "admin",             CVS_REQF_RESP },
1.1       jfb       117: };
                    118:
1.18      jfb       119: struct cvs_resp cvs_responses[] = {
                    120:        { CVS_RESP_OK,         "ok"                     },
                    121:        { CVS_RESP_ERROR,      "error"                  },
                    122:        { CVS_RESP_VALIDREQ,   "Valid-requests"         },
                    123:        { CVS_RESP_M,          "M"                      },
                    124:        { CVS_RESP_MBINARY,    "Mbinary"                },
                    125:        { CVS_RESP_MT,         "MT"                     },
                    126:        { CVS_RESP_E,          "E"                      },
                    127:        { CVS_RESP_F,          "F"                      },
                    128:        { CVS_RESP_CREATED,    "Created"                },
                    129:        { CVS_RESP_UPDATED,    "Updated"                },
                    130:        { CVS_RESP_UPDEXIST,   "Update-existing"        },
                    131:        { CVS_RESP_MERGED,     "Merged"                 },
                    132:        { CVS_RESP_REMOVED,    "Removed"                },
1.63      joris     133:        { CVS_RESP_RMENTRY,    "Remove-entry"           },
1.18      jfb       134:        { CVS_RESP_CKSUM,      "Checksum"               },
                    135:        { CVS_RESP_CLRSTATDIR, "Clear-static-directory" },
                    136:        { CVS_RESP_SETSTATDIR, "Set-static-directory"   },
                    137:        { CVS_RESP_NEWENTRY,   "New-entry"              },
                    138:        { CVS_RESP_CHECKEDIN,  "Checked-in"             },
                    139:        { CVS_RESP_MODE,       "Mode"                   },
                    140:        { CVS_RESP_MODTIME,    "Mod-time"               },
                    141:        { CVS_RESP_MODXPAND,   "Module-expansion"       },
                    142:        { CVS_RESP_SETSTICKY,  "Set-sticky"             },
                    143:        { CVS_RESP_CLRSTICKY,  "Clear-sticky"           },
                    144:        { CVS_RESP_RCSDIFF,    "Rcs-diff"               },
                    145:        { CVS_RESP_TEMPLATE,   "Template"               },
1.34      jfb       146:        { CVS_RESP_COPYFILE,   "Copy-file"              },
1.1       jfb       147: };
                    148:
1.68      xsa       149: #define CVS_NBREQ      (sizeof(cvs_requests)/sizeof(cvs_requests[0]))
                    150: #define CVS_NBRESP     (sizeof(cvs_responses)/sizeof(cvs_responses[0]))
1.17      jfb       151:
                    152: /* hack to receive the remote version without outputting it */
1.18      jfb       153: u_int cvs_version_sent = 0;
1.17      jfb       154:
1.13      jfb       155: static char  cvs_proto_buf[4096];
                    156:
                    157: /*
1.37      david     158:  * Output files for protocol logging when the CVS_CLIENT_LOG environment
1.13      jfb       159:  * variable is set.
                    160:  */
                    161: static int   cvs_server_logon = 0;
                    162: static FILE *cvs_server_inlog = NULL;
                    163: static FILE *cvs_server_outlog = NULL;
                    164:
1.28      jfb       165: static pid_t cvs_subproc_pid;
                    166:
1.32      jfb       167: /* last directory sent with cvs_senddir() */
                    168: static char cvs_lastdir[MAXPATHLEN] = "";
                    169:
1.13      jfb       170:
                    171: /*
                    172:  * cvs_connect()
                    173:  *
                    174:  * Open a client connection to the cvs server whose address is given in
                    175:  * the <root> variable.  The method used to connect depends on the
                    176:  * setting of the CVS_RSH variable.
1.17      jfb       177:  * Once the connection has been established, we first send the list of
                    178:  * responses we support and request the list of supported requests from the
                    179:  * server.  Then, a version request is sent and various global flags are sent.
1.13      jfb       180:  * Returns 0 on success, or -1 on failure.
                    181:  */
                    182: int
                    183: cvs_connect(struct cvsroot *root)
                    184: {
1.20      jfb       185:        int argc, infd[2], outfd[2], errfd[2];
1.13      jfb       186:        char *argv[16], *cvs_server_cmd, *vresp;
                    187:
1.36      jfb       188:        if (root->cr_method == CVS_METHOD_PSERVER) {
                    189:                cvs_log(LP_ERR, "no pserver support due to security issues");
                    190:                return (-1);
                    191:        } else if ((root->cr_method == CVS_METHOD_KSERVER) ||
                    192:            (root->cr_method == CVS_METHOD_GSERVER) ||
                    193:            (root->cr_method == CVS_METHOD_EXT) ||
                    194:            (root->cr_method == CVS_METHOD_FORK)) {
                    195:                cvs_log(LP_ERR, "connection method not supported yet");
                    196:                return (-1);
                    197:        }
1.33      jfb       198:
                    199:        if (root->cr_flags & CVS_ROOT_CONNECTED) {
                    200:                cvs_log(LP_NOTICE, "already connected to CVSROOT");
                    201:                return (0);
                    202:        }
                    203:
1.13      jfb       204:        if (pipe(infd) == -1) {
                    205:                cvs_log(LP_ERRNO,
                    206:                    "failed to create input pipe for client connection");
                    207:                return (-1);
                    208:        }
                    209:
                    210:        if (pipe(outfd) == -1) {
                    211:                cvs_log(LP_ERRNO,
                    212:                    "failed to create output pipe for client connection");
                    213:                (void)close(infd[0]);
                    214:                (void)close(infd[1]);
                    215:                return (-1);
                    216:        }
                    217:
1.20      jfb       218:        if (pipe(errfd) == -1) {
                    219:                cvs_log(LP_ERRNO,
                    220:                    "failed to create error pipe for client connection");
                    221:                (void)close(infd[0]);
                    222:                (void)close(infd[1]);
                    223:                (void)close(outfd[0]);
                    224:                (void)close(outfd[1]);
                    225:                return (-1);
                    226:        }
                    227:
1.28      jfb       228:        cvs_subproc_pid = fork();
                    229:        if (cvs_subproc_pid == -1) {
1.13      jfb       230:                cvs_log(LP_ERRNO, "failed to fork for cvs server connection");
                    231:                return (-1);
1.30      deraadt   232:        } else if (cvs_subproc_pid == 0) {
1.13      jfb       233:                if ((dup2(infd[0], STDIN_FILENO) == -1) ||
1.23      jfb       234:                    (dup2(outfd[1], STDOUT_FILENO) == -1)) {
1.13      jfb       235:                        cvs_log(LP_ERRNO,
                    236:                            "failed to setup standard streams for cvs server");
                    237:                        return (-1);
                    238:                }
                    239:                (void)close(infd[1]);
                    240:                (void)close(outfd[0]);
1.20      jfb       241:                (void)close(errfd[0]);
1.13      jfb       242:
                    243:                argc = 0;
                    244:                argv[argc++] = cvs_rsh;
                    245:
                    246:                if (root->cr_user != NULL) {
                    247:                        argv[argc++] = "-l";
                    248:                        argv[argc++] = root->cr_user;
                    249:                }
                    250:
                    251:                cvs_server_cmd = getenv("CVS_SERVER");
                    252:                if (cvs_server_cmd == NULL)
1.20      jfb       253:                        cvs_server_cmd = CVS_SERVER_DEFAULT;
1.13      jfb       254:
                    255:                argv[argc++] = root->cr_host;
                    256:                argv[argc++] = cvs_server_cmd;
                    257:                argv[argc++] = "server";
                    258:                argv[argc] = NULL;
                    259:
                    260:                execvp(argv[0], argv);
                    261:                cvs_log(LP_ERRNO, "failed to exec");
1.46      xsa       262:                exit(1);
1.13      jfb       263:        }
                    264:
                    265:        /* we are the parent */
                    266:        (void)close(infd[0]);
                    267:        (void)close(outfd[1]);
1.20      jfb       268:        (void)close(errfd[1]);
1.13      jfb       269:
                    270:        root->cr_srvin = fdopen(infd[1], "w");
                    271:        if (root->cr_srvin == NULL) {
                    272:                cvs_log(LP_ERRNO, "failed to create pipe stream");
                    273:                return (-1);
                    274:        }
                    275:
                    276:        root->cr_srvout = fdopen(outfd[0], "r");
                    277:        if (root->cr_srvout == NULL) {
                    278:                cvs_log(LP_ERRNO, "failed to create pipe stream");
1.20      jfb       279:                return (-1);
                    280:        }
                    281:
1.13      jfb       282:        /* make the streams line-buffered */
                    283:        (void)setvbuf(root->cr_srvin, NULL, _IOLBF, 0);
                    284:        (void)setvbuf(root->cr_srvout, NULL, _IOLBF, 0);
                    285:
                    286:        cvs_initlog();
                    287:
                    288:        /*
                    289:         * Send the server the list of valid responses, then ask for valid
                    290:         * requests.
                    291:         */
                    292:
1.40      jfb       293:        if ((vresp = cvs_resp_getvalid()) == NULL) {
1.13      jfb       294:                cvs_log(LP_ERR, "can't generate list of valid responses");
                    295:                return (-1);
                    296:        }
                    297:
                    298:        if (cvs_sendreq(root, CVS_REQ_VALIDRESP, vresp) < 0) {
1.40      jfb       299:                cvs_log(LP_ERR, "failed to get valid responses");
                    300:                free(vresp);
                    301:                return (-1);
1.13      jfb       302:        }
                    303:        free(vresp);
                    304:
                    305:        if (cvs_sendreq(root, CVS_REQ_VALIDREQ, NULL) < 0) {
                    306:                cvs_log(LP_ERR, "failed to get valid requests from server");
                    307:                return (-1);
                    308:        }
                    309:
1.62      xsa       310:        /* send the CVSROOT to the server */
                    311:        if (cvs_sendreq(root, CVS_REQ_ROOT, root->cr_dir) < 0)
1.54      joris     312:                return (-1);
                    313:
1.40      jfb       314:        /* don't fail if this request doesn't work */
1.17      jfb       315:        if (cvs_sendreq(root, CVS_REQ_VERSION, NULL) < 0)
1.40      jfb       316:                cvs_log(LP_WARN, "failed to get remote version");
1.17      jfb       317:
1.13      jfb       318:        /* now share our global options with the server */
1.61      joris     319:        if ((verbosity <= 1) &&
1.40      jfb       320:            (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-q") < 0))
                    321:                return (-1);
1.61      joris     322:        if ((verbosity == 0) &&
1.40      jfb       323:            (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-Q") < 0))
                    324:                return (-1);
                    325:
1.66      xsa       326:        if ((cvs_noexec == 1) &&
                    327:            (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-n") < 0))
1.53      jfb       328:                return (-1);
1.66      xsa       329:        if ((cvs_nolog == 1) &&
                    330:            (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-l") < 0))
1.40      jfb       331:                return (-1);
1.66      xsa       332:        if ((cvs_readonly == 1) &&
                    333:            (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-r") < 0))
1.40      jfb       334:                return (-1);
1.66      xsa       335:        if ((cvs_trace == 1) &&
                    336:            (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-t") < 0))
1.13      jfb       337:                return (-1);
                    338:
                    339:        /* not sure why, but we have to send this */
                    340:        if (cvs_sendreq(root, CVS_REQ_USEUNCHANGED, NULL) < 0)
                    341:                return (-1);
                    342:
                    343:        cvs_log(LP_DEBUG, "connected to %s", root->cr_host);
                    344:
1.33      jfb       345:        root->cr_flags |= CVS_ROOT_CONNECTED;
                    346:
1.13      jfb       347:        return (0);
                    348: }
                    349:
                    350:
                    351: /*
                    352:  * cvs_disconnect()
                    353:  *
                    354:  * Disconnect from the cvs server.
                    355:  */
                    356: void
                    357: cvs_disconnect(struct cvsroot *root)
                    358: {
1.33      jfb       359:        if (!(root->cr_flags & CVS_ROOT_CONNECTED))
                    360:                return;
                    361:
1.13      jfb       362:        cvs_log(LP_DEBUG, "closing connection to %s", root->cr_host);
                    363:        if (root->cr_srvin != NULL) {
                    364:                (void)fclose(root->cr_srvin);
                    365:                root->cr_srvin = NULL;
                    366:        }
                    367:        if (root->cr_srvout != NULL) {
                    368:                (void)fclose(root->cr_srvout);
                    369:                root->cr_srvout = NULL;
                    370:        }
1.33      jfb       371:
                    372:        root->cr_flags &= ~CVS_ROOT_CONNECTED;
1.13      jfb       373: }
                    374:
1.1       jfb       375:
                    376: /*
                    377:  * cvs_req_getbyid()
                    378:  *
                    379:  */
1.13      jfb       380: struct cvs_req*
1.1       jfb       381: cvs_req_getbyid(int reqid)
                    382: {
                    383:        u_int i;
                    384:
                    385:        for (i = 0; i < CVS_NBREQ; i++)
                    386:                if (cvs_requests[i].req_id == reqid)
1.13      jfb       387:                        return &(cvs_requests[i]);
1.31      tedu      388:
1.1       jfb       389:        return (NULL);
                    390: }
                    391:
                    392:
                    393: /*
                    394:  * cvs_req_getbyname()
                    395:  */
1.13      jfb       396: struct cvs_req*
1.1       jfb       397: cvs_req_getbyname(const char *rname)
                    398: {
                    399:        u_int i;
                    400:
                    401:        for (i = 0; i < CVS_NBREQ; i++)
                    402:                if (strcmp(cvs_requests[i].req_str, rname) == 0)
1.13      jfb       403:                        return &(cvs_requests[i]);
1.1       jfb       404:
1.13      jfb       405:        return (NULL);
1.1       jfb       406: }
                    407:
                    408:
                    409: /*
                    410:  * cvs_req_getvalid()
                    411:  *
                    412:  * Build a space-separated list of all the requests that this protocol
                    413:  * implementation supports.
                    414:  */
1.67      xsa       415: char *
1.1       jfb       416: cvs_req_getvalid(void)
                    417: {
                    418:        u_int i;
                    419:        size_t len;
                    420:        char *vrstr;
                    421:        BUF *buf;
                    422:
                    423:        buf = cvs_buf_alloc(512, BUF_AUTOEXT);
                    424:        if (buf == NULL)
                    425:                return (NULL);
                    426:
                    427:        cvs_buf_set(buf, cvs_requests[0].req_str,
                    428:            strlen(cvs_requests[0].req_str), 0);
                    429:
                    430:        for (i = 1; i < CVS_NBREQ; i++) {
                    431:                if ((cvs_buf_putc(buf, ' ') < 0) ||
                    432:                    (cvs_buf_append(buf, cvs_requests[i].req_str,
                    433:                    strlen(cvs_requests[i].req_str)) < 0)) {
                    434:                        cvs_buf_free(buf);
                    435:                        return (NULL);
                    436:                }
                    437:        }
                    438:
                    439:        /* NUL-terminate */
                    440:        if (cvs_buf_putc(buf, '\0') < 0) {
                    441:                cvs_buf_free(buf);
                    442:                return (NULL);
                    443:        }
                    444:
                    445:        len = cvs_buf_size(buf);
                    446:        vrstr = (char *)malloc(len);
1.27      jfb       447:        if (vrstr == NULL) {
                    448:                cvs_buf_free(buf);
                    449:                return (NULL);
                    450:        }
1.1       jfb       451:
                    452:        cvs_buf_copy(buf, 0, vrstr, len);
                    453:        cvs_buf_free(buf);
                    454:
                    455:        return (vrstr);
                    456: }
                    457:
                    458:
                    459: /*
                    460:  * cvs_resp_getbyid()
                    461:  *
                    462:  */
1.13      jfb       463: struct cvs_resp*
1.1       jfb       464: cvs_resp_getbyid(int respid)
                    465: {
                    466:        u_int i;
                    467:
1.44      tedu      468:        for (i = 0; i < CVS_NBRESP; i++)
1.13      jfb       469:                if (cvs_responses[i].resp_id == (u_int)respid)
                    470:                        return &(cvs_responses[i]);
1.31      tedu      471:
1.1       jfb       472:        return (NULL);
                    473: }
                    474:
                    475:
                    476: /*
                    477:  * cvs_resp_getbyname()
                    478:  */
1.67      xsa       479: struct cvs_resp *
1.1       jfb       480: cvs_resp_getbyname(const char *rname)
                    481: {
                    482:        u_int i;
                    483:
1.44      tedu      484:        for (i = 0; i < CVS_NBRESP; i++)
1.1       jfb       485:                if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13      jfb       486:                        return &(cvs_responses[i]);
1.1       jfb       487:
1.13      jfb       488:        return (NULL);
1.1       jfb       489: }
                    490:
                    491:
                    492: /*
                    493:  * cvs_resp_getvalid()
                    494:  *
                    495:  * Build a space-separated list of all the responses that this protocol
                    496:  * implementation supports.
                    497:  */
1.67      xsa       498: char *
1.1       jfb       499: cvs_resp_getvalid(void)
                    500: {
                    501:        u_int i;
                    502:        size_t len;
                    503:        char *vrstr;
                    504:        BUF *buf;
                    505:
                    506:        buf = cvs_buf_alloc(512, BUF_AUTOEXT);
                    507:        if (buf == NULL)
                    508:                return (NULL);
                    509:
                    510:        cvs_buf_set(buf, cvs_responses[0].resp_str,
                    511:            strlen(cvs_responses[0].resp_str), 0);
                    512:
                    513:        for (i = 1; i < CVS_NBRESP; i++) {
                    514:                if ((cvs_buf_putc(buf, ' ') < 0) ||
                    515:                    (cvs_buf_append(buf, cvs_responses[i].resp_str,
                    516:                    strlen(cvs_responses[i].resp_str)) < 0)) {
                    517:                        cvs_buf_free(buf);
                    518:                        return (NULL);
                    519:                }
                    520:        }
                    521:
                    522:        /* NUL-terminate */
                    523:        if (cvs_buf_putc(buf, '\0') < 0) {
                    524:                cvs_buf_free(buf);
                    525:                return (NULL);
                    526:        }
                    527:
                    528:        len = cvs_buf_size(buf);
                    529:        vrstr = (char *)malloc(len);
1.27      jfb       530:        if (vrstr == NULL) {
                    531:                cvs_buf_free(buf);
                    532:                return (NULL);
                    533:        }
1.1       jfb       534:
                    535:        cvs_buf_copy(buf, 0, vrstr, len);
                    536:        cvs_buf_free(buf);
                    537:
                    538:        return (vrstr);
                    539: }
                    540:
                    541:
                    542: /*
                    543:  * cvs_sendfile()
                    544:  *
                    545:  * Send the mode and size of a file followed by the file's contents.
                    546:  * Returns 0 on success, or -1 on failure.
                    547:  */
                    548: int
1.13      jfb       549: cvs_sendfile(struct cvsroot *root, const char *path)
1.1       jfb       550: {
1.51      xsa       551:        int fd, l;
1.1       jfb       552:        ssize_t ret;
                    553:        char buf[4096];
                    554:        struct stat st;
                    555:
1.38      jfb       556:        cvs_log(LP_TRACE, "Sending file `%s' to server", basename(path));
                    557:
1.1       jfb       558:        if (stat(path, &st) == -1) {
                    559:                cvs_log(LP_ERRNO, "failed to stat `%s'", path);
                    560:                return (-1);
                    561:        }
                    562:
1.24      jfb       563:        if (cvs_modetostr(st.st_mode, buf, sizeof(buf)) < 0)
                    564:                return (-1);
                    565:
1.1       jfb       566:        fd = open(path, O_RDONLY, 0);
                    567:        if (fd == -1) {
1.38      jfb       568:                cvs_log(LP_ERRNO, "failed to open `%s'", path);
1.1       jfb       569:                return (-1);
                    570:        }
                    571:
1.38      jfb       572:        if (cvs_sendln(root, buf) < 0) {
                    573:                (void)close(fd);
                    574:                return (-1);
                    575:        }
1.51      xsa       576:        l = snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
                    577:        if (l == -1 || l >= (int)sizeof(buf))
                    578:                return (-1);
                    579:
1.38      jfb       580:        if (cvs_sendln(root, buf) < 0) {
                    581:                (void)close(fd);
                    582:                return (-1);
                    583:        }
1.1       jfb       584:
                    585:        while ((ret = read(fd, buf, sizeof(buf))) != 0) {
                    586:                if (ret == -1) {
1.24      jfb       587:                        (void)close(fd);
1.1       jfb       588:                        cvs_log(LP_ERRNO, "failed to read file `%s'", path);
                    589:                        return (-1);
                    590:                }
                    591:
1.38      jfb       592:                if (cvs_sendraw(root, buf, (size_t)ret) < 0) {
                    593:                        (void)close(fd);
                    594:                        return (-1);
                    595:                }
1.1       jfb       596:        }
                    597:
                    598:        (void)close(fd);
                    599:        return (0);
                    600: }
                    601:
                    602:
                    603: /*
                    604:  * cvs_recvfile()
                    605:  *
                    606:  * Receive the mode and size of a file followed the file's contents and
                    607:  * create or update the file whose path is <path> with the received
                    608:  * information.
                    609:  */
1.14      jfb       610: BUF*
                    611: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1       jfb       612: {
                    613:        size_t len;
                    614:        ssize_t ret;
                    615:        off_t fsz, cnt;
                    616:        char buf[4096], *ep;
1.14      jfb       617:        BUF *fbuf;
                    618:
                    619:        fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
                    620:        if (fbuf == NULL)
                    621:                return (NULL);
1.1       jfb       622:
1.13      jfb       623:        if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14      jfb       624:            (cvs_strtomode(buf, mode) < 0)) {
1.44      tedu      625:                cvs_buf_free(fbuf);
1.14      jfb       626:                return (NULL);
1.1       jfb       627:        }
                    628:
1.50      joris     629:        if (cvs_getln(root, buf, sizeof(buf)) < 0) {
                    630:                cvs_buf_free(fbuf);
                    631:                return (NULL);
                    632:        }
1.1       jfb       633:
                    634:        fsz = (off_t)strtol(buf, &ep, 10);
                    635:        if (*ep != '\0') {
                    636:                cvs_log(LP_ERR, "parse error in file size transmission");
1.44      tedu      637:                cvs_buf_free(fbuf);
1.14      jfb       638:                return (NULL);
1.1       jfb       639:        }
                    640:
                    641:        cnt = 0;
                    642:        do {
                    643:                len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6       jfb       644:                if (len == 0)
                    645:                        break;
1.13      jfb       646:                ret = cvs_recvraw(root, buf, len);
1.1       jfb       647:                if (ret == -1) {
1.14      jfb       648:                        cvs_buf_free(fbuf);
                    649:                        return (NULL);
1.1       jfb       650:                }
                    651:
1.14      jfb       652:                if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
                    653:                        cvs_log(LP_ERR,
                    654:                            "failed to append received file data");
                    655:                        cvs_buf_free(fbuf);
                    656:                        return (NULL);
1.1       jfb       657:                }
                    658:
                    659:                cnt += (off_t)ret;
                    660:        } while (cnt < fsz);
                    661:
1.14      jfb       662:        return (fbuf);
1.1       jfb       663: }
1.12      jfb       664:
                    665: /*
                    666:  * cvs_sendreq()
                    667:  *
                    668:  * Send a request to the server of type <rid>, with optional arguments
                    669:  * contained in <arg>, which should not be terminated by a newline.
                    670:  * Returns 0 on success, or -1 on failure.
                    671:  */
                    672: int
1.13      jfb       673: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12      jfb       674: {
1.51      xsa       675:        int ret, l;
1.13      jfb       676:        struct cvs_req *req;
1.12      jfb       677:
                    678:        if (root->cr_srvin == NULL) {
1.36      jfb       679:                cvs_log(LP_ERR, "cannot send request %u: Not connected", rid);
1.12      jfb       680:                return (-1);
                    681:        }
                    682:
1.13      jfb       683:        req = cvs_req_getbyid(rid);
                    684:        if (req == NULL) {
1.12      jfb       685:                cvs_log(LP_ERR, "unsupported request type %u", rid);
                    686:                return (-1);
                    687:        }
                    688:
1.18      jfb       689:        /* is this request supported by the server? */
                    690:        if (!CVS_GETVR(root, req->req_id)) {
1.57      joris     691:                if (rid == CVS_REQ_VERSION) {
                    692:                        ret = cvs_sendreq(root, CVS_REQ_NOOP, arg);
                    693:                } else {
                    694:                        cvs_log(LP_WARN,
                    695:                            "remote end does not support request `%s'",
                    696:                            req->req_str);
                    697:                        ret = -1;
                    698:                }
                    699:                return (ret);
1.18      jfb       700:        }
                    701:
1.51      xsa       702:        l = snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
1.13      jfb       703:            req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.51      xsa       704:        if (l == -1 || l >= (int)sizeof(cvs_proto_buf))
                    705:                return (-1);
1.12      jfb       706:
                    707:        if (cvs_server_inlog != NULL)
1.13      jfb       708:                fputs(cvs_proto_buf, cvs_server_inlog);
1.12      jfb       709:
1.13      jfb       710:        ret = fputs(cvs_proto_buf, root->cr_srvin);
1.12      jfb       711:        if (ret == EOF) {
                    712:                cvs_log(LP_ERRNO, "failed to send request to server");
                    713:                return (-1);
                    714:        }
1.17      jfb       715:
                    716:        if (rid == CVS_REQ_VERSION)
                    717:                cvs_version_sent = 1;
1.12      jfb       718:
1.13      jfb       719:        if (req->req_flags & CVS_REQF_RESP)
1.12      jfb       720:                ret = cvs_getresp(root);
                    721:
1.13      jfb       722:        return (ret);
1.12      jfb       723: }
                    724:
                    725:
                    726: /*
                    727:  * cvs_getresp()
                    728:  *
                    729:  * Get a response from the server.  This call will actually read and handle
                    730:  * responses from the server until one of the response handlers returns
1.37      david     731:  * non-zero (either an error occurred or the end of the response was reached).
1.12      jfb       732:  * Returns the number of handled commands on success, or -1 on failure.
                    733:  */
                    734: int
1.13      jfb       735: cvs_getresp(struct cvsroot *root)
1.12      jfb       736: {
1.13      jfb       737:        int nbcmd, ret;
                    738:        size_t len;
1.12      jfb       739:
                    740:        nbcmd = 0;
                    741:
                    742:        do {
                    743:                /* wait for incoming data */
1.13      jfb       744:                if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12      jfb       745:                    root->cr_srvout) == NULL) {
                    746:                        if (feof(root->cr_srvout))
                    747:                                return (0);
                    748:                        cvs_log(LP_ERRNO,
                    749:                            "failed to read response from server");
                    750:                        return (-1);
                    751:                }
                    752:
                    753:                if (cvs_server_outlog != NULL)
1.13      jfb       754:                        fputs(cvs_proto_buf, cvs_server_outlog);
1.12      jfb       755:
1.13      jfb       756:                if ((len = strlen(cvs_proto_buf)) != 0) {
1.42      joris     757:                        /* if len - 1 != '\n' the line is truncated */
1.43      joris     758:                        if (cvs_proto_buf[len - 1] == '\n')
1.13      jfb       759:                                cvs_proto_buf[--len] = '\0';
1.12      jfb       760:                }
                    761:
1.13      jfb       762:                ret = cvs_resp_handle(root, cvs_proto_buf);
1.12      jfb       763:                nbcmd++;
                    764:        } while (ret == 0);
                    765:
                    766:        if (ret > 0)
                    767:                ret = nbcmd;
                    768:        return (ret);
                    769: }
                    770:
                    771:
                    772: /*
1.13      jfb       773:  * cvs_getln()
                    774:  *
1.18      jfb       775:  * Get a line from the remote end and store it in <lbuf>.  The terminating
1.13      jfb       776:  * newline character is stripped from the result.
1.55      jfb       777:  * Returns the length in bytes of the line (not including the NUL byte), or
                    778:  * -1 on failure.
1.13      jfb       779:  */
                    780: int
                    781: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
                    782: {
                    783:        size_t rlen;
1.18      jfb       784:        FILE *in;
1.13      jfb       785:
1.18      jfb       786:        if (cvs_cmdop == CVS_OP_SERVER)
                    787:                in = stdin;
                    788:        else
                    789:                in = root->cr_srvout;
                    790:
                    791:        if (fgets(lbuf, len, in) == NULL) {
                    792:                if (ferror(in)) {
                    793:                        cvs_log(LP_ERRNO, "failed to read line");
1.13      jfb       794:                        return (-1);
                    795:                }
                    796:
1.18      jfb       797:                if (feof(in))
1.13      jfb       798:                        *lbuf = '\0';
                    799:        }
                    800:
                    801:        if (cvs_server_outlog != NULL)
                    802:                fputs(lbuf, cvs_server_outlog);
                    803:
                    804:        rlen = strlen(lbuf);
                    805:        if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
                    806:                lbuf[--rlen] = '\0';
                    807:
1.55      jfb       808:        return (rlen);
1.13      jfb       809: }
                    810:
1.19      jfb       811:
1.13      jfb       812: /*
1.12      jfb       813:  * cvs_sendresp()
                    814:  *
1.19      jfb       815:  * Send a response of type <rid> to the client, with optional arguments
1.12      jfb       816:  * contained in <arg>, which should not be terminated by a newline.
                    817:  * Returns 0 on success, or -1 on failure.
                    818:  */
                    819: int
                    820: cvs_sendresp(u_int rid, const char *arg)
                    821: {
                    822:        int ret;
1.19      jfb       823:        struct cvs_resp *resp;
1.12      jfb       824:
                    825:        resp = cvs_resp_getbyid(rid);
1.13      jfb       826:        if (resp == NULL) {
1.12      jfb       827:                cvs_log(LP_ERR, "unsupported response type %u", rid);
                    828:                return (-1);
                    829:        }
                    830:
1.19      jfb       831:        ret = fputs(resp->resp_str, stdout);
1.12      jfb       832:        if (ret == EOF) {
                    833:                cvs_log(LP_ERRNO, "failed to send response to client");
1.30      deraadt   834:        } else {
1.19      jfb       835:                if (arg != NULL) {
                    836:                        putc(' ', stdout);
                    837:                        fputs(arg, stdout);
                    838:                }
1.12      jfb       839:                putc('\n', stdout);
                    840:        }
                    841:        return (0);
                    842: }
                    843:
                    844:
1.19      jfb       845: #ifdef notyet
1.12      jfb       846: /*
                    847:  * cvs_getreq()
                    848:  *
                    849:  * Get a request from the client.
                    850:  */
                    851: int
                    852: cvs_getreq(void)
                    853: {
                    854:        int nbcmd;
                    855:
                    856:        nbcmd = 0;
                    857:
                    858:        do {
                    859:                /* wait for incoming data */
1.13      jfb       860:                if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12      jfb       861:                    stdin) == NULL) {
                    862:                        if (feof(stdin))
                    863:                                return (0);
                    864:                        cvs_log(LP_ERRNO,
                    865:                            "failed to read request from client");
                    866:                        return (-1);
                    867:                }
                    868:
1.13      jfb       869:                if ((len = strlen(cvs_proto_buf)) != 0) {
                    870:                        if (cvs_proto_buf[len - 1] != '\n') {
1.12      jfb       871:                                /* truncated line */
1.30      deraadt   872:                        } else
1.13      jfb       873:                                cvs_proto_buf[--len] = '\0';
1.12      jfb       874:                }
                    875:
1.13      jfb       876:                ret = cvs_resp_handle(cvs_proto_buf);
1.12      jfb       877:        } while (ret == 0);
                    878: }
                    879: #endif
                    880:
                    881:
                    882: /*
                    883:  * cvs_sendln()
                    884:  *
1.18      jfb       885:  * Send a single line <line> string to the remote end.  The line is sent as is,
1.12      jfb       886:  * without any modifications.
                    887:  * Returns 0 on success, or -1 on failure.
                    888:  */
                    889: int
                    890: cvs_sendln(struct cvsroot *root, const char *line)
                    891: {
                    892:        int nl;
                    893:        size_t len;
1.18      jfb       894:        FILE *out;
                    895:
                    896:        if (cvs_cmdop == CVS_OP_SERVER)
                    897:                out = stdout;
                    898:        else
                    899:                out = root->cr_srvin;
1.12      jfb       900:
                    901:        nl = 0;
                    902:        len = strlen(line);
                    903:
                    904:        if ((len > 0) && (line[len - 1] != '\n'))
                    905:                nl = 1;
                    906:
                    907:        if (cvs_server_inlog != NULL) {
                    908:                fputs(line, cvs_server_inlog);
                    909:                if (nl)
1.55      jfb       910:                        putc('\n', cvs_server_inlog);
1.12      jfb       911:        }
1.18      jfb       912:        fputs(line, out);
1.12      jfb       913:        if (nl)
1.55      jfb       914:                putc('\n', out);
1.12      jfb       915:        return (0);
                    916: }
                    917:
                    918:
                    919: /*
1.13      jfb       920:  * cvs_sendraw()
1.12      jfb       921:  *
                    922:  * Send the first <len> bytes from the buffer <src> to the server.
                    923:  */
                    924: int
1.13      jfb       925: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12      jfb       926: {
1.18      jfb       927:        FILE *out;
                    928:
                    929:        if (cvs_cmdop == CVS_OP_SERVER)
                    930:                out = stdout;
                    931:        else
                    932:                out = root->cr_srvin;
                    933:
1.12      jfb       934:        if (cvs_server_inlog != NULL)
                    935:                fwrite(src, sizeof(char), len, cvs_server_inlog);
1.18      jfb       936:        if (fwrite(src, sizeof(char), len, out) < len) {
1.38      jfb       937:                cvs_log(LP_ERR, "failed to send data");
1.12      jfb       938:                return (-1);
                    939:        }
                    940:
                    941:        return (0);
                    942: }
                    943:
                    944:
                    945: /*
1.13      jfb       946:  * cvs_recvraw()
1.12      jfb       947:  *
                    948:  * Receive the first <len> bytes from the buffer <src> to the server.
                    949:  */
                    950: ssize_t
1.13      jfb       951: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12      jfb       952: {
                    953:        size_t ret;
1.18      jfb       954:        FILE *in;
                    955:
                    956:        if (cvs_cmdop == CVS_OP_SERVER)
                    957:                in = stdin;
                    958:        else
                    959:                in = root->cr_srvout;
1.12      jfb       960:
1.18      jfb       961:        ret = fread(dst, sizeof(char), len, in);
1.12      jfb       962:        if (ret == 0)
                    963:                return (-1);
                    964:        if (cvs_server_outlog != NULL)
                    965:                fwrite(dst, sizeof(char), len, cvs_server_outlog);
                    966:        return (ssize_t)ret;
                    967: }
                    968:
                    969:
                    970: /*
1.13      jfb       971:  * cvs_senddir()
1.12      jfb       972:  *
1.32      jfb       973:  * Send a `Directory' request along with the 2 paths that follow it.  If
                    974:  * the directory info to be sent is the same as the last info sent, the
                    975:  * call does nothing and simply returns without an error.
1.12      jfb       976:  */
                    977: int
1.31      tedu      978: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12      jfb       979: {
1.60      xsa       980:        size_t len;
1.28      jfb       981:        char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12      jfb       982:
1.47      jfb       983:        if (dir->cf_type != DT_DIR)
                    984:                return (-1);
                    985:
1.32      jfb       986:        cvs_file_getpath(dir, lbuf, sizeof(lbuf));
1.45      joris     987:        if (strcmp(lbuf, cvs_lastdir) == 0 && cvs_cmdop != CVS_OP_CHECKOUT)
1.32      jfb       988:                return (0);
                    989:
1.47      jfb       990:        if (dir->cf_repo == NULL)
1.28      jfb       991:                strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
1.51      xsa       992:        else {
1.60      xsa       993:                len = cvs_path_cat(root->cr_dir, dir->cf_repo, rbuf,
                    994:                    sizeof(rbuf));
                    995:                if (len >= sizeof(rbuf))
1.51      xsa       996:                        return (-1);
                    997:        }
1.12      jfb       998:
1.28      jfb       999:
                   1000:        if ((cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf) < 0) ||
                   1001:            (cvs_sendln(root, rbuf) < 0))
1.12      jfb      1002:                return (-1);
1.32      jfb      1003:
                   1004:        strlcpy(cvs_lastdir, lbuf, sizeof(cvs_lastdir));
1.12      jfb      1005:
                   1006:        return (0);
                   1007: }
                   1008:
                   1009:
                   1010: /*
1.13      jfb      1011:  * cvs_sendarg()
1.12      jfb      1012:  *
                   1013:  * Send the argument <arg> to the server.  The argument <append> is used to
                   1014:  * determine if the argument should be simply appended to the last argument
                   1015:  * sent or if it should be created as a new argument (0).
                   1016:  */
                   1017: int
1.13      jfb      1018: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12      jfb      1019: {
1.13      jfb      1020:        return cvs_sendreq(root, ((append == 0) ?
                   1021:            CVS_REQ_ARGUMENT : CVS_REQ_ARGUMENTX), arg);
1.12      jfb      1022: }
                   1023:
                   1024:
                   1025: /*
1.13      jfb      1026:  * cvs_sendentry()
1.12      jfb      1027:  *
                   1028:  * Send an `Entry' request to the server along with the mandatory fields from
                   1029:  * the CVS entry <ent> (which are the name and revision).
                   1030:  */
                   1031: int
1.47      jfb      1032: cvs_sendentry(struct cvsroot *root, const CVSFILE *file)
1.12      jfb      1033: {
1.51      xsa      1034:        int l;
1.12      jfb      1035:        char ebuf[128], numbuf[64];
                   1036:
1.47      jfb      1037:        if (file->cf_type != DT_REG) {
                   1038:                cvs_log(LP_ERR, "attempt to send Entry for non-regular file");
                   1039:                return (-1);
                   1040:        }
1.48      joris    1041:
                   1042:        /* don't send Entry for unknown files */
                   1043:        if (file->cf_cvstat == CVS_FST_UNKNOWN)
                   1044:                return (0);
1.47      jfb      1045:
1.51      xsa      1046:        l = snprintf(ebuf, sizeof(ebuf), "/%s/%s%s///", file->cf_name,
1.49      joris    1047:            (file->cf_cvstat == CVS_FST_REMOVED) ? "-" : "",
1.47      jfb      1048:            rcsnum_tostr(file->cf_lrev, numbuf, sizeof(numbuf)));
1.51      xsa      1049:        if (l == -1 || l >= (int)sizeof(ebuf)) {
                   1050:                errno = ENAMETOOLONG;
                   1051:                cvs_log(LP_ERRNO, "%s", ebuf);
                   1052:                return (-1);
                   1053:        }
1.12      jfb      1054:
1.13      jfb      1055:        return cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12      jfb      1056: }
                   1057:
                   1058:
                   1059: /*
1.13      jfb      1060:  * cvs_initlog()
1.12      jfb      1061:  *
                   1062:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                   1063:  * set.  In this case, the variable's value is used as a path to which the
                   1064:  * appropriate suffix is added (".in" for server input and ".out" for server
                   1065:  * output.
                   1066:  * Returns 0 on success, or -1 on failure.
                   1067:  */
                   1068: static int
1.13      jfb      1069: cvs_initlog(void)
1.12      jfb      1070: {
1.52      joris    1071:        int l;
                   1072:        u_int i;
                   1073:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                   1074:        char rpath[MAXPATHLEN], *s;
                   1075:        struct stat st;
                   1076:        time_t now;
                   1077:        struct passwd *pwd;
1.12      jfb      1078:
1.13      jfb      1079:        /* avoid doing it more than once */
                   1080:        if (cvs_server_logon)
                   1081:                return (0);
                   1082:
1.12      jfb      1083:        env = getenv("CVS_CLIENT_LOG");
                   1084:        if (env == NULL)
                   1085:                return (0);
                   1086:
1.52      joris    1087:        if ((envdup = strdup(env)) == NULL)
                   1088:                return (-1);
                   1089:
                   1090:        if ((s = strchr(envdup, '%')) != NULL)
                   1091:                *s = '\0';
                   1092:
                   1093:        strlcpy(buf, env, sizeof(buf));
                   1094:        strlcpy(rpath, envdup, sizeof(rpath));
1.69    ! moritz   1095:        free(envdup);
1.52      joris    1096:
                   1097:        s = buf;
                   1098:        while ((s = strchr(s, '%')) != NULL) {
                   1099:                *s++;
                   1100:                switch (*s) {
                   1101:                case 'c':
                   1102:                        strlcpy(fpath, cvs_command, sizeof(fpath));
                   1103:                        break;
                   1104:                case 'd':
                   1105:                        time(&now);
1.59      niallo   1106:                        strlcpy(fpath, ctime(&now), sizeof(fpath));
1.52      joris    1107:                        break;
                   1108:                case 'p':
                   1109:                        snprintf(fpath, sizeof(fpath), "%d", getpid());
                   1110:                        break;
                   1111:                case 'u':
                   1112:                        if ((pwd = getpwuid(getuid())) != NULL)
                   1113:                                strlcpy(fpath, pwd->pw_name, sizeof(fpath));
                   1114:                        else
                   1115:                                fpath[0] = '\0';
                   1116:                        endpwent();
                   1117:                        break;
                   1118:                default:
                   1119:                        fpath[0] = '\0';
                   1120:                        break;
1.58      joris    1121:                }
1.52      joris    1122:
                   1123:                if (fpath[0] != '\0') {
                   1124:                        strlcat(rpath, "-", sizeof(rpath));
                   1125:                        strlcat(rpath, fpath, sizeof(rpath));
                   1126:                }
                   1127:        }
                   1128:
                   1129:        for (i = 0; i < UINT_MAX; i++) {
                   1130:                l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
                   1131:                if (l == -1 || l >= (int)sizeof(fpath)) {
                   1132:                        errno = ENAMETOOLONG;
                   1133:                        cvs_log(LP_ERRNO, "%s", fpath);
                   1134:                        return (-1);
                   1135:                }
                   1136:
                   1137:                if (stat(fpath, &st) != -1)
                   1138:                        continue;
                   1139:
                   1140:                if (errno != ENOENT)
                   1141:                        return (-1);
                   1142:
                   1143:                break;
                   1144:        }
                   1145:
1.12      jfb      1146:        cvs_server_inlog = fopen(fpath, "w");
                   1147:        if (cvs_server_inlog == NULL) {
                   1148:                cvs_log(LP_ERRNO, "failed to open server input log `%s'",
                   1149:                    fpath);
                   1150:                return (-1);
                   1151:        }
                   1152:
1.52      joris    1153:        for (i = 0; i < UINT_MAX; i++) {
                   1154:                l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
                   1155:                if (l == -1 || l >= (int)sizeof(fpath)) {
                   1156:                        errno = ENAMETOOLONG;
                   1157:                        cvs_log(LP_ERRNO, "%s", fpath);
                   1158:                        return (-1);
                   1159:                }
                   1160:
                   1161:                if (stat(fpath, &st) != -1)
                   1162:                        continue;
                   1163:
                   1164:                if (errno != ENOENT)
                   1165:                        return (-1);
                   1166:
                   1167:                break;
                   1168:        }
                   1169:
1.12      jfb      1170:        cvs_server_outlog = fopen(fpath, "w");
                   1171:        if (cvs_server_outlog == NULL) {
                   1172:                cvs_log(LP_ERRNO, "failed to open server output log `%s'",
                   1173:                    fpath);
                   1174:                return (-1);
                   1175:        }
                   1176:
                   1177:        /* make the streams line-buffered */
                   1178:        setvbuf(cvs_server_inlog, NULL, _IOLBF, 0);
                   1179:        setvbuf(cvs_server_outlog, NULL, _IOLBF, 0);
                   1180:
1.13      jfb      1181:        cvs_server_logon = 1;
1.12      jfb      1182:
                   1183:        return (0);
                   1184: }