summaryrefslogtreecommitdiff
path: root/source3/rpc_server/srv_lookup.c
blob: fc73edc4edb7d97167c1f3de7eecab62a56dd872 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589

/* 
 *  Unix SMB/Netbios implementation.
 *  Version 1.9.
 *  RPC Pipe client / server routines
 *  Copyright (C) Andrew Tridgell              1992-1998
 *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
 *  Copyright (C) Paul Ashton                  1997-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.
 */

/*
 *
 *

 this module provides nt user / nt rid lookup functions.
 users, local groups, domain groups.

 no unix / samba functions should be called in this module:
 it should purely provide a gateway to the password database API,
 the local group database API or the domain group database API,
 but first checking built-in rids.

 did i say rids?  oops, what about "S-1-1" the "Everyone" group
 and other such well-known sids...

 speed is not of the essence: no particular optimisation is in place.

 *
 *
 */

#include "includes.h"
#include "nterr.h"

extern int DEBUGLEVEL;

extern fstring global_sam_name;
extern DOM_SID global_sam_sid;
extern DOM_SID global_sid_S_1_5_20;

extern rid_name builtin_alias_rids[];
extern rid_name domain_user_rids[];
extern rid_name domain_group_rids[];

int make_dom_gids(DOMAIN_GRP *mem, int num_members, DOM_GID **ppgids)
{
	int count;
	int i;
	DOM_GID *gids = NULL;

	*ppgids = NULL;

	DEBUG(4,("make_dom_gids: %d\n", num_members));

	if (mem == NULL || num_members == 0)
	{
		return 0;
	}

	for (i = 0, count = 0; i < num_members && count < LSA_MAX_GROUPS; i++) 
	{
		uint32 status;

		uint32 rid;
		DOM_SID sid;
		uint8  type;

		uint8  attr  = mem[count].attr;
		char   *name = mem[count].name;

		become_root(True);
		status = lookup_name(name, &sid, &type);
		unbecome_root(True);

		if (status == 0x0 && !sid_front_equal(&global_sam_sid, &sid))
		{
			fstring sid_str;
			sid_to_string(sid_str, &sid);
			DEBUG(1,("make_dom_gids: unknown sid %s for groupname %s\n",
			          sid_str, name));
		}
		else if (status == 0x0)
		{
			sid_split_rid(&sid, &rid);

			gids = (DOM_GID *)Realloc( gids, sizeof(DOM_GID) * (count+1) );

			if (gids == NULL)
			{
				DEBUG(0,("make_dom_gids: Realloc fail !\n"));
				return 0;
			}

			gids[count].g_rid = rid;
			gids[count].attr  = attr;

			DEBUG(5,("group name: %s rid: %d attr: %d\n",
			          name, rid, attr));
			count++;
		}
		else
		{
			DEBUG(1,("make_dom_gids: unknown groupname %s\n", name));
		}
	}

	*ppgids = gids;
	return count;
}

/*******************************************************************
 gets a domain user's groups
 ********************************************************************/
int get_domain_user_groups(DOMAIN_GRP_MEMBER **grp_members, uint32 group_rid)
{
	DOMAIN_GRP *grp;
	int num_mem;

	if (grp_members == NULL) return 0;

	grp = getgrouprid(group_rid, grp_members, &num_mem);

	if (grp == NULL)
	{
		return 0;
	}

	return num_mem;
}


/*******************************************************************
 lookup_builtin_sid
 ********************************************************************/
uint32 lookup_builtin_sid(DOM_SID *sid, char *name, uint8 *type)
{
	uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;

	status = (status != 0x0) ? lookup_wk_user_sid (sid, name, type) : status;
	status = (status != 0x0) ? lookup_wk_group_sid(sid, name, type) : status;
	status = (status != 0x0) ? lookup_wk_alias_sid(sid, name, type) : status;

	return status;
}


/*******************************************************************
 lookup_added_sid - names that have been added to the SAM database by admins.
 ********************************************************************/
uint32 lookup_added_sid(DOM_SID *sid, char *name, uint8 *type)
{
	uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;

	status = (status != 0x0) ? lookup_user_sid (sid, name, type) : status;
	status = (status != 0x0) ? lookup_group_sid(sid, name, type) : status;
	status = (status != 0x0) ? lookup_alias_sid(sid, name, type) : status;

	return status;
}


/*******************************************************************
 lookup_sid
 ********************************************************************/
uint32 lookup_sid(DOM_SID *sid, char *name, uint8 *type)
{
	uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;

	status = (status != 0x0) ? lookup_builtin_sid(sid, name, type) : status;
	status = (status != 0x0) ? lookup_added_sid   (sid, name, type) : status;

	return status;
}


/*******************************************************************
 lookup_wk_group_sid
 ********************************************************************/
uint32 lookup_wk_group_sid(DOM_SID *sid, char *group_name, uint8 *type)
{
	int i = 0; 
	uint32 rid;
	DOM_SID tmp;

	(*type) = SID_NAME_WKN_GRP;

	sid_copy(&tmp, sid);
	sid_split_rid(&tmp, &rid);

	if (!sid_equal(&global_sid_S_1_5_20, &tmp))
	{
		return 0xC0000000 | NT_STATUS_NONE_MAPPED;
	}

	DEBUG(5,("lookup_wk_group_sid: rid: %d", rid));

	while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
	{
		i++;
	}

	if (domain_group_rids[i].rid != 0)
	{
		fstrcpy(group_name, domain_group_rids[i].name);
		DEBUG(5,(" = %s\n", group_name));
		return 0x0;
	}

	DEBUG(5,(" none mapped\n"));
	return 0xC0000000 | NT_STATUS_NONE_MAPPED;
}

/*******************************************************************
 lookup_group_sid
 ********************************************************************/
uint32 lookup_group_sid(DOM_SID *sid, char *group_name, uint8 *type)
{
	pstring sid_str;
	uint32 rid;
	DOM_SID tmp;
	DOMAIN_GRP *grp = NULL;
	uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;

	(*type) = SID_NAME_DOM_GRP;

	sid_to_string(sid_str, sid);
	DEBUG(5,("lookup_group_sid: sid: %s", sid_str));

	sid_copy(&tmp, sid);
	sid_split_rid(&tmp, &rid);

	if (!sid_equal(&global_sam_sid, &tmp))
	{
		DEBUG(5,("not our SID\n"));
		return 0xC0000000 | NT_STATUS_NONE_MAPPED;
	}

	grp = getgrouprid(rid, NULL, NULL);

	if (grp != NULL)
	{
		fstrcpy(group_name, grp->name);
		DEBUG(5,(" = %s\n", group_name));
		return 0x0;
	}

	DEBUG(5,(" none mapped\n"));
	return status;
}

/*******************************************************************
 lookup_wk_alias_sid
 ********************************************************************/
uint32 lookup_wk_alias_sid(DOM_SID *sid, char *alias_name, uint8 *type)
{
	int i = 0; 
	uint32 rid;
	DOM_SID tmp;

	(*type) = SID_NAME_ALIAS;

	sid_copy(&tmp, sid);
	sid_split_rid(&tmp, &rid);

	if (!sid_equal(&global_sid_S_1_5_20, &tmp))
	{
		return 0xC0000000 | NT_STATUS_NONE_MAPPED;
	}

	DEBUG(5,("lookup_wk_alias_sid: rid: %d", rid));

	while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
	{
		i++;
	}

	if (builtin_alias_rids[i].rid != 0)
	{
		fstrcpy(alias_name, builtin_alias_rids[i].name);
		DEBUG(5,(" = %s\n", alias_name));
		return 0x0;
	}

	DEBUG(5,(" none mapped\n"));
	return 0xC0000000 | NT_STATUS_NONE_MAPPED;
}

/*******************************************************************
 lookup_alias_sid
 ********************************************************************/
uint32 lookup_alias_sid(DOM_SID *sid, char *alias_name, uint8 *type)
{
	pstring sid_str;
	uint32 rid;
	DOM_SID tmp;
	LOCAL_GRP *als = NULL;
	uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;

	(*type) = SID_NAME_ALIAS;

	sid_to_string(sid_str, sid);
	DEBUG(5,("lookup_alias_sid: sid: %s", sid_str));

	sid_copy(&tmp, sid);
	sid_split_rid(&tmp, &rid);

	if (!sid_equal(&global_sam_sid, &tmp))
	{
		DEBUG(5,("not our SID\n"));
		return 0xC0000000 | NT_STATUS_NONE_MAPPED;
	}

	als = getaliasrid(rid, NULL, NULL);

	if (als != NULL)
	{
		fstrcpy(alias_name, als->name);
		DEBUG(5,(" = %s\n", alias_name));
		return 0x0;
	}

	DEBUG(5,(" none mapped\n"));
	return status;
}

/*******************************************************************
 lookup well-known user name
 ********************************************************************/
uint32 lookup_wk_user_sid(DOM_SID *sid, char *user_name, uint8 *type)
{
	int i = 0;
	uint32 rid;
	DOM_SID tmp;

	(*type) = SID_NAME_USER;

	sid_copy(&tmp, sid);
	sid_split_rid(&tmp, &rid);

	if (!sid_equal(&global_sid_S_1_5_20, &tmp))
	{
		return 0xC0000000 | NT_STATUS_NONE_MAPPED;
	}

	DEBUG(5,("lookup_wk_user_sid: rid: %d", rid));

	/* look up the well-known domain user rids first */
	while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
	{
		i++;
	}

	if (domain_user_rids[i].rid != 0)
	{
		fstrcpy(user_name, domain_user_rids[i].name);
		DEBUG(5,(" = %s\n", user_name));
		return 0x0;
	}

	DEBUG(5,(" none mapped\n"));
	return 0xC0000000 | NT_STATUS_NONE_MAPPED;
}

/*******************************************************************
 lookup user name
 ********************************************************************/
uint32 lookup_user_sid(DOM_SID *sid, char *user_name, uint8 *type)
{
	struct sam_disp_info *disp_info;
	uint32 rid;
	DOM_SID tmp;

	(*type) = SID_NAME_USER;

	sid_copy(&tmp, sid);
	sid_split_rid(&tmp, &rid);

	if (sid_equal(&global_sam_sid, &tmp))
	{
		DEBUG(5,("lookup_user_sid in SAM %s: rid: %d",
		          global_sam_name, rid));

		/* find the user account */
		become_root(True);
		disp_info = getsamdisprid(rid);
		unbecome_root(True);

		if (disp_info != NULL)
		{
			fstrcpy(user_name, disp_info->nt_name);
			DEBUG(5,(" = %s\n", user_name));
			return 0x0;
		}

		DEBUG(5,(" none mapped\n"));
	}

	return 0xC0000000 | NT_STATUS_NONE_MAPPED;
}

/*******************************************************************
 lookup_group_rid
 ********************************************************************/
uint32 lookup_added_group_name(const char *grp_name, const char *domain,
				DOM_SID *sid, uint8 *type)
{
	DOMAIN_GRP *grp = NULL;
	(*type) = SID_NAME_DOM_GRP;

	DEBUG(5,("lookup_added_group_name: name: %s", grp_name));

	if (!strequal(domain, global_sam_name))
	{
		DEBUG(5,(" not our domain\n"));
		return 0xC0000000 | NT_STATUS_NONE_MAPPED;
	}

	grp = getgroupntnam(grp_name, NULL, NULL);

	if (grp != NULL)
	{
		sid_copy(sid, &global_sam_sid);
		sid_append_rid(sid, grp->rid);

		DEBUG(5,(" = 0x%x\n", grp->rid));
		return 0x0;
	}

	DEBUG(5,(" none mapped\n"));
	return 0xC0000000 | NT_STATUS_NONE_MAPPED;
}

/*******************************************************************
 lookup_added_alias_name
 ********************************************************************/
uint32 lookup_added_alias_name(const char *als_name, const char *domain,
				DOM_SID *sid, uint8 *type)
{
	LOCAL_GRP *als = NULL;
	(*type) = SID_NAME_ALIAS;

	DEBUG(5,("lookup_added_alias_name: name: %s\%s", domain, als_name));

	if (!strequal(domain, global_sam_name))
	{
		DEBUG(5,(" not our domain\n"));
		return 0xC0000000 | NT_STATUS_NONE_MAPPED;
	}

	als = getaliasntnam(als_name, NULL, NULL);

	if (als != NULL)
	{
		sid_copy(sid, &global_sam_sid);
		sid_append_rid(sid, als->rid);

		DEBUG(5,(" = 0x%x\n", als->rid));
		return 0x0;
	}

	DEBUG(5,(" none mapped\n"));
	return 0xC0000000 | NT_STATUS_NONE_MAPPED;
}

/*******************************************************************
 lookup_added_user_rid
 ********************************************************************/
uint32 lookup_added_user_rids(char *nt_name,
		uint32 *usr_rid, uint32 *grp_rid)
{
	struct sam_passwd *sam_pass;
	(*usr_rid) = 0;
	(*grp_rid) = 0;

	/* find the user account */
	become_root(True);
	sam_pass = getsam21pwntnam(nt_name);
	unbecome_root(True);

	if (sam_pass != NULL)
	{
		(*usr_rid) = sam_pass->user_rid ;
		(*grp_rid) = sam_pass->group_rid;
		return 0x0;
	}

	return 0xC0000000 | NT_STATUS_NONE_MAPPED;
}

/*******************************************************************
 lookup_added_user_name
 ********************************************************************/
static uint32 lookup_added_user_name(const char *nt_name, const char *domain,
				DOM_SID *sid, uint8 *type)
{
	struct sam_passwd *sam_pass;
	(*type) = SID_NAME_USER;

	if (!strequal(domain, global_sam_name))
	{
		return 0xC0000000 | NT_STATUS_NONE_MAPPED;
	}

	/* find the user account */
	become_root(True);
	sam_pass = getsam21pwntnam(nt_name);
	unbecome_root(True);

	if (sam_pass != NULL)
	{
		sid_copy(sid, &global_sam_sid);
		sid_append_rid(sid, sam_pass->user_rid);

		return 0x0;
	}

	return 0xC0000000 | NT_STATUS_NONE_MAPPED;
}

/*******************************************************************
 lookup_grp_name
 ********************************************************************/
static uint32 lookup_grp_name(const char *name, const char *domain,
				DOM_SID *sid, uint8 *type)
{
	uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;

	status = (status != 0x0) ? lookup_wk_group_name     (name, domain, sid, type) : status;
	status = (status != 0x0) ? lookup_builtin_alias_name(name, domain, sid, type) : status;
	status = (status != 0x0) ? lookup_added_group_name  (name, domain, sid, type) : status;
	status = (status != 0x0) ? lookup_added_alias_name  (name, domain, sid, type) : status;

	return status;
}

/*******************************************************************
 lookup_user_name
 ********************************************************************/
static uint32 lookup_user_name(const char *name, const char *domain,
				DOM_SID *sid, uint8 *type)
{
	uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;

	status = (status != 0x0) ? lookup_wk_user_name   (name, domain, sid, type) : status;
	status = (status != 0x0) ? lookup_added_user_name(name, domain, sid, type) : status;

	return status;
}

/*******************************************************************
 lookup_name
 ********************************************************************/
uint32 lookup_name(char *name, DOM_SID *sid, uint8 *type)
{
	uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
	fstring domain;
	fstring user;
	
	split_domain_name(name, domain, user);

	if (!strequal(domain, global_sam_name))
	{
		DEBUG(0,("lookup_name: remote domain %s not supported\n", domain));
		return status;
	}

	status = (status != 0x0) ? lookup_user_name    (name, domain, sid, type) : status;
	status = (status != 0x0) ? lookup_grp_name     (name, domain, sid, type) : status;
#if 0
	status = (status != 0x0) ? lookup_domain_name  (domain, sid, type) : status;
#endif

	return status;
}