home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Mail / qpopper-2.4-MIHS / pop_user.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-11  |  3.7 KB  |  173 lines

  1. /*
  2.  * Copyright (c) 1989 Regents of the University of California.
  3.  * All rights reserved.  The Berkeley software License Agreement
  4.  * specifies the terms and conditions for redistribution.
  5.  */
  6.  
  7. /*
  8.  * Copyright (c) 1997 by Qualcomm Incorporated.
  9.  */
  10.  
  11. #include <config.h>
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <pwd.h>
  15. #include<sys/stat.h>
  16. #include <string.h>
  17.  
  18. # include <flock.h>
  19. #ifndef HAVE_INDEX
  20. # define index(s, c)    strchr(s, c)
  21. #endif
  22. #if HAVE_STRINGS_H
  23. # include <strings.h>
  24. #endif
  25.  
  26. #if HAVE_FCNTL_H
  27. # include <fcntl.h>
  28. #endif
  29. #if HAVE_SYS_FILE_H
  30. # include <sys/file.h>
  31. #endif
  32.  
  33. #ifdef GDBM
  34. # include <gdbm.h>
  35. #else
  36. # if HAVE_NDBM_H
  37. #  include <ndbm.h>
  38. # endif
  39. #endif
  40.  
  41. #include "popper.h"
  42.  
  43. /* 
  44.  *  user:   Prompt for the user name at the start of a POP session
  45.  */
  46.  
  47. int pop_user (p)
  48. POP     *   p;
  49. {
  50.     /* If there is an APOP database entry then don't allow a cleartext
  51.        password over the net */
  52. # ifdef APOP
  53. #ifdef GDBM
  54.     char apop_file[BUFSIZ];
  55.     GDBM_FILE db;
  56. #else
  57.     char apop_dir[BUFSIZ];
  58.     DBM    *db;
  59. #endif
  60.     int       fid;
  61.     struct passwd *pw;
  62.     struct stat st;
  63.     datum    key, value;
  64. # endif
  65.  
  66. #ifdef KERBEROS
  67.     if (p->kerberos && strcmp(p->pop_parm[1], p->user)) {
  68.     pop_log(p, LOG_WARNING, "%s: auth failed: %s.%s@@%s vs %s",
  69.         p->client, kdata.pname, kdata.pinst, kdata.prealm, 
  70.         p->pop_parm[1]);
  71.         return(pop_msg(p,POP_FAILURE,
  72.                "Wrong username supplied (%s vs. %s).", p->user,
  73.                p->pop_parm[1]));
  74.     }
  75. #endif
  76.  
  77.     /*  Save the user name */
  78.     (void)strncpy(p->user, p->pop_parm[1], sizeof(p->user));
  79.     p->user[sizeof(p->user)-1] = 0;
  80.  
  81. # ifdef APOP_ONLY
  82.     return(pop_auth_fail(p, POP_FAILURE,
  83.         "You must use APOP authentication to connect to this server"));
  84. # endif
  85.  
  86. # ifdef APOP
  87.  
  88.     /* If this call fails then the database is not accessable (doesn't
  89.        exist?) in which case we can ignore an APOP user trying to
  90.        access the popper with a cleartext password.
  91.          */
  92.  
  93.     if (((pw = getpwnam(p->user)) != NULL) &&
  94. #ifdef GDBM
  95.     ((db = gdbm_open(APOP, 512, GDBM_READER, 0, 0)) != NULL))
  96. #else
  97.     ((db = dbm_open(APOP, O_RDONLY, 0)) != NULL)) 
  98. #endif
  99.     {
  100.  
  101. #ifdef GDBM
  102.         (void) strncpy(apop_file, APOP, sizeof(apop_file) - 1);
  103.     apop_file[sizeof(apop_file) - 1] = '\0';
  104. #else
  105.     (void) strncpy(apop_dir, APOP, sizeof(apop_dir) - 5);
  106. # if defined(BSD44_DBM)
  107.     (void) strcat(apop_dir, ".db");
  108. # else
  109.     (void) strcat(apop_dir, ".dir");
  110. # endif
  111. #endif
  112. #ifdef GDBM
  113.     if (stat (apop_file, &st) != -1 && (st.st_mode & 0777) != 0600) 
  114. #else
  115.     if (stat (apop_dir, &st) != -1 && (st.st_mode & 0777) != 0600) 
  116. #endif
  117.     {
  118. #ifdef GDBM
  119.         gdbm_close (db);
  120. #else
  121.         dbm_close (db);
  122. #endif
  123.         return(pop_auth_fail(p, POP_FAILURE,
  124.         "POP authorization DB has wrong mode (0%o)",st.st_mode & 0777));
  125.     }
  126. #ifdef GDBM
  127.     fid = open(apop_file, O_RDONLY);
  128. #else
  129.     fid = open(apop_dir, O_RDONLY);
  130. #endif
  131.     if(fid == -1) {
  132.         int e = errno;
  133. #ifdef GDBM
  134.         gdbm_close (db);
  135. #else
  136.         dbm_close (db);
  137. #endif
  138.         return(pop_auth_fail(p, POP_FAILURE,
  139.             "unable to lock POP authorization DB (%s)", strerror(e)));
  140.     }
  141.     if (flock (fid, LOCK_SH) == -1) {
  142.         int e = errno;
  143.         (void) close(fid);
  144. #ifdef GDBM
  145.         gdbm_close (db);
  146. #else
  147.         dbm_close (db);
  148. #endif 
  149.         return(pop_auth_fail(p, POP_FAILURE,
  150.             "unable to lock POP authorization DB (%s)", strerror(e)));
  151.     }
  152.     key.dsize = strlen (key.dptr = p->user) + 1;
  153. #ifdef GDBM
  154.     value = gdbm_fetch (db, key);
  155.     gdbm_close(db);
  156. #else
  157.     value = dbm_fetch (db, key);
  158.     dbm_close (db);
  159. #endif
  160.     (void) close(fid);
  161.  
  162.     if (value.dptr != NULL) {
  163.         return(pop_auth_fail(p, POP_FAILURE,
  164.         "You must use APOP to connect to this server"));
  165.     }
  166.     }
  167. #endif /* APOP */
  168.  
  169.     /*  Tell the user that the password is required */
  170.     return (pop_msg(p,POP_SUCCESS,"Password required for %s.",p->user));
  171. }
  172.  
  173.