PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/source3/lib/username.c

https://bitbucket.org/mikedep333/rdssamba4
C | 232 lines | 129 code | 42 blank | 61 comment | 27 complexity | 98426b1196ccf6028c6d1e21273e2397 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, LGPL-3.0
  1. /*
  2. Unix SMB/CIFS implementation.
  3. Username handling
  4. Copyright (C) Andrew Tridgell 1992-1998
  5. Copyright (C) Jeremy Allison 1997-2001.
  6. Copyright (C) Andrew Bartlett 2002
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "includes.h"
  19. #include "system/passwd.h"
  20. #include "memcache.h"
  21. #include "../lib/util/util_pw.h"
  22. /* internal functions */
  23. static struct passwd *uname_string_combinations(char *s, TALLOC_CTX *mem_ctx,
  24. struct passwd * (*fn) (TALLOC_CTX *mem_ctx, const char *),
  25. int N);
  26. static struct passwd *uname_string_combinations2(char *s, TALLOC_CTX *mem_ctx, int offset,
  27. struct passwd * (*fn) (TALLOC_CTX *mem_ctx, const char *),
  28. int N);
  29. static struct passwd *getpwnam_alloc_cached(TALLOC_CTX *mem_ctx, const char *name)
  30. {
  31. struct passwd *pw, *for_cache;
  32. pw = (struct passwd *)memcache_lookup_talloc(
  33. NULL, GETPWNAM_CACHE, data_blob_string_const_null(name));
  34. if (pw != NULL) {
  35. return tcopy_passwd(mem_ctx, pw);
  36. }
  37. pw = sys_getpwnam(name);
  38. if (pw == NULL) {
  39. return NULL;
  40. }
  41. for_cache = tcopy_passwd(talloc_tos(), pw);
  42. if (for_cache == NULL) {
  43. return NULL;
  44. }
  45. memcache_add_talloc(NULL, GETPWNAM_CACHE,
  46. data_blob_string_const_null(name), &for_cache);
  47. return tcopy_passwd(mem_ctx, pw);
  48. }
  49. /****************************************************************************
  50. Flush all cached passwd structs.
  51. ****************************************************************************/
  52. void flush_pwnam_cache(void)
  53. {
  54. memcache_flush(NULL, GETPWNAM_CACHE);
  55. }
  56. /****************************************************************************
  57. Get a users home directory.
  58. ****************************************************************************/
  59. char *get_user_home_dir(TALLOC_CTX *mem_ctx, const char *user)
  60. {
  61. struct passwd *pass;
  62. char *result;
  63. /* Ensure the user exists. */
  64. pass = Get_Pwnam_alloc(mem_ctx, user);
  65. if (!pass)
  66. return(NULL);
  67. /* Return home directory from struct passwd. */
  68. result = talloc_move(mem_ctx, &pass->pw_dir);
  69. TALLOC_FREE(pass);
  70. return result;
  71. }
  72. /****************************************************************************
  73. * A wrapper for sys_getpwnam(). The following variations are tried:
  74. * - as transmitted
  75. * - in all lower case if this differs from transmitted
  76. * - in all upper case if this differs from transmitted
  77. * - using lp_usernamelevel() for permutations.
  78. ****************************************************************************/
  79. static struct passwd *Get_Pwnam_internals(TALLOC_CTX *mem_ctx,
  80. const char *user, char *user2)
  81. {
  82. struct passwd *ret = NULL;
  83. if (!user2 || !(*user2))
  84. return(NULL);
  85. if (!user || !(*user))
  86. return(NULL);
  87. /* Try in all lower case first as this is the most
  88. common case on UNIX systems */
  89. strlower_m(user2);
  90. DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2));
  91. ret = getpwnam_alloc_cached(mem_ctx, user2);
  92. if(ret)
  93. goto done;
  94. /* Try as given, if username wasn't originally lowercase */
  95. if(strcmp(user, user2) != 0) {
  96. DEBUG(5,("Trying _Get_Pwnam(), username as given is %s\n",
  97. user));
  98. ret = getpwnam_alloc_cached(mem_ctx, user);
  99. if(ret)
  100. goto done;
  101. }
  102. /* Try as uppercase, if username wasn't originally uppercase */
  103. strupper_m(user2);
  104. if(strcmp(user, user2) != 0) {
  105. DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",
  106. user2));
  107. ret = getpwnam_alloc_cached(mem_ctx, user2);
  108. if(ret)
  109. goto done;
  110. }
  111. /* Try all combinations up to usernamelevel */
  112. strlower_m(user2);
  113. DEBUG(5,("Checking combinations of %d uppercase letters in %s\n",
  114. lp_usernamelevel(), user2));
  115. ret = uname_string_combinations(user2, mem_ctx, getpwnam_alloc_cached,
  116. lp_usernamelevel());
  117. done:
  118. DEBUG(5,("Get_Pwnam_internals %s find user [%s]!\n",ret ?
  119. "did":"didn't", user));
  120. return ret;
  121. }
  122. /****************************************************************************
  123. Get_Pwnam wrapper without modification.
  124. NOTE: This with NOT modify 'user'!
  125. This will return an allocated structure
  126. ****************************************************************************/
  127. struct passwd *Get_Pwnam_alloc(TALLOC_CTX *mem_ctx, const char *user)
  128. {
  129. fstring user2;
  130. if ( *user == '\0' ) {
  131. DEBUG(10,("Get_Pwnam: empty username!\n"));
  132. return NULL;
  133. }
  134. fstrcpy(user2, user);
  135. DEBUG(5,("Finding user %s\n", user));
  136. return Get_Pwnam_internals(mem_ctx, user, user2);
  137. }
  138. /* The functions below have been taken from password.c and slightly modified */
  139. /****************************************************************************
  140. Apply a function to upper/lower case combinations
  141. of a string and return true if one of them returns true.
  142. Try all combinations with N uppercase letters.
  143. offset is the first char to try and change (start with 0)
  144. it assumes the string starts lowercased
  145. ****************************************************************************/
  146. static struct passwd *uname_string_combinations2(char *s, TALLOC_CTX *mem_ctx,
  147. int offset,
  148. struct passwd *(*fn)(TALLOC_CTX *mem_ctx, const char *),
  149. int N)
  150. {
  151. ssize_t len = (ssize_t)strlen(s);
  152. int i;
  153. struct passwd *ret;
  154. if (N <= 0 || offset >= len)
  155. return(fn(mem_ctx, s));
  156. for (i=offset;i<(len-(N-1));i++) {
  157. char c = s[i];
  158. if (!islower_m((int)c))
  159. continue;
  160. s[i] = toupper_m(c);
  161. ret = uname_string_combinations2(s, mem_ctx, i+1, fn, N-1);
  162. if(ret)
  163. return(ret);
  164. s[i] = c;
  165. }
  166. return(NULL);
  167. }
  168. /****************************************************************************
  169. Apply a function to upper/lower case combinations
  170. of a string and return true if one of them returns true.
  171. Try all combinations with up to N uppercase letters.
  172. offset is the first char to try and change (start with 0)
  173. it assumes the string starts lowercased
  174. ****************************************************************************/
  175. static struct passwd * uname_string_combinations(char *s, TALLOC_CTX *mem_ctx,
  176. struct passwd * (*fn)(TALLOC_CTX *mem_ctx, const char *),
  177. int N)
  178. {
  179. int n;
  180. struct passwd *ret;
  181. for (n=1;n<=N;n++) {
  182. ret = uname_string_combinations2(s,mem_ctx,0,fn,n);
  183. if(ret)
  184. return(ret);
  185. }
  186. return(NULL);
  187. }