home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / server / auth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  2.7 KB  |  153 lines

  1. #ifndef lint
  2. static char    *sccsid = "@(#)$Header: auth.c,v 1.2 90/08/10 22:59:08 sob Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Simple user/password authentication
  7.  *
  8.  * client must supply "authinfo user <userid>"
  9.  * followed by "authinfo pass <password>"
  10.  * which will be looked up in the server machine's password file.
  11.  * Password must match userid, userid must have gid matching the
  12.  * /etc/group entry for "nntp"
  13.  *
  14.  * note that passwords travel over the network in plaintext.  This
  15.  * can be a problem but isn't easy to remedy.  At least it's as safe
  16.  * as logging in over the network would be
  17.  *
  18.  */
  19.  
  20. #include "common.h"
  21. #include <grp.h>
  22.  
  23. extern timeout();
  24. extern char *crypt();
  25.  
  26. #ifdef    AUTH
  27.  
  28. extern int    Needauth;
  29. extern char    User[];
  30.  
  31. doauth(argc,argv)
  32. int argc;
  33. char *argv[];
  34.     {
  35.     if (argc != 3)
  36.         {
  37.         printf("%d Syntax error\r\n", ERR_CMDSYN);
  38.         fflush(stdout);
  39.         return;
  40.         }
  41.  
  42.     if (!strcasecmp(argv[0],"authcap"))
  43.         {
  44.         printf("%d authcap not implemented\r\n", ERR_COMMAND);
  45.         fflush(stdout);
  46.         return;
  47.         }
  48.  
  49.     if (!strcasecmp(argv[0],"authsys"))
  50.         {
  51.         printf("%d authsys not implemented\r\n", ERR_COMMAND);
  52.         fflush(stdout);
  53.         return;
  54.         }
  55.  
  56.     /* fall through into 'authinfo' */
  57.     if (strcasecmp(argv[0],"authinfo"))
  58.         {
  59.         printf("%d command not recognized\r\n", ERR_COMMAND);
  60.         fflush(stdout);
  61.         return;
  62.         }
  63.     
  64.     if (!Needauth)
  65.         {
  66.         printf("%d Authorization already completed\r\n", ERR_AUTHREJ);
  67.         fflush(stdout);
  68.         return;
  69.         }
  70.  
  71.     if (!strcasecmp(argv[1],"user"))
  72.         {
  73.         if (strlen(User))
  74.             {
  75.             printf("%d USER already specified\r\n", ERR_AUTHREJ);
  76.             fflush(stdout);
  77.             return;
  78.             }
  79.         getuser(argv[2]);
  80.         return;
  81.         }
  82.  
  83.     if (!strcasecmp(argv[1],"pass"))
  84.         {
  85.         if (strlen(User) < 1)
  86.             {
  87.             printf("%d USER required first\r\n", ERR_AUTHREJ);
  88.             fflush(stdout);
  89.             return;
  90.             }
  91.         getpass(argv[2]);
  92.         return;
  93.         }
  94.     }
  95.  
  96. /* get userid and prompt for password */
  97. getuser(p)
  98. char *p;
  99. {
  100.     strncpy(User,p,8);
  101.     User[8] = 0;
  102.     /* get the password */
  103.     printf("%d PASS required\r\n", NEED_AUTHDATA);
  104.     fflush(stdout);
  105. }
  106.  
  107. /* password */
  108. getpass(p)
  109. char *p;
  110. {
  111.     static char pass[10];
  112.     char *cp, *namep;
  113.     struct passwd *pwd;
  114.     struct group *grp;
  115.     int i;
  116.     int nntpgid;
  117.  
  118.     strncpy(pass,p,8);
  119.     pass[8] = 0;
  120.     /* check for valid login */
  121.     pwd = getpwnam(User);
  122.     namep = NULL;
  123.  
  124.     if (pwd != NULL)
  125.         namep = crypt(pass, pwd->pw_passwd);
  126.     
  127.     grp = getgrnam("nntp");
  128.  
  129.     if (grp == NULL || pwd == NULL || namep == NULL
  130.             || strcmp(namep, pwd->pw_passwd)
  131.             || pwd->pw_gid != grp->gr_gid)
  132.         {
  133. #ifdef SYSLOG
  134.         syslog(LOG_ERR, "AUTHENTICATION ERROR");
  135. #endif
  136.         printf("%d Authentication error\r\n",ERR_ACCESS);
  137.         (void) fflush(stdout);
  138.         (void) fflush(stdout);
  139.         exit(1);
  140.         }
  141.  
  142. #ifdef SYSLOG
  143. #ifdef LOG
  144.     syslog(LOG_INFO, "user %s", User);
  145. #endif
  146. #endif
  147.     printf("%d Authentication accepted\r\n",OK_AUTH);
  148.     fflush(stdout);
  149.     Needauth = 0;
  150. }
  151.  
  152. #endif AUTH
  153.