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

Annotation of src/usr.bin/cvs/util.c, Revision 1.4

1.1       jfb         1: /*     $OpenBSD$       */
                      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: #include <sys/types.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <md5.h>
                     31: #include <errno.h>
1.2       jfb        32: #include <fcntl.h>
1.1       jfb        33: #include <stdio.h>
                     34: #include <stdlib.h>
                     35: #include <unistd.h>
                     36: #include <string.h>
                     37:
                     38: #include "cvs.h"
                     39: #include "log.h"
                     40:
                     41:
                     42: /* letter -> mode type map */
                     43: static const int cvs_modetypes[26] = {
                     44:        -1, -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1,
                     45:        -1,  2, -1, -1, -1, -1, -1,  0, -1, -1, -1, -1, -1,
                     46: };
                     47:
                     48: /* letter -> mode map */
                     49: static const mode_t cvs_modes[3][26] = {
                     50:        {
                     51:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     52:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     53:                0,  0,       0,       S_IRUSR, 0,  0,  0,    /* n - u */
                     54:                0,  S_IWUSR, S_IXUSR, 0,       0             /* v - z */
                     55:        },
                     56:        {
                     57:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     58:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     59:                0,  0,       0,       S_IRGRP, 0,  0,  0,    /* n - u */
                     60:                0,  S_IWGRP, S_IXGRP, 0,       0             /* v - z */
                     61:        },
                     62:        {
                     63:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     64:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     65:                0,  0,       0,       S_IROTH, 0,  0,  0,    /* n - u */
                     66:                0,  S_IWOTH, S_IXOTH, 0,       0             /* v - z */
                     67:        }
                     68: };
                     69:
                     70:
                     71: /* octal -> string */
                     72: static const char *cvs_modestr[8] = {
                     73:        "", "x", "w", "wx", "r", "rx", "rw", "rwx"
                     74: };
                     75:
                     76:
                     77:
                     78:
                     79: /*
                     80:  * cvs_readrepo()
                     81:  *
                     82:  * Read the path stored in the `Repository' CVS file for a given directory
                     83:  * <dir>, and store that path into the buffer pointed to by <dst>, whose size
                     84:  * is <len>.
                     85:  */
                     86:
                     87: int
                     88: cvs_readrepo(const char *dir, char *dst, size_t len)
                     89: {
                     90:        size_t dlen;
                     91:        FILE *fp;
                     92:        char repo_path[MAXPATHLEN];
                     93:
                     94:        snprintf(repo_path, sizeof(repo_path), "%s/CVS/Repository", dir);
                     95:        fp = fopen(repo_path, "r");
                     96:        if (fp == NULL) {
                     97:                return (-1);
                     98:        }
                     99:
                    100:        if (fgets(dst, (int)len, fp) == NULL) {
                    101:                if (ferror(fp)) {
                    102:                        cvs_log(LP_ERRNO, "failed to read from `%s'",
                    103:                            repo_path);
                    104:                }
                    105:                (void)fclose(fp);
                    106:                return (-1);
                    107:        }
                    108:        dlen = strlen(dst);
                    109:        if ((dlen > 0) && (dst[dlen - 1] == '\n'))
                    110:                dst[--dlen] = '\0';
                    111:
                    112:        (void)fclose(fp);
                    113:        return (0);
                    114: }
                    115:
                    116:
                    117: /*
                    118:  * cvs_strtomode()
                    119:  *
                    120:  * Read the contents of the string <str> and generate a permission mode from
                    121:  * the contents of <str>, which is assumed to have the mode format of CVS.
                    122:  * The CVS protocol specification states that any modes or mode types that are
                    123:  * not recognized should be silently ignored.  This function does not return
                    124:  * an error in such cases, but will issue warnings.
                    125:  * Returns 0 on success, or -1 on failure.
                    126:  */
                    127:
                    128: int
                    129: cvs_strtomode(const char *str, mode_t *mode)
                    130: {
1.2       jfb       131:        char type;
1.1       jfb       132:        mode_t m;
                    133:        char buf[32], ms[4], *sp, *ep;
                    134:
                    135:        m = 0;
                    136:        strlcpy(buf, str, sizeof(buf));
                    137:        sp = buf;
                    138:        ep = sp;
                    139:
                    140:        for (sp = buf; ep != NULL; sp = ep + 1) {
                    141:                ep = strchr(sp, ',');
                    142:                if (ep != NULL)
1.2       jfb       143:                        *ep = '\0';
1.1       jfb       144:
1.2       jfb       145:                if (sscanf(sp, "%c=%3s", &type, ms) != 2) {
1.1       jfb       146:                        cvs_log(LP_WARN, "failed to scan mode string `%s'", sp);
                    147:                        continue;
                    148:                }
                    149:
                    150:                if ((type <= 'a') || (type >= 'z') ||
                    151:                    (cvs_modetypes[type - 'a'] == -1)) {
                    152:                        cvs_log(LP_WARN,
                    153:                            "invalid mode type `%c'"
                    154:                            " (`u', `g' or `o' expected), ignoring", type);
                    155:                        continue;
                    156:                }
                    157:
                    158:                /* make type contain the actual mode index */
                    159:                type = cvs_modetypes[type - 'a'];
                    160:
                    161:                for (sp = ms; *sp != '\0'; sp++) {
                    162:                        if ((*sp <= 'a') || (*sp >= 'z') ||
                    163:                            (cvs_modes[type][*sp - 'a'] == 0)) {
                    164:                                cvs_log(LP_WARN,
                    165:                                    "invalid permission bit `%c'", *sp);
                    166:                        }
                    167:                        else
                    168:                                m |= cvs_modes[type][*sp - 'a'];
                    169:                }
                    170:        }
                    171:
                    172:        *mode = m;
                    173:
                    174:        return (0);
                    175: }
                    176:
                    177:
                    178: /*
                    179:  * cvs_modetostr()
                    180:  *
                    181:  * Returns 0 on success, or -1 on failure.
                    182:  */
                    183:
                    184: int
                    185: cvs_modetostr(mode_t mode, char *buf, size_t len)
                    186: {
                    187:        size_t l;
                    188:        char tmp[16], *bp;
                    189:        mode_t um, gm, om;
                    190:
                    191:        um = (mode & S_IRWXU) >> 6;
                    192:        gm = (mode & S_IRWXG) >> 3;
                    193:        om = mode & S_IRWXO;
                    194:
                    195:        bp = buf;
                    196:        *bp = '\0';
                    197:        l = 0;
                    198:
                    199:        if (um) {
                    200:                snprintf(tmp, sizeof(tmp), "u=%s", cvs_modestr[um]);
                    201:                l = strlcat(buf, tmp, len);
                    202:        }
                    203:        if (gm) {
                    204:                if (um)
                    205:                        strlcat(buf, ",", len);
                    206:                snprintf(tmp, sizeof(tmp), "g=%s", cvs_modestr[gm]);
                    207:                strlcat(buf, tmp, len);
                    208:        }
                    209:        if (om) {
                    210:                if (um || gm)
                    211:                        strlcat(buf, ",", len);
                    212:                snprintf(tmp, sizeof(tmp), "o=%s", cvs_modestr[gm]);
                    213:                strlcat(buf, tmp, len);
                    214:        }
                    215:
                    216:        return (0);
                    217: }
                    218:
                    219:
                    220: /*
                    221:  * cvs_cksum()
                    222:  *
                    223:  * Calculate the MD5 checksum of the file whose path is <file> and generate
                    224:  * a CVS-format 32 hex-digit string, which is stored in <dst>, whose size is
                    225:  * given in <len> and must be at least 33.
                    226:  * Returns 0 on success, or -1 on failure.
                    227:  */
                    228:
                    229: int
                    230: cvs_cksum(const char *file, char *dst, size_t len)
                    231: {
                    232:        if (len < CVS_CKSUM_LEN) {
                    233:                cvs_log(LP_WARN, "buffer too small for checksum");
                    234:                return (-1);
                    235:        }
                    236:        if (MD5File(file, dst) == NULL) {
                    237:                cvs_log(LP_ERRNO, "failed to generate file checksum");
                    238:                return (-1);
                    239:        }
                    240:
                    241:        return (0);
                    242: }
                    243:
                    244:
                    245: /*
                    246:  * cvs_splitpath()
                    247:  *
                    248:  * Split a path <path> into the directory portion and the filename portion
                    249:  * and copy them in <dir> and <file>, whose lengths are <dlen> and <flen>,
                    250:  * unless they are NULL.
                    251:  * Returns 0 on success, or -1 on failure.
                    252:  */
                    253:
                    254: int
                    255: cvs_splitpath(const char *path, char *dir, size_t dlen, char *file, size_t flen)
                    256: {
                    257:        size_t rlen;
                    258:        const char *sp;
                    259:        struct stat st;
                    260:
                    261:        sp = strrchr(path, '/');
                    262:        if (sp == NULL) {
                    263:                if (stat(path, &st) == -1)
                    264:                        return (-1);
                    265:
                    266:                if (S_ISDIR(st.st_mode)) {
                    267:                        if (dir != NULL)
                    268:                                strlcpy(dir, path, dlen);
                    269:                        if (file != NULL)
                    270:                                file[0] = '\0';
                    271:                }
                    272:                else {
                    273:                        if (file != NULL)
                    274:                                strlcpy(file, path, flen);
                    275:                        if (dir != NULL)
                    276:                                strlcpy(dir, ".", dlen);
                    277:                }
                    278:        }
                    279:        else {
1.4     ! jfb       280:                rlen = MIN(dlen - 1, (size_t)(sp - path));
1.1       jfb       281:                if (dir != NULL) {
                    282:                        strncpy(dir, path, rlen);
                    283:                        dir[rlen] = '\0';
                    284:                }
                    285:
                    286:                sp++;
                    287:                if (file != NULL)
                    288:                        strlcpy(file, sp, flen);
                    289:        }
                    290:
                    291:        return (0);
                    292: }
                    293:
                    294:
                    295: /*
                    296:  * cvs_getargv()
                    297:  *
                    298:  * Parse a line contained in <line> and generate an argument vector by
                    299:  * splitting the line on spaces and tabs.  The resulting vector is stored in
                    300:  * <argv>, which can accept up to <argvlen> entries.
                    301:  * Returns the number of arguments in the vector, or -1 if an error occured.
                    302:  */
                    303:
                    304: int
                    305: cvs_getargv(const char *line, char **argv, int argvlen)
                    306: {
                    307:        u_int i;
                    308:        int argc, err;
                    309:        char linebuf[256], qbuf[128], *lp, *cp, *arg;
                    310:
                    311:        strlcpy(linebuf, line, sizeof(linebuf));
                    312:        memset(argv, 0, sizeof(argv));
                    313:        argc = 0;
                    314:
                    315:        /* build the argument vector */
                    316:        err = 0;
                    317:        for (lp = linebuf; lp != NULL;) {
                    318:                if (*lp == '"') {
                    319:                        /* double-quoted string */
                    320:                        lp++;
                    321:                        i = 0;
                    322:                        memset(qbuf, 0, sizeof(qbuf));
                    323:                        while (*lp != '"') {
                    324:                                if (*lp == '\0') {
                    325:                                        cvs_log(LP_ERR, "no terminating quote");
                    326:                                        err++;
                    327:                                        break;
                    328:                                }
                    329:                                else if (*lp == '\\')
                    330:                                        lp++;
                    331:
                    332:                                qbuf[i++] = *lp++;
                    333:                                if (i == sizeof(qbuf)) {
                    334:                                        err++;
                    335:                                        break;
                    336:                                }
                    337:                        }
                    338:
                    339:                        arg = qbuf;
                    340:                }
                    341:                else {
                    342:                        cp = strsep(&lp, " \t");
                    343:                        if (cp == NULL)
                    344:                                break;
                    345:                        else if (*cp == '\0')
                    346:                                continue;
                    347:
                    348:                        arg = cp;
                    349:                }
                    350:
                    351:                argv[argc] = strdup(arg);
                    352:                if (argv[argc] == NULL) {
                    353:                        cvs_log(LP_ERRNO, "failed to copy argument");
                    354:                        err++;
                    355:                        break;
                    356:                }
                    357:                argc++;
                    358:        }
                    359:
                    360:        if (err) {
                    361:                /* ditch the argument vector */
                    362:                for (i = 0; i < (u_int)argc; i++)
                    363:                        free(argv[i]);
                    364:                argc = -1;
                    365:        }
                    366:
                    367:        return (argc);
                    368: }
                    369:
                    370:
                    371: /*
                    372:  * cvs_freeargv()
                    373:  *
                    374:  * Free an argument vector previously generated by cvs_getargv().
                    375:  */
                    376:
                    377: void
                    378: cvs_freeargv(char **argv, int argc)
                    379: {
                    380:        int i;
                    381:
                    382:        for (i = 0; i < argc; i++)
                    383:                free(argv[i]);
1.2       jfb       384: }
                    385:
                    386:
                    387: /*
                    388:  * cvs_mkadmin()
                    389:  *
                    390:  * Create the CVS administrative files within the directory <cdir>.
                    391:  * Returns 0 on success, or -1 on failure.
                    392:  */
                    393:
                    394: int
                    395: cvs_mkadmin(struct cvs_file *cdir, mode_t mode)
                    396: {
                    397:        char path[MAXPATHLEN];
                    398:        FILE *fp;
                    399:        CVSENTRIES *ef;
                    400:        struct cvsroot *root;
                    401:
                    402:        snprintf(path, sizeof(path), "%s/" CVS_PATH_CVSDIR, cdir->cf_path);
                    403:        if (mkdir(path, mode) == -1) {
                    404:                cvs_log(LP_ERRNO, "failed to create directory %s", path);
                    405:                return (-1);
                    406:        }
                    407:
                    408:        ef = cvs_ent_open(cdir->cf_path, O_WRONLY);
                    409:        (void)cvs_ent_close(ef);
                    410:
                    411:        snprintf(path, sizeof(path), "%s/" CVS_PATH_ROOTSPEC, cdir->cf_path);
                    412:        fp = fopen(path, "w");
                    413:        if (fp == NULL) {
                    414:                cvs_log(LP_ERRNO, "failed to open %s", path);
                    415:                return (-1);
                    416:        }
                    417:        root = cdir->cf_ddat->cd_root;
                    418:        if (root->cr_user != NULL) {
                    419:                fprintf(fp, "%s", root->cr_user);
                    420:                if (root->cr_pass != NULL)
                    421:                        fprintf(fp, ":%s", root->cr_pass);
                    422:                if (root->cr_host != NULL)
                    423:                        putc('@', fp);
                    424:        }
                    425:
                    426:        if (root->cr_host != NULL) {
                    427:                fprintf(fp, "%s", root->cr_host);
                    428:                if (root->cr_dir != NULL)
                    429:                        putc(':', fp);
                    430:        }
                    431:        if (root->cr_dir)
                    432:                fprintf(fp, "%s", root->cr_dir);
1.3       jfb       433:        putc('\n', fp);
1.2       jfb       434:        (void)fclose(fp);
                    435:
                    436:        if (cdir->cf_ddat->cd_repo != NULL) {
                    437:                snprintf(path, sizeof(path), "%s/" CVS_PATH_REPOSITORY,
                    438:                    cdir->cf_path);
                    439:                fp = fopen(path, "w");
                    440:                if (fp == NULL) {
                    441:                        cvs_log(LP_ERRNO, "failed to open %s", path);
                    442:                        return (-1);
                    443:                }
1.3       jfb       444:                fprintf(fp, "%s\n", cdir->cf_ddat->cd_repo);
1.2       jfb       445:                (void)fclose(fp);
                    446:        }
                    447:
                    448:
                    449:        return (0);
1.1       jfb       450: }