3 * Unix SMB/Netbios implementation.
5 * RPC Pipe client / server routines
6 * Copyright (C) Andrew Tridgell 1992-1998
7 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
8 * Copyright (C) Paul Ashton 1997-1998.
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 2 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, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* this module apparently provides an implementation of DCE/RPC over a
26 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
27 * documentation are available (in on-line form) from the X-Open group.
29 * this module should provide a level of abstraction between SMB
30 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
31 * data copies, and network traffic.
33 * in this version, which takes a "let's learn what's going on and
34 * get something running" approach, there is additional network
35 * traffic generated, but the code should be easier to understand...
37 * ... if you read the docs. or stare at packets for weeks on end.
44 extern int DEBUGLEVEL;
45 extern DOM_SID global_machine_sid;
48 * A list of the rids of well known BUILTIN and Domain users
52 rid_name builtin_alias_rids[] =
54 { BUILTIN_ALIAS_RID_ADMINS , "Administrators" },
55 { BUILTIN_ALIAS_RID_USERS , "Users" },
56 { BUILTIN_ALIAS_RID_GUESTS , "Guests" },
57 { BUILTIN_ALIAS_RID_POWER_USERS , "Power Users" },
59 { BUILTIN_ALIAS_RID_ACCOUNT_OPS , "Account Operators" },
60 { BUILTIN_ALIAS_RID_SYSTEM_OPS , "System Operators" },
61 { BUILTIN_ALIAS_RID_PRINT_OPS , "Print Operators" },
62 { BUILTIN_ALIAS_RID_BACKUP_OPS , "Backup Operators" },
63 { BUILTIN_ALIAS_RID_REPLICATOR , "Replicator" },
67 /* array lookup of well-known Domain RID users. */
68 rid_name domain_user_rids[] =
70 { DOMAIN_USER_RID_ADMIN , "Administrator" },
71 { DOMAIN_USER_RID_GUEST , "Guest" },
75 /* array lookup of well-known Domain RID groups. */
76 rid_name domain_group_rids[] =
78 { DOMAIN_GROUP_RID_ADMINS , "Domain Admins" },
79 { DOMAIN_GROUP_RID_USERS , "Domain Users" },
80 { DOMAIN_GROUP_RID_GUESTS , "Domain Guests" },
84 /**************************************************************************
85 Check if a name matches any of the well known SID values.
86 ***************************************************************************/
88 BOOL lookup_wellknown_sid_from_name(char *windows_name, DOM_SID *psid)
93 for( i = 0; builtin_alias_rids[i].name != NULL; i++) {
94 rnp = &builtin_alias_rids[i];
95 if(strequal(rnp->name, windows_name)) {
96 string_to_sid( psid, "S-1-5-32" );
97 SMB_ASSERT_ARRAY(psid->sub_auths, psid->num_auths+1);
98 psid->sub_auths[psid->num_auths++] = rnp->rid;
103 for( i = 0; domain_user_rids[i].name != NULL; i++ ) {
104 rnp = &domain_user_rids[i];
105 if(strequal(rnp->name, windows_name)) {
106 *psid = global_machine_sid;
107 SMB_ASSERT_ARRAY(psid->sub_auths, psid->num_auths+1);
108 psid->sub_auths[psid->num_auths++] = rnp->rid;
113 for( i = 0; domain_group_rids[i].name != NULL; i++ ) {
114 rnp = &domain_group_rids[i];
115 if(strequal(rnp->name, windows_name)) {
116 *psid = global_machine_sid;
117 SMB_ASSERT_ARRAY(psid->sub_auths, psid->num_auths+1);
118 psid->sub_auths[psid->num_auths++] = rnp->rid;
126 int make_dom_gids(char *gids_str, DOM_GID **ppgids)
135 DEBUG(4,("make_dom_gids: %s\n", gids_str));
137 if (gids_str == NULL || *gids_str == 0)
140 for (count = 0, ptr = gids_str;
141 next_token(&ptr, s2, NULL, sizeof(s2));
145 gids = (DOM_GID *)malloc( sizeof(DOM_GID) * count );
148 DEBUG(0,("make_dom_gids: malloc fail !\n"));
152 for (count = 0, ptr = gids_str;
153 next_token(&ptr, s2, NULL, sizeof(s2)) &&
154 count < LSA_MAX_GROUPS;
157 /* the entries are of the form GID/ATTR, ATTR being optional.*/
162 attr = strchr(s2,'/');
167 attr = "7"; /* default value for attribute is 7 */
169 /* look up the RID string and see if we can turn it into a rid number */
170 for (i = 0; builtin_alias_rids[i].name != NULL; i++)
172 if (strequal(builtin_alias_rids[i].name, s2))
174 rid = builtin_alias_rids[i].rid;
184 DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n", s2, attr));
189 gids[count].g_rid = rid;
190 gids[count].attr = atoi(attr);
192 DEBUG(5,("group id: %d attr: %d\n", gids[count].g_rid, gids[count].attr));
200 /*******************************************************************
201 turns a DCE/RPC request into a DCE/RPC reply
203 this is where the data really should be split up into an array of
204 headers and data sections.
206 ********************************************************************/
207 BOOL create_rpc_reply(pipes_struct *p,
208 uint32 data_start, uint32 data_end)
210 DEBUG(5,("create_rpc_reply: data_start: %d data_end: %d max_tsize: %d\n",
211 data_start, data_end, p->hdr_ba.bba.max_tsize));
213 mem_buf_init(&(p->rhdr.data), 0);
214 mem_alloc_data(p->rhdr.data, 0x18);
219 p->hdr_resp.alloc_hint = data_end - data_start; /* calculate remaining data to be sent */
220 p->hdr.pkt_type = RPC_RESPONSE; /* mark header as an rpc response */
222 /* set up rpc header (fragmentation issues) */
225 p->hdr.flags = RPC_FLG_FIRST;
232 if (p->hdr_resp.alloc_hint + 0x18 <= p->hdr_ba.bba.max_tsize)
234 p->hdr.flags |= RPC_FLG_LAST;
235 p->hdr.frag_len = p->hdr_resp.alloc_hint + 0x18;
239 p->hdr.frag_len = p->hdr_ba.bba.max_tsize;
242 p->rhdr.data->offset.start = 0;
243 p->rhdr.data->offset.end = 0x18;
245 /* store the header in the data stream */
247 smb_io_rpc_hdr ("hdr", &(p->hdr ), &(p->rhdr), 0);
248 smb_io_rpc_hdr_resp("resp", &(p->hdr_resp), &(p->rhdr), 0);
250 return p->rhdr.data != NULL && p->rhdr.offset == 0x18;
254 /*******************************************************************
255 receives a netlogon pipe and responds.
256 ********************************************************************/
257 static BOOL api_rpc_command(pipes_struct *p,
258 char *rpc_name, struct api_struct *api_rpc_cmds,
262 DEBUG(4,("api_rpc_command: %s op 0x%x - ", rpc_name, p->hdr_req.opnum));
264 for (fn_num = 0; api_rpc_cmds[fn_num].name; fn_num++)
266 if (api_rpc_cmds[fn_num].opnum == p->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL)
268 DEBUG(3,("api_rpc_command: %s\n", api_rpc_cmds[fn_num].name));
273 if (api_rpc_cmds[fn_num].name == NULL)
275 DEBUG(4, ("unknown\n"));
279 /* start off with 1024 bytes, and a large safety margin too */
280 mem_buf_init(&(p->rdata.data), SAFETY_MARGIN);
281 mem_alloc_data(p->rdata.data, 1024);
286 p->rdata.data->offset.start = 0;
287 p->rdata.data->offset.end = 0xffffffff;
289 /* do the actual command */
291 api_rpc_cmds[fn_num].fn(p->uid, data, &(p->rdata));
293 if (p->rdata.data == NULL || p->rdata.offset == 0)
295 mem_free_data(p->rdata.data);
299 mem_realloc_data(p->rdata.data, p->rdata.offset);
301 DEBUG(10,("called %s\n", rpc_name));
307 /*******************************************************************
308 receives a netlogon pipe and responds.
309 ********************************************************************/
310 BOOL api_rpcTNP(pipes_struct *p, char *rpc_name, struct api_struct *api_rpc_cmds,
313 if (data == NULL || data->data == NULL)
315 DEBUG(2,("%s: NULL data received\n", rpc_name));
319 /* read the rpc header */
320 smb_io_rpc_hdr_req("req", &(p->hdr_req), data, 0);
322 /* interpret the command */
323 if (!api_rpc_command(p, rpc_name, api_rpc_cmds, data))
328 /* create the rpc header */
329 if (!create_rpc_reply(p, 0, p->rdata.offset))
334 p->frag_len_left = p->hdr.frag_len - p->file_offset;
335 p->next_frag_start = p->hdr.frag_len;
337 /* set up the data chain */
338 p->rhdr.data->offset.start = 0;
339 p->rhdr.data->offset.end = p->rhdr.offset;
340 p->rhdr.data->next = p->rdata.data;
342 p->rdata.data->offset.start = p->rhdr.data->offset.end;
343 p->rdata.data->offset.end = p->rhdr.data->offset.end + p->rdata.offset;
344 p->rdata.data->next = NULL;
350 /*******************************************************************
351 gets a domain user's groups
352 ********************************************************************/
353 void get_domain_user_groups(char *domain_groups, char *user)
357 if (domain_groups == NULL || user == NULL) return;
359 /* any additional groups this user is in. e.g power users */
360 pstrcpy(domain_groups, lp_domain_groups());
362 /* can only be a user or a guest. cannot be guest _and_ admin */
363 if (user_in_list(user, lp_domain_guest_group()))
365 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
366 pstrcat(domain_groups, tmp);
368 DEBUG(3,("domain guest group access %s granted\n", tmp));
372 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
373 pstrcat(domain_groups, tmp);
375 DEBUG(3,("domain group access %s granted\n", tmp));
377 if (user_in_list(user, lp_domain_admin_group()))
379 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
380 pstrcat(domain_groups, tmp);
382 DEBUG(3,("domain admin group access %s granted\n", tmp));
388 /*******************************************************************
390 ********************************************************************/
391 uint32 lookup_group_name(uint32 rid, char *group_name, uint32 *type)
394 (*type) = SID_NAME_DOM_GRP;
396 DEBUG(5,("lookup_group_name: rid: %d", rid));
398 while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
403 if (domain_group_rids[i].rid != 0)
405 fstrcpy(group_name, domain_group_rids[i].name);
406 DEBUG(5,(" = %s\n", group_name));
410 DEBUG(5,(" none mapped\n"));
411 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
414 /*******************************************************************
416 ********************************************************************/
417 uint32 lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
420 (*type) = SID_NAME_WKN_GRP;
422 DEBUG(5,("lookup_alias_name: rid: %d", rid));
424 while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
429 if (builtin_alias_rids[i].rid != 0)
431 fstrcpy(alias_name, builtin_alias_rids[i].name);
432 DEBUG(5,(" = %s\n", alias_name));
436 DEBUG(5,(" none mapped\n"));
437 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
440 /*******************************************************************
442 ********************************************************************/
443 uint32 lookup_user_name(uint32 rid, char *user_name, uint32 *type)
445 struct sam_disp_info *disp_info;
447 (*type) = SID_NAME_USER;
449 DEBUG(5,("lookup_user_name: rid: %d", rid));
451 /* look up the well-known domain user rids first */
452 while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
457 if (domain_user_rids[i].rid != 0)
459 fstrcpy(user_name, domain_user_rids[i].name);
460 DEBUG(5,(" = %s\n", user_name));
464 /* ok, it's a user. find the user account */
466 disp_info = getsamdisprid(rid);
469 if (disp_info != NULL)
471 fstrcpy(user_name, disp_info->smb_name);
472 DEBUG(5,(" = %s\n", user_name));
476 DEBUG(5,(" none mapped\n"));
477 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
480 /*******************************************************************
482 ********************************************************************/
483 uint32 lookup_group_rid(char *group_name, uint32 *rid)
486 int i = -1; /* start do loop at -1 */
488 do /* find, if it exists, a group rid for the group name*/
491 (*rid) = domain_group_rids[i].rid;
492 grp_name = domain_group_rids[i].name;
494 } while (grp_name != NULL && !strequal(grp_name, group_name));
496 return (grp_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
499 /*******************************************************************
501 ********************************************************************/
502 uint32 lookup_alias_rid(char *alias_name, uint32 *rid)
505 int i = -1; /* start do loop at -1 */
507 do /* find, if it exists, a alias rid for the alias name*/
510 (*rid) = builtin_alias_rids[i].rid;
511 als_name = builtin_alias_rids[i].name;
513 } while (als_name != NULL && !strequal(als_name, alias_name));
515 return (als_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
518 /*******************************************************************
520 ********************************************************************/
521 uint32 lookup_user_rid(char *user_name, uint32 *rid)
523 struct smb_passwd *smb_pass;
526 /* find the user account */
528 smb_pass = getsmbpwnam(user_name);
531 if (smb_pass != NULL)
533 /* lkclXXXX SHOULD use name_to_rid() here! */
534 (*rid) = smb_pass->smb_userid;
538 return 0xC0000000 | NT_STATUS_NONE_MAPPED;