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