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