home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / shadow / part12 / dialup.c next >
C/C++ Source or Header  |  1993-08-14  |  3KB  |  159 lines

  1. /*
  2.  * Copyright 1989, 1990, 1991, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #ifndef    BSD
  14. #include <string.h>
  15. #else
  16. #include <strings.h>
  17. #define    strchr    index
  18. #define    strrchr    rindex
  19. #endif
  20. #include "dialup.h"
  21.  
  22. #ifndef    lint
  23. static    char    sccsid[] = "@(#)dialup.c    3.5    17:31:19    04 Aug 1991";
  24. #endif
  25.  
  26. static    FILE    *dialpwd;
  27.  
  28. void
  29. setduent ()
  30. {
  31.     if (dialpwd)
  32.         rewind (dialpwd);
  33.     else
  34.         dialpwd = fopen (DIALPWD, "r");
  35. }
  36.  
  37. void
  38. endduent ()
  39. {
  40.     if (dialpwd)
  41.         fclose (dialpwd);
  42.  
  43.     dialpwd = (FILE *) 0;
  44. }
  45.  
  46. struct dialup *
  47. fgetduent (fp)
  48. FILE    *fp;
  49. {
  50.     static    struct    dialup    dialup;    /* static structure to point to */
  51.     static    char    shell[128];    /* some space for a login shell */
  52.     static    char    passwd[128];    /* some space for dialup password */
  53.     char    buf[BUFSIZ];
  54.     char    *cp;
  55.     char    *cp2;
  56.  
  57.     if (! fp)
  58.         return 0;
  59.  
  60.     if (! fp || feof (fp))
  61.         return ((struct dialup *) 0);
  62.  
  63.     while (fgets (buf, BUFSIZ, fp) == buf && buf[0] == '#')
  64.         ;
  65.  
  66.     if (feof (fp))
  67.         return ((struct dialup *) 0);
  68.  
  69.     if (cp = strchr (buf, '\n'))
  70.         *cp = '\0';
  71.  
  72.     if (! (cp = strchr (buf, ':')))
  73.         return ((struct dialup *) 0);
  74.  
  75.     if (cp - buf > sizeof shell)    /* something is fishy ... */
  76.         return ((struct dialup *) 0);
  77.  
  78.     *cp++ = '\0';
  79.     (void) strcpy (shell, buf);
  80.     shell[cp - buf] = '\0';
  81.  
  82.     if (cp2 = strchr (cp, ':'))
  83.         *cp2 = '\0';
  84.  
  85.     if (strlen (cp) + 1 > sizeof passwd) /* something is REALLY fishy */
  86.         return ((struct dialup *) 0);
  87.  
  88.     (void) strcpy (passwd, cp);
  89.  
  90.     dialup.du_shell = shell;
  91.     dialup.du_passwd = passwd;
  92.  
  93.     return (&dialup);
  94. }
  95.  
  96. struct dialup *
  97. getduent ()
  98. {
  99.     if (! dialpwd)
  100.         setduent ();
  101.  
  102.     return fgetduent (dialpwd);
  103. }
  104.  
  105. struct    dialup    *getdushell (shell)
  106. char    *shell;
  107. {
  108.     struct    dialup    *dialup;
  109.  
  110.     while (dialup = getduent ()) {
  111.         if (strcmp (shell, dialup->du_shell) == 0)
  112.             return (dialup);
  113.  
  114.         if (strcmp (dialup->du_shell, "*") == 0)
  115.             return (dialup);
  116.     }
  117.     return ((struct dialup *) 0);
  118. }
  119.  
  120. int    isadialup (tty)
  121. char    *tty;
  122. {
  123.     FILE    *fp;
  124.     char    buf[BUFSIZ];
  125.     int    dialup = 0;
  126.  
  127.     if (! (fp = fopen (DIALUPS, "r")))
  128.         return (0);
  129.  
  130.     while (fgets (buf, BUFSIZ, fp) == buf) {
  131.         if (buf[0] == '#')
  132.             continue;
  133.  
  134.         buf[strlen (buf) - 1] = '\0';
  135.  
  136.         if (strcmp (buf, tty) == 0) {
  137.             dialup = 1;
  138.             break;
  139.         }
  140.     }
  141.     fclose (fp);
  142.  
  143.     return (dialup);
  144. }
  145.  
  146. int
  147. putduent (dial, fp)
  148. struct    dialup    *dial;
  149. FILE    *fp;
  150. {
  151.     if (! fp || ! dial)
  152.         return -1;
  153.  
  154.     if (fprintf (fp, "%s:%s\n", dial->du_shell, dial->du_passwd) == EOF)
  155.         return -1;
  156.  
  157.     return 0;
  158. }
  159.