| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Username handling
|
|---|
| 4 | Copyright (C) Andrew Tridgell 1992-1998
|
|---|
| 5 | Copyright (C) Jeremy Allison 1997-2001.
|
|---|
| 6 | Copyright (C) Volker Lendecke 2006
|
|---|
| 7 |
|
|---|
| 8 | This program is free software; you can redistribute it and/or modify
|
|---|
| 9 | it under the terms of the GNU General Public License as published by
|
|---|
| 10 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 11 | (at your option) any later version.
|
|---|
| 12 |
|
|---|
| 13 | This program is distributed in the hope that it will be useful,
|
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 16 | GNU General Public License for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License
|
|---|
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 |
|
|---|
| 24 | /*******************************************************************
|
|---|
| 25 | Map a username from a dos name to a unix name by looking in the username
|
|---|
| 26 | map. Note that this modifies the name in place.
|
|---|
| 27 | This is the main function that should be called *once* on
|
|---|
| 28 | any incoming or new username - in order to canonicalize the name.
|
|---|
| 29 | This is being done to de-couple the case conversions from the user mapping
|
|---|
| 30 | function. Previously, the map_username was being called
|
|---|
| 31 | every time Get_Pwnam_alloc was called.
|
|---|
| 32 | Returns True if username was changed, false otherwise.
|
|---|
| 33 | ********************************************************************/
|
|---|
| 34 |
|
|---|
| 35 | static char *last_from, *last_to;
|
|---|
| 36 |
|
|---|
| 37 | static const char *get_last_from(void)
|
|---|
| 38 | {
|
|---|
| 39 | if (!last_from) {
|
|---|
| 40 | return "";
|
|---|
| 41 | }
|
|---|
| 42 | return last_from;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | static const char *get_last_to(void)
|
|---|
| 46 | {
|
|---|
| 47 | if (!last_to) {
|
|---|
| 48 | return "";
|
|---|
| 49 | }
|
|---|
| 50 | return last_to;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | static bool set_last_from_to(const char *from, const char *to)
|
|---|
| 54 | {
|
|---|
| 55 | char *orig_from = last_from;
|
|---|
| 56 | char *orig_to = last_to;
|
|---|
| 57 |
|
|---|
| 58 | last_from = SMB_STRDUP(from);
|
|---|
| 59 | last_to = SMB_STRDUP(to);
|
|---|
| 60 |
|
|---|
| 61 | SAFE_FREE(orig_from);
|
|---|
| 62 | SAFE_FREE(orig_to);
|
|---|
| 63 |
|
|---|
| 64 | if (!last_from || !last_to) {
|
|---|
| 65 | SAFE_FREE(last_from);
|
|---|
| 66 | SAFE_FREE(last_to);
|
|---|
| 67 | return false;
|
|---|
| 68 | }
|
|---|
| 69 | return true;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | bool map_username(fstring user)
|
|---|
| 73 | {
|
|---|
| 74 | XFILE *f;
|
|---|
| 75 | char *mapfile = lp_username_map();
|
|---|
| 76 | char *s;
|
|---|
| 77 | char buf[512];
|
|---|
| 78 | bool mapped_user = False;
|
|---|
| 79 | char *cmd = lp_username_map_script();
|
|---|
| 80 |
|
|---|
| 81 | if (!*user)
|
|---|
| 82 | return false;
|
|---|
| 83 |
|
|---|
| 84 | if (strequal(user,get_last_to()))
|
|---|
| 85 | return false;
|
|---|
| 86 |
|
|---|
| 87 | if (strequal(user,get_last_from())) {
|
|---|
| 88 | DEBUG(3,("Mapped user %s to %s\n",user,get_last_to()));
|
|---|
| 89 | fstrcpy(user,get_last_to());
|
|---|
| 90 | return true;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /* first try the username map script */
|
|---|
| 94 |
|
|---|
| 95 | if ( *cmd ) {
|
|---|
| 96 | char **qlines;
|
|---|
| 97 | char *command = NULL;
|
|---|
| 98 | int numlines, ret, fd;
|
|---|
| 99 |
|
|---|
| 100 | command = talloc_asprintf(talloc_tos(),
|
|---|
| 101 | "%s \"%s\"",
|
|---|
| 102 | cmd,
|
|---|
| 103 | user);
|
|---|
| 104 | if (!command) {
|
|---|
| 105 | return false;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | DEBUG(10,("Running [%s]\n", command));
|
|---|
| 109 | ret = smbrun(command, &fd);
|
|---|
| 110 | DEBUGADD(10,("returned [%d]\n", ret));
|
|---|
| 111 |
|
|---|
| 112 | if ( ret != 0 ) {
|
|---|
| 113 | if (fd != -1)
|
|---|
| 114 | close(fd);
|
|---|
| 115 | return False;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | numlines = 0;
|
|---|
| 119 | qlines = fd_lines_load(fd, &numlines,0);
|
|---|
| 120 | DEBUGADD(10,("Lines returned = [%d]\n", numlines));
|
|---|
| 121 | close(fd);
|
|---|
| 122 |
|
|---|
| 123 | /* should be either no lines or a single line with the mapped username */
|
|---|
| 124 |
|
|---|
| 125 | if (numlines && qlines) {
|
|---|
| 126 | DEBUG(3,("Mapped user %s to %s\n", user, qlines[0] ));
|
|---|
| 127 | fstrcpy( user, qlines[0] );
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | file_lines_free(qlines);
|
|---|
| 131 |
|
|---|
| 132 | return numlines != 0;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /* ok. let's try the mapfile */
|
|---|
| 136 | if (!*mapfile)
|
|---|
| 137 | return False;
|
|---|
| 138 |
|
|---|
| 139 | f = x_fopen(mapfile,O_RDONLY, 0);
|
|---|
| 140 | if (!f) {
|
|---|
| 141 | DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
|
|---|
| 142 | return False;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | DEBUG(4,("Scanning username map %s\n",mapfile));
|
|---|
| 146 |
|
|---|
| 147 | while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
|
|---|
| 148 | char *unixname = s;
|
|---|
| 149 | char *dosname = strchr_m(unixname,'=');
|
|---|
| 150 | char **dosuserlist;
|
|---|
| 151 | bool return_if_mapped = False;
|
|---|
|
|---|