Advertisement
FlyFar

util.c

May 16th, 2024
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.65 KB | Cybersecurity | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <pwd.h>
  5. #include <syslog.h>
  6. #include <unistd.h>
  7. #include <netdb.h>
  8.  
  9. #ifdef SHADOWPWD
  10. #include <shadow.h>
  11. #endif
  12.  
  13. #include "pop3.h"
  14.  
  15. char flash_buf[SVR_BUFSIZ];
  16.  
  17. extern FILE *logfp;
  18. extern int mypid;
  19. extern int debug;
  20.  
  21. /**************************************************************************/
  22.  
  23. int
  24. passwd_verify_user(user,pass)
  25. char *user;
  26. char *pass;
  27. {
  28.     struct passwd *pwd;
  29.     char *cp;
  30. #ifdef SHADOWPWD
  31.     struct spwd *spwd = NULL;
  32. #endif
  33.     /* DTS added 03July96 to force lower case names */
  34.     for(cp=user; *cp; cp++) { *cp = tolower(*cp); }
  35.  
  36.     pwd = getpwnam(user);
  37.     if (pwd == NULL) return -1;
  38.     if (pwd->pw_passwd[0] == '!' || !strcmp(pwd->pw_passwd,"*NONE*")) {
  39.         syslog (SYSLOGPRI, "User locked: %s", user);                   
  40.             return -1;
  41.         }
  42.     if ( !strncmp(pwd->pw_shell,ACCT_SUSP_STR,strlen(ACCT_SUSP_STR))) {
  43.             return -2;
  44.         }
  45. #ifdef SHADOWPWD
  46.    
  47.     if (pwd && (strlen(pwd->pw_passwd) == 1) ) {
  48.         if (pwd->pw_passwd[0] == 'x' || pwd->pw_passwd[0] == '*') {
  49.             spwd = getspnam(user);
  50.                 if (!spwd) {
  51.                     syslog (SYSLOGPRI,
  52.                 "No shadow entry for user %s",user);
  53.                     return -1;
  54.                 }
  55.                 pwd->pw_passwd = spwd->sp_pwdp;
  56.        }
  57.         }
  58. #endif 
  59.     if( !strcmp( pwd->pw_passwd, crypt(pass,pwd->pw_passwd)) )
  60.             return(setuid(pwd->pw_uid));
  61.         else
  62.         return(-1);
  63.  
  64. } /* end passwd_verify_user */
  65.  
  66. /* Verify a usercode/password */
  67. int
  68. verify_user(user,pass)
  69. char *user;
  70. char *pass;
  71. {
  72. #ifdef TACACS_AUTH
  73.     return( tacacs_verify_user(user,pass) );
  74. /*  #elif VIRTUAL
  75.  *  return( virtual_verify_user(user,pass) );
  76.  */
  77. #else
  78.     return( passwd_verify_user(user,pass) );
  79. #endif
  80. }
  81.  
  82. char *
  83. fgetl(buf,n,fp)
  84. char *buf;  /* Buffer for text */
  85. int n;      /* Size of buffer */
  86. FILE *fp;   /* Stream to read from */
  87. {
  88.     if (fgets(buf,n,fp) == NULL)
  89.         return(NULL);
  90.     if ((strlen(buf) == (n-1))&&(buf[n-1] != LF_CHAR)) {
  91.         buf[n-1] = LF_CHAR;
  92.         while (fgets(flash_buf,SVR_BUFSIZ,fp) != NULL) {
  93.             if (strlen(flash_buf) != (SVR_BUFSIZ-1))
  94.                 break;
  95.             if (flash_buf[SVR_BUFSIZ-1] == LF_CHAR)
  96.                 break;
  97.         }
  98.     }
  99.     return(buf);
  100. }
  101.  
  102. /* Prepare client command for server */
  103. void
  104. cmd_prepare(buf)
  105. char *buf;
  106. {
  107.     char *cp;
  108.  
  109.     if (buf == NULL)
  110.         return;
  111.     /* Convert command verb to lowercase */
  112.     while (*buf != NULL_CHAR) {
  113.         if (isupper(*buf))
  114.             *buf = tolower(*buf);
  115.         else if (isspace(*buf))
  116.             break;
  117.         ++buf;
  118.     }
  119.     /* Strip trailing whitespace from client command */
  120.     if ((cp = strchr(buf,CR_CHAR)) != NULL) {
  121.         while ((cp != buf)&&(isspace(*cp))) --cp;
  122.         if (!isspace(*cp)) ++cp;
  123.         *cp = NULL_CHAR;
  124.     }
  125.     if ((cp = strchr(buf,LF_CHAR)) != NULL) {
  126.         while ((cp != buf)&&(isspace(*cp))) --cp;
  127.         if (!isspace(*cp)) ++cp;
  128.         *cp = NULL_CHAR;
  129.     }
  130. }
  131.  
  132. /**************************************************************************/
  133.  
  134. /* Send an error message and exit POP3 server */
  135. void
  136. fail(err)
  137. int err;
  138. {
  139.     char *cp;
  140.  
  141.     switch(err) {
  142.     /* DTS 10Oct96 [ 1.005d ] - created error FAIL_DUP_READ */
  143.     case FAIL_DUP_READ:         /* Folder already being read */
  144.         cp = "Folder already being read";
  145.         break;
  146.     case FAIL_FILE_ERROR:           /* File I/O error */
  147.         cp = "File I/O error";
  148.         break;
  149.     case FAIL_HANGUP:           /* Client hung up on us */
  150.         cp = "Lost connection to client";
  151.         break;
  152.     case FAIL_LOST_CLIENT:          /* Timeout waiting for client */
  153.         cp = "Timeout waiting for command from client";
  154.         break;
  155.     case FAIL_OUT_OF_MEMORY:        /* Failed malloc() */
  156.         cp = "Out of memory!";
  157.         break;
  158.     case FAIL_PROGERR:          /* Fatal program error */
  159.         cp = "Fatal program error!";
  160.         break;
  161.     case FAIL_PIPE:             /* Rec'd SIGPIPE - DTS 28Jul96*/
  162.         cp = "Received pipe failure signal!";
  163.         break;
  164.     case FAIL_CONFUSION:            /* Shouldnt happen */
  165.     default:
  166.         cp = "Complete confusion!";
  167.         break;
  168.     }
  169.     fprintf(stdout,"-ERR POP3 Server Abnormal Shutdown: %s\r\n",cp);
  170.     fflush(stdout);
  171.     if ( debug ) {
  172.         fprintf(logfp,"[%d] -ERR POP3 Server Abnormal Shutdown: %s\r\n",mypid,cp);
  173.         fclose(logfp);
  174.     }
  175.     exit(err);              /* Exit with error */
  176. }
  177.  
  178. /**************************************************************************/
  179.  
  180. char *
  181. get_fqdn()
  182. {
  183. static char fqdn[SVR_BUFSIZ];
  184. char buff[100];
  185. struct hostent *h;
  186.  
  187.     if (fqdn[0] != '\0')
  188.         return fqdn;
  189.  
  190.     if (gethostname(buff, sizeof(buff)) != 0)
  191.     {
  192.         perror("get_fqdn");
  193.         return NULL;
  194.     }
  195.  
  196.     h = gethostbyname(buff);
  197.     if (!h)
  198.     {
  199.         fprintf(stderr, "get_fqdn: gethostbyname() failed\n");
  200.         return NULL;
  201.     }
  202.  
  203.     strncpy(fqdn, h->h_name, SVR_BUFSIZ-1);
  204.     return fqdn;
  205. }
  206.  
  207. /**************************************************************************/
  208.  
  209.  
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement