home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / tcpip / amitcp-support / ncftp-1.5.6 / src / rcs / ftprc.c,v < prev    next >
Encoding:
Text File  |  1994-06-29  |  12.4 KB  |  632 lines

  1. head    14020.12;
  2. access;
  3. symbols
  4.     ORIGINAL:14020.11;
  5. locks; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 14020.12
  10. date    93.10.10.20.03.06;    author alph;    state Exp;
  11. branches;
  12. next    14020.11;
  13.  
  14. 14020.11
  15. date    93.07.09.10.58.37;    author alph;    state Exp;
  16. branches;
  17. next    ;
  18.  
  19.  
  20. desc
  21. @Original version
  22. @
  23.  
  24.  
  25. 14020.12
  26. log
  27. @amigados changes
  28. @
  29. text
  30. @/* ftprc.c */
  31.  
  32. /*  $RCSfile: ftprc.c,v $
  33.  *  $Revision: 1.1 $
  34.  *  $Date: 1993/10/02 10:34:34 $
  35.  */
  36.  
  37. #include "sys.h"
  38. #include <sys/types.h>
  39. #include <sys/param.h>
  40. #include <sys/stat.h>
  41.  
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include <time.h>
  45.  
  46. #include "util.h"
  47. #include "ftprc.h"
  48. #include "main.h"
  49. #include "cmds.h"
  50. #include "set.h"
  51. #include "defaults.h"
  52. #include "copyright.h"
  53.  
  54. /* ftprc.c global variables */
  55. siteptr                    firstsite = NULL, lastsite = NULL;
  56. recentsite                recents[dMAXRECENTS];
  57. int                        nRecents = 0;
  58. int                        nSites = 0;
  59. int                        keep_recent = dRECENT_ON;
  60. longstring                rcname;
  61. longstring                recent_file;
  62. int                        parsing_rc = 0;
  63.  
  64. extern char                *line, *margv[];
  65. extern int                margc, fromatty;
  66. extern string            anon_password;        /* most likely your email address */
  67. extern struct userinfo    uinfo;
  68.  
  69. #ifndef amigados /* make filename from path and name */
  70. #define strmfp(d,p,n)    (void) sprintf(d, "%s%s", p, n);
  71. #endif
  72.  
  73. int thrash_rc(void)
  74. {
  75.     struct stat            st;
  76.     string                word, str;
  77.     longstring            cwd;
  78.     char                *cp, *dp, *rc;
  79.     FILE                *fp;
  80.     int                    i;
  81. #ifdef amigados
  82.     char rc2[255]; /* FIXME */
  83. #endif
  84.     (void) get_cwd(cwd, sizeof(cwd));
  85. #ifndef amigados
  86.     if (cwd[strlen(cwd) - 1] != '/')
  87.         (void) Strncat(cwd, "/");
  88. #endif
  89.  
  90.     /* Because some versions of regular ftp complain about ncftp's
  91.      * #set commands, FTPRC takes precedence over NETRC.
  92.      */
  93.     cp = getenv("DOTDIR");
  94.     for (i=0; i<2; i++) {
  95.         rc = (i == 0) ? FTPRC : NETRC;
  96.  
  97.         strmfp(rcname, cwd, rc);
  98.         if (stat(rcname, &st) == 0)
  99.             goto foundrc;
  100.         
  101. #ifndef amigados
  102.         (void) sprintf(rcname, "%s.%s", cwd, rc);
  103. #else
  104.         (void) sprintf(rc2, ".%s", rc);
  105.         strmfp(rcname, cwd, rc2);
  106. #endif
  107.         if (stat(rcname, &st) == 0)
  108.             goto foundrc;
  109.  
  110.         if (cp != NULL) {
  111. #ifndef amigados
  112.             (void) sprintf(rcname, "%s/.%s", cp, rc);
  113. #else
  114.             strmfp(rcname, cp, rc2);
  115. #endif
  116.             if (stat(rcname, &st) == 0)
  117.                 goto foundrc;
  118.         }
  119. #ifndef amigados
  120.         (void) sprintf(rcname, "%s/.%s", uinfo.homedir, rc);
  121. #else
  122.         strmfp(rcname, uinfo.homedir, rc2);
  123. #endif
  124.         if (stat(rcname, &st) == 0)
  125.             goto foundrc;
  126.     }
  127.  
  128.     return (0);    /* it's OK not to have an rc. */
  129.     
  130. foundrc:
  131. #ifndef amigados    
  132.     if ((st.st_mode & 077) != 0)                /* rc must be unreadable by others. */
  133.         (void) chmod(rcname, 0600);
  134. #endif
  135.  
  136.     if ((fp = fopen(rcname, "r")) == NULL) {
  137.         PERROR("thrash_rc", rcname);
  138.         return -1;
  139.     }
  140.     
  141.     parsing_rc = 1;
  142.     while (cp = FGets(str, fp)) {
  143.         while (isspace(*cp)) ++cp;        /* skip leading space. */
  144.         if (*cp == '#') {
  145.             if ((strncmp("set", ++cp, (size_t)3) == 0) || (strncmp("unset", cp, (size_t)5) == 0)) {
  146.                 (void) strcpy(line, cp);
  147.                 makeargv();
  148.                 (void) set(margc, margv);            
  149.                 /* setting or unsetting a variable. */
  150.             } /* else a comment. */
  151.         } else {
  152.             if (strncmp(cp, "machine", (size_t) 7) == 0) {
  153.                 /* We have a new machine record. */
  154.                 cp += 7;
  155.                 while (isspace(*cp)) ++cp;    /* skip delimiting space. */
  156.                 dp = word;
  157.                 while (*cp && !isspace(*cp)) *dp++ = *cp++;    /* copy the name. */
  158.                 *dp = 0;
  159.                 AddNewSitePtr(word);
  160.             }
  161.         }
  162.     }
  163.     (void) fclose(fp);
  164.     parsing_rc = 0;
  165.     return 1;
  166. }    /* thrash_rc */
  167.  
  168.  
  169.  
  170.  
  171. void AddNewSitePtr(char *word)
  172. {
  173.     siteptr            s;
  174.  
  175.     if (s = (siteptr) malloc(sizeof(site))) {
  176.         s->next = NULL;
  177.         if (s->name = malloc(strlen(word) + 1)) {
  178.             (void) strcpy(s->name, word);
  179.             if (firstsite == NULL)
  180.                 firstsite = lastsite = s;
  181.             else {
  182.                 lastsite->next = s;
  183.                 lastsite = s;
  184.             }
  185.             ++nSites;
  186.         } else {
  187.             Free(s);
  188.         }
  189.     }
  190. }    /* AddNewSitePtr */
  191.  
  192.  
  193.  
  194.  
  195. static int RecentCmp(recentsite *a, recentsite *b)
  196. {
  197.     int i = 1;
  198.     
  199.     if (a->lastcall > b->lastcall)
  200.         i = -1;
  201.     else if (a->lastcall == b->lastcall)
  202.         i = 0;
  203.     return i;
  204. }    /* RecentCmp */
  205.  
  206.  
  207.  
  208.  
  209. static siteptr FindNetrcSite(char *host)
  210. {
  211.     register siteptr s, s2;
  212.  
  213.     /* see if 'host' is in our list of favorite sites (in NETRC). */
  214.     for (s = firstsite; s != NULL; s2=s->next, s=s2) {
  215.         if (strstr(s->name, host) != NULL) {
  216.             return s;
  217.         }
  218.     }
  219.     return NULL;
  220. }    /* FindNetrcSite */
  221.  
  222.  
  223.  
  224.  
  225. static recentsite *FindRecentSite(char *host)
  226. {
  227.     register recentsite        *r;
  228.     register int            i;
  229.  
  230.     /* see if 'host' is in our list of favorite sites (in recent-log). */
  231.     for (i=0; i<nRecents; i++) {
  232.         r = &recents[i];
  233.         if (strstr(r->name, host) != NULL) {
  234.             return r;
  235.         }
  236.     }
  237.     return NULL;
  238. }    /* FindRecentSite */
  239.  
  240.  
  241.  
  242.  
  243. void ReadRecentSitesFile(void)
  244. {
  245.     FILE *rfp;
  246.     recentsite *r;
  247.     char name[64], dir[256];
  248.     string str;
  249.  
  250.     nRecents = 0;
  251.     if (recent_file[0] != 0 && keep_recent) {
  252.         rfp = fopen(recent_file, "r");
  253.         if (rfp != NULL) {
  254.             for (; nRecents < dMAXRECENTS; ) {
  255.                 r = &recents[nRecents];
  256.                 if (FGets(str, rfp) == NULL)
  257.                     break;
  258.                 if (sscanf(str, "%s %lu %s", name, (unsigned long *) &r->lastcall, dir) == 3) {
  259.                     if ((r->name = NewString(name)) != NULL) {
  260.                         r->dir = NewString(dir);
  261.                         if (r->dir != NULL)
  262.                             nRecents++;
  263.                         else free(r->name);
  264.                     }
  265.                 }
  266.             }
  267.             (void) fclose(rfp);
  268.         }
  269.     }
  270. }    /* ReadRecentSitesFile */
  271.  
  272.  
  273.  
  274. static void SortRecentList(void)
  275. {
  276.     QSort(recents, nRecents, sizeof(recentsite), RecentCmp);
  277. }    /* SortRecentList */
  278.  
  279.  
  280.  
  281.  
  282. void WriteRecentSitesFile(void)
  283. {
  284.     FILE            *rfp;
  285.     recentsite        *r;
  286.     int                i;
  287.  
  288.     if ((recent_file[0] != 0) && (nRecents > 0) && (keep_recent)) {
  289.         rfp = fopen(recent_file, "w");
  290.         SortRecentList();
  291.         if (rfp != NULL) {
  292.             for (i=0; i<nRecents; i++) {
  293.                 r = &recents[i];
  294.                 (void) fprintf(rfp, "%-32s %11lu %s\n", r->name,
  295.                     (unsigned long) r->lastcall, r->dir);
  296.             }
  297.             (void) fclose(rfp);
  298.             (void) chmod(recent_file, 0600);
  299.         }
  300.     }
  301. }    /* WriteRecentSitesFile */
  302.  
  303.  
  304.  
  305.  
  306. void AddRecentSite(char *host, char *lastdir)
  307. {
  308.     char            *nhost, *ndir;
  309.     recentsite        *r;
  310.     
  311.     if (keep_recent) {
  312.         nhost = NewString(host);
  313.         /* Use '/' to denote that the current directory wasn't known,
  314.          * because we won't try to cd to the root directory.
  315.          */
  316.         ndir = NewString(*lastdir ? lastdir : "/");
  317.         
  318.         /* Don't bother if we don't have the memory, or if it is already
  319.          * in our NETRC.
  320.          */
  321.         if ((ndir != NULL) && (nhost != NULL) && (FindNetrcSite(host) == NULL)) {
  322.             if (nRecents == dMAXRECENTS) {
  323.                 SortRecentList();
  324.                 r = &recents[dMAXRECENTS - 1];
  325.                 if (r->name != NULL)
  326.                     free(r->name);
  327.                 if (r->dir != NULL)
  328.                     free(r->dir);
  329.             } else {
  330.                 r = &recents[nRecents];
  331.                 nRecents++;
  332.             }
  333.             r->name = nhost;            
  334.             r->dir = ndir;
  335.             (void) time(&r->lastcall);
  336.             SortRecentList();
  337.         }
  338.     }
  339. }    /* AddRecentSite */
  340.  
  341.  
  342.  
  343.  
  344. /*
  345.  * After you are done with a site (by closing it or quitting), we
  346.  * need to update the list of recent sites called.
  347.  */
  348. void UpdateRecentSitesList(char *host, char *lastdir)
  349. {
  350.     recentsite *r;
  351.     char *ndir;
  352.  
  353.     if (keep_recent) {    
  354.         r = FindRecentSite(host);
  355.         if (r == NULL)
  356.             AddRecentSite(host, lastdir);
  357.         else {
  358.             /* Update the last time connected, and the directory we left in. */
  359.             if ((ndir = NewString(*lastdir ? lastdir : "/")) != NULL) {
  360.                 free(r->dir);
  361.                 r->dir = ndir;
  362.             }
  363.             (void) time(&r->lastcall);
  364.         }
  365.     }
  366. }    /* UpdateRecentSitesList */
  367.  
  368.  
  369.  
  370. /*
  371.  * Prints out the number of sites we know about, so the user can figure out
  372.  * an abbreviation or type it's number to open (setpeer).
  373.  */
  374. void PrintSiteList(void)
  375. {
  376.     int                        i, j;
  377.     siteptr                    s, s2;
  378.  
  379.     if (fromatty) {
  380.         j = 0;
  381.         i = 1;
  382.         if (nRecents > 0) {
  383.             j++;
  384.             (void) printf("Recently called sites:\n");
  385.             for (; i<=nRecents; i++) {
  386.                 (void) printf("%4d. %-32s", i, recents[i-1].name);
  387.                 i++;
  388.                 if (i <= nRecents) {
  389.                     (void) printf("%5d. %-32s", i, recents[i-1].name);
  390.                 } else {
  391.                     (void) printf("\n");
  392.                     break;
  393.                 }
  394.                 (void) printf("\n");
  395.             }
  396.         }
  397.         if (nSites > 0) {
  398.             j++;
  399.             (void) printf("Sites in your netrc (%s):\n", rcname);
  400.             for (s = firstsite; s != NULL; s2=s->next, s=s2, ++i) {
  401.                 (void) printf("%4d. %-32s", i, s->name);
  402.                 s2=s->next;
  403.                 s=s2;
  404.                 i++;
  405.                 if (s != NULL) {
  406.                     (void) printf("%5d. %-32s", i, s->name);
  407.                 } else {
  408.                     (void) printf("\n");
  409.                     break;
  410.                 }
  411.                 (void) printf("\n");
  412.             }
  413.         }
  414.         if (j > 0) {
  415.             (void) printf("\
  416. Note that you can specify an abbreviation of any name, or #x, where x is the\n\
  417. number of the site you want to connect to.\n\n");
  418.         }
  419.     }
  420. }    /* PrintRecentSiteList */
  421.  
  422.  
  423.  
  424.  
  425. /*
  426.  * Given a sitename, check to see if the name was really an abbreviation
  427.  * of a site in the NETRC, or a site in our list of recently connected
  428.  * sites.  Also check if the name was in the format #x, where x which
  429.  * would mean to use recents[x].name as the site; if x was greater than
  430.  * the number of sites in the recent list, then index into the netrc
  431.  * site list.
  432.  */
  433. void GetFullSiteName(char *host, char *lastdir)
  434. {
  435.     register siteptr        s, s2;
  436.     register recentsite        *r;
  437.     char                    *ndir, *nhost, *cp;
  438.     int                        x, i, isAllDigits;
  439.  
  440.     ndir = nhost = NULL;
  441.     x = 0;
  442.  
  443.     /* Don't allow just numbers as abbreviations;  "open 2" could be
  444.      * confused between site numbers in the open 'menu,' like
  445.      * "2. unlinfo.unl.edu" and IP numbers "128.93.2.1" or even numbers
  446.      * in the site name like "simtel20.army.mil."
  447.      */
  448.     
  449.     for (isAllDigits = 1, cp = host; *cp != 0; cp++) {
  450.         if (!isdigit(*cp)) {
  451.             isAllDigits = 0;
  452.             break;
  453.         }
  454.     }
  455.  
  456.     if (!isAllDigits) {
  457.         /* Try matching the abbreviation, since it isn't just a number. */
  458.         /* see if 'host' is in our list of favorite sites (in NETRC). */
  459.         if ((s = FindNetrcSite(host)) != NULL) {
  460.             nhost = s->name;
  461.         } else if ((r = FindRecentSite(host)) != NULL) {
  462.             nhost = r->name;
  463.             ndir = r->dir;
  464.         }
  465.     } else if (sscanf(host[0]=='#' ? host+1 : host, "%d", &x) != 1) {
  466.         x = 0;
  467.     }
  468.  
  469.     if (--x >= 0) {
  470.         if (x < nRecents) {
  471.             nhost = recents[x].name;
  472.             ndir = recents[x].dir;
  473.         } else {
  474.             x -= nRecents;
  475.             if (x < nSites) {
  476.                 for (i = 0, s = firstsite; i < x; s2=s->next, s=s2)
  477.                     ++i;                
  478.                 nhost = s->name;
  479.             }
  480.         }
  481.     }
  482.  
  483.     if (nhost != NULL) {
  484.         (void) strcpy(host, nhost);
  485.         if (lastdir != NULL) {
  486.             *lastdir = 0;
  487.             /* Don't cd if the dir is the root directory. */
  488.             if (ndir != NULL && (strcmp("/", ndir) != 0))
  489.                 (void) strcpy(lastdir, ndir);
  490.         }
  491.     }
  492. }    /* GetFullSiteName */
  493.  
  494.  
  495.  
  496.  
  497. int ruserpass2(char *host, char **username, char **pass, char **acct)
  498. {
  499.     FILE            *fp;
  500.     char            *cp, *dp, *dst, *ep;
  501.     str32            macname;
  502.     char            *varname;
  503.     int                site_found;
  504.     string            str;
  505.     static string    auser;
  506.     static str32    apass, aacct;
  507.  
  508.     site_found = 0;
  509.  
  510.     if ((fp = fopen(rcname, "r")) != NULL) {
  511.         parsing_rc = 1;
  512.         while (FGets(str, fp)) {
  513.             if (cp = strstr(str, "machine")) {
  514.                 /* Look for the machine token. */
  515.                 cp += 7;
  516.                 while (isspace(*cp))
  517.                     cp++;
  518.             } else
  519.                 continue;
  520.             /* if (strstr(cp, host) != NULL) { */
  521.             if (strncmp(host, cp, strlen(host)) == 0) {
  522.                 site_found = 1;
  523.                 while (!isspace(*cp))
  524.                     ++cp;        /* skip the site name. */
  525.                 do {
  526.                     /* Skip any comments ahead of time. */
  527.                     for (dp=cp; *dp; dp++) {
  528.                         if (*dp == '#') {
  529.                             *dp = 0;
  530.                             break;
  531.                         }
  532.                     }
  533.  
  534.                     ep = cp;
  535.                     while (1) {
  536.                         varname = strtok(ep, RC_DELIM);
  537.                         if (!varname) break;
  538.                         dst = ep = NULL;
  539.                         switch (*varname) {
  540.                             case 'u':    /* user */
  541.                                 *username = dst = auser;
  542.                                 break;
  543.                             case 'l':    /* login */
  544.                                 *username = dst = auser;
  545.                                 break;
  546.                             case 'p':    /* password */
  547.                                 *pass = dst = apass;
  548.                                 break;
  549.                             case 'a':    /* account */
  550.                                 *acct = dst = aacct;
  551.                                 break;
  552.                         /*    case 'd':  /o default */
  553.                         /* unused -- use 'set anon_password.' */
  554.                             case 'm':    /* macdef or machine */
  555.                                 if (strcmp(varname, "macdef"))
  556.                                     goto done;    /* new machine record... */
  557.                                 dst = macname;
  558.                                 break;
  559.                             default:
  560.                                 (void) fprintf(stderr, "Unknown .netrc keyword \"%s\"\n",
  561.                                     varname
  562.                                 );
  563.                         }
  564.                         if (dst) {
  565.                             dp = strtok(ep, RC_DELIM);
  566.                             if (dp)
  567.                                 (void) strcpy(dst, dp);
  568.                             if (dst == macname) {
  569.                                 /*
  570.                                  *    Read in the lines of the macro.
  571.                                  *    The macro's end is denoted by
  572.                                  *    a blank line.
  573.                                  */
  574.                                 (void) make_macro(macname, fp);
  575.                                 goto nextline;
  576.                             }
  577.                         }
  578.                     }
  579. nextline: ;
  580.                 } while (cp = FGets(str, fp));
  581.                 break;
  582.             }        /* end if we found the machine record. */
  583.         }
  584. done:
  585.         parsing_rc = 0;
  586.         (void) fclose(fp);
  587.     }
  588.  
  589.     if (!site_found) {
  590.         /* didn't find it in the rc. */
  591.         return (0);
  592.     }
  593.  
  594.     return (1);    /* found */
  595. }    /* ruserpass2 */
  596.  
  597. /* eof ftprc.c */
  598. @
  599.  
  600.  
  601. 14020.11
  602. log
  603. @checked in with -k by alph at 1993/10/10 19:59:56
  604. @
  605. text
  606. @d4 2
  607. a5 2
  608.  *  $Revision: 14020.11 $
  609.  *  $Date: 93/07/09 10:58:37 $
  610. d40 4
  611. d52 3
  612. a54 1
  613.  
  614. d56 1
  615. d59 1
  616. d68 1
  617. a68 1
  618.         (void) sprintf(rcname, "%s%s", cwd, rc);
  619. d72 1
  620. d74 4
  621. d82 1
  622. d84 3
  623. d90 1
  624. a90 1
  625.  
  626. d92 3
  627. d101 2
  628. a102 1
  629. foundrc:    
  630. d105 1
  631. @
  632.