libsmbconf: Convert smbconf_transaction_*() to sbcErr.
[Samba.git] / source3 / auth / server_info_sam.c
blob5fd78a30fa7eafe3aa61713ee660d0230e9d83b4
1 /*
2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001
6 Copyright (C) Jeremy Allison 2000-2001
7 Copyright (C) Rafal Szczesniak 2002
8 Copyright (C) Volker Lendecke 2006
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "auth.h"
26 #include "smbd/globals.h"
27 #include "../libcli/auth/libcli_auth.h"
28 #include "nsswitch/winbind_client.h"
29 #include "passdb.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_AUTH
35 /***************************************************************************
36 Is the incoming username our own machine account ?
37 If so, the connection is almost certainly from winbindd.
38 ***************************************************************************/
40 static bool is_our_machine_account(const char *username)
42 bool ret;
43 char *truncname = NULL;
44 size_t ulen = strlen(username);
46 if (ulen == 0 || username[ulen-1] != '$') {
47 return false;
49 truncname = SMB_STRDUP(username);
50 if (!truncname) {
51 return false;
53 truncname[ulen-1] = '\0';
54 ret = strequal(truncname, global_myname());
55 SAFE_FREE(truncname);
56 return ret;
59 /***************************************************************************
60 Make (and fill) a user_info struct from a struct samu
61 ***************************************************************************/
63 NTSTATUS make_server_info_sam(struct auth_serversupplied_info **server_info,
64 struct samu *sampass)
66 struct passwd *pwd;
67 struct auth_serversupplied_info *result;
68 const char *username = pdb_get_username(sampass);
69 NTSTATUS status;
71 if ( !(result = make_server_info(NULL)) ) {
72 return NT_STATUS_NO_MEMORY;
75 if ( !(pwd = Get_Pwnam_alloc(result, username)) ) {
76 DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
77 pdb_get_username(sampass)));
78 TALLOC_FREE(result);
79 return NT_STATUS_NO_SUCH_USER;
82 status = samu_to_SamInfo3(result, sampass, global_myname(),
83 &result->info3, &result->extra);
84 if (!NT_STATUS_IS_OK(status)) {
85 TALLOC_FREE(result);
86 return status;
89 result->unix_name = pwd->pw_name;
90 /* Ensure that we keep pwd->pw_name, because we will free pwd below */
91 talloc_steal(result, pwd->pw_name);
92 result->utok.gid = pwd->pw_gid;
93 result->utok.uid = pwd->pw_uid;
95 TALLOC_FREE(pwd);
97 result->sanitized_username = sanitize_username(result,
98 result->unix_name);
99 if (result->sanitized_username == NULL) {
100 TALLOC_FREE(result);
101 return NT_STATUS_NO_MEMORY;
104 if (IS_DC && is_our_machine_account(username)) {
106 * This is a hack of monstrous proportions.
107 * If we know it's winbindd talking to us,
108 * we know we must never recurse into it,
109 * so turn off contacting winbindd for this
110 * entire process. This will get fixed when
111 * winbindd doesn't need to talk to smbd on
112 * a PDC. JRA.
115 (void)winbind_off();
117 DEBUG(10, ("make_server_info_sam: our machine account %s "
118 "turning off winbindd requests.\n", username));
121 DEBUG(5,("make_server_info_sam: made server info for user %s -> %s\n",
122 pdb_get_username(sampass), result->unix_name));
124 *server_info = result;
126 return NT_STATUS_OK;