00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "includes.h"
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
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
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
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
00174
00175
00176 fstrcpy(last_from,user);
00177 fstrcpy(last_to,user);
00178
00179 return mapped_user;
00180 }