summaryrefslogtreecommitdiff
path: root/source3/utils/netlookup.c
blob: 315a89d5a8c0f3983f7dcd2cc6b2e5f9ad9c6506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
   Unix SMB/CIFS implementation.

   Name lookup.

   Copyright (C) Jeremy Allison 2005

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "utils/net.h"

/********************************************************
 Connection cachine struct. Goes away when ctx destroyed.
********************************************************/

struct con_struct {
	bool failed_connect;
	NTSTATUS err;
	struct cli_state *cli;
	struct rpc_pipe_client *lsapipe;
	POLICY_HND pol;
};

static struct con_struct *cs;

/********************************************************
 Close connection on context destruction.
********************************************************/

static int cs_destructor(struct con_struct *p)
{
	if (cs->cli) {
		cli_shutdown(cs->cli);
	}
	cs = NULL;
	return 0;
}

/********************************************************
 Create the connection to localhost.
********************************************************/

static struct con_struct *create_cs(TALLOC_CTX *ctx, NTSTATUS *perr)
{
	NTSTATUS nt_status;
	struct in_addr loopback_ip;

	loopback_ip.s_addr = htonl(INADDR_LOOPBACK);
	*perr = NT_STATUS_OK;

	if (cs) {
		if (cs->failed_connect) {
			*perr = cs->err;
			return NULL;
		}
		return cs;
	}

	cs = TALLOC_P(ctx, struct con_struct);
	if (!cs) {
		*perr = NT_STATUS_NO_MEMORY;
		return NULL;
	}

	ZERO_STRUCTP(cs);
	talloc_set_destructor(cs, cs_destructor);

	/* Connect to localhost with given username/password. */
	/* JRA. Pretty sure we can just do this anonymously.... */
#if 0
	if (!opt_password && !opt_machine_pass) {
		char *pass = getpass("Password:");
		if (pass) {
			opt_password = SMB_STRDUP(pass);
		}
	}
#endif

	nt_status = cli_full_connection(&cs->cli, global_myname(), global_myname(),
					&loopback_ip, 0,
					"IPC$", "IPC",
#if 0
					opt_user_name,
					opt_workgroup,
					opt_password,
#else
					"",
					opt_workgroup,
					"",
#endif
					0,
					Undefined,
					NULL);

	if (!NT_STATUS_IS_OK(nt_status)) {
		DEBUG(2,("create_cs: Connect failed. Error was %s\n", nt_errstr(nt_status)));
		cs->failed_connect = True;
		cs->err = nt_status;
		*perr = nt_status;
		return NULL;
	}

	cs->lsapipe = cli_rpc_pipe_open_noauth(cs->cli,
					PI_LSARPC,
					&nt_status);

	if (cs->lsapipe == NULL) {
		DEBUG(2,("create_cs: open LSA pipe failed. Error was %s\n", nt_errstr(nt_status)));
		cs->failed_connect = True;
		cs->err = nt_status;
		*perr = nt_status;
		return NULL;
	}

	nt_status = rpccli_lsa_open_policy(cs->lsapipe, ctx, True,
				SEC_RIGHTS_MAXIMUM_ALLOWED,
				&cs->pol);

	if (!NT_STATUS_IS_OK(nt_status)) {
		DEBUG(2,("create_cs: rpccli_lsa_open_policy failed. Error was %s\n", nt_errstr(nt_status)));
		cs->failed_connect = True;
		cs->err = nt_status;
		*perr = nt_status;
		return NULL;
	}

	return cs;
}

/********************************************************
 Do a lookup_sids call to localhost.
 Check if the local machine is authoritative for this sid. We can't
 check if this is our SID as that's stored in the root-read-only
 secrets.tdb.
 The local smbd will also ask winbindd for us, so we don't have to.
********************************************************/

NTSTATUS net_lookup_name_from_sid(TALLOC_CTX *ctx,
				DOM_SID *psid,
				const char **ppdomain,
				const char **ppname)
{
	NTSTATUS nt_status;
	struct con_struct *csp = NULL;
	char **domains;
	char **names;
	enum lsa_SidType *types;

	*ppdomain = NULL;
	*ppname = NULL;

	csp = create_cs(ctx, &nt_status);
	if (csp == NULL) {
		return nt_status;
	}

	nt_status = rpccli_lsa_lookup_sids(csp->lsapipe, ctx,
						&csp->pol,
						1, psid,
						&domains,
						&names,
						&types);

	if (!NT_STATUS_IS_OK(nt_status)) {
		return nt_status;
	}

	*ppdomain = domains[0];
	*ppname = names[0];
	/* Don't care about type here. */

        /* Converted OK */
        return NT_STATUS_OK;
}

/********************************************************
 Do a lookup_names call to localhost.
********************************************************/

NTSTATUS net_lookup_sid_from_name(TALLOC_CTX *ctx, const char *full_name, DOM_SID *pret_sid)
{
	NTSTATUS nt_status;
	struct con_struct *csp = NULL;
	DOM_SID *sids = NULL;
	enum lsa_SidType *types = NULL;

	csp = create_cs(ctx, &nt_status);
	if (csp == NULL) {
		return nt_status;
	}

	nt_status = rpccli_lsa_lookup_names(csp->lsapipe, ctx,
						&csp->pol,
						1,
						&full_name,
					        NULL, 1,
						&sids, &types);

	if (!NT_STATUS_IS_OK(nt_status)) {
		return nt_status;
	}

	*pret_sid = sids[0];

        /* Converted OK */
        return NT_STATUS_OK;
}