From 301d51e13a1aa4e633e2da161b0dd260a8a499cd Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 13 Feb 2006 17:08:25 +0000 Subject: r13494: Merge the stuff I've done in head the last days. Volker (This used to be commit bb40e544de68f01a6e774753f508e69373b39899) --- source3/smbd/map_username.c | 178 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 source3/smbd/map_username.c (limited to 'source3/smbd/map_username.c') diff --git a/source3/smbd/map_username.c b/source3/smbd/map_username.c new file mode 100644 index 0000000000..a6025fcf47 --- /dev/null +++ b/source3/smbd/map_username.c @@ -0,0 +1,178 @@ +/* + Unix SMB/CIFS implementation. + Username handling + Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Jeremy Allison 1997-2001. + Copyright (C) Volker Lendecke 2006 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +/******************************************************************* + Map a username from a dos name to a unix name by looking in the username + map. Note that this modifies the name in place. + This is the main function that should be called *once* on + any incoming or new username - in order to canonicalize the name. + This is being done to de-couple the case conversions from the user mapping + function. Previously, the map_username was being called + every time Get_Pwnam was called. + Returns True if username was changed, false otherwise. +********************************************************************/ + +BOOL map_username(fstring user) +{ + static BOOL initialised=False; + static fstring last_from,last_to; + XFILE *f; + char *mapfile = lp_username_map(); + char *s; + pstring buf; + BOOL mapped_user = False; + char *cmd = lp_username_map_script(); + + if (!*user) + return False; + + if (strequal(user,last_to)) + return False; + + if (strequal(user,last_from)) { + DEBUG(3,("Mapped user %s to %s\n",user,last_to)); + fstrcpy(user,last_to); + return True; + } + + /* first try the username map script */ + + if ( *cmd ) { + char **qlines; + pstring command; + int numlines, ret, fd; + + pstr_sprintf( command, "%s \"%s\"", cmd, user ); + + DEBUG(10,("Running [%s]\n", command)); + ret = smbrun(command, &fd); + DEBUGADD(10,("returned [%d]\n", ret)); + + if ( ret != 0 ) { + if (fd != -1) + close(fd); + return False; + } + + numlines = 0; + qlines = fd_lines_load(fd, &numlines,0); + DEBUGADD(10,("Lines returned = [%d]\n", numlines)); + close(fd); + + /* should be either no lines or a single line with the mapped username */ + + if (numlines) { + DEBUG(3,("Mapped user %s to %s\n", user, qlines[0] )); + fstrcpy( user, qlines[0] ); + } + + file_lines_free(qlines); + + return numlines != 0; + } + + /* ok. let's try the mapfile */ + + if (!*mapfile) + return False; + + if (!initialised) { + *last_from = *last_to = 0; + initialised = True; + } + + f = x_fopen(mapfile,O_RDONLY, 0); + if (!f) { + DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) )); + return False; + } + + DEBUG(4,("Scanning username map %s\n",mapfile)); + + while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) { + char *unixname = s; + char *dosname = strchr_m(unixname,'='); + char **dosuserlist; + BOOL return_if_mapped = False; + + if (!dosname) + continue; + + *dosname++ = 0; + + while (isspace((int)*unixname)) + unixname++; + + if ('!' == *unixname) { + return_if_mapped = True; + unixname++; + while (*unixname && isspace((int)*unixname)) + unixname++; + } + + if (!*unixname || strchr_m("#;",*unixname)) + continue; + + { + int l = strlen(unixname); + while (l && isspace((int)unixname[l-1])) { + unixname[l-1] = 0; + l--; + } + } + + dosuserlist = str_list_make(dosname, NULL); + if (!dosuserlist) { + DEBUG(0,("Unable to build user list\n")); + return False; + } + + if (strchr_m(dosname,'*') || + user_in_list(user, (const char **)dosuserlist)) { + DEBUG(3,("Mapped user %s to %s\n",user,unixname)); + mapped_user = True; + fstrcpy( last_from,user ); + fstrcpy( user, unixname ); + fstrcpy( last_to,user ); + if ( return_if_mapped ) { + str_list_free (&dosuserlist); + x_fclose(f); + return True; + } + } + + str_list_free (&dosuserlist); + } + + x_fclose(f); + + /* + * Setup the last_from and last_to as an optimization so + * that we don't scan the file again for the same user. + */ + fstrcpy(last_from,user); + fstrcpy(last_to,user); + + return mapped_user; +} -- cgit From 4f636b57c1330677db3ff17fd91b880cec38eb2c Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 1 Mar 2006 15:11:56 +0000 Subject: r13771: BUG 3534: ignore lines in the username map file with no right hand list rather than bailing out (This used to be commit acff5163ca7be59e01438f7cf63faef9ed54b820) --- source3/smbd/map_username.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'source3/smbd/map_username.c') diff --git a/source3/smbd/map_username.c b/source3/smbd/map_username.c index a6025fcf47..1f523cc89f 100644 --- a/source3/smbd/map_username.c +++ b/source3/smbd/map_username.c @@ -142,10 +142,12 @@ BOOL map_username(fstring user) } } + /* skip lines like 'user = ' */ + dosuserlist = str_list_make(dosname, NULL); if (!dosuserlist) { - DEBUG(0,("Unable to build user list\n")); - return False; + DEBUG(0,("Bad username map entry. Unable to build user list. Ignoring.\n")); + continue; } if (strchr_m(dosname,'*') || -- cgit From 8beeeffd6ec2a31ab6e28ae38c92c92d5be3a8c1 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 28 Jun 2006 01:25:29 +0000 Subject: r16591: Belt and braces approach to shut Klocwork up - bug #2001. Jeremy. (This used to be commit d5c1028498de0346b7a35cc132b8081e04e639cc) --- source3/smbd/map_username.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/smbd/map_username.c') diff --git a/source3/smbd/map_username.c b/source3/smbd/map_username.c index 1f523cc89f..7cbde3c59e 100644 --- a/source3/smbd/map_username.c +++ b/source3/smbd/map_username.c @@ -82,7 +82,7 @@ BOOL map_username(fstring user) /* should be either no lines or a single line with the mapped username */ - if (numlines) { + if (numlines && qlines) { DEBUG(3,("Mapped user %s to %s\n", user, qlines[0] )); fstrcpy( user, qlines[0] ); } -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/smbd/map_username.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/smbd/map_username.c') diff --git a/source3/smbd/map_username.c b/source3/smbd/map_username.c index 7cbde3c59e..78a37ea143 100644 --- a/source3/smbd/map_username.c +++ b/source3/smbd/map_username.c @@ -7,7 +7,7 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/smbd/map_username.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/smbd/map_username.c') diff --git a/source3/smbd/map_username.c b/source3/smbd/map_username.c index 78a37ea143..38881b3b2d 100644 --- a/source3/smbd/map_username.c +++ b/source3/smbd/map_username.c @@ -16,8 +16,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -- cgit From 30191d1a5704ad2b158386b511558972d539ce47 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 18 Oct 2007 17:40:25 -0700 Subject: RIP BOOL. Convert BOOL -> bool. I found a few interesting bugs in various places whilst doing this (places that assumed BOOL == int). I also need to fix the Samba4 pidl generation (next checkin). Jeremy. (This used to be commit f35a266b3cbb3e5fa6a86be60f34fe340a3ca71f) --- source3/smbd/map_username.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source3/smbd/map_username.c') diff --git a/source3/smbd/map_username.c b/source3/smbd/map_username.c index 38881b3b2d..d9ad81a3e1 100644 --- a/source3/smbd/map_username.c +++ b/source3/smbd/map_username.c @@ -32,15 +32,15 @@ Returns True if username was changed, false otherwise. ********************************************************************/ -BOOL map_username(fstring user) +bool map_username(fstring user) { - static BOOL initialised=False; + static bool initialised=False; static fstring last_from,last_to; XFILE *f; char *mapfile = lp_username_map(); char *s; pstring buf; - BOOL mapped_user = False; + bool mapped_user = False; char *cmd = lp_username_map_script(); if (!*user) @@ -113,7 +113,7 @@ BOOL map_username(fstring user) char *unixname = s; char *dosname = strchr_m(unixname,'='); char **dosuserlist; - BOOL return_if_mapped = False; + bool return_if_mapped = False; if (!dosname) continue; -- cgit From 9441d1ba87313e0ecc6e6971a25e7ad0c280fdd7 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 12 Nov 2007 18:12:26 -0800 Subject: More pstring removal from smbd/*.c Jeremy. (This used to be commit 01663c2312467ceebeb2e2fb1aa432ad96c626e5) --- source3/smbd/map_username.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'source3/smbd/map_username.c') diff --git a/source3/smbd/map_username.c b/source3/smbd/map_username.c index d9ad81a3e1..c04e0f1ae2 100644 --- a/source3/smbd/map_username.c +++ b/source3/smbd/map_username.c @@ -39,30 +39,36 @@ bool map_username(fstring user) XFILE *f; char *mapfile = lp_username_map(); char *s; - pstring buf; + char buf[512]; bool mapped_user = False; char *cmd = lp_username_map_script(); - + if (!*user) - return False; - + return false; + if (strequal(user,last_to)) - return False; + return false; if (strequal(user,last_from)) { DEBUG(3,("Mapped user %s to %s\n",user,last_to)); fstrcpy(user,last_to); - return True; + return true; } - + /* first try the username map script */ - + if ( *cmd ) { char **qlines; - pstring command; + char *command = NULL; int numlines, ret, fd; - pstr_sprintf( command, "%s \"%s\"", cmd, user ); + command = talloc_asprintf(talloc_tos(), + "%s \"%s\"", + cmd, + user); + if (!command) { + return false; + } DEBUG(10,("Running [%s]\n", command)); ret = smbrun(command, &fd); @@ -87,7 +93,7 @@ bool map_username(fstring user) } file_lines_free(qlines); - + return numlines != 0; } -- cgit From 192aae0564c641fa06f90a064c033fa52d8cf549 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 17 Dec 2007 17:27:29 -0800 Subject: Remove more static fstrings (yes this little cache should be in the rbtree....). Jeremy. (This used to be commit 97cfdae4052d46a35040d4c1a4ade8bf2c41dbc7) --- source3/smbd/map_username.c | 68 ++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 19 deletions(-) (limited to 'source3/smbd/map_username.c') diff --git a/source3/smbd/map_username.c b/source3/smbd/map_username.c index c04e0f1ae2..bde755eff6 100644 --- a/source3/smbd/map_username.c +++ b/source3/smbd/map_username.c @@ -32,10 +32,45 @@ Returns True if username was changed, false otherwise. ********************************************************************/ +static char *last_from, *last_to; + +static const char *get_last_from(void) +{ + if (!last_from) { + return ""; + } + return last_from; +} + +static const char *get_last_to(void) +{ + if (!last_to) { + return ""; + } + return last_to; +} + +static bool set_last_from_to(const char *from, const char *to) +{ + char *orig_from = last_from; + char *orig_to = last_to; + + last_from = SMB_STRDUP(from); + last_to = SMB_STRDUP(to); + + SAFE_FREE(orig_from); + SAFE_FREE(orig_to); + + if (!last_from || !last_to) { + SAFE_FREE(last_from); + SAFE_FREE(last_to); + return false; + } + return true; +} + bool map_username(fstring user) { - static bool initialised=False; - static fstring last_from,last_to; XFILE *f; char *mapfile = lp_username_map(); char *s; @@ -46,12 +81,12 @@ bool map_username(fstring user) if (!*user) return false; - if (strequal(user,last_to)) + if (strequal(user,get_last_to())) return false; - if (strequal(user,last_from)) { - DEBUG(3,("Mapped user %s to %s\n",user,last_to)); - fstrcpy(user,last_to); + if (strequal(user,get_last_from())) { + DEBUG(3,("Mapped user %s to %s\n",user,get_last_to())); + fstrcpy(user,get_last_to()); return true; } @@ -98,15 +133,9 @@ bool map_username(fstring user) } /* ok. let's try the mapfile */ - if (!*mapfile) return False; - if (!initialised) { - *last_from = *last_to = 0; - initialised = True; - } - f = x_fopen(mapfile,O_RDONLY, 0); if (!f) { DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) )); @@ -135,7 +164,7 @@ bool map_username(fstring user) while (*unixname && isspace((int)*unixname)) unixname++; } - + if (!*unixname || strchr_m("#;",*unixname)) continue; @@ -159,27 +188,28 @@ bool map_username(fstring user) user_in_list(user, (const char **)dosuserlist)) { DEBUG(3,("Mapped user %s to %s\n",user,unixname)); mapped_user = True; - fstrcpy( last_from,user ); + + set_last_from_to(user, unixname); fstrcpy( user, unixname ); - fstrcpy( last_to,user ); + if ( return_if_mapped ) { str_list_free (&dosuserlist); x_fclose(f); return True; } } - + str_list_free (&dosuserlist); } x_fclose(f); /* - * Setup the last_from and last_to as an optimization so + * Setup the last_from and last_to as an optimization so * that we don't scan the file again for the same user. */ - fstrcpy(last_from,user); - fstrcpy(last_to,user); + + set_last_from_to(user, user); return mapped_user; } -- cgit From e518e19bc0000019f131354f55e9f5b55f6a2c5e Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Wed, 19 Dec 2007 15:02:59 +0100 Subject: Remove Get_Pwnam and its associated static variable All callers are replaced by Get_Pwnam_alloc (This used to be commit 735f59315497113aebadcf9ad387e3dbfffa284a) --- source3/smbd/map_username.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/smbd/map_username.c') diff --git a/source3/smbd/map_username.c b/source3/smbd/map_username.c index bde755eff6..7290f70547 100644 --- a/source3/smbd/map_username.c +++ b/source3/smbd/map_username.c @@ -28,7 +28,7 @@ any incoming or new username - in order to canonicalize the name. This is being done to de-couple the case conversions from the user mapping function. Previously, the map_username was being called - every time Get_Pwnam was called. + every time Get_Pwnam_alloc was called. Returns True if username was changed, false otherwise. ********************************************************************/ -- cgit From 2762b9a97582b9b28fd5985ba8e3d0299126820e Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 4 Feb 2008 20:57:35 +0100 Subject: Always pass a TALLOC_CTX to str_list_make and str_list_copy (This used to be commit e2c9fc4cf5f0ff725330fa44f53782db65fca37e) --- source3/smbd/map_username.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/smbd/map_username.c') diff --git a/source3/smbd/map_username.c b/source3/smbd/map_username.c index 7290f70547..7536758bcb 100644 --- a/source3/smbd/map_username.c +++ b/source3/smbd/map_username.c @@ -178,7 +178,7 @@ bool map_username(fstring user) /* skip lines like 'user = ' */ - dosuserlist = str_list_make(dosname, NULL); + dosuserlist = str_list_make(talloc_tos(), dosname, NULL); if (!dosuserlist) { DEBUG(0,("Bad username map entry. Unable to build user list. Ignoring.\n")); continue; @@ -193,13 +193,13 @@ bool map_username(fstring user) fstrcpy( user, unixname ); if ( return_if_mapped ) { - str_list_free (&dosuserlist); + TALLOC_FREE(dosuserlist); x_fclose(f); return True; } } - str_list_free (&dosuserlist); + TALLOC_FREE(dosuserlist); } x_fclose(f); -- cgit