home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / XARCHIE2.TAR / get_vdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-03  |  13.9 KB  |  484 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <copyright.h>.
  6.  *
  7.  * v1.2.1 - 10/23/91 (bpk) - added missing code
  8.  * v1.2.0 - 09/17/91 (bpk) - added new get_vdir code
  9.  * v1.1.1 - 08/30/91 (bpk) - cast rindex()
  10.  */
  11.  
  12. #include <copyright.h>
  13. #include <stdio.h>
  14. #if defined(USG) || defined(UTS) || defined(_AIX)
  15. # include <string.h>
  16. #else
  17. # include <strings.h>
  18. #endif
  19.  
  20. #include <pfs.h>
  21. #include <pprot.h>
  22. #include <perrno.h>
  23. #include <pcompat.h>
  24. #include <pauthent.h>
  25. #include <pmachine.h>
  26.  
  27. #ifdef DEBUG
  28. extern int    pfs_debug;
  29. #endif
  30.  
  31. extern int    pwarn;
  32. extern char    p_warn_string[];
  33. extern int    perrno;
  34. extern char    p_err_string[];
  35.  
  36. /*
  37.  * get_vdir - Get contents of a directory given its location
  38.  *
  39.  *          GET_VDIR takes a directory location, a list of desired
  40.  *          components, a pointer to a directory structure to be 
  41.  *          filled in, and flags.  It then queries the appropriate 
  42.  *          directory server and retrieves the desired information.
  43.  *
  44.  *      ARGS:   dhost       - Host on which directory resides
  45.  *              dfile       - Directory on that host
  46.  *              components  - The names from the directory we want
  47.  *        dir        - Structure to be filled in
  48.  *            flags       - Options.  See FLAGS
  49.  *        filters     - filters to be applied to result 
  50.  *              acomp       - Pointer to remaining components
  51.  *
  52.  *     FLAGS:    GVD_UNION   - Do not expand union links
  53.  *        GVD_EXPAND  - Expand union links locally
  54.  *        GVD_REMEXP  - Request remote expansion (& local if refused)
  55.  *        GVD_LREMEXP - Request remote expansion of local union links
  56.  *        GVD_VERIFY  - Only verify that args are for a directory
  57.  *              GVD_ATTRIB  - Request attributes from directory server
  58.  *              GVD_NOSORT  - Do not sort links when adding to directory
  59.  *
  60.  *   RETURNS:   PSUCCESS (0) or error code
  61.  *        On some codes addition information in p_err_string
  62.  *
  63.  *     NOTES:   If acomp is non-null the string it points to might be modified
  64.  *
  65.  *              If the directory passed as an argument already has
  66.  *        links or union links, then those lists will be freed
  67.  *              before the new contents are filled in.
  68.  *
  69.  *              If a filter is passed to the procedure, and application of
  70.  *              the filter results in additional union link, then those links
  71.  *              will (or will not) be expanded as specified in the FLAGS field.
  72.  *
  73.  *              If the list of components in NULL, or the null string, then
  74.  *              get_vdir will return all links in the requested directory.
  75.  *
  76.  *      BUGS:   Doesn't process union links yet
  77.  *              Doesn't process errors returned from server
  78.  *        Doesn't expand union links if requested to
  79.  */
  80. int
  81. get_vdir(dhost,dfile,components,dir,flags,filters,acomp)
  82.     char    *dhost;        /* Host on which directory resides           */
  83.     char    *dfile;        /* Name of file on that host                 */
  84.     char    *components;    /* Component name (wildcards allowed)        */
  85.     VDIR    dir;        /* Structure to be filled in             */
  86.     long    flags;        /* Flags                         */
  87.     VLINK    filters;    /* Filters to be applied to result           */
  88.     char    *acomp;        /* Components left to be resolved            */
  89.     {
  90.         PTEXT    request;    /* Text of request to dir server             */
  91.     PTEXT    resp;            /* Response from dir server                 */
  92.  
  93.     char    ulcomp[MAX_VPATH];/* Work space for new current component    */
  94.     char    *comp = components;
  95.  
  96.     VLINK    cur_link = NULL;/* Current link being filled in              */
  97.     VLINK     exp = NULL;     /* The current ulink being expanded         */
  98.     VLINK    pul = NULL;     /* Prev union link (insert new one after it) */
  99.     VLINK    l;        /* Temp link pointer                  */
  100.     int    mcomp;        /* Flag - check multiple components          */
  101.     int    unresp;        /* Flag - received unresolved response       */
  102.     int    getattrib = 0;  /* Get attributes from server                */
  103.     int    vl_insert_flag; /* Flags to vl_insert                        */
  104.  
  105.     int    fwdcnt = MAX_FWD_DEPTH;
  106.  
  107.     int    no_links = 0;   /* Count of number of links found         */
  108.  
  109.     char    options[40];    /* LIST option                               */
  110.     char    *opt;           /* After leading +                           */
  111.  
  112.     PAUTH    authinfo;
  113.  
  114.     /* Treat null string like NULL (return entire directory) */
  115.     if(!components || !*components) comp = NULL;
  116.  
  117.     if(acomp && !filters) mcomp = 1;
  118.     else mcomp = 0;
  119.  
  120.     if(flags&GVD_ATTRIB) {
  121.         getattrib++;
  122.         flags &= (~GVD_ATTRIB);
  123.     }
  124.  
  125.     if(flags&GVD_NOSORT) vl_insert_flag = VLI_NOSORT;
  126.     else vl_insert_flag = VLI_ALLOW_CONF;
  127.     flags &= (~GVD_NOSORT);
  128.  
  129.     if(filters) comp = NULL;
  130.  
  131.     perrno = 0;
  132.  
  133.     authinfo = get_pauth(PFSA_UNAUTHENTICATED);
  134.  
  135.     *options = '\0';
  136.  
  137.     if(getattrib) {
  138.         strcat(options,"+ATTRIBUTES");
  139.         flags &= (~GVD_ATTRIB);
  140.     }
  141.  
  142.     if(!filters) { /* Can't do remote expansion if filters to be applied */
  143.         if(flags == GVD_REMEXP) strcat(options,"+EXPAND");
  144.         if(flags == GVD_LREMEXP) strcat(options,"+LEXPAND");
  145.     }
  146.  
  147.     /* If all we are doing is verifying that dfile is a directory */
  148.     /* then we do not want a big response from the directory      */
  149.     /* server.  A NOT-FOUND is sufficient.                  */
  150.     if(flags == GVD_VERIFY)
  151. #ifdef NEWVERIFYOPT
  152.         strcat(options,"+VERIFY");
  153. #else
  154.     comp = "%#$PRobably_nOn_existaNT$#%";
  155. #endif
  156.  
  157.     if(*options) opt = options+1;
  158.     else opt = "''";
  159.  
  160.     startover:
  161.     request = ptalloc();
  162.  
  163.     sprintf(request->start,
  164.         "VERSION %d\nAUTHENTICATOR %s %s\nDIRECTORY ASCII %s\nLIST %s COMPONENTS %s%s%s\n",
  165.         VFPROT_VNO, authinfo->auth_type, authinfo->authenticator,
  166.         dfile,opt, (comp ? comp : ""), (mcomp ? "/" : ""),
  167.         (mcomp ? acomp : ""));
  168.  
  169.     request->length = strlen(request->start);
  170.  
  171. #ifdef DEBUG
  172.     if(pfs_debug > 2)
  173.         fprintf(stderr,"Sending message to dirsrv:\n%s",request->start);
  174. #endif
  175.  
  176.     resp = dirsend(request,dhost,0);
  177.  
  178. #ifdef DEBUG
  179.     if(pfs_debug && (resp == NULL)) {
  180.         fprintf(stderr,"Dirsend failed: %d\n",perrno);
  181.     }
  182. #endif
  183.  
  184.     /* If we don't get a response, then if the requested       */
  185.     /* directory, return error, if a ulink, mark it unexpanded */
  186.     if(resp == NULL) {
  187.         if(exp) exp->expanded = FAILED;
  188.         else return(perrno);
  189.     }
  190.  
  191.     unresp = 0;
  192.  
  193.     /* Here we must parse reponse and put in directory */
  194.     /* While looking at each packet            */
  195.     while(resp) {
  196.         PTEXT        vtmp;
  197.         char        *line;
  198.  
  199.         vtmp = resp;
  200. #ifdef DEBUG
  201.         if(pfs_debug > 3) fprintf(stderr,"%s\n",resp->start);
  202. #endif
  203.         /* Look at each line in packet */
  204.         for(line = resp->start;line != NULL;line = nxtline(line)) {
  205.         switch (*line) {
  206.             
  207.             /* Temporary variables to hold link info */
  208.             char    l_linktype;
  209.             char     l_name[MAX_DIR_LINESIZE];
  210.             char    l_type[MAX_DIR_LINESIZE];
  211.             char     l_htype[MAX_DIR_LINESIZE];
  212.             char     l_host[MAX_DIR_LINESIZE];
  213.             char     l_ntype[MAX_DIR_LINESIZE];
  214.             char     l_fname[MAX_DIR_LINESIZE];
  215.             int        l_version;
  216.             char     t_unresolved[MAX_DIR_LINESIZE];
  217.             int        l_magic;
  218.             int        tmp;
  219.  
  220.         case 'L': /* LINK or LINK-INFO */
  221.             if(strncmp(line,"LINK-INFO",9) == 0) {
  222.             PATTRIB        at;
  223.             PATTRIB        last_at;
  224.             at = parse_attribute(line);
  225.             if(!at) break;
  226.  
  227.             /* Cant have link info without a link */
  228.             if(!cur_link) {
  229.                 perrno = DIRSRV_BAD_FORMAT;
  230.                 atfree(at);
  231.                 break;
  232.             }
  233.             
  234.             if(cur_link->lattrib) {
  235.                 last_at = cur_link->lattrib;
  236.                 while(last_at->next) last_at = last_at->next;
  237.                 at->previous = last_at;
  238.                 last_at->next = at;
  239.             }
  240.             else {
  241.                 cur_link->lattrib = at;
  242.                 at->previous = NULL;
  243.             }
  244.             break;
  245.             }
  246.  
  247.             /* Not LINK-INFO, must be LINK - if not check for error */
  248.             if(strncmp(line,"LINK",4) != 0) goto scanerr;
  249.  
  250.             /* If only verifying, don't want to change dir */
  251.             if(flags == GVD_VERIFY) {
  252.             break;
  253.             }
  254.             /* If first link and some links in dir, free them */
  255.             if(!no_links++) {
  256.             if(dir->links) vllfree(dir->links); dir->links=NULL;
  257.             if(dir->ulinks) vllfree(dir->ulinks); dir->ulinks=NULL;
  258.             }
  259.             
  260.             cur_link = vlalloc();
  261.  
  262.             /* parse and insert file info */
  263.             tmp = sscanf(line,"LINK %c %s %s %s %s %s %s %d %d", &l_linktype,
  264.                  l_type, l_name, l_htype, l_host, 
  265.                  l_ntype, l_fname, &(cur_link->version),
  266.                  &(cur_link->f_magic_no));
  267.  
  268.             if(tmp != 9) {
  269.             perrno = DIRSRV_BAD_FORMAT;
  270.             vlfree(cur_link);
  271.             break;
  272.             }
  273.  
  274.             cur_link->linktype = l_linktype;
  275.             cur_link->type = stcopyr(l_type,cur_link->type);
  276.             cur_link->name = stcopyr(unquote(l_name),cur_link->name);
  277.             cur_link->hosttype = stcopyr(l_htype,cur_link->hosttype);
  278.             cur_link->host = stcopyr(l_host,cur_link->host);
  279.             cur_link->nametype = stcopyr(l_ntype,cur_link->nametype);
  280.             cur_link->filename = stcopyr(l_fname,cur_link->filename);
  281.  
  282.             /* Double check to make sure we don't get */
  283.             /* back unwanted components              */
  284.             /* OK to keep if special (URP) links      */
  285.             /* or if mcomp specified                  */
  286.             if(!mcomp && (cur_link->linktype == 'L') && 
  287.                (!wcmatch(cur_link->name,comp))) {
  288.             vlfree(cur_link);
  289.             break;
  290.             }
  291.  
  292.             /* If other optional info was sent back, it must */
  293.             /* also be parsed before inserting link     ***  */
  294.             
  295.             
  296.             if(cur_link->linktype == 'L') 
  297.             vl_insert(cur_link,dir,vl_insert_flag);
  298.             else {
  299.             tmp = ul_insert(cur_link,dir,pul);
  300.  
  301.             /* If inserted after pul, next one after cur_link */
  302.             if(pul && (!tmp || (tmp == UL_INSERT_SUPERSEDING)))
  303.                 pul = cur_link;
  304.             }
  305.             
  306.             break;
  307.  
  308.         case 'F': /* FILTER, FAILURE or FORWARDED*/
  309.             /* FORWARDED */
  310.             if(strncmp(line,"FORWARDED",9) == 0) {
  311.             if(fwdcnt-- <= 0) {
  312.                 ptlfree(resp);
  313.                 perrno = PFS_MAX_FWD_DEPTH;
  314.                 return(perrno);
  315.             }
  316.             /* parse and start over */
  317.  
  318.             tmp = sscanf(line,"FORWARDED %s %s %s %s %d %d", 
  319.                      l_htype,l_host,l_ntype,l_fname,
  320.                      &l_version, &l_magic);
  321.  
  322.             dhost = stcopy(l_host);
  323.             dfile = stcopy(l_fname);
  324.  
  325.             if(tmp < 4) {
  326.                 perrno = DIRSRV_BAD_FORMAT;
  327.                 break;
  328.             }
  329.  
  330.             ptlfree(resp);
  331.             goto startover;
  332.             }
  333.             if(strncmp(line,"FILTER",6) != 0) goto scanerr;
  334.             break;
  335.  
  336.  
  337.         case 'M': /* MULTI-PACKET (processed by dirsend) */
  338.         case 'P': /* PACKET (processed by dirsend) */
  339.             break;
  340.  
  341.         case 'N': /* NOT-A-DIRECTORY or NONE-FOUND */
  342.             /* NONE-FOUND, we just have no links to insert */
  343.             /* It is not an error, but we must clear any   */
  344.             /* old links in the directory arg              */
  345.             if(strncmp(line,"NONE-FOUND",10) == 0) {
  346.             /* If only verifying, don't want to change dir */
  347.             if(flags == GVD_VERIFY) {
  348.                 break;
  349.             }
  350.  
  351.             /* If first link and some links in dir, free them */
  352.             if(!no_links++) {
  353.                 if(dir->links) vllfree(dir->links);
  354.                 if(dir->ulinks) vllfree(dir->ulinks);
  355.                 dir->links = NULL;
  356.                 dir->ulinks = NULL;
  357.             }
  358.             break;
  359.             }
  360.             /* If NOT-A-DIRECTORY or anything else, scan error */
  361.             goto scanerr;
  362.  
  363.         case 'U': /* UNRESOLVED */
  364.             if(strncmp(line,"UNRESOLVED",10) != 0) {
  365.             goto scanerr;
  366.             }
  367.             tmp = sscanf(line,"UNRESOLVED %s", t_unresolved);
  368.             if(tmp < 1) {
  369.             perrno = DIRSRV_BAD_FORMAT;
  370.             break;
  371.             }
  372.             /* If multiple components were resolved */
  373.             if(strlen(t_unresolved) < strlen(acomp)) {
  374.             strcpy(ulcomp,acomp);
  375.             /* ulcomp is the components that were resolved */
  376.             *(ulcomp+strlen(acomp)-strlen(t_unresolved)-1) = '\0';
  377.             /* Comp gets the last component resolved */
  378.             comp = (char *) rindex(ulcomp,'/');
  379.             if(comp) comp++;
  380.             else comp = ulcomp;
  381.             /* Let rd_vdir know what remains */
  382.             strcpy(acomp,t_unresolved);
  383.             }
  384.             unresp = 1;
  385.             break;
  386.  
  387.         case 'V': /* VERSION-NOT-SUPPORTED */
  388.             if(strncmp(line,"VERSION-NOT-SUPPORTED",21) == 0) {
  389.             perrno = DIRSRV_BAD_VERS;
  390.             return(perrno);
  391.             }
  392.             goto scanerr;
  393.  
  394.         scanerr:
  395.         default:
  396.             if(*line && (tmp = scan_error(line))) {
  397.             ptlfree(resp);
  398.             return(tmp);
  399.             }
  400.             break;
  401.         }
  402.         }
  403.  
  404.         resp = resp->next;
  405.  
  406.         ptfree(vtmp);
  407.     }
  408.  
  409.     /* We sent multiple components and weren't told any */
  410.     /* were unresolved                                  */
  411.     if(mcomp && !unresp) {
  412.         /* ulcomp is the components that were resolved */
  413.         strcpy(ulcomp,acomp);
  414.         /* Comp gets the last component resolved */
  415.         comp = (char *) rindex(ulcomp,'/');
  416.         if(comp) comp++;
  417.         else comp = ulcomp;
  418.         /* If we have union links to resolve, only one component remains */
  419.         mcomp = 0;
  420.         /* Let rd_vdir know what remains */
  421.         *acomp = '\0';
  422.     }
  423.  
  424.     /* If only verifying, we already know it is a directory */
  425.     if(flags == GVD_VERIFY) return(PSUCCESS);
  426.  
  427.     /* Don't return if matching was delayed by the need to filter    */
  428.     /* if FIND specified, and dir->links is non null, then we have   */
  429.     /* found a match, and should return.                             */
  430.     if((flags & GVD_FIND) && dir->links && (!filters))
  431.         return(PSUCCESS);
  432.  
  433.     /* If expand specified, and ulinks must be expanded, making sure */
  434.         /* that the order of the links is maintained properly            */
  435.  
  436. expand_ulinks:
  437.  
  438.     if((flags != GVD_UNION) && (flags != GVD_VERIFY)) {
  439.  
  440.         l = dir->ulinks;
  441.  
  442.         /* Find first unexpanded ulink */
  443.         while(l && l->expanded && (l->linktype == 'U')) l = l->next;
  444.         
  445.         /* Only expand if a FILE or DIRECTORY -  Mark as  */
  446.             /* failed otherwise                               */
  447.         /* We must still add support for symbolic ulinks */
  448.         if(l) {
  449.         if ((strcmp(l->type,"DIRECTORY") == 0) || 
  450.             (strcmp(l->type,"FILE") == 0)) {
  451.             l->expanded = TRUE;
  452.             exp = l;
  453.             pul = l;
  454.             dhost = l->host;
  455.             dfile = l->filename;
  456.             goto startover; /* was get_contents; */
  457.         }
  458.         else l->expanded = FAILED;
  459.         }
  460.     }
  461.  
  462.     /* Double check to make sure we don't get */
  463.     /* back unwanted components          */
  464.     /* OK to keep if special (URP) links      */
  465.     if(components && *components) {
  466.         l = dir->links;
  467.         while(l) {
  468.         VLINK    ol;
  469.         if((l->linktype == 'L') && (!wcmatch(l->name,components))) {
  470.             if(l == dir->links)
  471.             dir->links = l->next;
  472.             else l->previous->next = l->next;
  473.             if(l->next) l->next->previous = l->previous;
  474.             ol = l;
  475.             l = l->next;
  476.             vlfree(ol);
  477.         }
  478.         else l = l->next;
  479.         }
  480.     }
  481.  
  482.     return(PSUCCESS);
  483.     }
  484.