Annotation of 43BSDTahoe/new/xns/examples/vpfile/vpfile.c, revision 1.1.1.1

1.1       root        1: #ifndef lint
                      2: static char *rcsid = "$Header: vpfile.c,v 1.2 87/04/01 08:23:06 ed Exp $";
                      3: #endif lint
                      4: 
                      5: /*
                      6:  * Copyright (c) 1986, 1987 Xerox Corporation.
                      7:  */
                      8: 
                      9: /* $Log:       vpfile.c,v $
                     10:  * Revision 1.2  87/04/01  08:23:06  ed
                     11:  * Changed for new MakeSecondaryCreds call.
                     12:  * 
                     13:  * Revision 1.1  87/03/17  16:28:30  ed
                     14:  * Initial revision
                     15:  * 
                     16:  * 
                     17:  */
                     18: 
                     19: #include <stdio.h>
                     20: #include <sys/file.h>
                     21: #include <sys/time.h>
                     22: #include <sys/param.h>
                     23: #include <sys/stat.h>
                     24: #include <netns/ns.h>
                     25: #include <netns/sp.h>
                     26: #include <xnscourier/FilingSubset1.h>
                     27: #include <xnscourier/Filing4.h>
                     28: #include <xnscourier/except.h>
                     29: #undef __Clearinghouse2                        /* Filing4.h defs this */
                     30: #include <xnscourier/CH.h>
                     31: #include <xnscourier/filetypes.h>
                     32: 
                     33: #define ROOT_DIRECTORY "/"
                     34: 
                     35: CourierConnection *connected;
                     36: Clearinghouse3_ObjectName hostobjname;
                     37: Authentication3_Verifier verifier;
                     38: 
                     39: /* the following 3 items make up the current session */
                     40: FilingSubset1_Session session; /* the current session */
                     41: Clearinghouse3_ObjectName username;
                     42: FilingSubset1_Handle rootHandle;
                     43: char cur_dir[512]= 0;
                     44: char cur_pathname[512]= 0;
                     45: char cur_name[512]= 0;
                     46: char *myhostname, mydir[512], olddir[512];
                     47: 
                     48: static FilingSubset1_ControlSequence nullControls = {0,0};
                     49: static FilingSubset1_ScopeSequence nullScope = {0,0};
                     50: 
                     51: /* global data used to communicate with BDT procedures
                     52:  */
                     53: extern GetAttributeSequences(), 
                     54:        listproc();
                     55: 
                     56: char *rindex(), *typetostring();
                     57: char *AttrToString();
                     58: Boolean AttrToBoolean();
                     59: LongCardinal AttrToLongCardinal();
                     60: Cardinal AttrToCardinal();
                     61: 
                     62: static (*ProcEachSeq)();
                     63: 
                     64: Boolean files_found= FALSE;
                     65: Boolean filing_subset= TRUE;
                     66: Boolean usefiling= TRUE;
                     67: 
                     68: char *service, oldservice[100];
                     69: 
                     70: main(argc, argv)
                     71: int argc;
                     72: char *argv[];
                     73: {
                     74:        char *file, *rightbrkt;
                     75:        char *malloc();
                     76:        int i;
                     77:        CourierConnection *hookup();
                     78:        int opt;
                     79:        extern int optind;
                     80:        extern char *optarg;
                     81: 
                     82:        static char *options= "f";
                     83:        static char *usage= "Usage: %s [-f] file1 ... filen\n";
                     84: 
                     85:        if ( argc < 1 ) {
                     86:                fprintf(stderr, usage, argv[0]);
                     87:                exit(1);
                     88:        }
                     89: 
                     90:        gethostname(myhostname=malloc(100),100);
                     91:        getwd(mydir);
                     92: 
                     93:        while ((opt= getopt(argc, argv, options)) != EOF) 
                     94:                switch (opt) {
                     95:                        case 'f':
                     96:                                usefiling= 0;
                     97:                                break;
                     98: 
                     99:                        default:
                    100:                                fprintf(stderr, "Invalid command option -%c\n", opt);
                    101:                                exit(1);
                    102:                }
                    103: 
                    104:        for ( ; optind < argc ; optind++ ) {
                    105:                if ( getserviceandfile(argv[optind], &service, &file) == 0 ) {
                    106:                        fprintf(stderr, "Invalid name %s\n", argv[optind]);
                    107:                        exit(1);
                    108:                }
                    109: 
                    110:                if ( service == 0 ) {
                    111:                        locallist(argv[optind]);
                    112:                        continue;
                    113:                }
                    114:                DURING {
                    115:                        if ( strcmp(oldservice, service) != 0 ) {
                    116:                                if ( (connected= hookup(service)) == (CourierConnection *)0 ) {
                    117:                                        fprintf(stderr, "\nCan't connect to %s\n", service);
                    118:                                        continue;
                    119:                                }
                    120:                                login(0,0);
                    121:                                strcpy(oldservice, service);
                    122: 
                    123:                                printf("\n\n(%s):\n", service);
                    124:                        }
                    125: 
                    126:                        remotelist(file);
                    127: 
                    128:                } HANDLER {
                    129:                        FilingErrMsg(Exception.Code, Exception.Message);
                    130:                } END_HANDLER;
                    131: 
                    132:        }
                    133: 
                    134:        return(0);
                    135: }
                    136: 
                    137: getserviceandfile(name, srvcptr, fileptr)
                    138: char *name;
                    139: char **srvcptr, **fileptr;
                    140: {
                    141:        char *sptr, *fptr;
                    142:        char *index();
                    143: 
                    144:        *srvcptr= 0;
                    145: 
                    146:        /*
                    147:         * look for Xerox forms first:
                    148:         *      [host]filename
                    149:         */
                    150: 
                    151:        if ( (sptr= index(name, '[')) != 0 ) {
                    152:                if ( (fptr= index(sptr, ']')) != 0 ) {
                    153:                        *fptr= '\0';
                    154:                        *srvcptr= sptr + 1;
                    155:                        *fileptr= fptr + 1;
                    156:                        return(1);
                    157:                } else
                    158:                        return(0);
                    159:        }
                    160: 
                    161:        /*
                    162:         *      (host)filename
                    163:         */
                    164: 
                    165:        if ( (sptr= index(name, '(')) != 0 ) {
                    166:                if ( (fptr= index(sptr, ')')) != 0 ) {
                    167:                        *fptr= '\0';
                    168:                        *srvcptr= sptr + 1;
                    169:                        *fileptr= fptr + 1;
                    170:                        return(1);
                    171:                } else
                    172:                        return(0);
                    173:        }
                    174: 
                    175:        /*
                    176:         * look for XNS style with trailing : delimiter
                    177:         * (assumes no : in file name, use alternate spec instead)
                    178:         *      object:domain:organization:filename
                    179:         *              domain & organization are optional
                    180:         */
                    181: 
                    182:        if ( (fptr= rindex(name, ':')) != 0 ) {
                    183:                *fptr= '\0';
                    184:                *srvcptr= name;
                    185:                *fileptr= fptr + 1;
                    186:                return(1);
                    187:        } 
                    188:        return(1);
                    189: }
                    190: 
                    191: copyhandle(dest,src)
                    192:        FilingSubset1_Handle dest,src;
                    193: {
                    194:        if (dest == (Unspecified *) 0) {
                    195:                fprintf(stderr,"Oops.  dest is null in copyhandle\n");
                    196:                exit(1);
                    197:        }
                    198:        dest[0] = src[0];
                    199:        dest[1] = src[1];
                    200: }
                    201: 
                    202: getdirhandle(filename, handle)
                    203:        char *filename;
                    204:        FilingSubset1_Handle handle;
                    205: {
                    206:        FilingSubset1_Attribute pathattr[1];
                    207:        FilingSubset1_AttributeSequence attrseq;
                    208:        FilingSubset1_OpenResults openresult;
                    209:        Filing4_OpenResults openresult2;
                    210:        char *slash;
                    211: 
                    212:        if (filename == (char *)0 || *filename == '\000' || (strcmp(filename, "/") == 0) ) {
                    213:                strcpy(cur_pathname, "/");
                    214:                strcpy(cur_name, "/");
                    215:                if ( filing_subset )
                    216:                        copyhandle(handle,FilingSubset1_nullHandle);
                    217:                else
                    218:                        copyhandle(handle,rootHandle);
                    219:                printf("\n/ :\n\n");
                    220:                return;
                    221:        } else if ( filename[0] == '/' ) {
                    222:                strcpy(cur_pathname, filename);
                    223:        } else {
                    224:                strcpy(cur_pathname, cur_dir);
                    225:                if ( strcmp(cur_pathname, "/") != 0 )
                    226:                        strcat(cur_pathname, "/");
                    227:                strcat(cur_pathname, filename); 
                    228:        }
                    229: 
                    230:        if ( (slash= rindex(cur_pathname,'/')) == NULL )
                    231:                strcpy(cur_name, cur_pathname);
                    232:        else
                    233:                strcpy(cur_name, slash+1);
                    234: 
                    235:        if ( filing_subset ) {
                    236:                copyhandle(handle, FilingSubset1_nullHandle);
                    237:        } else {
                    238:                if ( slash == cur_pathname) {
                    239:                        copyhandle(handle, rootHandle);
                    240:                        printf("\n/ :\n\n");
                    241:                        return;
                    242:                }
                    243: 
                    244:                attrseq.length = 1;
                    245:                attrseq.sequence = pathattr;
                    246:                pathattr[0].type = FilingSubset1_pathname;
                    247:                copyhandle(handle, FilingSubset1_nullHandle);
                    248:                *slash= '\0';           /* separate pathname from name */
                    249: #ifdef XEROXFSCOMPATIBILITY
                    250:                if ( cur_pathname[0] == '/' )
                    251:                        StringToAttr(cur_pathname+1, &pathattr[0]);
                    252:                else
                    253:                        StringToAttr(cur_pathname, &pathattr[0]);
                    254: #else XEROXFSCOMPATIBILITY
                    255:                StringToAttr(cur_pathname, &pathattr[0]);
                    256: #endif XEROXFSCOMPATIBILITY
                    257:                printf("\n%s :\n\n",cur_pathname);
                    258:                *slash= '/';            /* and put back */
                    259:                alarm(0);
                    260:                if ( filing_subset ) {
                    261:                        openresult = FilingSubset1_Open(connected, NULL, attrseq,
                    262:                                                  handle, nullControls,
                    263:                                                  session);
                    264:                        copyhandle(handle, openresult.file);
                    265:                } else {
                    266:                        openresult2 = Filing4_Open(connected, NULL, attrseq,
                    267:                                                  handle, nullControls,
                    268:                                                  session);
                    269:                        copyhandle(handle, openresult2.file);
                    270:                }
                    271:        }
                    272: }
                    273: 
                    274: freefilehandle(handle)
                    275:        FilingSubset1_Handle handle;
                    276: {
                    277:        if (handle[0] == FilingSubset1_nullHandle[0] &&
                    278:            handle[1] == FilingSubset1_nullHandle[1])
                    279:                return;         /* don't free nullHandle */
                    280:        if (handle[0] == rootHandle[0] &&
                    281:            handle[1] == rootHandle[1])
                    282:                return;         /* don't free root directory */
                    283:        alarm(0);
                    284:        if ( filing_subset )
                    285:                FilingSubset1_Close(connected, NULL, handle, session);
                    286:        else
                    287:                Filing4_Close(connected, NULL, handle, session);
                    288: }
                    289: 
                    290: CourierConnection *
                    291: hookup(name)
                    292:        char *name;
                    293: {
                    294:        register struct ns_addr *hostaddr;
                    295:        extern struct ns_addr *getXNSaddr();
                    296:        Clearinghouse3_ObjectName defaultobjname;
                    297:        static char hnamebuf[128];
                    298:        CourierConnection *cconn;
                    299: 
                    300:        CH_NameDefault(&defaultobjname);
                    301:        hostobjname = CH_StringToName(name, &defaultobjname);
                    302:        if ((hostaddr = CH_LookupAddrDN( hostobjname, 0, hnamebuf, 128))) {
                    303:                /* should check here to be sure host is a file service */
                    304:                hostaddr->x_port = htons(5); /* ?? */
                    305:                cconn = CourierOpen(hostaddr);
                    306:                /* reset objname to flush wildcards */
                    307:                /* clear_Clearinghouse3_ThreePartName(&hostobjname); */
                    308:                hostobjname = CH_StringToName(hnamebuf, &defaultobjname);
                    309:        }
                    310:        return(cconn);
                    311: }
                    312: 
                    313: 
                    314: login(name,pwd)
                    315:        char *pwd;
                    316:        char *name;
                    317: {
                    318:        FilingSubset1_Credentials credentials;
                    319:        FilingSubset1_LogonResults logonresult;
                    320:        FilingSubset1_LogonResults *resultptr= &logonresult;
                    321:        Filing4_LogonResults logonresult2;
                    322:        FilingSubset1_AttributeSequence attrseq;
                    323:        FilingSubset1_OpenResults openresult;
                    324:        Filing4_OpenResults openresult2;
                    325: 
                    326: 
                    327:        if ( name != 0 )
                    328:                username = CH_StringToName(name,&hostobjname);
                    329: 
                    330:        if ( usefiling ) {
                    331:                usefiling= 0;
                    332:                filing_subset= FALSE;
                    333:                if ( name == 0 && pwd == 0 ) {
                    334:                        GetSimpleCredsAndVerifier(&username, 0, 
                    335:                                                        &credentials.primary, &verifier);
                    336:                } else {
                    337:                        MakeSimpleCredsAndVerifier(&username,pwd,
                    338:                                                &credentials.primary, &verifier);
                    339:                }
                    340:                logonresult2= Filing4_Logon(connected, NULL, hostobjname,
                    341:                                                credentials.primary, verifier);
                    342:                resultptr= (FilingSubset1_LogonResults *) &logonresult2;
                    343:                
                    344:        } else {
                    345:                usefiling= 0;
                    346:                if ( name == 0 && pwd == 0 ) {
                    347:                        GetSimpleCredsAndVerifier(&username, 0, &credentials.primary, &verifier);
                    348:                        MakeSecondaryCreds(hostobjname.object, username.object, 0, &credentials.secondary);
                    349:                } else {
                    350:                        MakeSimpleCredsAndVerifier(0, pwd, &credentials.primary, &verifier);
                    351:                        MakeSecondaryCreds(hostobjname.object, name, pwd, &credentials.secondary);
                    352:                }
                    353:                filing_subset= TRUE;
                    354:                DURING 
                    355:                        logonresult = FilingSubset1_Logon(connected, NULL, hostobjname,
                    356:                                            credentials, verifier);
                    357:                HANDLER {
                    358:                        switch (Exception.Code) {
                    359:                        case REJECT_ERROR:
                    360:                                filing_subset= FALSE;
                    361:                                logonresult2= Filing4_Logon(connected, NULL, hostobjname,
                    362:                                                        credentials.primary, verifier);
                    363:                                resultptr= (FilingSubset1_LogonResults *) &logonresult2;
                    364:                                break;
                    365:                        default:
                    366:                                RERAISE;
                    367:                        }
                    368:                } END_HANDLER;
                    369:        }
                    370: 
                    371:        if ( filing_subset )
                    372:                session = resultptr->session;
                    373:        else
                    374:                session = resultptr->session;
                    375: 
                    376:        attrseq.length= 0;
                    377:        attrseq.sequence= 0;
                    378:        if ( filing_subset ) {
                    379:                openresult= FilingSubset1_Open(connected, NULL, attrseq,
                    380:                                         FilingSubset1_nullHandle, nullControls,
                    381:                                         session);
                    382:                copyhandle(rootHandle, openresult.file);
                    383:        } else {
                    384:                openresult2= Filing4_Open(connected, NULL, attrseq,
                    385:                                         FilingSubset1_nullHandle, nullControls,
                    386:                                         session);
                    387:                copyhandle(rootHandle, openresult2.file);
                    388:        }
                    389:        strcpy(cur_dir, ROOT_DIRECTORY);
                    390: }
                    391: 
                    392: remotelist(remote)
                    393:        char *remote;
                    394: {
                    395:        FilingSubset1_Handle dirhandle; /* note: an array */
                    396:        FilingSubset1_AttributeTypeSequence typeseq;
                    397:        FilingSubset1_AttributeType tsvals[10];
                    398:        FilingSubset1_ScopeSequence scopeseq;
                    399:        FilingSubset1_Scope scope;
                    400: 
                    401:        typeseq.length = 0;  typeseq.sequence = tsvals;
                    402:        scopeseq.length= 1; scopeseq.sequence= &scope;
                    403:        scope.designator= FilingSubset1_filter;
                    404:        scope.FilingSubset1_filter_case.designator= FilingSubset1_matches;
                    405:        if ( filing_subset )
                    406:                scope.FilingSubset1_filter_case.FilingSubset1_matches_case.attribute.type= FilingSubset1_pathname;
                    407:        else
                    408:                scope.FilingSubset1_filter_case.FilingSubset1_matches_case.attribute.type= FilingSubset1_name;
                    409: 
                    410:        getdirhandle(remote, dirhandle);
                    411: 
                    412:        if ( filing_subset )
                    413:                StringToAttr(cur_pathname,&scope.FilingSubset1_filter_case.FilingSubset1_matches_case.attribute);
                    414:        else
                    415:                StringToAttr(cur_name,&scope.FilingSubset1_filter_case.FilingSubset1_matches_case.attribute);
                    416: 
                    417:        typeseq.length = 2;
                    418:        typeseq.sequence[0] = FilingSubset1_pathname;
                    419:        typeseq.sequence[1] = FilingSubset1_type;
                    420: 
                    421:        ProcEachSeq = listproc;
                    422:        if ( filing_subset )
                    423:                FilingSubset1_List(connected, GetAttributeSequences, dirhandle,
                    424:                             typeseq, scopeseq,
                    425:                             BulkData1_immediateSink, session);
                    426:        else
                    427:                Filing4_List(connected, GetAttributeSequences, dirhandle,
                    428:                             typeseq, scopeseq,
                    429:                             BulkData1_immediateSink, session);
                    430: 
                    431: 
                    432:        if ( !files_found )
                    433:                fprintf(stderr, "\n(%s)%s not found\n", service, remote);
                    434: 
                    435:        freefilehandle(dirhandle);
                    436: 
                    437: }
                    438: 
                    439: listproc(attr)
                    440:        FilingSubset1_AttributeSequence attr;
                    441: {
                    442:        int i;
                    443:        FilingSubset1_AttributeType t;
                    444:        char *thisname, *name;
                    445:        LongCardinal thistype;
                    446: 
                    447:        files_found= TRUE;
                    448: 
                    449:        for (i = 0; i < attr.length; i++) {
                    450:                t = attr.sequence[i].type;
                    451:                if (t == FilingSubset1_name || t == FilingSubset1_pathname) {
                    452:                        thisname = AttrToString(&attr.sequence[i]);
                    453:                } else if (t == FilingSubset1_type) {
                    454:                        thistype = AttrToLongCardinal(&attr.sequence[i]);
                    455:                }
                    456:        }
                    457: 
                    458:        if ( (name= rindex(thisname, '/')) == 0 )
                    459:                name= thisname;
                    460:        else
                    461:                name++;
                    462: 
                    463:        printf("  %s:\t%s\n", name, typetostring(thistype));
                    464:        clear_String(&thisname);
                    465: }
                    466: 
                    467: 
                    468: #define MAXPACKS 20
                    469: static
                    470: GetAttributeSequences(conn)
                    471:        CourierConnection *conn;
                    472: {
                    473:        int count, i;
                    474:        Unspecified buffer[MAXWORDS*MAXPACKS], *bp, *bufend;
                    475:        FilingSubset1_StreamOfAttributeSequence attrs;
                    476:        
                    477:        files_found= FALSE;
                    478: 
                    479:        bufend = buffer;
                    480:        bp = buffer+((MAXWORDS-1)*MAXPACKS);    /* end of available space */
                    481:        while ((count = BDTread(conn, (char*)bufend, 
                    482:                                MAXWORDS*sizeof(Unspecified))) > 0) {
                    483:                bufend += count/sizeof(Unspecified);
                    484:                if (bufend > bp) {
                    485:                        fprintf(stderr,"BDT read too big to fit\n");
                    486:                        BDTabort(conn);
                    487:                        /* should clear out stuff here if we knew how much */
                    488:                }
                    489:        }
                    490:        bp = buffer;
                    491:        while (bp < bufend) {
                    492:                bp += internalize_FilingSubset1_StreamOfAttributeSequence(&attrs,bp);
                    493:                if (0 == (int) attrs.designator) {
                    494:                   for (i=0; i < attrs.nextSegment_case.segment.length; i++) {
                    495:                        (*ProcEachSeq)(
                    496:                                attrs.nextSegment_case.segment.sequence[i]);
                    497:                   }
                    498:                   free(attrs.nextSegment_case.segment.sequence);
                    499:                } else {
                    500:                   for (i = 0; i < attrs.lastSegment_case.length; i++) {
                    501:                        (*ProcEachSeq)(
                    502:                                attrs.lastSegment_case.sequence[i]);
                    503:                   }
                    504:                   free(attrs.lastSegment_case.sequence);
                    505:                   return;
                    506:                }
                    507:        }
                    508: }
                    509: 
                    510: 
                    511: locallist(file)
                    512:        char *file;
                    513: {
                    514:        FILE *fin, *fopen();
                    515:        int type;
                    516:        char *dir, *slash;
                    517: 
                    518:        if ( strcmp(oldservice, myhostname) != 0 ) {
                    519:                printf("\n\n(%s):\n", myhostname);
                    520:                strcpy(oldservice, myhostname);
                    521:        }
                    522: 
                    523:        if ( (slash= rindex(file, '/')) == 0 ) {
                    524:                dir= mydir;
                    525:        } else {
                    526:                *slash= '\0';
                    527:                dir= (slash == file ? "/" : file);
                    528:        }
                    529: 
                    530:        if ( strcmp(dir, olddir) != 0 ) {
                    531:                printf("\n%s:\n\n", dir);
                    532:                strcpy(olddir, dir);
                    533:        }
                    534: 
                    535:        if ( slash ) *slash= '/';
                    536: 
                    537:        if ( access(file, R_OK | F_OK ) == -1 ) {
                    538:                fprintf(stderr, "  %s:\tError ", (slash ? slash+1 : file));
                    539:                perror("");
                    540:                return;
                    541:        }
                    542: 
                    543:        type= get_type(file);
                    544: 
                    545:        printf("  %s:\t%s\n", (slash ? slash+1 : file), typetostring(type));
                    546: 
                    547: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.