summaryrefslogtreecommitdiff
path: root/source3/auth/auth_rhosts.c
blob: e310fa80fd5de1f452f04f519b0b624592fe828d (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* 
   Unix SMB/CIFS implementation.
   Main SMB reply routines
   Copyright (C) Andrew Tridgell 1992-1998
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_AUTH

/****************************************************************************
 Create a SAM_ACCOUNT - either by looking in the pdb, or by faking it up from
 unix info.
****************************************************************************/

static NTSTATUS auth_get_sam_account(const char *user, SAM_ACCOUNT **account) 
{
	BOOL pdb_ret;
	NTSTATUS nt_status;
	if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(account))) {
		return nt_status;
	}
	
	become_root();
	pdb_ret = pdb_getsampwnam(*account, user);
	unbecome_root();

	if (!pdb_ret) {
		
		struct passwd *pass = Get_Pwnam(user);
		if (!pass) 
			return NT_STATUS_NO_SUCH_USER;

		if (!NT_STATUS_IS_OK(nt_status = pdb_fill_sam_pw(*account, pass))) {
			return nt_status;
		}
	}
	return NT_STATUS_OK;
}

/****************************************************************************
 Read the a hosts.equiv or .rhosts file and check if it
 allows this user from this machine.
****************************************************************************/

static BOOL check_user_equiv(const char *user, const char *remote, const char *equiv_file)
{
	int plus_allowed = 1;
	char *file_host;
	char *file_user;
	char **lines = file_lines_load(equiv_file, NULL,0);
	int i;

	DEBUG(5, ("check_user_equiv %s %s %s\n", user, remote, equiv_file));
	if (! lines) {
		return False;
	}
	for (i=0; lines[i]; i++) {
		char *buf = lines[i];
		trim_char(buf,' ',' ');

		if (buf[0] != '#' && buf[0] != '\n') {
			BOOL is_group = False;
			int plus = 1;
			char *bp = buf;

			if (strcmp(buf, "NO_PLUS\n") == 0) {
				DEBUG(6, ("check_user_equiv NO_PLUS\n"));
				plus_allowed = 0;
			} else {
				if (buf[0] == '+') {
					bp++;
					if (*bp == '\n' && plus_allowed) {
						/* a bare plus means everbody allowed */
						DEBUG(6, ("check_user_equiv everybody allowed\n"));
						file_lines_free(lines);
						return True;
					}
				} else if (buf[0] == '-') {
					bp++;
					plus = 0;
				}
				if (*bp == '@') {
					is_group = True;
					bp++;
				}
				file_host = strtok(bp, " \t\n");
				file_user = strtok(NULL, " \t\n");
				DEBUG(7, ("check_user_equiv %s %s\n", file_host ? file_host : "(null)", 
					file_user ? file_user : "(null)" ));

				if (file_host && *file_host) {
					BOOL host_ok = False;

#if defined(HAVE_NETGROUP) && defined(HAVE_YP_GET_DEFAULT_DOMAIN)
					if (is_group) {
						static char *mydomain = NULL;
						if (!mydomain) {
							yp_get_default_domain(&mydomain);
						}
						if (mydomain && innetgr(file_host,remote,user,mydomain)) {
							host_ok = True;
						}
					}
#else
					if (is_group) {
						DEBUG(1,("Netgroups not configured\n"));
						continue;
					}
#endif

					/* is it this host */
					/* the fact that remote has come from a call of gethostbyaddr
					 * means that it may have the fully qualified domain name
					 * so we could look up the file version to get it into
					 * a canonical form, but I would rather just type it
					 * in full in the equiv file
					 */

					if (!host_ok && !is_group && strequal(remote, file_host)) {
						host_ok = True;
					}

					if (!host_ok) {
						continue;
					}

					/* is it this user */
					if (file_user == 0 || strequal(user, file_user)) {
						DEBUG(5, ("check_user_equiv matched %s%s %s\n",
							(plus ? "+" : "-"), file_host,
							(file_user ? file_user : "")));
						file_lines_free(lines);
						return (plus ? True : False);
					}
				}
			}
		}
	}

	file_lines_free(lines);
	return False;
}

/****************************************************************************
check for a possible hosts equiv or rhosts entry for the user
****************************************************************************/

static BOOL check_hosts_equiv(SAM_ACCOUNT *account)
{
	uid_t uid;
	char *fname = NULL;

	fname = lp_hosts_equiv();
	if (!sid_to_uid(pdb_get_user_sid(account), &uid))
		return False;

	/* note: don't allow hosts.equiv on root */
	if (fname && *fname && uid != 0) {
		if (check_user_equiv(pdb_get_username(account),client_name(),fname))
			return True;
	}
  
	return False;
}


/****************************************************************************
 Check for a valid .rhosts/hosts.equiv entry for this user
****************************************************************************/

static NTSTATUS check_hostsequiv_security(const struct auth_context *auth_context,
					  void *my_private_data, 
					  TALLOC_CTX *mem_ctx,
					  const auth_usersupplied_info *user_info, 
					  auth_serversupplied_info **server_info)
{
	NTSTATUS nt_status;
	SAM_ACCOUNT *account = NULL;
	if (!NT_STATUS_IS_OK(nt_status = 
			     auth_get_sam_account(user_info->internal_username,
						  &account))) {
		if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) 
			nt_status = NT_STATUS_NOT_IMPLEMENTED;
		return nt_status;
	}

	if (check_hosts_equiv(account)) {
		nt_status = make_server_info_sam(server_info, account);
		if (!NT_STATUS_IS_OK(nt_status)) {
			pdb_free_sam(&account);
		}
	} else {
		pdb_free_sam(&account);
		nt_status = NT_STATUS_NOT_IMPLEMENTED;
	}

	return nt_status;
}

/* module initialisation */
static NTSTATUS auth_init_hostsequiv(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
{
	if (!make_auth_methods(auth_context, auth_method)) {
		return NT_STATUS_NO_MEMORY;
	}

	(*auth_method)->auth = check_hostsequiv_security;
	(*auth_method)->name = "hostsequiv";
	return NT_STATUS_OK;
}


/****************************************************************************
 Check for a valid .rhosts/hosts.equiv entry for this user
****************************************************************************/

static NTSTATUS check_rhosts_security(const struct auth_context *auth_context,
				      void *my_private_data, 
				      TALLOC_CTX *mem_ctx,
				      const auth_usersupplied_info *user_info, 
				      auth_serversupplied_info **server_info)
{
	NTSTATUS nt_status;
	SAM_ACCOUNT *account = NULL;
	pstring rhostsfile;
	const char *home;
	
	if (!NT_STATUS_IS_OK(nt_status = 
			     auth_get_sam_account(user_info->internal_username,
						  &account))) {
		if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) 
			nt_status = NT_STATUS_NOT_IMPLEMENTED;
		return nt_status;
	}

	home = pdb_get_unix_homedir(account);

	if (home) {
		slprintf(rhostsfile, sizeof(rhostsfile)-1, "%s/.rhosts", home);
		become_root();
		if (check_user_equiv(pdb_get_username(account),client_name(),rhostsfile)) {
			nt_status = make_server_info_sam(server_info, account);
			if (!NT_STATUS_IS_OK(nt_status)) {
				pdb_free_sam(&account);
			}
		} else {
			pdb_free_sam(&account);
		}
		unbecome_root();
	} else {
		pdb_free_sam(&account);
		nt_status = NT_STATUS_NOT_IMPLEMENTED;
	}
	
	return nt_status;
}

/* module initialisation */
static NTSTATUS auth_init_rhosts(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
{
	if (!make_auth_methods(auth_context, auth_method)) {
		return NT_STATUS_NO_MEMORY;
	}

	(*auth_method)->auth = check_rhosts_security;
	(*auth_method)->name = "rhosts";
	return NT_STATUS_OK;
}

NTSTATUS auth_rhosts_init(void)
{
	smb_register_auth(AUTH_INTERFACE_VERSION, "rhosts", auth_init_rhosts);
	smb_register_auth(AUTH_INTERFACE_VERSION, "hostsequiv", auth_init_hostsequiv);
	return NT_STATUS_OK;
}