smbd/map_username.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    Username handling
00004    Copyright (C) Andrew Tridgell 1992-1998
00005    Copyright (C) Jeremy Allison 1997-2001.
00006    Copyright (C) Volker Lendecke 2006
00007    
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012    
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017    
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 
00023 #include "includes.h"
00024 
00025 /*******************************************************************
00026  Map a username from a dos name to a unix name by looking in the username
00027  map. Note that this modifies the name in place.
00028  This is the main function that should be called *once* on
00029  any incoming or new username - in order to canonicalize the name.
00030  This is being done to de-couple the case conversions from the user mapping
00031  function. Previously, the map_username was being called
00032  every time Get_Pwnam was called.
00033  Returns True if username was changed, false otherwise.
00034 ********************************************************************/
00035 
00036 BOOL map_username(fstring user)
00037 {
00038         static BOOL initialised=False;
00039         static fstring last_from,last_to;
00040         XFILE *f;
00041         char *mapfile = lp_username_map();
00042         char *s;
00043         pstring buf;
00044         BOOL mapped_user = False;
00045         char *cmd = lp_username_map_script();
00046         
00047         if (!*user)
00048                 return False;
00049                 
00050         if (strequal(user,last_to))
00051                 return False;
00052 
00053         if (strequal(user,last_from)) {
00054                 DEBUG(3,("Mapped user %s to %s\n",user,last_to));
00055                 fstrcpy(user,last_to);
00056                 return True;
00057         }
00058         
00059         /* first try the username map script */
00060         
00061         if ( *cmd ) {
00062                 char **qlines;
00063                 pstring command;
00064                 int numlines, ret, fd;
00065 
00066                 pstr_sprintf( command, "%s \"%s\"", cmd, user );
00067 
00068                 DEBUG(10,("Running [%s]\n", command));
00069                 ret = smbrun(command, &fd);
00070                 DEBUGADD(10,("returned [%d]\n", ret));
00071 
00072                 if ( ret != 0 ) {
00073                         if (fd != -1)
00074                                 close(fd);
00075                         return False;
00076                 }
00077 
00078                 numlines = 0;
00079                 qlines = fd_lines_load(fd, &numlines,0);
00080                 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
00081                 close(fd);
00082 
00083                 /* should be either no lines or a single line with the mapped username */
00084 
00085                 if (numlines && qlines) {
00086                         DEBUG(3,("Mapped user %s to %s\n", user, qlines[0] ));
00087                         fstrcpy( user, qlines[0] );
00088                 }
00089 
00090                 file_lines_free(qlines);
00091                 
00092                 return numlines != 0;
00093         }
00094 
00095         /* ok.  let's try the mapfile */
00096         
00097         if (!*mapfile)
00098                 return False;
00099 
00100         if (!initialised) {
00101                 *last_from = *last_to = 0;
00102                 initialised = True;
00103         }
00104   
00105         f = x_fopen(mapfile,O_RDONLY, 0);
00106         if (!f) {
00107                 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
00108                 return False;
00109         }
00110 
00111         DEBUG(4,("Scanning username map %s\n",mapfile));
00112 
00113         while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
00114                 char *unixname = s;
00115                 char *dosname = strchr_m(unixname,'=');
00116                 char **dosuserlist;
00117                 BOOL return_if_mapped = False;
00118 
00119                 if (!dosname)
00120                         continue;
00121 
00122                 *dosname++ = 0;
00123 
00124                 while (isspace((int)*unixname))
00125                         unixname++;
00126 
00127                 if ('!' == *unixname) {
00128                         return_if_mapped = True;
00129                         unixname++;
00130                         while (*unixname && isspace((int)*unixname))
00131                                 unixname++;
00132                 }
00133     
00134                 if (!*unixname || strchr_m("#;",*unixname))
00135                         continue;
00136 
00137                 {
00138                         int l = strlen(unixname);
00139                         while (l && isspace((int)unixname[l-1])) {
00140                                 unixname[l-1] = 0;
00141                                 l--;
00142                         }
00143                 }
00144 
00145                 /* skip lines like 'user = ' */
00146 
00147                 dosuserlist = str_list_make(dosname, NULL);
00148                 if (!dosuserlist) {
00149                         DEBUG(0,("Bad username map entry.  Unable to build user list.  Ignoring.\n"));
00150                         continue;
00151                 }
00152 
00153                 if (strchr_m(dosname,'*') ||
00154                     user_in_list(user, (const char **)dosuserlist)) {
00155                         DEBUG(3,("Mapped user %s to %s\n",user,unixname));
00156                         mapped_user = True;
00157                         fstrcpy( last_from,user );
00158                         fstrcpy( user, unixname );
00159                         fstrcpy( last_to,user );
00160                         if ( return_if_mapped ) {
00161                                 str_list_free (&dosuserlist);
00162                                 x_fclose(f);
00163                                 return True;
00164                         }
00165                 }
00166     
00167                 str_list_free (&dosuserlist);
00168         }
00169 
00170         x_fclose(f);
00171 
00172         /*
00173          * Setup the last_from and last_to as an optimization so 
00174          * that we don't scan the file again for the same user.
00175          */
00176         fstrcpy(last_from,user);
00177         fstrcpy(last_to,user);
00178 
00179         return mapped_user;
00180 }

Sambaに対してSat Aug 29 21:23:24 2009に生成されました。  doxygen 1.4.7