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 int make_dom_gids(char *gids_str, DOM_GID **ppgids)
93 DEBUG(4,("make_dom_gids: %s\n", gids_str));
95 if (gids_str == NULL || *gids_str == 0)
98 for (count = 0, ptr = gids_str;
99 next_token(&ptr, s2, NULL, sizeof(s2));
103 gids = (DOM_GID *)malloc( sizeof(DOM_GID) * count );
106 DEBUG(0,("make_dom_gids: malloc fail !\n"));
110 for (count = 0, ptr = gids_str;
111 next_token(&ptr, s2, NULL, sizeof(s2)) &&
112 count < LSA_MAX_GROUPS;
115 /* the entries are of the form GID/ATTR, ATTR being optional.*/
120 attr = strchr(s2,'/');
125 attr = "7"; /* default value for attribute is 7 */
127 /* look up the RID string and see if we can turn it into a rid number */
128 for (i = 0; builtin_alias_rids[i].name != NULL; i++)
130 if (strequal(builtin_alias_rids[i].name, s2))
132 rid = builtin_alias_rids[i].rid;
142 DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n", s2, attr));
147 gids[count].g_rid = rid;
148 gids[count].attr = atoi(attr);
150 DEBUG(5,("group id: %d attr: %d\n", gids[count].g_rid, gids[count].attr));
158 /*******************************************************************
159 turns a DCE/RPC request into a DCE/RPC reply
161 this is where the data really should be split up into an array of
162 headers and data sections.
164 ********************************************************************/
165 BOOL create_rpc_reply(pipes_struct *p,
166 uint32 data_start, uint32 data_end)
168 DEBUG(5,("create_rpc_reply: data_start: %d data_end: %d max_tsize: %d\n",
169 data_start, data_end, p->hdr_ba.bba.max_tsize));
171 mem_buf_init(&(p->rhdr.data), 0);
172 mem_alloc_data(p->rhdr.data, 0x18);
177 p->hdr_resp.alloc_hint = data_end - data_start; /* calculate remaining data to be sent */
178 p->hdr.pkt_type = RPC_RESPONSE; /* mark header as an rpc response */
180 /* set up rpc header (fragmentation issues) */
183 p->hdr.flags = RPC_FLG_FIRST;
190 if (p->hdr_resp.alloc_hint + 0x18 <= p->hdr_ba.bba.max_tsize)
192 p->hdr.flags |= RPC_FLG_LAST;
193 p->hdr.frag_len = p->hdr_resp.alloc_hint + 0x18;
197 p->hdr.frag_len = p->hdr_ba.bba.max_tsize;
200 p->rhdr.data->offset.start = 0;
201 p->rhdr.data->offset.end = 0x18;
203 /* store the header in the data stream */
205 smb_io_rpc_hdr ("hdr", &(p->hdr ), &(p->rhdr), 0);
206 smb_io_rpc_hdr_resp("resp", &(p->hdr_resp), &(p->rhdr), 0);
208 return p->rhdr.data != NULL && p->rhdr.offset == 0x18;
212 /*******************************************************************
213 receives a netlogon pipe and responds.
214 ********************************************************************/
215 static BOOL api_rpc_command(pipes_struct *p,
216 char *rpc_name, struct api_struct *api_rpc_cmds,
220 DEBUG(4,("api_rpc_command: %s op 0x%x - ", rpc_name, p->hdr_req.opnum));
222 for (fn_num = 0; api_rpc_cmds[fn_num].name; fn_num++)
224 if (api_rpc_cmds[fn_num].opnum == p->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL)
226 DEBUG(3,("api_rpc_command: %s\n", api_rpc_cmds[fn_num].name));
231 if (api_rpc_cmds[fn_num].name == NULL)
233 DEBUG(4, ("unknown\n"));
237 /* start off with 1024 bytes, and a large safety margin too */
238 mem_buf_init(&(p->rdata.data), SAFETY_MARGIN);
239 mem_alloc_data(p->rdata.data, 1024);
244 p->rdata.data->offset.start = 0;
245 p->rdata.data->offset.end = 0xffffffff;
247 /* do the actual command */
249 api_rpc_cmds[fn_num].fn(p->vuid, data, &(p->rdata));
251 if (p->rdata.data == NULL || p->rdata.offset == 0)
253 mem_free_data(p->rdata.data);
257 mem_realloc_data(p->rdata.data, p->rdata.offset);
259 DEBUG(10,("called %s\n", rpc_name));
265 /*******************************************************************
266 receives a netlogon pipe and responds.
267 ********************************************************************/
268 BOOL api_rpcTNP(pipes_struct *p, char *rpc_name, struct api_struct *api_rpc_cmds,
271 if (data == NULL || data->data == NULL)
273 DEBUG(2,("%s: NULL data received\n", rpc_name));
277 /* read the rpc header */
278 smb_io_rpc_hdr_req("req", &(p->hdr_req), data, 0);
280 /* interpret the command */
281 if (!api_rpc_command(p, rpc_name, api_rpc_cmds, data))
286 /* create the rpc header */
287 if (!create_rpc_reply(p, 0, p->rdata.offset))
292 p->frag_len_left = p->hdr.frag_len - p->file_offset;
293 p->next_frag_start = p->hdr.frag_len;
295 /* set up the data chain */
296 p->rhdr.data->offset.start = 0;
297 p->rhdr.data->offset.end = p->rhdr.offset;
298 p->rhdr.data->next = p->rdata.data;
300 p->rdata.data->offset.start = p->rhdr.data->offset.end;
301 p->rdata.data->offset.end = p->rhdr.data->offset.end + p->rdata.offset;
302 p->rdata.data->next = NULL;
308 /*******************************************************************
309 gets a domain user's groups
310 ********************************************************************/
311 void get_domain_user_groups(char *domain_groups, char *user)
315 if (domain_groups == NULL || user == NULL) return;
317 /* any additional groups this user is in. e.g power users */
318 pstrcpy(domain_groups, lp_domain_groups());
320 /* can only be a user or a guest. cannot be guest _and_ admin */
321 if (user_in_list(user, lp_domain_guest_group()))
323 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
324 pstrcat(domain_groups, tmp);
326 DEBUG(3,("domain guest group access %s granted\n", tmp));
330 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
331 pstrcat(domain_groups, tmp);
333 DEBUG(3,("domain group access %s granted\n", tmp));
335 if (user_in_list(user, lp_domain_admin_group()))
337 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
338 pstrcat(domain_groups, tmp);
340 DEBUG(3,("domain admin group access %s granted\n", tmp));
346 /*******************************************************************
348 ********************************************************************/
349 uint32 lookup_group_name(uint32 rid, char *group_name, uint32 *type)
352 (*type) = SID_NAME_DOM_GRP;
354 DEBUG(5,("lookup_group_name: rid: %d", rid));
356 while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
361 if (domain_group_rids[i].rid != 0)
363 fstrcpy(group_name, domain_group_rids[i].name);
364 DEBUG(5,(" = %s\n", group_name));
368 DEBUG(5,(" none mapped\n"));
369 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
372 /*******************************************************************
374 ********************************************************************/
375 uint32 lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
378 (*type) = SID_NAME_WKN_GRP;
380 DEBUG(5,("lookup_alias_name: rid: %d", rid));
382 while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
387 if (builtin_alias_rids[i].rid != 0)
389 fstrcpy(alias_name, builtin_alias_rids[i].name);
390 DEBUG(5,(" = %s\n", alias_name));
394 DEBUG(5,(" none mapped\n"));
395 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
398 /*******************************************************************
400 ********************************************************************/
401 uint32 lookup_user_name(uint32 rid, char *user_name, uint32 *type)
403 struct sam_disp_info *disp_info;
405 (*type) = SID_NAME_USER;
407 DEBUG(5,("lookup_user_name: rid: %d", rid));
409 /* look up the well-known domain user rids first */
410 while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
415 if (domain_user_rids[i].rid != 0)
417 fstrcpy(user_name, domain_user_rids[i].name);
418 DEBUG(5,(" = %s\n", user_name));
422 /* ok, it's a user. find the user account */
424 disp_info = getsamdisprid(rid);
427 if (disp_info != NULL)
429 fstrcpy(user_name, disp_info->smb_name);
430 DEBUG(5,(" = %s\n", user_name));
434 DEBUG(5,(" none mapped\n"));
435 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
438 /*******************************************************************
440 ********************************************************************/
441 uint32 lookup_group_rid(char *group_name, uint32 *rid)
444 int i = -1; /* start do loop at -1 */
446 do /* find, if it exists, a group rid for the group name*/
449 (*rid) = domain_group_rids[i].rid;
450 grp_name = domain_group_rids[i].name;
452 } while (grp_name != NULL && !strequal(grp_name, group_name));
454 return (grp_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
457 /*******************************************************************
459 ********************************************************************/
460 uint32 lookup_alias_rid(char *alias_name, uint32 *rid)
463 int i = -1; /* start do loop at -1 */
465 do /* find, if it exists, a alias rid for the alias name*/
468 (*rid) = builtin_alias_rids[i].rid;
469 als_name = builtin_alias_rids[i].name;
471 } while (als_name != NULL && !strequal(als_name, alias_name));
473 return (als_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
476 /*******************************************************************
478 ********************************************************************/
479 uint32 lookup_user_rid(char *user_name, uint32 *rid)
481 struct sam_passwd *sam_pass;
484 /* find the user account */
486 sam_pass = getsam21pwnam(user_name);
489 if (sam_pass != NULL)
491 (*rid) = sam_pass->user_rid;
495 return 0xC0000000 | NT_STATUS_NONE_MAPPED;