auth: Simplify is_our_machine_account()
[Samba.git] / source3 / auth / server_info_sam.c
blobb9c8b0a0959093eadd783c400d52c210094fbbb4
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 "nsswitch/winbind_client.h"
27 #include "passdb.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_AUTH
33 /***************************************************************************
34 Is the incoming username our own machine account ?
35 If so, the connection is almost certainly from winbindd.
36 ***************************************************************************/
38 static bool is_our_machine_account(const char *username)
40 bool ret;
41 size_t ulen = strlen(username);
43 if (ulen == 0 || username[ulen-1] != '$') {
44 return false;
46 ret = strnequal(username, lp_netbios_name(), ulen-1);
47 return ret;
50 /***************************************************************************
51 Make (and fill) a user_info struct from a struct samu
52 ***************************************************************************/
54 NTSTATUS make_server_info_sam(TALLOC_CTX *mem_ctx,
55 struct samu *sampass,
56 struct auth_serversupplied_info **pserver_info)
58 struct passwd *pwd;
59 struct auth_serversupplied_info *server_info;
60 const char *username = pdb_get_username(sampass);
61 TALLOC_CTX *tmp_ctx = talloc_stackframe();
62 NTSTATUS status;
64 server_info = make_server_info(tmp_ctx);
65 if (server_info == NULL) {
66 status = NT_STATUS_NO_MEMORY;
67 goto out;
70 pwd = Get_Pwnam_alloc(tmp_ctx, username);
71 if (pwd == NULL) {
72 DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
73 pdb_get_username(sampass)));
74 status = NT_STATUS_NO_SUCH_USER;
75 goto out;
78 status = samu_to_SamInfo3(server_info,
79 sampass,
80 lp_netbios_name(),
81 &server_info->info3,
82 &server_info->extra);
83 if (!NT_STATUS_IS_OK(status)) {
84 goto out;
87 server_info->unix_name = talloc_move(server_info, &pwd->pw_name);
89 server_info->utok.gid = pwd->pw_gid;
90 server_info->utok.uid = pwd->pw_uid;
92 if (IS_DC && is_our_machine_account(username)) {
94 * This is a hack of monstrous proportions.
95 * If we know it's winbindd talking to us,
96 * we know we must never recurse into it,
97 * so turn off contacting winbindd for this
98 * entire process. This will get fixed when
99 * winbindd doesn't need to talk to smbd on
100 * a PDC. JRA.
103 (void)winbind_off();
105 DEBUG(10, ("make_server_info_sam: our machine account %s "
106 "turning off winbindd requests.\n", username));
109 DEBUG(5,("make_server_info_sam: made server info for user %s -> %s\n",
110 pdb_get_username(sampass), server_info->unix_name));
112 *pserver_info = talloc_move(mem_ctx, &server_info);
114 status = NT_STATUS_OK;
115 out:
116 talloc_free(tmp_ctx);
118 return status;