home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Utilities / RemoteCommand / Source / execServer.subproj / userInfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-11  |  3.8 KB  |  159 lines

  1. // -------------------------------------------------------------------------------------
  2. // user information functions
  3. // -------------------------------------------------------------------------------------
  4. // Permission is granted to freely redistribute this source code, and to use fragments
  5. // of this code in your own applications if you find them to be useful.  This class/module,
  6. // along with the source code, come with no warranty of any kind, and the user assumes
  7. // all responsibility for its use.
  8. // -------------------------------------------------------------------------------------
  9.  
  10. #import <stdio.h>
  11. #import <string.h>
  12. #import <ctype.h>
  13. #import <libc.h>
  14. #import <pwd.h>
  15. #import <sys/types.h>
  16. #import <sys/stat.h>
  17. #import <sys/param.h>
  18. #import <sys/dir.h>
  19. #import <mach/cthreads.h>
  20. #import <mach/mach_traps.h>
  21.  
  22. // -----------------------------------------------------------------------------------------
  23. // future use
  24. #define    userLOCK        /* no-op */
  25. #define    userUNLOCK        /* no-op */
  26.  
  27. // -----------------------------------------------------------------------------------------
  28. // XUserGetpwnam() and XUserGetpwuid() are NOT thread safe!
  29.  
  30. /* wrapper for getpwnam */
  31. struct passwd *XUserGetpwnam(const char *name)
  32. {
  33.     struct passwd    *pw;
  34.     userLOCK;
  35.     pw = name? getpwnam(name) : getpwuid(getuid());
  36.     userUNLOCK;
  37.     return pw;
  38. }
  39.  
  40. /* wrapper for getpwuid */
  41. struct passwd *XUserGetpwuid(uid_t uid)
  42. {
  43.     struct passwd    *pw;
  44.     userLOCK;
  45.     pw = getpwuid(uid);
  46.     userUNLOCK;
  47.     return pw;
  48. }
  49.  
  50. // -----------------------------------------------------------------------------------------
  51.  
  52. /* return true if euid == root */
  53. int XUserIsRootEuid()
  54. {
  55.     int    isRoot;
  56.     userLOCK;
  57.     isRoot = (getpwnam("root")->pw_uid == geteuid())? 1 : 0;
  58.     userUNLOCK;
  59.     return isRoot;
  60. }
  61.  
  62. // -----------------------------------------------------------------------------------------
  63.  
  64. /* return true if user is valid */
  65. int XUserIsValid(const char *user)
  66. {
  67.     struct passwd    *pw;
  68.     userLOCK;
  69.     pw = user? getpwnam((char*)user) : getpwuid(getuid());
  70.     userUNLOCK;
  71.     return pw? 1: 0;
  72. }
  73.  
  74. /* return current user name */
  75. char *XUserRealName(const char *user, char *realName)
  76. {
  77.     char    *rtn = (char*)NULL;
  78.     if (realName) {
  79.         struct passwd *pw;
  80.         userLOCK;
  81.         if (!(pw = user? getpwnam((char*)user) : getpwuid(getuid()))) *realName = 0;
  82.         else {
  83.             char *bp = realName, *gp = pw->pw_gecos;
  84.             while (*gp == '*') gp++;
  85.             while (*gp && (*gp != ',')) *bp++ = *gp++;
  86.             *bp = 0;
  87.             rtn = realName;
  88.         }
  89.         userUNLOCK;
  90.     }
  91.     return rtn;
  92. }
  93.  
  94. /* return user uid */
  95. uid_t XUserUid(const char *userName)
  96. {
  97.     uid_t    uid = -1;
  98.     if (userName) {
  99.         struct passwd *pw;
  100.         userLOCK;
  101.         if (pw = getpwnam(userName)) uid = pw->pw_uid;
  102.         userUNLOCK;
  103.     }
  104.     return uid;
  105. }
  106.  
  107. /* return user home directory */
  108. char *XUserHomeDirectory(const char *user, char *dir)
  109. {
  110.     char    *rtn = (char*)NULL;
  111.     if (dir) {
  112.         struct passwd *pw;
  113.         userLOCK;
  114.         if (!(pw = user? getpwnam((char*)user) : getpwuid(getuid()))) *dir = 0;
  115.         else rtn = strcpy(dir, pw->pw_dir);
  116.         userUNLOCK;
  117.     }
  118.     return rtn;
  119. }
  120.  
  121. /* return current user name */
  122. char *XMyUserName(char *userName)
  123. {
  124.     char    *rtn = (char*)NULL;
  125.     if (userName) {
  126.         struct passwd *pw;
  127.         userLOCK;
  128.         if (!(pw = getpwuid(getuid()))) *userName = 0;
  129.         else rtn = strcpy(userName, pw->pw_name);
  130.         userUNLOCK;
  131.     }
  132.     return rtn;
  133. }
  134.  
  135. /* return true if specified user is current user */
  136. int XIsCurrentUser(const char *userName)
  137. {
  138.     int    isCurrent = 0;
  139.     if (userName) {
  140.         struct passwd *pw;
  141.         userLOCK;
  142.         if ((pw = getpwnam(userName)) && (pw->pw_uid == getuid())) isCurrent = 1;
  143.         userUNLOCK;
  144.     }
  145.     return isCurrent;
  146. }
  147.  
  148. /* verify user password */
  149. int XUserVerifyPassword(const char *user, const char *password)
  150. {
  151.     int                ok = 0;
  152.     struct passwd    *pw;
  153.     userLOCK;
  154.     if (user && (pw = getpwnam((char*)user)) && pw->pw_passwd && *pw->pw_passwd &&
  155.         !strcmp(crypt((char*)password, pw->pw_passwd), pw->pw_passwd)) ok = 1;
  156.     userUNLOCK;
  157.     return ok;
  158. }
  159.