bounds check next_token() to prevent possible buffer overflows
[vlendec/samba-autobuild/.git] / source3 / rpc_server / srv_util.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
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.
9  *  
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.
14  *  
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.
19  *  
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.
23  */
24
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.
28  *
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.
32  *
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...
36  *
37  *  ... if you read the docs.  or stare at packets for weeks on end.
38  *
39  */
40
41 #include "includes.h"
42 #include "nterr.h"
43
44 extern int DEBUGLEVEL;
45 extern DOM_SID global_machine_sid;
46
47 /*
48  * A list of the rids of well known BUILTIN and Domain users
49  * and groups.
50  */
51
52 rid_name builtin_alias_rids[] =
53 {  
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" },
58    
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" },
64     { 0                             , NULL }
65 };
66
67 /* array lookup of well-known Domain RID users. */
68 rid_name domain_user_rids[] =
69 {  
70     { DOMAIN_USER_RID_ADMIN         , "Administrator" },
71     { DOMAIN_USER_RID_GUEST         , "Guest" },
72     { 0                             , NULL }
73 };
74
75 /* array lookup of well-known Domain RID groups. */
76 rid_name domain_group_rids[] =
77 {  
78     { DOMAIN_GROUP_RID_ADMINS       , "Domain Admins" },
79     { DOMAIN_GROUP_RID_USERS        , "Domain Users" },
80     { DOMAIN_GROUP_RID_GUESTS       , "Domain Guests" },
81     { 0                             , NULL }
82 };
83
84 /**************************************************************************
85  Check if a name matches any of the well known SID values.
86 ***************************************************************************/
87
88 BOOL lookup_wellknown_sid_from_name(char *windows_name, DOM_SID *psid)
89 {
90   rid_name *rnp;
91   int i;
92
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;
99       return True;
100     }
101   }
102
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;
109       return True;
110     }
111   }
112
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;
119       return True;
120     }
121   }
122
123   return False;
124 }
125
126 int make_dom_gids(char *gids_str, DOM_GID **ppgids)
127 {
128   char *ptr;
129   pstring s2;
130   int count;
131   DOM_GID *gids;
132
133   *ppgids = NULL;
134
135   DEBUG(4,("make_dom_gids: %s\n", gids_str));
136
137   if (gids_str == NULL || *gids_str == 0)
138     return 0;
139
140   for (count = 0, ptr = gids_str; 
141        next_token(&ptr, s2, NULL, sizeof(s2)); 
142        count++)
143     ;
144
145   gids = (DOM_GID *)malloc( sizeof(DOM_GID) * count );
146   if(!gids)
147   {
148     DEBUG(0,("make_dom_gids: malloc fail !\n"));
149     return 0;
150   }
151
152   for (count = 0, ptr = gids_str; 
153        next_token(&ptr, s2, NULL, sizeof(s2)) && 
154                count < LSA_MAX_GROUPS; 
155        count++) 
156   {
157     /* the entries are of the form GID/ATTR, ATTR being optional.*/
158     char *attr;
159     uint32 rid = 0;
160     int i;
161
162     attr = strchr(s2,'/');
163     if (attr)
164       *attr++ = 0;
165
166     if (!attr || !*attr)
167       attr = "7"; /* default value for attribute is 7 */
168
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++)
171     {
172       if (strequal(builtin_alias_rids[i].name, s2))
173       {
174         rid = builtin_alias_rids[i].rid;
175         break;
176       }
177     }
178
179     if (rid == 0)
180       rid = atoi(s2);
181
182     if (rid == 0)
183     {
184       DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n", s2, attr));
185       count--;
186     }
187     else
188     {
189       gids[count].g_rid = rid;
190       gids[count].attr  = atoi(attr);
191
192       DEBUG(5,("group id: %d attr: %d\n", gids[count].g_rid, gids[count].attr));
193     }
194   }
195
196   *ppgids = gids;
197   return count;
198 }
199
200 /*******************************************************************
201  turns a DCE/RPC request into a DCE/RPC reply
202
203  this is where the data really should be split up into an array of
204  headers and data sections.
205
206  ********************************************************************/
207 BOOL create_rpc_reply(pipes_struct *p,
208                                 uint32 data_start, uint32 data_end)
209 {
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));
212
213         mem_buf_init(&(p->rhdr.data), 0);
214         mem_alloc_data(p->rhdr.data, 0x18);
215
216         p->rhdr.align = 4;
217         p->rhdr.io = False;
218
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 */
221
222         /* set up rpc header (fragmentation issues) */
223         if (data_start == 0)
224         {
225                 p->hdr.flags = RPC_FLG_FIRST;
226         }
227         else
228         {
229                 p->hdr.flags = 0;
230         }
231
232         if (p->hdr_resp.alloc_hint + 0x18 <= p->hdr_ba.bba.max_tsize)
233         {
234                 p->hdr.flags |= RPC_FLG_LAST;
235                 p->hdr.frag_len = p->hdr_resp.alloc_hint + 0x18;
236         }
237         else
238         {
239                 p->hdr.frag_len = p->hdr_ba.bba.max_tsize;
240         }
241
242         p->rhdr.data->offset.start = 0;
243         p->rhdr.data->offset.end   = 0x18;
244
245         /* store the header in the data stream */
246         p->rhdr.offset = 0;
247         smb_io_rpc_hdr   ("hdr", &(p->hdr   ), &(p->rhdr), 0);
248         smb_io_rpc_hdr_resp("resp", &(p->hdr_resp), &(p->rhdr), 0);
249
250         return p->rhdr.data != NULL && p->rhdr.offset == 0x18;
251 }
252
253
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,
259                                 prs_struct *data)
260 {
261         int fn_num;
262         DEBUG(4,("api_rpc_command: %s op 0x%x - ", rpc_name, p->hdr_req.opnum));
263
264         for (fn_num = 0; api_rpc_cmds[fn_num].name; fn_num++)
265         {
266                 if (api_rpc_cmds[fn_num].opnum == p->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL)
267                 {
268                         DEBUG(3,("api_rpc_command: %s\n", api_rpc_cmds[fn_num].name));
269                         break;
270                 }
271         }
272
273         if (api_rpc_cmds[fn_num].name == NULL)
274         {
275                 DEBUG(4, ("unknown\n"));
276                 return False;
277         }
278
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);
282
283         p->rdata.io = False;
284         p->rdata.align = 4;
285
286         p->rdata.data->offset.start = 0;
287         p->rdata.data->offset.end   = 0xffffffff;
288
289         /* do the actual command */
290         p->rdata.offset = 0; 
291         api_rpc_cmds[fn_num].fn(p->uid, data, &(p->rdata));
292
293         if (p->rdata.data == NULL || p->rdata.offset == 0)
294         {
295                 mem_free_data(p->rdata.data);
296                 return False;
297         }
298
299         mem_realloc_data(p->rdata.data, p->rdata.offset);
300
301     DEBUG(10,("called %s\n", rpc_name));
302
303         return True;
304 }
305
306
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,
311                                 prs_struct *data)
312 {
313         if (data == NULL || data->data == NULL)
314         {
315                 DEBUG(2,("%s: NULL data received\n", rpc_name));
316                 return False;
317         }
318
319         /* read the rpc header */
320         smb_io_rpc_hdr_req("req", &(p->hdr_req), data, 0);
321
322         /* interpret the command */
323         if (!api_rpc_command(p, rpc_name, api_rpc_cmds, data))
324         {
325                 return False;
326         }
327
328         /* create the rpc header */
329         if (!create_rpc_reply(p, 0, p->rdata.offset))
330         {
331                 return False;
332         }
333
334         p->frag_len_left   = p->hdr.frag_len - p->file_offset;
335         p->next_frag_start = p->hdr.frag_len; 
336         
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;
341
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;
345
346         return True;
347 }
348
349
350 /*******************************************************************
351  gets a domain user's groups
352  ********************************************************************/
353 void get_domain_user_groups(char *domain_groups, char *user)
354 {
355         pstring tmp;
356
357         if (domain_groups == NULL || user == NULL) return;
358
359         /* any additional groups this user is in.  e.g power users */
360         pstrcpy(domain_groups, lp_domain_groups());
361
362         /* can only be a user or a guest.  cannot be guest _and_ admin */
363         if (user_in_list(user, lp_domain_guest_group()))
364         {
365                 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
366                 pstrcat(domain_groups, tmp);
367
368                 DEBUG(3,("domain guest group access %s granted\n", tmp));
369         }
370         else
371         {
372                 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
373                 pstrcat(domain_groups, tmp);
374
375                 DEBUG(3,("domain group access %s granted\n", tmp));
376
377                 if (user_in_list(user, lp_domain_admin_group()))
378                 {
379                         slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
380                         pstrcat(domain_groups, tmp);
381
382                         DEBUG(3,("domain admin group access %s granted\n", tmp));
383                 }
384         }
385 }
386
387
388 /*******************************************************************
389  lookup_group_name
390  ********************************************************************/
391 uint32 lookup_group_name(uint32 rid, char *group_name, uint32 *type)
392 {
393         int i = 0; 
394         (*type) = SID_NAME_DOM_GRP;
395
396         DEBUG(5,("lookup_group_name: rid: %d", rid));
397
398         while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
399         {
400                 i++;
401         }
402
403         if (domain_group_rids[i].rid != 0)
404         {
405                 fstrcpy(group_name, domain_group_rids[i].name);
406                 DEBUG(5,(" = %s\n", group_name));
407                 return 0x0;
408         }
409
410         DEBUG(5,(" none mapped\n"));
411         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
412 }
413
414 /*******************************************************************
415  lookup_alias_name
416  ********************************************************************/
417 uint32 lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
418 {
419         int i = 0; 
420         (*type) = SID_NAME_WKN_GRP;
421
422         DEBUG(5,("lookup_alias_name: rid: %d", rid));
423
424         while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
425         {
426                 i++;
427         }
428
429         if (builtin_alias_rids[i].rid != 0)
430         {
431                 fstrcpy(alias_name, builtin_alias_rids[i].name);
432                 DEBUG(5,(" = %s\n", alias_name));
433                 return 0x0;
434         }
435
436         DEBUG(5,(" none mapped\n"));
437         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
438 }
439
440 /*******************************************************************
441  lookup_user_name
442  ********************************************************************/
443 uint32 lookup_user_name(uint32 rid, char *user_name, uint32 *type)
444 {
445         struct sam_disp_info *disp_info;
446         int i = 0;
447         (*type) = SID_NAME_USER;
448
449         DEBUG(5,("lookup_user_name: rid: %d", rid));
450
451         /* look up the well-known domain user rids first */
452         while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
453         {
454                 i++;
455         }
456
457         if (domain_user_rids[i].rid != 0)
458         {
459                 fstrcpy(user_name, domain_user_rids[i].name);
460                 DEBUG(5,(" = %s\n", user_name));
461                 return 0x0;
462         }
463
464         /* ok, it's a user.  find the user account */
465         become_root(True);
466         disp_info = getsamdisprid(rid);
467         unbecome_root(True);
468
469         if (disp_info != NULL)
470         {
471                 fstrcpy(user_name, disp_info->smb_name);
472                 DEBUG(5,(" = %s\n", user_name));
473                 return 0x0;
474         }
475
476         DEBUG(5,(" none mapped\n"));
477         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
478 }
479
480 /*******************************************************************
481  lookup_group_rid
482  ********************************************************************/
483 uint32 lookup_group_rid(char *group_name, uint32 *rid)
484 {
485         char *grp_name;
486         int i = -1; /* start do loop at -1 */
487
488         do /* find, if it exists, a group rid for the group name*/
489         {
490                 i++;
491                 (*rid) = domain_group_rids[i].rid;
492                 grp_name = domain_group_rids[i].name;
493
494         } while (grp_name != NULL && !strequal(grp_name, group_name));
495
496         return (grp_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
497 }
498
499 /*******************************************************************
500  lookup_alias_rid
501  ********************************************************************/
502 uint32 lookup_alias_rid(char *alias_name, uint32 *rid)
503 {
504         char *als_name;
505         int i = -1; /* start do loop at -1 */
506
507         do /* find, if it exists, a alias rid for the alias name*/
508         {
509                 i++;
510                 (*rid) = builtin_alias_rids[i].rid;
511                 als_name = builtin_alias_rids[i].name;
512
513         } while (als_name != NULL && !strequal(als_name, alias_name));
514
515         return (als_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
516 }
517
518 /*******************************************************************
519  lookup_user_rid
520  ********************************************************************/
521 uint32 lookup_user_rid(char *user_name, uint32 *rid)
522 {
523         struct smb_passwd *smb_pass;
524         (*rid) = 0;
525
526         /* find the user account */
527         become_root(True);
528         smb_pass = getsmbpwnam(user_name);
529         unbecome_root(True);
530
531         if (smb_pass != NULL)
532         {
533                 /* lkclXXXX SHOULD use name_to_rid() here! */
534                 (*rid) = smb_pass->smb_userid;
535                 return 0x0;
536         }
537
538         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
539 }