- renamed some of the passdb.c functions: they have a prefix pdb_ on them
[samba.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
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[] = 
50 {
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" },
55
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" },
61         { 0                             , NULL }
62 };
63
64 /* array lookup of well-known Domain RID users. */
65 rid_name domain_user_rids[] = 
66 {
67         { DOMAIN_USER_RID_ADMIN         , "Administrator" },
68         { DOMAIN_USER_RID_GUEST         , "Guest" },
69         { 0                             , NULL }
70 };
71
72 /* array lookup of well-known Domain RID groups. */
73 rid_name domain_group_rids[] = 
74 {
75         { DOMAIN_GROUP_RID_ADMINS       , "domain admins" },
76         { DOMAIN_GROUP_RID_USERS        , "domain users" },
77         { DOMAIN_GROUP_RID_GUESTS       , "domain guests" },
78         { 0                             , NULL }
79 };
80
81
82 int make_dom_gids(char *gids_str, DOM_GID **ppgids)
83 {
84   char *ptr;
85   pstring s2;
86   int count;
87   DOM_GID *gids;
88
89   *ppgids = NULL;
90
91   DEBUG(4,("make_dom_gids: %s\n", gids_str));
92
93   if (gids_str == NULL || *gids_str == 0)
94     return 0;
95
96   for (count = 0, ptr = gids_str; next_token(&ptr, s2, NULL); count++)
97     ;
98
99   gids = (DOM_GID *)malloc( sizeof(DOM_GID) * count );
100   if(!gids)
101   {
102     DEBUG(0,("make_dom_gids: malloc fail !\n"));
103     return 0;
104   }
105
106   for (count = 0, ptr = gids_str; next_token(&ptr, s2, NULL) && 
107                        count < LSA_MAX_GROUPS; count++) 
108   {
109     /* the entries are of the form GID/ATTR, ATTR being optional.*/
110     char *attr;
111     uint32 rid = 0;
112     int i;
113
114     attr = strchr(s2,'/');
115     if (attr)
116       *attr++ = 0;
117
118     if (!attr || !*attr)
119       attr = "7"; /* default value for attribute is 7 */
120
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++)
123     {
124       if (strequal(domain_alias_rids[i].name, s2))
125       {
126         rid = domain_alias_rids[i].rid;
127         break;
128       }
129     }
130
131     if (rid == 0)
132       rid = atoi(s2);
133
134     if (rid == 0)
135     {
136       DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n", s2, attr));
137       count--;
138     }
139     else
140     {
141       gids[count].g_rid = rid;
142       gids[count].attr  = atoi(attr);
143
144       DEBUG(5,("group id: %d attr: %d\n", gids[count].g_rid, gids[count].attr));
145     }
146   }
147
148   *ppgids = gids;
149   return count;
150 }
151
152 /*******************************************************************
153  turns a DCE/RPC request into a DCE/RPC reply
154
155  this is where the data really should be split up into an array of
156  headers and data sections.
157
158  ********************************************************************/
159 BOOL create_rpc_reply(pipes_struct *p,
160                                 uint32 data_start, uint32 data_end)
161 {
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));
164
165         mem_buf_init(&(p->rhdr.data), 0);
166         mem_alloc_data(p->rhdr.data, 0x18);
167
168         p->rhdr.align = 4;
169         p->rhdr.io = False;
170
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 */
173
174         /* set up rpc header (fragmentation issues) */
175         if (data_start == 0)
176         {
177                 p->hdr.flags = RPC_FLG_FIRST;
178         }
179         else
180         {
181                 p->hdr.flags = 0;
182         }
183
184         if (p->hdr_resp.alloc_hint + 0x18 <= p->hdr_ba.bba.max_tsize)
185         {
186                 p->hdr.flags |= RPC_FLG_LAST;
187                 p->hdr.frag_len = p->hdr_resp.alloc_hint + 0x18;
188         }
189         else
190         {
191                 p->hdr.frag_len = p->hdr_ba.bba.max_tsize;
192         }
193
194         p->rhdr.data->offset.start = 0;
195         p->rhdr.data->offset.end   = 0x18;
196
197         /* store the header in the data stream */
198         p->rhdr.offset = 0;
199         smb_io_rpc_hdr   ("hdr", &(p->hdr   ), &(p->rhdr), 0);
200         smb_io_rpc_hdr_resp("resp", &(p->hdr_resp), &(p->rhdr), 0);
201
202         return p->rhdr.data != NULL && p->rhdr.offset == 0x18;
203 }
204
205
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,
211                                 prs_struct *data)
212 {
213         int fn_num;
214         DEBUG(4,("api_rpc_command: %s op 0x%x - ", rpc_name, p->hdr_req.opnum));
215
216         for (fn_num = 0; api_rpc_cmds[fn_num].name; fn_num++)
217         {
218                 if (api_rpc_cmds[fn_num].opnum == p->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL)
219                 {
220                         DEBUG(3,("api_rpc_command: %s\n", api_rpc_cmds[fn_num].name));
221                         break;
222                 }
223         }
224
225         if (api_rpc_cmds[fn_num].name == NULL)
226         {
227                 DEBUG(4, ("unknown\n"));
228                 return False;
229         }
230
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);
234
235         p->rdata.io = False;
236         p->rdata.align = 4;
237
238         p->rdata.data->offset.start = 0;
239         p->rdata.data->offset.end   = 0xffffffff;
240
241         /* do the actual command */
242         p->rdata.offset = 0; 
243         api_rpc_cmds[fn_num].fn(p->uid, data, &(p->rdata));
244
245         if (p->rdata.data == NULL || p->rdata.offset == 0)
246         {
247                 mem_free_data(p->rdata.data);
248                 return False;
249         }
250
251         mem_realloc_data(p->rdata.data, p->rdata.offset);
252
253     DEBUG(10,("called %s\n", rpc_name));
254
255         return True;
256 }
257
258
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,
263                                 prs_struct *data)
264 {
265         if (data == NULL || data->data == NULL)
266         {
267                 DEBUG(2,("%s: NULL data received\n", rpc_name));
268                 return False;
269         }
270
271         /* read the rpc header */
272         smb_io_rpc_hdr_req("req", &(p->hdr_req), data, 0);
273
274         /* interpret the command */
275         if (!api_rpc_command(p, rpc_name, api_rpc_cmds, data))
276         {
277                 return False;
278         }
279
280         /* create the rpc header */
281         if (!create_rpc_reply(p, 0, p->rdata.offset))
282         {
283                 return False;
284         }
285
286         p->frag_len_left   = p->hdr.frag_len - p->file_offset;
287         p->next_frag_start = p->hdr.frag_len; 
288         
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;
293
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;
297
298         return True;
299 }
300
301
302 /*******************************************************************
303  gets a domain user's groups
304  ********************************************************************/
305 void get_domain_user_groups(char *domain_groups, char *user)
306 {
307         pstring tmp;
308
309         if (domain_groups == NULL || user == NULL) return;
310
311         /* any additional groups this user is in.  e.g power users */
312         pstrcpy(domain_groups, lp_domain_groups());
313
314         /* can only be a user or a guest.  cannot be guest _and_ admin */
315         if (user_in_list(user, lp_domain_guest_users()))
316         {
317                 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
318                 pstrcat(domain_groups, tmp);
319
320                 DEBUG(3,("domain guest access %s granted\n", tmp));
321         }
322         else
323         {
324                 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
325                 pstrcat(domain_groups, tmp);
326
327                 DEBUG(3,("domain user access %s granted\n", tmp));
328
329                 if (user_in_list(user, lp_domain_admin_users()))
330                 {
331                         slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
332                         pstrcat(domain_groups, tmp);
333
334                         DEBUG(3,("domain admin access %s granted\n", tmp));
335                 }
336         }
337 }
338
339
340 /*******************************************************************
341  lookup_group_name
342  ********************************************************************/
343 uint32 lookup_group_name(uint32 rid, char *group_name, uint32 *type)
344 {
345         int i = 0; 
346         (*type) = SID_NAME_DOM_GRP;
347
348         DEBUG(5,("lookup_group_name: rid: %d", rid));
349
350         while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
351         {
352                 i++;
353         }
354
355         if (domain_group_rids[i].rid != 0)
356         {
357                 fstrcpy(group_name, domain_group_rids[i].name);
358                 DEBUG(5,(" = %s\n", group_name));
359                 return 0x0;
360         }
361
362         DEBUG(5,(" none mapped\n"));
363         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
364 }
365
366 /*******************************************************************
367  lookup_alias_name
368  ********************************************************************/
369 uint32 lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
370 {
371         int i = 0; 
372         (*type) = SID_NAME_WKN_GRP;
373
374         DEBUG(5,("lookup_alias_name: rid: %d", rid));
375
376         while (domain_alias_rids[i].rid != rid && domain_alias_rids[i].rid != 0)
377         {
378                 i++;
379         }
380
381         if (domain_alias_rids[i].rid != 0)
382         {
383                 fstrcpy(alias_name, domain_alias_rids[i].name);
384                 DEBUG(5,(" = %s\n", alias_name));
385                 return 0x0;
386         }
387
388         DEBUG(5,(" none mapped\n"));
389         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
390 }
391
392 /*******************************************************************
393  lookup_user_name
394  ********************************************************************/
395 uint32 lookup_user_name(uint32 rid, char *user_name, uint32 *type)
396 {
397         struct smb_passwd *smb_pass;
398         uint32 unix_uid;
399         int i = 0;
400         (*type) = SID_NAME_USER;
401
402         DEBUG(5,("lookup_user_name: rid: %d", rid));
403
404         /* look up the well-known domain user rids first */
405         while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
406         {
407                 i++;
408         }
409
410         if (domain_user_rids[i].rid != 0)
411         {
412                 fstrcpy(user_name, domain_user_rids[i].name);
413                 DEBUG(5,(" = %s\n", user_name));
414                 return 0x0;
415         }
416
417         unix_uid = pdb_uid_to_user_rid(rid);
418         DEBUG(5,(" uid: %d", unix_uid));
419
420         /* ok, it's a user.  find the user account */
421         become_root(True);
422         smb_pass = getsampwuid(rid); /* lkclXXXX SHOULD use rid mapping here! */
423         unbecome_root(True);
424
425         if (smb_pass != NULL)
426         {
427                 fstrcpy(user_name, smb_pass->smb_name);
428                 DEBUG(5,(" = %s\n", user_name));
429                 return 0x0;
430         }
431
432         DEBUG(5,(" none mapped\n"));
433         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
434 }
435
436 /*******************************************************************
437  lookup_group_rid
438  ********************************************************************/
439 uint32 lookup_group_rid(char *group_name, uint32 *rid)
440 {
441         char *grp_name;
442         int i = -1; /* start do loop at -1 */
443
444         do /* find, if it exists, a group rid for the group name*/
445         {
446                 i++;
447                 (*rid) = domain_group_rids[i].rid;
448                 grp_name = domain_group_rids[i].name;
449
450         } while (grp_name != NULL && !strequal(grp_name, group_name));
451
452         return (grp_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
453 }
454
455 /*******************************************************************
456  lookup_alias_rid
457  ********************************************************************/
458 uint32 lookup_alias_rid(char *alias_name, uint32 *rid)
459 {
460         char *als_name;
461         int i = -1; /* start do loop at -1 */
462
463         do /* find, if it exists, a alias rid for the alias name*/
464         {
465                 i++;
466                 (*rid) = domain_alias_rids[i].rid;
467                 als_name = domain_alias_rids[i].name;
468
469         } while (als_name != NULL && !strequal(als_name, alias_name));
470
471         return (als_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
472 }
473
474 /*******************************************************************
475  lookup_user_rid
476  ********************************************************************/
477 uint32 lookup_user_rid(char *user_name, uint32 *rid)
478 {
479         struct smb_passwd *smb_pass;
480         (*rid) = 0;
481
482         /* find the user account */
483         become_root(True);
484         smb_pass = getsampwnam(user_name);
485         unbecome_root(True);
486
487         if (smb_pass != NULL)
488         {
489                 /* lkclXXXX SHOULD use name_to_rid() here! */
490                 (*rid) = smb_pass->smb_userid;
491                 return 0x0;
492         }
493
494         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
495 }