Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[samba.git] / source3 / rpc_parse / parse_creds.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  RPC Pipe client / server routines
5  *  Copyright (C) Andrew Tridgell              1992-1999,
6  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1999,
7  *  Copyright (C) Paul Ashton                  1997-1999.
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambgrpsge, MA 02139, USA.
22  */
23
24 #include "includes.h"
25
26 /*******************************************************************
27 makes a CREDS_UNIX structure.
28 ********************************************************************/
29 BOOL make_creds_unix(CREDS_UNIX *r_u, const char* user_name,
30                                 const char* requested_name,
31                                 const char* real_name,
32                                 BOOL guest)
33 {
34         if (r_u == NULL) return False;
35
36         DEBUG(5,("make_creds_unix\n"));
37
38         fstrcpy(r_u->user_name     , user_name);
39         fstrcpy(r_u->requested_name, requested_name);
40         fstrcpy(r_u->real_name     , real_name);
41         r_u->guest = guest;
42
43         return True;
44 }
45
46 /*******************************************************************
47 reads or writes a structure.
48 ********************************************************************/
49 BOOL creds_io_unix(char *desc, CREDS_UNIX *r_u, prs_struct *ps, int depth)
50 {
51         if (r_u == NULL) return False;
52
53         prs_debug(ps, depth, desc, "creds_io_unix");
54         depth++;
55
56         prs_align(ps);
57         prs_string("user_name", ps, depth,   r_u->user_name, strlen(r_u->user_name), sizeof(r_u->user_name));
58         prs_align(ps);
59         prs_string("requested_name", ps, depth,   r_u->requested_name, strlen(r_u->requested_name), sizeof(r_u->requested_name));
60         prs_align(ps);
61         prs_string("real_name", ps, depth,   r_u->real_name, strlen(r_u->real_name), sizeof(r_u->real_name));
62         prs_align(ps);
63         prs_uint32("guest", ps, depth, (uint32 *)&(r_u->guest));
64         return True;
65 }
66
67
68 /*******************************************************************
69 frees a structure.
70 ********************************************************************/
71 void creds_free_unix(CREDS_UNIX *r_u)
72 {
73 }
74
75 /*******************************************************************
76 makes a CREDS_UNIX_SEC structure.
77 ********************************************************************/
78 BOOL make_creds_unix_sec(CREDS_UNIX_SEC *r_u,
79                 uint32 uid, uint32 gid, uint32 num_grps, gid_t *grps)
80 {
81         int i;
82         if (r_u == NULL) return False;
83
84         DEBUG(5,("make_creds_unix_sec\n"));
85
86         r_u->uid      = uid;
87         r_u->gid      = gid;
88         r_u->num_grps = num_grps;
89         r_u->grps = (uint32*)malloc(sizeof(r_u->grps[0]) * r_u->num_grps);
90         if (r_u->grps == NULL && num_grps != 0)
91         {
92                 return False;
93         }
94         for (i = 0; i < num_grps; i++)
95         {
96                 r_u->grps[i] = (gid_t)grps[i];
97         }
98
99         return True;
100 }
101
102 /*******************************************************************
103 reads or writes a structure.
104 ********************************************************************/
105 BOOL creds_io_unix_sec(char *desc, CREDS_UNIX_SEC *r_u, prs_struct *ps, int depth)
106 {
107         uint32 i;
108
109         if (r_u == NULL) return False;
110
111         prs_debug(ps, depth, desc, "creds_io_unix_sec");
112         depth++;
113
114         prs_align(ps);
115
116         prs_uint32("uid", ps, depth, &(r_u->uid));
117         prs_uint32("gid", ps, depth, &(r_u->gid));
118         prs_uint32("num_grps", ps, depth, (uint32 *)&(r_u->num_grps));
119         if (r_u->num_grps != 0)
120         {
121                 uint32 *tgr;
122                 
123                 tgr = (uint32*)Realloc(r_u->grps,
124                                        sizeof(r_u->grps[0]) *
125                                        r_u->num_grps);
126                 if (tgr == NULL)
127                 {
128                         creds_free_unix_sec(r_u);
129                         return False;
130                 }
131                 else r_u->grps = tgr;
132         }
133         for (i = 0; i < r_u->num_grps; i++)
134         {
135                 prs_uint32("", ps, depth, &(r_u->grps[i]));
136         }
137         return True;
138 }
139
140
141 /*******************************************************************
142 frees a structure.
143 ********************************************************************/
144 void creds_free_unix_sec(CREDS_UNIX_SEC *r_u)
145 {
146         SAFE_FREE(r_u->grps);
147 }
148
149 /*******************************************************************
150 makes a CREDS_NT_SEC structure.
151 ********************************************************************/
152 BOOL make_creds_nt_sec(CREDS_NT_SEC *r_u,
153                 DOM_SID *sid, uint32 num_grps, uint32 *grps)
154 {
155         int i;
156         if (r_u == NULL) return False;
157
158         DEBUG(5,("make_creds_unix_sec\n"));
159
160         sid_copy(&r_u->sid, sid);
161         r_u->num_grps = num_grps;
162         r_u->grp_rids = (uint32*)malloc(sizeof(r_u->grp_rids[0]) * r_u->num_grps);
163
164         if (r_u->grp_rids == NULL && num_grps != 0)
165         {
166                 return False;
167         }
168         for (i = 0; i < num_grps; i++)
169         {
170                 r_u->grp_rids[i] = grps[i];
171         }
172
173         return True;
174 }
175
176 /*******************************************************************
177 reads or writes a structure.
178 ********************************************************************/
179 BOOL creds_io_nt_sec(char *desc, CREDS_NT_SEC *r_u, prs_struct *ps, int depth)
180 {
181         int i;
182         if (r_u == NULL) return False;
183
184         prs_debug(ps, depth, desc, "creds_io_nt");
185         depth++;
186
187         prs_align(ps);
188
189         smb_io_dom_sid ("sid", &r_u->sid, ps, depth);
190         prs_align(ps);
191
192         prs_uint32("num_grps", ps, depth, &(r_u->num_grps));
193         if (r_u->num_grps != 0)
194         {
195                 uint32 *tgrid;
196                 
197                 tgrid = (uint32*)Realloc(r_u->grp_rids,
198                                        sizeof(r_u->grp_rids[0]) *
199                                        r_u->num_grps);
200                 if (tgrid == NULL)
201                 {
202                         creds_free_nt_sec(r_u);
203                         return False;
204                 }
205                 else r_u->grp_rids = tgrid;
206         }
207         for (i = 0; i < r_u->num_grps; i++)
208         {
209                 prs_uint32("", ps, depth, &(r_u->grp_rids[i]));
210         }
211
212         return True;
213 }
214
215 /*******************************************************************
216 frees a structure.
217 ********************************************************************/
218 void creds_free_nt_sec(CREDS_NT_SEC *r_u)
219 {
220         SAFE_FREE(r_u->grp_rids);
221 }
222
223 /*******************************************************************
224 reads or writes a structure.
225 ********************************************************************/
226 BOOL creds_io_pwd_info(char *desc, struct pwd_info *pwd, prs_struct *ps, int depth)
227 {
228         if (pwd == NULL) return False;
229
230         prs_debug(ps, depth, desc, "creds_io_pwd_info");
231         depth++;
232
233         prs_align(ps);
234
235         prs_uint32("nullpwd", ps, depth, (uint32 *)&(pwd->null_pwd));
236         if (pwd->null_pwd)
237         {
238                 return True;
239         }
240         
241         prs_uint32("cleartext", ps, depth, (uint32 *)&(pwd->cleartext));
242         if (pwd->cleartext)
243         {
244                 prs_string("password", ps, depth,   pwd->password, strlen(pwd->password), sizeof(pwd->password));
245                 prs_align(ps);
246                 return True;
247         }
248         prs_uint32("crypted", ps, depth, (uint32 *)&(pwd->crypted));
249                 
250         prs_uint8s(False, "smb_lm_pwd", ps, depth, (unsigned char*)&pwd->smb_lm_pwd, sizeof(pwd->smb_lm_pwd));
251         prs_align(ps);
252         prs_uint8s(False, "smb_nt_pwd", ps, depth, (unsigned char*)&pwd->smb_nt_pwd, sizeof(pwd->smb_nt_pwd));
253         prs_align(ps);
254
255         prs_uint8s(False, "smb_lm_owf", ps, depth, (unsigned char*)&pwd->smb_lm_owf, sizeof(pwd->smb_lm_owf));
256         prs_align(ps);
257         prs_uint32("nt_owf_len", ps, depth, &(pwd->nt_owf_len));
258         if (pwd->nt_owf_len > sizeof(pwd->smb_nt_owf))
259         {
260                 return False;
261         }
262         prs_uint8s(False, "smb_nt_owf", ps, depth, (unsigned char*)&pwd->smb_nt_owf, pwd->nt_owf_len);
263         prs_align(ps);
264
265         prs_uint8s(False, "lm_cli_chal", ps, depth, (unsigned char*)&pwd->lm_cli_chal, sizeof(pwd->lm_cli_chal));
266         prs_align(ps);
267         prs_uint32("nt_cli_chal_len", ps, depth, &(pwd->nt_cli_chal_len));
268
269         if (pwd->nt_cli_chal_len > sizeof(pwd->nt_cli_chal))
270         {
271                 return False;
272         }
273         prs_uint8s(False, "nt_cli_chal", ps, depth, (unsigned char*)&pwd->nt_cli_chal, pwd->nt_cli_chal_len);
274         prs_align(ps);
275
276         return True;
277 }
278
279 /*******************************************************************
280 reads or writes a structure.
281 ********************************************************************/
282 BOOL creds_io_nt(char *desc, CREDS_NT *r_u, prs_struct *ps, int depth)
283 {
284         if (r_u == NULL) return False;
285
286         prs_debug(ps, depth, desc, "creds_io_nt");
287         depth++;
288
289         prs_align(ps);
290
291         /* lkclXXXX CHEAT!!!!!!!! */
292         prs_string("user_name", ps, depth,   r_u->user_name, strlen(r_u->user_name), sizeof(r_u->user_name));
293         prs_align(ps);
294         prs_string("domain", ps, depth,   r_u->domain, strlen(r_u->domain), sizeof(r_u->domain));
295         prs_align(ps);
296
297         creds_io_pwd_info("pwd", &r_u->pwd, ps, depth);
298         prs_align(ps);
299
300         prs_uint32("ntlmssp", ps, depth, &(r_u->ntlmssp_flags));
301
302         return True;
303 }
304
305 /*******************************************************************
306 frees a structure.
307 ********************************************************************/
308 void creds_free_nt(CREDS_NT *r_u)
309 {
310 }
311
312 /*******************************************************************
313 reads or writes a structure.
314 ********************************************************************/
315 BOOL creds_io_hybrid(char *desc, CREDS_HYBRID *r_u, prs_struct *ps, int depth)
316 {
317         if (r_u == NULL) return False;
318
319         prs_debug(ps, depth, desc, "creds_io_hybrid");
320         depth++;
321
322         prs_align(ps);
323
324         prs_uint32("reuse", ps, depth, (uint32 *)&(r_u->reuse));
325
326         prs_uint32("ptr_ntc", ps, depth, &(r_u->ptr_ntc));
327         prs_uint32("ptr_uxc", ps, depth, &(r_u->ptr_uxc));
328         prs_uint32("ptr_nts", ps, depth, &(r_u->ptr_nts));
329         prs_uint32("ptr_uxs", ps, depth, &(r_u->ptr_uxs));
330         prs_uint32("ptr_ssk", ps, depth, &(r_u->ptr_ssk));
331         if (r_u->ptr_ntc != 0)
332         {
333                 if (!creds_io_nt  ("ntc", &r_u->ntc, ps, depth)) return False;
334         }
335         if (r_u->ptr_uxc != 0)
336         {
337                 if (!creds_io_unix("uxc", &r_u->uxc, ps, depth)) return False;
338         }
339         if (r_u->ptr_nts != 0)
340         {
341                 if (!creds_io_nt_sec  ("nts", &r_u->nts, ps, depth)) return False;
342         }
343         if (r_u->ptr_uxs != 0)
344         {
345                 if (!creds_io_unix_sec("uxs", &r_u->uxs, ps, depth)) return False;
346         }
347         if (r_u->ptr_ssk != 0)
348         {
349                 prs_uint8s(False, "usr_sess_key", ps, depth, (unsigned char*)&r_u->usr_sess_key, sizeof(r_u->usr_sess_key));
350         }
351         else
352         {
353                 memset(r_u->usr_sess_key, 0, sizeof(r_u->usr_sess_key));
354         }
355         return True;
356 }
357
358 void copy_unix_creds(CREDS_UNIX *to, const CREDS_UNIX *from)
359 {
360         if (from == NULL)
361         {
362                 to->user_name[0] = 0;
363                 return;
364         }
365         fstrcpy(to->user_name, from->user_name);
366 }
367
368 void copy_nt_sec_creds(CREDS_NT_SEC *to, const CREDS_NT_SEC *from)
369 {
370         if (from == NULL)
371         {
372                 ZERO_STRUCTP(to);
373                 return;
374         }
375         sid_copy(&to->sid, &from->sid);
376         to->num_grps = 0;
377         to->grp_rids = NULL;
378
379         if (from->num_grps != 0)
380         {
381                 size_t size = from->num_grps * sizeof(from->grp_rids[0]);
382                 to->grp_rids = (uint32*)malloc(size);
383                 if (to->grp_rids == NULL)
384                 {
385                         return;
386                 }
387                 to->num_grps = from->num_grps;
388                 memcpy(to->grp_rids, from->grp_rids, size);
389         }
390 }
391
392 void copy_unix_sec_creds(CREDS_UNIX_SEC *to, const CREDS_UNIX_SEC *from)
393 {
394         if (from == NULL)
395         {
396                 to->uid = -1;
397                 to->gid = -1;
398                 to->num_grps = 0;
399                 to->grps = NULL;
400                 return;
401         }
402         to->uid = from->uid;
403         to->gid = from->gid;
404         to->num_grps = 0;
405         to->grps = NULL;
406
407         if (from->num_grps != 0)
408         {
409                 size_t size = from->num_grps * sizeof(from->grps[0]);
410                 to->grps = (uint32*)malloc(size);
411                 if (to->grps == NULL)
412                 {
413                         return;
414                 }
415                 to->num_grps = from->num_grps;
416                 memcpy(to->grps, from->grps, size);
417         }
418 }
419
420 void create_ntc_from_cli_state (CREDS_NT *to, const struct cli_state *cli_from)
421 {
422         /* 
423          * NULL credentials -- 
424          * if this gets executed, it is a programming error.
425          * fall through to copy_nt_creds() 
426          */
427         if (cli_from == NULL)
428         {
429                 copy_nt_creds (to, NULL);
430                 return;
431         }
432
433         safe_strcpy(to->domain   , cli_from->domain   , sizeof(cli_from->domain   )-1);
434         safe_strcpy(to->user_name, cli_from->user_name, sizeof(cli_from->user_name)-1);
435         memcpy(&to->pwd, &cli_from->pwd, sizeof(cli_from->pwd));
436         to->ntlmssp_flags = cli_from->ntlmssp_flags;
437         DEBUG(10,("create_ntc_fromcli_state: user %s domain %s flgs: %x\n",
438                to->user_name, to->domain,
439                to->ntlmssp_flags));
440
441 }
442
443
444 void copy_nt_creds(struct ntuser_creds *to,
445                                 const struct ntuser_creds *from)
446 {
447         if (from == NULL)
448         {
449                 DEBUG(10,("copy_nt_creds: null creds\n"));
450                 to->domain[0] = 0;
451                 to->user_name[0] = 0;
452                 pwd_set_nullpwd(&to->pwd);
453                 to->ntlmssp_flags = 0;
454
455                 return;
456         }
457         safe_strcpy(to->domain   , from->domain   , sizeof(from->domain   )-1);
458         safe_strcpy(to->user_name, from->user_name, sizeof(from->user_name)-1);
459         memcpy(&to->pwd, &from->pwd, sizeof(from->pwd));
460         to->ntlmssp_flags = from->ntlmssp_flags;
461         DEBUG(10,("copy_nt_creds: user %s domain %s flgs: %x\n",
462                to->user_name, to->domain, 
463                to->ntlmssp_flags));
464 }
465
466 void copy_user_creds(struct user_creds *to,
467                                 const struct user_creds *from)
468 {
469         ZERO_STRUCTP(to);
470         if (from == NULL)
471         {
472                 to->ptr_ntc = 0;
473                 to->ptr_uxc = 0;
474                 to->ptr_nts = 0;
475                 to->ptr_uxs = 0;
476                 to->ptr_ssk = 0;
477                 copy_nt_creds(&to->ntc, NULL);
478                 copy_unix_creds(&to->uxc, NULL);
479                 copy_nt_sec_creds(&to->nts, NULL);
480                 copy_unix_sec_creds(&to->uxs, NULL);
481                 to->reuse = False;
482                 return;
483         }
484
485         to->reuse = from->reuse;
486
487         to->ptr_nts = from->ptr_nts;
488         to->ptr_uxs = from->ptr_uxs;
489         to->ptr_ntc = from->ptr_ntc;
490         to->ptr_uxc = from->ptr_uxc;
491         to->ptr_ssk = from->ptr_ssk;
492
493         if (to->ptr_ntc != 0)
494         {
495                 copy_nt_creds(&to->ntc, &from->ntc);
496         }
497         if (to->ptr_uxc != 0)
498         {
499                 copy_unix_creds(&to->uxc, &from->uxc);
500         }
501         if (to->ptr_nts != 0)
502         {
503                 copy_nt_sec_creds(&to->nts, &from->nts);
504         }
505         if (to->ptr_uxs != 0)
506         {
507                 copy_unix_sec_creds(&to->uxs, &from->uxs);
508         }
509         if (to->ptr_ssk != 0)
510         {
511                 memcpy(to->usr_sess_key, from->usr_sess_key,
512                         sizeof(to->usr_sess_key));
513         }
514 }
515
516 void free_user_creds(struct user_creds *creds)
517 {
518         creds_free_unix(&creds->uxc);
519         creds_free_nt  (&creds->ntc);
520         creds_free_unix_sec(&creds->uxs);
521         creds_free_nt_sec  (&creds->nts);
522 }
523
524 /*******************************************************************
525 reads or writes a structure.
526 ********************************************************************/
527 BOOL creds_io_cmd(char *desc, CREDS_CMD *r_u, prs_struct *ps, int depth)
528 {
529         if (r_u == NULL) return False;
530
531         prs_debug(ps, depth, desc, "creds_io_cmd");
532         depth++;
533
534         prs_align(ps);
535
536         prs_uint16("version", ps, depth, &(r_u->version));
537         prs_uint16("command", ps, depth, &(r_u->command));
538         prs_uint32("pid    ", ps, depth, &(r_u->pid    ));
539
540         prs_string("name   ", ps, depth,   r_u->name, strlen(r_u->name), sizeof(r_u->name));
541         prs_align(ps);
542         
543         prs_uint32("ptr_creds", ps, depth, &(r_u->ptr_creds));
544         if (r_u->ptr_creds != 0)
545         {
546                 if (!creds_io_hybrid("creds", r_u->cred, ps, depth))
547                 {
548                         return False;
549                 }
550         }
551
552
553         return True;
554 }
555
556
557 BOOL create_ntuser_creds( prs_struct *ps,
558                                 const char* name, 
559                                 uint16 version, uint16 command,
560                                 uint32 pid,
561                                 const struct ntuser_creds *ntu,
562                                 BOOL reuse)
563 {
564         CREDS_CMD cmd;
565         struct user_creds usr;
566
567         ZERO_STRUCT(cmd);
568         ZERO_STRUCT(usr);
569
570         DEBUG(10,("create_user_creds: %s %d %d\n",
571                 name, version, command));
572
573         usr.reuse = reuse;
574
575         fstrcpy(cmd.name, name);
576         cmd.version = version;
577         cmd.command = command;
578         cmd.pid   = pid  ;
579         cmd.ptr_creds = ntu != NULL ? 1 : 0;
580         cmd.cred = &usr;
581
582         if (ntu != NULL)
583         {
584                 copy_nt_creds(&usr.ntc, ntu);
585                 usr.ptr_ntc = 1;
586         }
587         else
588         {
589                 usr.ptr_ntc = 0;
590         }
591                 
592         prs_init(ps, 1024, NULL, MARSHALL);
593
594         ps->data_offset = 4;
595         return creds_io_cmd("creds", &cmd, ps, 0);
596 }
597
598 BOOL create_user_creds( prs_struct *ps,
599                                 const char* name, 
600                                 uint16 version, uint16 command,
601                                 uint32 pid,
602                                 struct user_creds *usr)
603 {
604         CREDS_CMD cmd;
605
606         ZERO_STRUCT(cmd);
607
608         DEBUG(10,("create_user_creds: %s %d %d\n",
609                 name, version, command));
610
611         fstrcpy(cmd.name, name);
612         cmd.version = version;
613         cmd.command = command;
614         cmd.pid     = pid  ;
615         cmd.ptr_creds = usr != NULL ? 1 : 0;
616         cmd.cred = usr;
617
618         prs_init(ps, 1024, NULL, MARSHALL);
619
620         ps->data_offset = 4;
621         return creds_io_cmd("creds", &cmd, ps, 0);
622 }