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