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

Annotation of src/usr.bin/oldrdist/main.c, Revision 1.12

1.12    ! millert     1: /*     $OpenBSD: main.c,v 1.11 2002/02/16 21:27:50 millert Exp $       */
1.2       deraadt     2:
1.1       dm          3: /*
                      4:  * Copyright (c) 1983, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: static char copyright[] =
                     38: "@(#) Copyright (c) 1983, 1993\n\
                     39:        The Regents of the University of California.  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: /* from: static char sccsid[] = "@(#)main.c    8.1 (Berkeley) 6/9/93"; */
1.12    ! millert    44: static char *rcsid = "$OpenBSD: main.c,v 1.11 2002/02/16 21:27:50 millert Exp $";
1.1       dm         45: #endif /* not lint */
                     46:
1.12    ! millert    47: #include <stdarg.h>
1.1       dm         48: #include "defs.h"
                     49:
                     50: #define NHOSTS 100
                     51:
                     52: /*
                     53:  * Remote distribution program.
                     54:  */
                     55:
                     56: char   *distfile = NULL;
1.7       deraadt    57: #define _RDIST_TMP     "rdistXXXXXXXXXX"
1.1       dm         58: char   tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1];
                     59: char   *tempname;
                     60:
                     61: int    debug;          /* debugging flag */
                     62: int    nflag;          /* NOP flag, just print commands without executing */
                     63: int    qflag;          /* Quiet. Don't print messages */
                     64: int    options;        /* global options */
                     65: int    iamremote;      /* act as remote server for transfering files */
                     66:
                     67: FILE   *fin = NULL;    /* input file pointer */
                     68: int    rem = -1;       /* file descriptor to remote source/sink process */
                     69: char   host[32];       /* host name */
                     70: int    nerrs;          /* number of errors while sending/receiving */
                     71: char   user[10];       /* user's name */
                     72: char   homedir[128];   /* user's home directory */
                     73: int    userid;         /* user's user ID */
                     74: int    groupid;        /* user's group ID */
                     75:
                     76: struct passwd *pw;     /* pointer to static area used by getpwent */
                     77: struct group *gr;      /* pointer to static area used by getgrent */
                     78:
1.11      millert    79: static void usage(void);
                     80: static void docmdargs(int, char *[]);
1.1       dm         81:
                     82: int
                     83: main(argc, argv)
                     84:        int argc;
                     85:        char *argv[];
                     86: {
1.10      mpech      87:        char *arg;
1.9       pvalchev   88:        int cmdargs = 0;
1.1       dm         89:        char *dhosts[NHOSTS], **hp = dhosts;
                     90:
                     91:        pw = getpwuid(userid = getuid());
                     92:        if (pw == NULL) {
                     93:                fprintf(stderr, "%s: Who are you?\n", argv[0]);
                     94:                exit(1);
                     95:        }
                     96:        strcpy(user, pw->pw_name);
                     97:        strcpy(homedir, pw->pw_dir);
                     98:        groupid = pw->pw_gid;
                     99:        gethostname(host, sizeof(host));
                    100:        strcpy(tempfile, _PATH_TMP);
                    101:        strcat(tempfile, _RDIST_TMP);
1.3       millert   102:        tempname = xbasename(tempfile);
1.1       dm        103:
                    104:        while (--argc > 0) {
                    105:                if ((arg = *++argv)[0] != '-')
                    106:                        break;
                    107:                if (!strcmp(arg, "-Server"))
                    108:                        iamremote++;
                    109:                else while (*++arg)
                    110:                        switch (*arg) {
                    111:                        case 'f':
                    112:                                if (--argc <= 0)
                    113:                                        usage();
                    114:                                distfile = *++argv;
                    115:                                if (distfile[0] == '-' && distfile[1] == '\0')
                    116:                                        fin = stdin;
                    117:                                break;
                    118:
                    119:                        case 'm':
                    120:                                if (--argc <= 0)
                    121:                                        usage();
                    122:                                if (hp >= &dhosts[NHOSTS-2]) {
                    123:                                        fprintf(stderr, "rdist: too many destination hosts\n");
                    124:                                        exit(1);
                    125:                                }
                    126:                                *hp++ = *++argv;
                    127:                                break;
                    128:
                    129:                        case 'd':
                    130:                                if (--argc <= 0)
                    131:                                        usage();
                    132:                                define(*++argv);
                    133:                                break;
                    134:
                    135:                        case 'D':
                    136:                                debug++;
                    137:                                break;
                    138:
                    139:                        case 'c':
                    140:                                cmdargs++;
                    141:                                break;
                    142:
                    143:                        case 'n':
                    144:                                if (options & VERIFY) {
                    145:                                        printf("rdist: -n overrides -v\n");
                    146:                                        options &= ~VERIFY;
                    147:                                }
                    148:                                nflag++;
                    149:                                break;
                    150:
                    151:                        case 'q':
                    152:                                qflag++;
                    153:                                break;
                    154:
                    155:                        case 'b':
                    156:                                options |= COMPARE;
                    157:                                break;
                    158:
                    159:                        case 'R':
                    160:                                options |= REMOVE;
                    161:                                break;
                    162:
                    163:                        case 'v':
                    164:                                if (nflag) {
                    165:                                        printf("rdist: -n overrides -v\n");
                    166:                                        break;
                    167:                                }
                    168:                                options |= VERIFY;
                    169:                                break;
                    170:
                    171:                        case 'w':
                    172:                                options |= WHOLE;
                    173:                                break;
                    174:
                    175:                        case 'y':
                    176:                                options |= YOUNGER;
                    177:                                break;
                    178:
                    179:                        case 'h':
                    180:                                options |= FOLLOW;
                    181:                                break;
                    182:
                    183:                        case 'i':
                    184:                                options |= IGNLNKS;
                    185:                                break;
                    186:
                    187:                        default:
                    188:                                usage();
                    189:                        }
                    190:        }
                    191:        *hp = NULL;
                    192:
1.3       millert   193: #if    defined(DIRECT_RCMD)
1.1       dm        194:        seteuid(userid);
1.3       millert   195: #else  /* DIRECT_RCMD */
                    196:        if (!iamremote && getuid() != geteuid()) {
                    197:                error("This version of rdist should not be installed setuid.\n");
                    198:                exit(1);
                    199:        }
                    200: #endif /* DIRECT_RCMD */
1.5       millert   201:
                    202:        if (mktemp(tempfile) == NULL)
                    203:                fatal("cannot get temp file\n");
1.1       dm        204:
                    205:        if (iamremote) {
                    206:                server();
                    207:                exit(nerrs != 0);
                    208:        }
                    209:
                    210:        if (cmdargs)
                    211:                docmdargs(argc, argv);
                    212:        else {
                    213:                if (fin == NULL) {
                    214:                        if(distfile == NULL) {
                    215:                                if((fin = fopen("distfile","r")) == NULL)
                    216:                                        fin = fopen("Distfile", "r");
                    217:                        } else
                    218:                                fin = fopen(distfile, "r");
1.3       millert   219:                        if (fin == NULL) {
1.1       dm        220:                                perror(distfile ? distfile : "distfile");
                    221:                                exit(1);
                    222:                        }
                    223:                }
                    224:                yyparse();
                    225:                if (nerrs == 0)
                    226:                        docmds(dhosts, argc, argv);
                    227:        }
                    228:
                    229:        exit(nerrs != 0);
                    230: }
                    231:
                    232: static void
                    233: usage()
                    234: {
                    235:        printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
                    236:        printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
                    237:        exit(1);
                    238: }
                    239:
                    240: /*
                    241:  * rcp like interface for distributing files.
                    242:  */
                    243: static void
                    244: docmdargs(nargs, args)
                    245:        int nargs;
                    246:        char *args[];
                    247: {
1.10      mpech     248:        struct namelist *nl, *prev;
                    249:        char *cp;
1.1       dm        250:        struct namelist *files, *hosts;
                    251:        struct subcmd *cmds;
                    252:        char *dest;
                    253:        static struct namelist tnl = { NULL, NULL };
                    254:        int i;
                    255:
                    256:        if (nargs < 2)
                    257:                usage();
                    258:
                    259:        prev = NULL;
                    260:        for (i = 0; i < nargs - 1; i++) {
                    261:                nl = makenl(args[i]);
                    262:                if (prev == NULL)
                    263:                        files = prev = nl;
                    264:                else {
                    265:                        prev->n_next = nl;
                    266:                        prev = nl;
                    267:                }
                    268:        }
                    269:
                    270:        cp = args[i];
1.3       millert   271:        if ((dest = strchr(cp, ':')) != NULL)
1.1       dm        272:                *dest++ = '\0';
                    273:        tnl.n_name = cp;
                    274:        hosts = expand(&tnl, E_ALL);
                    275:        if (nerrs)
                    276:                exit(1);
                    277:
                    278:        if (dest == NULL || *dest == '\0')
                    279:                cmds = NULL;
                    280:        else {
                    281:                cmds = makesubcmd(INSTALL);
                    282:                cmds->sc_options = options;
                    283:                cmds->sc_name = dest;
                    284:        }
                    285:
                    286:        if (debug) {
                    287:                printf("docmdargs()\nfiles = ");
                    288:                prnames(files);
                    289:                printf("hosts = ");
                    290:                prnames(hosts);
                    291:        }
                    292:        insert(NULL, files, hosts, cmds);
                    293:        docmds(NULL, 0, NULL);
                    294: }
                    295:
                    296: /*
                    297:  * Print a list of NAME blocks (mostly for debugging).
                    298:  */
                    299: void
                    300: prnames(nl)
1.10      mpech     301:        struct namelist *nl;
1.1       dm        302: {
                    303:        printf("( ");
                    304:        while (nl != NULL) {
                    305:                printf("%s ", nl->n_name);
                    306:                nl = nl->n_next;
                    307:        }
                    308:        printf(")\n");
                    309: }
                    310:
                    311: void
                    312: warn(const char *fmt, ...)
                    313: {
                    314:        extern int yylineno;
                    315:        va_list ap;
1.12    ! millert   316:
1.1       dm        317:        va_start(ap, fmt);
                    318:        (void)fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
                    319:        (void)vfprintf(stderr, fmt, ap);
                    320:        (void)fprintf(stderr, "\n");
                    321:        va_end(ap);
1.3       millert   322: }
                    323:
                    324: /*
                    325:  * Private version of basename()
                    326:  */
                    327: char *xbasename(path)
                    328:        char *path;
                    329: {
1.10      mpech     330:        char *cp;
1.3       millert   331:
                    332:        if (cp = strrchr(path, '/'))
                    333:                return(cp+1);
                    334:        else
                    335:                return(path);
1.1       dm        336: }