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;
46 /* array lookup of well-known RID aliases. the purpose of these escapes me.. */
47 /* XXXX this structure should not have the well-known RID groups added to it,
48 i.e the DOMAIN_GROUP_RID_ADMIN/USER/GUEST. */
49 rid_name domain_alias_rids[] =
51 { DOMAIN_ALIAS_RID_ADMINS , "admins" },
52 { DOMAIN_ALIAS_RID_USERS , "users" },
53 { DOMAIN_ALIAS_RID_GUESTS , "guests" },
54 { DOMAIN_ALIAS_RID_POWER_USERS , "power_users" },
56 { DOMAIN_ALIAS_RID_ACCOUNT_OPS , "account_ops" },
57 { DOMAIN_ALIAS_RID_SYSTEM_OPS , "system_ops" },
58 { DOMAIN_ALIAS_RID_PRINT_OPS , "print_ops" },
59 { DOMAIN_ALIAS_RID_BACKUP_OPS , "backup_ops" },
60 { DOMAIN_ALIAS_RID_REPLICATOR , "replicator" },
64 /* array lookup of well-known Domain RID users. */
65 rid_name domain_user_rids[] =
67 { DOMAIN_USER_RID_ADMIN , "Administrator" },
68 { DOMAIN_USER_RID_GUEST , "Guest" },
72 /* array lookup of well-known Domain RID groups. */
73 rid_name domain_group_rids[] =
75 { DOMAIN_GROUP_RID_ADMINS , "domain admins" },
76 { DOMAIN_GROUP_RID_USERS , "domain users" },
77 { DOMAIN_GROUP_RID_GUESTS , "domain guests" },
82 int make_dom_gids(char *gids_str, DOM_GID **ppgids)
91 DEBUG(4,("make_dom_gids: %s\n", gids_str));
93 if (gids_str == NULL || *gids_str == 0)
96 for (count = 0, ptr = gids_str; next_token(&ptr, s2, NULL); count++)
99 gids = (DOM_GID *)malloc( sizeof(DOM_GID) * count );
102 DEBUG(0,("make_dom_gids: malloc fail !\n"));
106 for (count = 0, ptr = gids_str; next_token(&ptr, s2, NULL) &&
107 count < LSA_MAX_GROUPS; count++)
109 /* the entries are of the form GID/ATTR, ATTR being optional.*/
114 attr = strchr(s2,'/');
119 attr = "7"; /* default value for attribute is 7 */
121 /* look up the RID string and see if we can turn it into a rid number */
122 for (i = 0; domain_alias_rids[i].name != NULL; i++)
124 if (strequal(domain_alias_rids[i].name, s2))
126 rid = domain_alias_rids[i].rid;
136 DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n", s2, attr));
141 gids[count].g_rid = rid;
142 gids[count].attr = atoi(attr);
144 DEBUG(5,("group id: %d attr: %d\n", gids[count].g_rid, gids[count].attr));
152 /*******************************************************************
153 turns a DCE/RPC request into a DCE/RPC reply
155 this is where the data really should be split up into an array of
156 headers and data sections.
158 ********************************************************************/
159 BOOL create_rpc_reply(pipes_struct *p,
160 uint32 data_start, uint32 data_end)
162 DEBUG(5,("create_rpc_reply: data_start: %d data_end: %d max_tsize: %d\n",
163 data_start, data_end, p->hdr_ba.bba.max_tsize));
165 mem_buf_init(&(p->rhdr.data), 0);
166 mem_alloc_data(p->rhdr.data, 0x18);
171 p->hdr_resp.alloc_hint = data_end - data_start; /* calculate remaining data to be sent */
172 p->hdr.pkt_type = RPC_RESPONSE; /* mark header as an rpc response */
174 /* set up rpc header (fragmentation issues) */
177 p->hdr.flags = RPC_FLG_FIRST;
184 if (p->hdr_resp.alloc_hint + 0x18 <= p->hdr_ba.bba.max_tsize)
186 p->hdr.flags |= RPC_FLG_LAST;
187 p->hdr.frag_len = p->hdr_resp.alloc_hint + 0x18;
191 p->hdr.frag_len = p->hdr_ba.bba.max_tsize;
194 p->rhdr.data->offset.start = 0;
195 p->rhdr.data->offset.end = 0x18;
197 /* store the header in the data stream */
199 smb_io_rpc_hdr ("hdr", &(p->hdr ), &(p->rhdr), 0);
200 smb_io_rpc_hdr_resp("resp", &(p->hdr_resp), &(p->rhdr), 0);
202 return p->rhdr.data != NULL && p->rhdr.offset == 0x18;
206 /*******************************************************************
207 receives a netlogon pipe and responds.
208 ********************************************************************/
209 static BOOL api_rpc_command(pipes_struct *p,
210 char *rpc_name, struct api_struct *api_rpc_cmds,
214 DEBUG(4,("api_rpc_command: %s op 0x%x - ", rpc_name, p->hdr_req.opnum));
216 for (fn_num = 0; api_rpc_cmds[fn_num].name; fn_num++)
218 if (api_rpc_cmds[fn_num].opnum == p->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL)
220 DEBUG(3,("api_rpc_command: %s\n", api_rpc_cmds[fn_num].name));
225 if (api_rpc_cmds[fn_num].name == NULL)
227 DEBUG(4, ("unknown\n"));
231 /* start off with 1024 bytes, and a large safety margin too */
232 mem_buf_init(&(p->rdata.data), SAFETY_MARGIN);
233 mem_alloc_data(p->rdata.data, 1024);
238 p->rdata.data->offset.start = 0;
239 p->rdata.data->offset.end = 0xffffffff;
241 /* do the actual command */
243 api_rpc_cmds[fn_num].fn(p->uid, data, &(p->rdata));
245 if (p->rdata.data == NULL || p->rdata.offset == 0)
247 mem_free_data(p->rdata.data);
251 mem_realloc_data(p->rdata.data, p->rdata.offset);
253 DEBUG(10,("called %s\n", rpc_name));
259 /*******************************************************************
260 receives a netlogon pipe and responds.
261 ********************************************************************/
262 BOOL api_rpcTNP(pipes_struct *p, char *rpc_name, struct api_struct *api_rpc_cmds,
265 if (data == NULL || data->data == NULL)
267 DEBUG(2,("%s: NULL data received\n", rpc_name));
271 /* read the rpc header */
272 smb_io_rpc_hdr_req("req", &(p->hdr_req), data, 0);
274 /* interpret the command */
275 if (!api_rpc_command(p, rpc_name, api_rpc_cmds, data))
280 /* create the rpc header */
281 if (!create_rpc_reply(p, 0, p->rdata.offset))
286 p->frag_len_left = p->hdr.frag_len - p->file_offset;
287 p->next_frag_start = p->hdr.frag_len;
289 /* set up the data chain */
290 p->rhdr.data->offset.start = 0;
291 p->rhdr.data->offset.end = p->rhdr.offset;
292 p->rhdr.data->next = p->rdata.data;
294 p->rdata.data->offset.start = p->rhdr.data->offset.end;
295 p->rdata.data->offset.end = p->rhdr.data->offset.end + p->rdata.offset;
296 p->rdata.data->next = NULL;
302 /*******************************************************************
303 gets a domain user's groups
304 ********************************************************************/
305 void get_domain_user_groups(char *domain_groups, char *user)
309 if (domain_groups == NULL || user == NULL) return;
311 /* any additional groups this user is in. e.g power users */
312 pstrcpy(domain_groups, lp_domain_groups());
314 /* can only be a user or a guest. cannot be guest _and_ admin */
315 if (user_in_list(user, lp_domain_guest_users()))
317 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
318 pstrcat(domain_groups, tmp);
320 DEBUG(3,("domain guest access %s granted\n", tmp));
324 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
325 pstrcat(domain_groups, tmp);
327 DEBUG(3,("domain user access %s granted\n", tmp));
329 if (user_in_list(user, lp_domain_admin_users()))
331 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
332 pstrcat(domain_groups, tmp);
334 DEBUG(3,("domain admin access %s granted\n", tmp));
340 /*******************************************************************
342 ********************************************************************/
343 uint32 lookup_group_name(uint32 rid, char *group_name, uint32 *type)
346 (*type) = SID_NAME_DOM_GRP;
348 DEBUG(5,("lookup_group_name: rid: %d", rid));
350 while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
355 if (domain_group_rids[i].rid != 0)
357 fstrcpy(group_name, domain_group_rids[i].name);
358 DEBUG(5,(" = %s\n", group_name));
362 DEBUG(5,(" none mapped\n"));
363 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
366 /*******************************************************************
368 ********************************************************************/
369 uint32 lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
372 (*type) = SID_NAME_WKN_GRP;
374 DEBUG(5,("lookup_alias_name: rid: %d", rid));
376 while (domain_alias_rids[i].rid != rid && domain_alias_rids[i].rid != 0)
381 if (domain_alias_rids[i].rid != 0)
383 fstrcpy(alias_name, domain_alias_rids[i].name);
384 DEBUG(5,(" = %s\n", alias_name));
388 DEBUG(5,(" none mapped\n"));
389 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
392 /*******************************************************************
394 ********************************************************************/
395 uint32 lookup_user_name(uint32 rid, char *user_name, uint32 *type)
397 struct smb_passwd *smb_pass;
400 (*type) = SID_NAME_USER;
402 DEBUG(5,("lookup_user_name: rid: %d", rid));
404 /* look up the well-known domain user rids first */
405 while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
410 if (domain_user_rids[i].rid != 0)
412 fstrcpy(user_name, domain_user_rids[i].name);
413 DEBUG(5,(" = %s\n", user_name));
417 unix_uid = pdb_uid_to_user_rid(rid);
418 DEBUG(5,(" uid: %d", unix_uid));
420 /* ok, it's a user. find the user account */
422 smb_pass = getsampwuid(rid); /* lkclXXXX SHOULD use rid mapping here! */
425 if (smb_pass != NULL)
427 fstrcpy(user_name, smb_pass->smb_name);
428 DEBUG(5,(" = %s\n", user_name));
432 DEBUG(5,(" none mapped\n"));
433 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
436 /*******************************************************************
438 ********************************************************************/
439 uint32 lookup_group_rid(char *group_name, uint32 *rid)
442 int i = -1; /* start do loop at -1 */
444 do /* find, if it exists, a group rid for the group name*/
447 (*rid) = domain_group_rids[i].rid;
448 grp_name = domain_group_rids[i].name;
450 } while (grp_name != NULL && !strequal(grp_name, group_name));
452 return (grp_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
455 /*******************************************************************
457 ********************************************************************/
458 uint32 lookup_alias_rid(char *alias_name, uint32 *rid)
461 int i = -1; /* start do loop at -1 */
463 do /* find, if it exists, a alias rid for the alias name*/
466 (*rid) = domain_alias_rids[i].rid;
467 als_name = domain_alias_rids[i].name;
469 } while (als_name != NULL && !strequal(als_name, alias_name));
471 return (als_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
474 /*******************************************************************
476 ********************************************************************/
477 uint32 lookup_user_rid(char *user_name, uint32 *rid)
479 struct smb_passwd *smb_pass;
482 /* find the user account */
484 smb_pass = getsampwnam(user_name);
487 if (smb_pass != NULL)
489 /* lkclXXXX SHOULD use name_to_rid() here! */
490 (*rid) = smb_pass->smb_userid;
494 return 0xC0000000 | NT_STATUS_NONE_MAPPED;