home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume34 / ncftp / part03 / ftprc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-08  |  5.0 KB  |  234 lines

  1. /* ftprc.c */
  2.  
  3. #include "sys.h"
  4. #include <sys/types.h>
  5. #include <sys/param.h>
  6. #include <sys/stat.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include "ftpdefs.h"
  12. #include "defaults.h"
  13. #include "ftprc.h"
  14. #include "main.h"
  15. #include "cmds.h"
  16. #include "copyright.h"
  17.  
  18. /* ftprc.c global variables */
  19. siteptr                    firstsite = NULL, lastsite = NULL;
  20. char                    rcname[MAXPATHLEN];
  21.  
  22. extern char                *line, *margv[];
  23. extern int                margc;
  24. extern string            anon_password;        /* most likely your email address */
  25. extern struct userinfo    uinfo;
  26.  
  27. int thrash_rc(void)
  28. {
  29.     struct stat            st;
  30.     string                word, str;
  31.     char                *cp, *dp;
  32.     FILE                *fp;
  33.  
  34.     (void) get_cwd(rcname, sizeof(rcname));
  35.     (void) Strncat(rcname, RCNAME);
  36.     if (stat(rcname, &st) == 0)
  37.         goto foundrc;
  38.     
  39.     (void) get_cwd(rcname, sizeof(rcname));
  40.     (void) Strncat(rcname, ".");
  41.     (void) Strncat(rcname, RCNAME);
  42.     if (stat(rcname, &st) == 0)
  43.         goto foundrc;
  44.  
  45.     (void) sprintf(rcname, "%s/.%s", uinfo.homedir, RCNAME);
  46.     if (stat(rcname, &st) == 0)
  47.         goto foundrc;
  48.  
  49.     return (0);    /* it's OK not to have an rc. */
  50.     
  51. foundrc:    
  52.     if ((st.st_mode & 077) != 0)                /* rc must be unreadable by others. */
  53.         (void) chmod(rcname, 0600);
  54.  
  55.     if ((fp = fopen(rcname, "r")) == NULL) {
  56.         Perror(rcname);
  57.         return -1;
  58.     }
  59.     
  60.     while (cp = FGets(str, fp)) {
  61.         while (isspace(*cp)) ++cp;        /* skip leading space. */
  62.         if (*cp == '#') {
  63.             if ((strncmp("set", ++cp, (size_t)3) == 0) || (strncmp("unset", cp, (size_t)5) == 0)) {
  64.                 (void) strcpy(line, cp);
  65.                 makeargv();
  66.                 set(margc, margv);            
  67.                 /* setting or unsetting a variable. */
  68.             } /* else a comment. */
  69.         } else {
  70.             if (strncmp(cp, "machine", (size_t) 7) == 0) {
  71.                 /* We have a new machine record. */
  72.                 cp += 7;
  73.                 while (isspace(*cp)) ++cp;    /* skip delimiting space. */
  74.                 dp = word;
  75.                 while (*cp && !isspace(*cp)) *dp++ = *cp++;    /* copy the name. */
  76.                 *dp = 0;
  77.                 AddNewSitePtr(word);
  78.             }
  79.         }
  80.     }
  81.     (void) fclose(fp);
  82.     return 1;
  83. }    /* thrash_rc */
  84.  
  85.  
  86.  
  87. void AddNewSitePtr(char *word)
  88. {
  89.     siteptr            s;
  90.  
  91.     if (s = (siteptr) malloc(sizeof(site))) {
  92.         s->next = NULL;
  93.         if (s->name = malloc(strlen(word) + 1)) {
  94.             (void) strcpy(s->name, word);
  95.             if (firstsite == NULL)
  96.                 firstsite = lastsite = s;
  97.             else {
  98.                 lastsite->next = s;
  99.                 lastsite = s;
  100.             }
  101.         } else {
  102.             free(s);
  103.         }
  104.     }
  105. }    /* AddNewSitePtr */
  106.  
  107.  
  108.  
  109.  
  110. void GetFullSiteName(char *host)
  111. {
  112.     register siteptr s, s2;
  113.  
  114.     /* see if 'host' is in our list of favorite sites. */
  115.     for (s = firstsite; s != NULL; s2=s->next, s=s2)
  116.         if (strstr(s->name, host) != NULL) {
  117.             (void) strcpy(host, s->name);
  118.             break;
  119.         }
  120. }    /* GetFullSiteName */
  121.  
  122.  
  123.  
  124.  
  125. int ruserpass2(char *host, char **username, char **pass, char **acct)
  126. {
  127.     FILE            *fp;
  128.     char            *cp, *dp, *dst, *ep;
  129.     str32            macname;
  130.     char            *varname;
  131.     int                site_found;
  132.     string            str;
  133.     static string    auser;
  134.     static str32    apass, aacct;
  135.  
  136.     site_found = 0;
  137.  
  138.     if ((fp = fopen(rcname, "r")) != NULL) {
  139.         while (FGets(str, fp)) {
  140.             if (cp = strstr(str, "machine")) {
  141.                 /* Look for the machine token. */
  142.                 cp += 7;
  143.                 while (isspace(*cp))
  144.                     cp++;
  145.             } else
  146.                 continue;
  147.             if (strncmp(cp, host, strlen(host)) == 0) {
  148.                 site_found = 1;
  149.                 while (!isspace(*cp))
  150.                     ++cp;        /* skip the site name. */
  151.                 do {
  152.                     /* Skip any comments ahead of time. */
  153.                     for (dp=cp; *dp; dp++) {
  154.                         if (*dp == '#') {
  155.                             *dp = 0;
  156.                             break;
  157.                         }
  158.                     }
  159.  
  160.                     ep = cp;
  161.                     while (1) {
  162.                         varname = strtok(ep, RC_DELIM);
  163.                         if (!varname) break;
  164.                         dst = ep = NULL;
  165.                         switch (*varname) {
  166.                             case 'u':    /* user */
  167.                                 *username = dst = auser;
  168.                                 break;
  169.                             case 'l':    /* login */
  170.                                 *username = dst = auser;
  171.                                 break;
  172.                             case 'p':    /* password */
  173.                                 *pass = dst = apass;
  174.                                 break;
  175.                             case 'a':    /* account */
  176.                                 *acct = dst = aacct;
  177.                                 break;
  178.                         /*    case 'd':  /* default */
  179.                         /* unused -- use 'set anon_password.' */
  180.                             case 'm':    /* macdef or machine */
  181.                                 if (strcmp(varname, "macdef"))
  182.                                     goto done;    /* new machine record... */
  183.                                 dst = macname;
  184.                                 break;
  185.                             default:
  186.                                 (void) fprintf(stderr, "Unknown %s keyword \"%s\"\n",
  187.                                     RCNAME,
  188.                                     varname
  189.                                 );
  190.                         }
  191.                         if (dst) {
  192.                             dp = strtok(ep, RC_DELIM);
  193.                             if (dp)
  194.                                 (void) strcpy(dst, dp);
  195.                             if (dst == macname) {
  196.                                 /*
  197.                                  *    Read in the lines of the macro.
  198.                                  *    The macro's end is denoted by
  199.                                  *    a blank line.
  200.                                  */
  201.                                 (void) make_macro(macname, fp);
  202.                                 goto nextline;
  203.                             }
  204.                         }
  205.                     }
  206. nextline: ;
  207.                 } while (cp = FGets(str, fp));
  208.                 break;
  209.             }        /* end if we found the machine record. */
  210.         }
  211. done:
  212.         (void) fclose(fp);
  213.     }
  214.  
  215.     if (!site_found) {
  216.         /* didn't find it in the rc. */
  217.         return (0);
  218.     }
  219.  
  220.     if (*username == NULL) {
  221.         *username = "anonymous";
  222.         *pass = anon_password;
  223.     }
  224.  
  225.     /* Make sure the password looks like an address. */
  226.     if (strcmp(*username, "anonymous") == 0) {
  227.         if (*pass == NULL || index(*pass, '@') == NULL)
  228.             *pass = anon_password;
  229.     }
  230.     return (1);    /* found */
  231. }    /* ruserpass2 */
  232.  
  233. /* eof ftprc.c */
  234.