username.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. Username handling
  5. Copyright (C) Andrew Tridgell 1992-1998
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include "includes.h"
  19. extern int DEBUGLEVEL;
  20. /* internal functions */
  21. static struct passwd *uname_string_combinations(char *s, struct passwd * (*fn) (const char *), int N);
  22. static struct passwd *uname_string_combinations2(char *s, int offset, struct passwd * (*fn) (const char *), int N);
  23. /****************************************************************************
  24. get a users home directory.
  25. ****************************************************************************/
  26. const char *get_home_dir(char *user)
  27. {
  28. struct passwd *pass;
  29. pass = Get_Pwnam(user);
  30. if (!pass) return(NULL);
  31. return(pass->pw_dir);
  32. }
  33. #if 0 /* Fix possible buffer overflow in sscanf(unixname,"%s",user) if uncomment */
  34. /*******************************************************************
  35. map a username from a dos name to a unix name by looking in the username
  36. map. Note that this modifies the name in place.
  37. This is the main function that should be called *once* on
  38. any incoming or new username - in order to canonicalize the name.
  39. This is being done to de-couple the case conversions from the user mapping
  40. function. Previously, the map_username was being called
  41. every time Get_Pwnam was called.
  42. Returns True if username was changed, false otherwise.
  43. ********************************************************************/
  44. BOOL map_username(const char *user)
  45. {
  46. static BOOL initialised=False;
  47. static fstring last_from,last_to;
  48. FILE *f;
  49. char *mapfile = lp_username_map();
  50. char *s;
  51. pstring buf;
  52. BOOL mapped_user = False;
  53. if (!*user)
  54. return False;
  55. if (!*mapfile)
  56. return False;
  57. if (!initialised) {
  58. *last_from = *last_to = 0;
  59. initialised = True;
  60. }
  61. if (strequal(user,last_to))
  62. return False;
  63. if (strequal(user,last_from)) {
  64. DEBUG(3,("Mapped user %s to %s\n",user,last_to));
  65. fstrcpy(user,last_to);
  66. return True;
  67. }
  68. f = sys_fopen(mapfile,"r");
  69. if (!f) {
  70. DEBUG(0,("can't open username map %s\n",mapfile));
  71. return False;
  72. }
  73. DEBUG(4,("Scanning username map %s\n",mapfile));
  74. while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
  75. char *unixname = s;
  76. char *dosname = strchr(unixname,'=');
  77. BOOL return_if_mapped = False;
  78. if (!dosname)
  79. continue;
  80. *dosname++ = 0;
  81. while (isspace(*unixname))
  82. unixname++;
  83. if ('!' == *unixname) {
  84. return_if_mapped = True;
  85. unixname++;
  86. while (*unixname && isspace(*unixname))
  87. unixname++;
  88. }
  89. if (!*unixname || strchr("#;",*unixname))
  90. continue;
  91. {
  92. int l = strlen(unixname);
  93. while (l && isspace(unixname[l-1])) {
  94. unixname[l-1] = 0;
  95. l--;
  96. }
  97. }
  98. if (strchr(dosname,'*') || user_in_list(user,dosname)) {
  99. DEBUG(3,("Mapped user %s to %s\n",user,unixname));
  100. mapped_user = True;
  101. fstrcpy(last_from,user);
  102. sscanf(unixname,"%s",user);
  103. fstrcpy(last_to,user);
  104. if(return_if_mapped) {
  105. fclose(f);
  106. return True;
  107. }
  108. }
  109. }
  110. fclose(f);
  111. /*
  112. * Setup the last_from and last_to as an optimization so
  113. * that we don't scan the file again for the same user.
  114. */
  115. fstrcpy(last_from,user);
  116. fstrcpy(last_to,user);
  117. return mapped_user;
  118. }
  119. #endif /* 0 */
  120. /****************************************************************************
  121. Get_Pwnam wrapper
  122. ****************************************************************************/
  123. static struct passwd *_Get_Pwnam(const char *s)
  124. {
  125. struct passwd *ret;
  126. ret = getpwnam(s);
  127. if (ret)
  128. {
  129. #ifdef HAVE_GETPWANAM
  130. struct passwd_adjunct *pwret;
  131. pwret = getpwanam(s);
  132. if (pwret)
  133. {
  134. free(ret->pw_passwd);
  135. ret->pw_passwd = pwret->pwa_passwd;
  136. }
  137. #endif
  138. }
  139. return(ret);
  140. }
  141. /****************************************************************************
  142. a wrapper for getpwnam() that tries with all lower and all upper case
  143. if the initial name fails. Also tried with first letter capitalised
  144. ****************************************************************************/
  145. struct passwd *Get_Pwnam(const char *a_user)
  146. {
  147. fstring user;
  148. int last_char;
  149. int usernamelevel = lp_usernamelevel();
  150. struct passwd *ret;
  151. if (!a_user || !(*a_user))
  152. return(NULL);
  153. StrnCpy(user,a_user,sizeof(user)-1);
  154. ret = _Get_Pwnam(user);
  155. if (ret) return(ret);
  156. strlower(user);
  157. ret = _Get_Pwnam(user);
  158. if (ret) return(ret);
  159. strupper(user);
  160. ret = _Get_Pwnam(user);
  161. if (ret) return(ret);
  162. /* try with first letter capitalised */
  163. if (strlen(user) > 1)
  164. strlower(user+1);
  165. ret = _Get_Pwnam(user);
  166. if (ret) return(ret);
  167. /* try with last letter capitalised */
  168. strlower(user);
  169. last_char = strlen(user)-1;
  170. user[last_char] = toupper(user[last_char]);
  171. ret = _Get_Pwnam(user);
  172. if (ret) return(ret);
  173. /* try all combinations up to usernamelevel */
  174. strlower(user);
  175. ret = uname_string_combinations(user, _Get_Pwnam, usernamelevel);
  176. if (ret) return(ret);
  177. return(NULL);
  178. }
  179. #if 0
  180. /****************************************************************************
  181. check if a user is in a netgroup user list
  182. ****************************************************************************/
  183. static BOOL user_in_netgroup_list(char *user,char *ngname)
  184. {
  185. #ifdef HAVE_NETGROUP
  186. static char *mydomain = NULL;
  187. if (mydomain == NULL)
  188. yp_get_default_domain(&mydomain);
  189. if(mydomain == NULL)
  190. {
  191. DEBUG(5,("Unable to get default yp domain\n"));
  192. }
  193. else
  194. {
  195. DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
  196. user, mydomain, ngname));
  197. DEBUG(5,("innetgr is %s\n",
  198. innetgr(ngname, NULL, user, mydomain)
  199. ? "TRUE" : "FALSE"));
  200. if (innetgr(ngname, NULL, user, mydomain))
  201. return (True);
  202. }
  203. #endif /* HAVE_NETGROUP */
  204. return False;
  205. }
  206. /****************************************************************************
  207. check if a user is in a UNIX user list
  208. ****************************************************************************/
  209. static BOOL user_in_group_list(char *user,char *gname)
  210. {
  211. #ifdef HAVE_GETGRNAM
  212. struct group *gptr;
  213. char **member;
  214. struct passwd *pass = Get_Pwnam(user,False);
  215. if (pass)
  216. {
  217. gptr = getgrgid(pass->pw_gid);
  218. if (gptr && strequal(gptr->gr_name,gname))
  219. return(True);
  220. }
  221. gptr = (struct group *)getgrnam(gname);
  222. if (gptr)
  223. {
  224. member = gptr->gr_mem;
  225. while (member && *member)
  226. {
  227. if (strequal(*member,user))
  228. return(True);
  229. member++;
  230. }
  231. }
  232. #endif /* HAVE_GETGRNAM */
  233. return False;
  234. }
  235. /****************************************************************************
  236. check if a user is in a user list - can check combinations of UNIX
  237. and netgroup lists.
  238. ****************************************************************************/
  239. BOOL user_in_list(char *user,char *list)
  240. {
  241. pstring tok;
  242. char *p=list;
  243. while (next_token(&p,tok,LIST_SEP, sizeof(tok)))
  244. {
  245. /*
  246. * Check raw username.
  247. */
  248. if (strequal(user,tok))
  249. return(True);
  250. /*
  251. * Now check to see if any combination
  252. * of UNIX and netgroups has been specified.
  253. */
  254. if(*tok == '@')
  255. {
  256. /*
  257. * Old behaviour. Check netgroup list
  258. * followed by UNIX list.
  259. */
  260. if(user_in_netgroup_list(user,&tok[1]))
  261. return True;
  262. if(user_in_group_list(user,&tok[1]))
  263. return True;
  264. }
  265. else if (*tok == '+')
  266. {
  267. if(tok[1] == '&')
  268. {
  269. /*
  270. * Search UNIX list followed by netgroup.
  271. */
  272. if(user_in_group_list(user,&tok[2]))
  273. return True;
  274. if(user_in_netgroup_list(user,&tok[2]))
  275. return True;
  276. }
  277. else
  278. {
  279. /*
  280. * Just search UNIX list.
  281. */
  282. if(user_in_group_list(user,&tok[1]))
  283. return True;
  284. }
  285. }
  286. else if (*tok == '&')
  287. {
  288. if(tok[1] == '&')
  289. {
  290. /*
  291. * Search netgroup list followed by UNIX list.
  292. */
  293. if(user_in_netgroup_list(user,&tok[2]))
  294. return True;
  295. if(user_in_group_list(user,&tok[2]))
  296. return True;
  297. }
  298. else
  299. {
  300. /*
  301. * Just search netgroup list.
  302. */
  303. if(user_in_netgroup_list(user,&tok[1]))
  304. return True;
  305. }
  306. }
  307. }
  308. return(False);
  309. }
  310. #endif /* 0 */
  311. /* The functions below have been taken from password.c and slightly modified */
  312. /****************************************************************************
  313. apply a function to upper/lower case combinations
  314. of a string and return true if one of them returns true.
  315. try all combinations with N uppercase letters.
  316. offset is the first char to try and change (start with 0)
  317. it assumes the string starts lowercased
  318. ****************************************************************************/
  319. static struct passwd *uname_string_combinations2(char *s,int offset,struct passwd *(*fn)(const char *),int N)
  320. {
  321. int len = strlen(s);
  322. int i;
  323. struct passwd *ret;
  324. #ifdef PASSWORD_LENGTH
  325. len = MIN(len,PASSWORD_LENGTH);
  326. #endif
  327. if (N <= 0 || offset >= len)
  328. return(fn(s));
  329. for (i=offset;i<(len-(N-1));i++)
  330. {
  331. char c = s[i];
  332. if (!islower(c)) continue;
  333. s[i] = toupper(c);
  334. ret = uname_string_combinations2(s,i+1,fn,N-1);
  335. if(ret) return(ret);
  336. s[i] = c;
  337. }
  338. return(NULL);
  339. }
  340. /****************************************************************************
  341. apply a function to upper/lower case combinations
  342. of a string and return true if one of them returns true.
  343. try all combinations with up to N uppercase letters.
  344. offset is the first char to try and change (start with 0)
  345. it assumes the string starts lowercased
  346. ****************************************************************************/
  347. static struct passwd * uname_string_combinations(char *s,struct passwd * (*fn)(const char *),int N)
  348. {
  349. int n;
  350. struct passwd *ret;
  351. for (n=1;n<=N;n++)
  352. {
  353. ret = uname_string_combinations2(s,0,fn,n);
  354. if(ret) return(ret);
  355. }
  356. return(NULL);
  357. }