home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / ncftp / older_versions / ncftp-3.2.2-src.tar.bz2 / ncftp-3.2.2-src.tar / ncftp-3.2.2 / libncftp / u_getpw.c < prev    next >
C/C++ Source or Header  |  2005-01-01  |  4KB  |  164 lines

  1. /* u_getpw.c
  2.  *
  3.  * Copyright (c) 1996-2005 Mike Gleason, NcFTP Software.
  4.  * All rights reserved.
  5.  *
  6.  */
  7.  
  8. #if defined(SOLARIS) && (SOLARIS >= 250)
  9. #    define _POSIX_PTHREAD_SEMANTICS 1
  10. #endif
  11.  
  12. #include "syshdrs.h"
  13. #ifdef PRAGMA_HDRSTOP
  14. #    pragma hdrstop
  15. #endif
  16.  
  17. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  18. #else
  19.  
  20. #ifdef BSDOS
  21. int getpwnam_r(const char *name, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result);
  22. int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result);
  23. #endif
  24.  
  25. #if (defined(AIX) && (AIX < 433))
  26. extern int _posix_getpwuid_r(uid_t, struct passwd *, char *, int
  27. , struct passwd **);
  28. extern int _posix_getpwnam_r(const char *, struct passwd *, char *, int, struct passwd **);
  29. #endif
  30.  
  31. int
  32. GetPwUid(struct passwd *pwp, const uid_t uid, char *const pwbuf, size_t pwbufsize)
  33. {
  34. #if ((defined(MACOSX)) && (defined(HAVE_GETPWUID_R)))
  35.     /* Allow backwards compat on 10.1 if building on 10.2+ */
  36. #    undef HAVE_GETPWUID_R
  37. #    undef HAVE_GETPWNAM_R
  38. #endif
  39.  
  40. #if defined(HAVE_GETPWUID_R) && ( (defined(SOLARIS) && (SOLARIS < 250)) || (defined(IRIX) && (IRIX < 6)) )
  41.     struct passwd *p;
  42.     memset(pwbuf, 0, pwbufsize);
  43.     p = getpwuid_r(uid, pwp, pwbuf, pwbufsize);
  44.     if (p != NULL)
  45.         return (0);
  46. #elif defined(HAVE_GETPWUID_R) && (defined(HPUX)) && (HPUX < 1100)
  47.     memset(pwbuf, 0, pwbufsize);
  48.     if (getpwuid_r(uid, pwp, pwbuf, pwbufsize) >= 0)
  49.         return (0);
  50. #elif defined(HAVE__POSIX_GETPWUID_R)
  51.     struct passwd *p;
  52.     memset(pwbuf, 0, pwbufsize);
  53.     p = NULL;
  54.     if ((_posix_getpwuid_r(uid, pwp, pwbuf, pwbufsize, &p) == 0) && (p != NULL)) {
  55.         return (0);
  56.     }
  57. #elif defined(HAVE_GETPWUID_R)
  58.     struct passwd *p;
  59.     memset(pwbuf, 0, pwbufsize);
  60.     p = NULL;
  61.     if ((getpwuid_r(uid, pwp, pwbuf, pwbufsize, &p) == 0) && (p != NULL)) {
  62.         return (0);
  63.     }
  64. #else
  65.     struct passwd *p;
  66.     p = getpwuid(uid);
  67.     if (p != NULL) {
  68.         memcpy(pwp, p, sizeof(struct passwd));
  69.         return (0);
  70.     } else {
  71.         memset(pwbuf, 0, pwbufsize);
  72.         memset(pwp, 0, sizeof(struct passwd));
  73.     }
  74. #endif
  75.     return (-1);
  76. }    /* GetPwUid */
  77.  
  78.  
  79.  
  80.  
  81. int
  82. GetPwNam(struct passwd *pwp, const char *const nam, char *const pwbuf, size_t pwbufsize)
  83. {
  84. #if defined(HAVE_GETPWNAM_R) && ( (defined(SOLARIS) && (SOLARIS < 250)) || (defined(IRIX) && (IRIX < 6)) )
  85.     struct passwd *p;
  86.     memset(pwbuf, 0, pwbufsize);
  87.     p = getpwnam_r(nam, pwp, pwbuf, pwbufsize);
  88.     if (p != NULL)
  89.         return (0);
  90. #elif defined(HAVE_GETPWNAM_R) && (defined(HPUX)) && (HPUX < 1100)
  91.     memset(pwbuf, 0, pwbufsize);
  92.     if (getpwnam_r(nam, pwp, pwbuf, pwbufsize) >= 0)
  93.         return (0);
  94. #elif defined(HAVE__POSIX_GETPWNAM_R)
  95.     struct passwd *p;
  96.     memset(pwbuf, 0, pwbufsize);
  97.     p = NULL;
  98.     if ((_posix_getpwnam_r(nam, pwp, pwbuf, pwbufsize, &p) == 0) && (p != NULL)) {
  99.         return (0);
  100.     }
  101. #elif defined(HAVE_GETPWNAM_R)
  102.     struct passwd *p;
  103.     memset(pwbuf, 0, pwbufsize);
  104.     p = NULL;
  105.     if ((getpwnam_r(nam, pwp, pwbuf, pwbufsize, &p) == 0) && (p != NULL)) {
  106.         return (0);
  107.     }
  108. #else
  109.     struct passwd *p;
  110.     p = getpwnam(nam);
  111.     if (p != NULL) {
  112.         memcpy(pwp, p, sizeof(struct passwd));
  113.         return (0);
  114.     } else {
  115.         memset(pwbuf, 0, pwbufsize);
  116.         memset(pwp, 0, sizeof(struct passwd));
  117.     }
  118. #endif
  119.     return (-1);
  120. }    /* GetPwNam */
  121.  
  122.  
  123.  
  124. /* This looks up the user's password entry, trying to look by the username.
  125.  * We have a couple of extra hacks in place to increase the probability
  126.  * that we can get the username.
  127.  */
  128. int
  129. GetMyPwEnt(struct passwd *pwp, char *const pwbuf, size_t pwbufsize)
  130. {
  131.     char *cp;
  132.     int rc;
  133. #ifdef HAVE_GETLOGIN_R
  134.     char logname[128];
  135. #endif
  136.     rc = GetPwUid(pwp, getuid(), pwbuf, pwbufsize);
  137.     if (rc == 0)
  138.         return (rc);
  139.  
  140.     cp = (char *) getenv("LOGNAME");
  141.     if (cp == NULL)
  142.         cp = (char *) getenv("USER");
  143.  
  144.     if (cp == NULL) {
  145.         /* Avoid getlogin() if possible, which may
  146.          * wade through a potentially large utmp file.
  147.          */
  148. #ifdef HAVE_GETLOGIN_R
  149.         memset(logname, 0, sizeof(logname));
  150.         (void) getlogin_r(logname, sizeof(logname) - 1);
  151.         cp = (logname[0] == '\0') ? NULL : logname;
  152. #else
  153.         cp = getlogin();
  154. #endif
  155.     }
  156.  
  157.     rc = -1;
  158.     if ((cp != NULL) && (cp[0] != '\0'))
  159.         rc = GetPwNam(pwp, cp, pwbuf, pwbufsize);
  160.     return (rc);
  161. }    /* GetMyPwEnt */
  162.  
  163. #endif    /* UNIX */
  164.