home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / shadow-2.pt3 / dialup.c < prev    next >
C/C++ Source or Header  |  1989-02-03  |  2KB  |  99 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "dialup.h"
  4.  
  5. static    FILE    *dialpwd;
  6.  
  7. void    setduent ()
  8. {
  9.     if (dialpwd)
  10.         rewind (dialpwd);
  11.     else
  12.         dialpwd = fopen (DIALPWD, "r");
  13. }
  14.  
  15. void    endduent ()
  16. {
  17.     if (dialpwd)
  18.         fclose (dialpwd);
  19.  
  20.     dialpwd = (FILE *) 0;
  21. }
  22.  
  23. struct    dialup    *getduent ()
  24. {
  25.     static    struct    dialup    dialup;    /* static structure to point to */
  26.     static    char    shell[64];    /* some space for a login shell */
  27.     static    char    passwd[16];    /* some space for dialup password */
  28.     char    buf[BUFSIZ];
  29.     char    *cp;
  30.  
  31.     if (! dialpwd)
  32.         setduent ();
  33.  
  34.     if (! dialpwd || feof (dialpwd))
  35.         return ((struct dialup *) 0);
  36.  
  37.     while (fgets (buf, BUFSIZ, dialpwd) == buf)
  38.         if (buf[0] == '#')
  39.             continue;
  40.  
  41.     if (feof (dialpwd))
  42.         return ((struct dialup *) 0);
  43.  
  44.     cp = strchr (buf, ':');
  45.     if (cp - buf > sizeof shell)    /* something is fishy ... */
  46.         return ((struct dialup *) 0);
  47.  
  48.     (void) strncpy (shell, buf, cp - buf);
  49.     shell[cp - buf] = '\0';
  50.  
  51.     if (strlen (cp + 1) > sizeof passwd) /* something is REALLY fishy */
  52.         return ((struct dialup *) 0);
  53.  
  54.     (void) strcpy (passwd, cp + 1);
  55.     passwd[strlen (passwd) - 1] = '\0';
  56.     if (cp = strchr (passwd, ':'))
  57.         *cp = '\0';
  58.  
  59.     dialup.du_shell = shell;
  60.     dialup.du_passwd = passwd;
  61.  
  62.     return (&dialup);
  63. }
  64.  
  65. struct    dialup    *getdushell (shell)
  66. char    *shell;
  67. {
  68.     struct    dialup    *dialup;
  69.  
  70.     while (dialup = getduent ())
  71.         if (strcmp (shell, dialup->du_shell) == 0)
  72.             return (dialup);
  73.  
  74.     return ((struct dialup *) 0);
  75. }
  76.  
  77. int    isadialup (tty)
  78. char    *tty;
  79. {
  80.     FILE    *fp;
  81.     char    buf[BUFSIZ];
  82.     int    dialup = 0;
  83.  
  84.     if (! (fp = fopen (DIALUPS, "r")))
  85.         return (0);
  86.  
  87.     while (fgets (buf, BUFSIZ, fp) == buf) {
  88.         buf[strlen (buf) - 1] = '\0';
  89.  
  90.         if (strcmp (buf, tty) == 0) {
  91.             dialup = 1;
  92.             break;
  93.         }
  94.     }
  95.     fclose (fp);
  96.  
  97.     return (dialup);
  98. }
  99.