PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/uClinux-dist/user/samba/source/lib/username.c

https://bitbucket.org/__wp__/mb-linux-msli
C | 228 lines | 112 code | 41 blank | 75 comment | 22 complexity | eb4fc17596b7179a97b0f9c2f1166762 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, LGPL-2.0, MPL-2.0, ISC, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, 0BSD, CC-BY-SA-3.0, GPL-3.0, LGPL-3.0, AGPL-1.0, Unlicense
  1. /*
  2. Unix SMB/CIFS implementation.
  3. Username handling
  4. Copyright (C) Andrew Tridgell 1992-1998
  5. Copyright (C) Jeremy Allison 1997-2001.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "includes.h"
  19. /* internal functions */
  20. static struct passwd *uname_string_combinations(char *s, TALLOC_CTX *mem_ctx,
  21. struct passwd * (*fn) (TALLOC_CTX *mem_ctx, const char *),
  22. int N);
  23. static struct passwd *uname_string_combinations2(char *s, TALLOC_CTX *mem_ctx, int offset,
  24. struct passwd * (*fn) (TALLOC_CTX *mem_ctx, const char *),
  25. int N);
  26. /****************************************************************************
  27. Get a users home directory.
  28. ****************************************************************************/
  29. char *get_user_home_dir(const char *user)
  30. {
  31. static struct passwd *pass;
  32. /* Ensure the user exists. */
  33. pass = Get_Pwnam(user);
  34. if (!pass)
  35. return(NULL);
  36. /* Return home directory from struct passwd. */
  37. return(pass->pw_dir);
  38. }
  39. /****************************************************************************
  40. * A wrapper for sys_getpwnam(). The following variations are tried:
  41. * - as transmitted
  42. * - in all lower case if this differs from transmitted
  43. * - in all upper case if this differs from transmitted
  44. * - using lp_usernamelevel() for permutations.
  45. ****************************************************************************/
  46. static struct passwd *Get_Pwnam_ret = NULL;
  47. static struct passwd *Get_Pwnam_internals(TALLOC_CTX *mem_ctx,
  48. const char *user, char *user2)
  49. {
  50. struct passwd *ret = NULL;
  51. if (!user2 || !(*user2))
  52. return(NULL);
  53. if (!user || !(*user))
  54. return(NULL);
  55. /* Try in all lower case first as this is the most
  56. common case on UNIX systems */
  57. strlower_m(user2);
  58. DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2));
  59. ret = getpwnam_alloc(mem_ctx, user2);
  60. if(ret)
  61. goto done;
  62. /* Try as given, if username wasn't originally lowercase */
  63. if(strcmp(user, user2) != 0) {
  64. DEBUG(5,("Trying _Get_Pwnam(), username as given is %s\n",
  65. user));
  66. ret = getpwnam_alloc(mem_ctx, user);
  67. if(ret)
  68. goto done;
  69. }
  70. /* Try as uppercase, if username wasn't originally uppercase */
  71. strupper_m(user2);
  72. if(strcmp(user, user2) != 0) {
  73. DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",
  74. user2));
  75. ret = getpwnam_alloc(mem_ctx, user2);
  76. if(ret)
  77. goto done;
  78. }
  79. /* Try all combinations up to usernamelevel */
  80. strlower_m(user2);
  81. DEBUG(5,("Checking combinations of %d uppercase letters in %s\n",
  82. lp_usernamelevel(), user2));
  83. ret = uname_string_combinations(user2, mem_ctx, getpwnam_alloc,
  84. lp_usernamelevel());
  85. done:
  86. DEBUG(5,("Get_Pwnam_internals %s find user [%s]!\n",ret ?
  87. "did":"didn't", user));
  88. return ret;
  89. }
  90. /****************************************************************************
  91. Get_Pwnam wrapper without modification.
  92. NOTE: This with NOT modify 'user'!
  93. This will return an allocated structure
  94. ****************************************************************************/
  95. struct passwd *Get_Pwnam_alloc(TALLOC_CTX *mem_ctx, const char *user)
  96. {
  97. fstring user2;
  98. struct passwd *ret;
  99. if ( *user == '\0' ) {
  100. DEBUG(10,("Get_Pwnam: empty username!\n"));
  101. return NULL;
  102. }
  103. fstrcpy(user2, user);
  104. DEBUG(5,("Finding user %s\n", user));
  105. ret = Get_Pwnam_internals(mem_ctx, user, user2);
  106. return ret;
  107. }
  108. /****************************************************************************
  109. Get_Pwnam wrapper without modification.
  110. NOTE: This with NOT modify 'user'!
  111. ****************************************************************************/
  112. struct passwd *Get_Pwnam(const char *user)
  113. {
  114. struct passwd *ret;
  115. ret = Get_Pwnam_alloc(NULL, user);
  116. /* This call used to just return the 'passwd' static buffer.
  117. This could then have accidental reuse implications, so
  118. we now malloc a copy, and free it in the next use.
  119. This should cause the (ab)user to segfault if it
  120. uses an old struct.
  121. This is better than useing the wrong data in security
  122. critical operations.
  123. The real fix is to make the callers free the returned
  124. malloc'ed data.
  125. */
  126. if (Get_Pwnam_ret) {
  127. TALLOC_FREE(Get_Pwnam_ret);
  128. }
  129. Get_Pwnam_ret = ret;
  130. return ret;
  131. }
  132. /* The functions below have been taken from password.c and slightly modified */
  133. /****************************************************************************
  134. Apply a function to upper/lower case combinations
  135. of a string and return true if one of them returns true.
  136. Try all combinations with N uppercase letters.
  137. offset is the first char to try and change (start with 0)
  138. it assumes the string starts lowercased
  139. ****************************************************************************/
  140. static struct passwd *uname_string_combinations2(char *s, TALLOC_CTX *mem_ctx,
  141. int offset,
  142. struct passwd *(*fn)(TALLOC_CTX *mem_ctx, const char *),
  143. int N)
  144. {
  145. ssize_t len = (ssize_t)strlen(s);
  146. int i;
  147. struct passwd *ret;
  148. if (N <= 0 || offset >= len)
  149. return(fn(mem_ctx, s));
  150. for (i=offset;i<(len-(N-1));i++) {
  151. char c = s[i];
  152. if (!islower_ascii((int)c))
  153. continue;
  154. s[i] = toupper_ascii(c);
  155. ret = uname_string_combinations2(s, mem_ctx, i+1, fn, N-1);
  156. if(ret)
  157. return(ret);
  158. s[i] = c;
  159. }
  160. return(NULL);
  161. }
  162. /****************************************************************************
  163. Apply a function to upper/lower case combinations
  164. of a string and return true if one of them returns true.
  165. Try all combinations with up to N uppercase letters.
  166. offset is the first char to try and change (start with 0)
  167. it assumes the string starts lowercased
  168. ****************************************************************************/
  169. static struct passwd * uname_string_combinations(char *s, TALLOC_CTX *mem_ctx,
  170. struct passwd * (*fn)(TALLOC_CTX *mem_ctx, const char *),
  171. int N)
  172. {
  173. int n;
  174. struct passwd *ret;
  175. for (n=1;n<=N;n++) {
  176. ret = uname_string_combinations2(s,mem_ctx,0,fn,n);
  177. if(ret)
  178. return(ret);
  179. }
  180. return(NULL);
  181. }