reverting Jeremy removal
[Samba.git] / source / rpc_parse / parse_creds.c
blob36e08f5746ecf357b71b92d32c77ba699b97499a
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.
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.
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.
24 #include "includes.h"
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)
34 if (r_u == NULL) return False;
36 DEBUG(5,("make_creds_unix\n"));
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;
43 return True;
46 /*******************************************************************
47 reads or writes a structure.
48 ********************************************************************/
49 BOOL creds_io_unix(char *desc, CREDS_UNIX *r_u, prs_struct *ps, int depth)
51 if (r_u == NULL) return False;
53 prs_debug(ps, depth, desc, "creds_io_unix");
54 depth++;
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;
68 /*******************************************************************
69 frees a structure.
70 ********************************************************************/
71 void creds_free_unix(CREDS_UNIX *r_u)
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)
81 int i;
82 if (r_u == NULL) return False;
84 DEBUG(5,("make_creds_unix_sec\n"));
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)
92 return False;
94 for (i = 0; i < num_grps; i++)
96 r_u->grps[i] = (gid_t)grps[i];
99 return True;
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)
107 uint32 i;
109 if (r_u == NULL) return False;
111 prs_debug(ps, depth, desc, "creds_io_unix_sec");
112 depth++;
114 prs_align(ps);
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)
121 uint32 *tgr;
123 tgr = (uint32*)Realloc(r_u->grps,
124 sizeof(r_u->grps[0]) *
125 r_u->num_grps);
126 if (tgr == NULL)
128 creds_free_unix_sec(r_u);
129 return False;
131 else r_u->grps = tgr;
133 for (i = 0; i < r_u->num_grps; i++)
135 prs_uint32("", ps, depth, &(r_u->grps[i]));
137 return True;
141 /*******************************************************************
142 frees a structure.
143 ********************************************************************/
144 void creds_free_unix_sec(CREDS_UNIX_SEC *r_u)
146 SAFE_FREE(r_u->grps);
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)
155 int i;
156 if (r_u == NULL) return False;
158 DEBUG(5,("make_creds_unix_sec\n"));
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);
164 if (r_u->grp_rids == NULL && num_grps != 0)
166 return False;
168 for (i = 0; i < num_grps; i++)
170 r_u->grp_rids[i] = grps[i];
173 return True;
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)
181 int i;
182 if (r_u == NULL) return False;
184 prs_debug(ps, depth, desc, "creds_io_nt");
185 depth++;
187 prs_align(ps);
189 smb_io_dom_sid ("sid", &r_u->sid, ps, depth);
190 prs_align(ps);
192 prs_uint32("num_grps", ps, depth, &(r_u->num_grps));
193 if (r_u->num_grps != 0)
195 uint32 *tgrid;
197 tgrid = (uint32*)Realloc(r_u->grp_rids,
198 sizeof(r_u->grp_rids[0]) *
199 r_u->num_grps);
200 if (tgrid == NULL)
202 creds_free_nt_sec(r_u);
203 return False;
205 else r_u->grp_rids = tgrid;
207 for (i = 0; i < r_u->num_grps; i++)
209 prs_uint32("", ps, depth, &(r_u->grp_rids[i]));
212 return True;
215 /*******************************************************************
216 frees a structure.
217 ********************************************************************/
218 void creds_free_nt_sec(CREDS_NT_SEC *r_u)
220 SAFE_FREE(r_u->grp_rids);
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)
228 if (pwd == NULL) return False;
230 prs_debug(ps, depth, desc, "creds_io_pwd_info");
231 depth++;
233 prs_align(ps);
235 prs_uint32("nullpwd", ps, depth, (uint32 *)&(pwd->null_pwd));
236 if (pwd->null_pwd)
238 return True;
241 prs_uint32("cleartext", ps, depth, (uint32 *)&(pwd->cleartext));
242 if (pwd->cleartext)
244 prs_string("password", ps, depth, pwd->password, strlen(pwd->password), sizeof(pwd->password));
245 prs_align(ps);
246 return True;
248 prs_uint32("crypted", ps, depth, (uint32 *)&(pwd->crypted));
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);
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))
260 return False;
262 prs_uint8s(False, "smb_nt_owf", ps, depth, (unsigned char*)&pwd->smb_nt_owf, pwd->nt_owf_len);
263 prs_align(ps);
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));
269 if (pwd->nt_cli_chal_len > sizeof(pwd->nt_cli_chal))
271 return False;
273 prs_uint8s(False, "nt_cli_chal", ps, depth, (unsigned char*)&pwd->nt_cli_chal, pwd->nt_cli_chal_len);
274 prs_align(ps);
276 return True;
279 /*******************************************************************
280 reads or writes a structure.
281 ********************************************************************/
282 BOOL creds_io_nt(char *desc, CREDS_NT *r_u, prs_struct *ps, int depth)
284 if (r_u == NULL) return False;
286 prs_debug(ps, depth, desc, "creds_io_nt");
287 depth++;
289 prs_align(ps);
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);
297 creds_io_pwd_info("pwd", &r_u->pwd, ps, depth);
298 prs_align(ps);
300 prs_uint32("ntlmssp", ps, depth, &(r_u->ntlmssp_flags));
302 return True;
305 /*******************************************************************
306 frees a structure.
307 ********************************************************************/
308 void creds_free_nt(CREDS_NT *r_u)
312 /*******************************************************************
313 reads or writes a structure.
314 ********************************************************************/
315 BOOL creds_io_hybrid(char *desc, CREDS_HYBRID *r_u, prs_struct *ps, int depth)
317 if (r_u == NULL) return False;
319 prs_debug(ps, depth, desc, "creds_io_hybrid");
320 depth++;
322 prs_align(ps);
324 prs_uint32("reuse", ps, depth, (uint32 *)&(r_u->reuse));
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)
333 if (!creds_io_nt ("ntc", &r_u->ntc, ps, depth)) return False;
335 if (r_u->ptr_uxc != 0)
337 if (!creds_io_unix("uxc", &r_u->uxc, ps, depth)) return False;
339 if (r_u->ptr_nts != 0)
341 if (!creds_io_nt_sec ("nts", &r_u->nts, ps, depth)) return False;
343 if (r_u->ptr_uxs != 0)
345 if (!creds_io_unix_sec("uxs", &r_u->uxs, ps, depth)) return False;
347 if (r_u->ptr_ssk != 0)
349 prs_uint8s(False, "usr_sess_key", ps, depth, (unsigned char*)&r_u->usr_sess_key, sizeof(r_u->usr_sess_key));
351 else
353 memset(r_u->usr_sess_key, 0, sizeof(r_u->usr_sess_key));
355 return True;
358 void copy_unix_creds(CREDS_UNIX *to, const CREDS_UNIX *from)
360 if (from == NULL)
362 to->user_name[0] = 0;
363 return;
365 fstrcpy(to->user_name, from->user_name);
368 void copy_nt_sec_creds(CREDS_NT_SEC *to, const CREDS_NT_SEC *from)
370 if (from == NULL)
372 ZERO_STRUCTP(to);
373 return;
375 sid_copy(&to->sid, &from->sid);
376 to->num_grps = 0;
377 to->grp_rids = NULL;
379 if (from->num_grps != 0)
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)
385 return;
387 to->num_grps = from->num_grps;
388 memcpy(to->grp_rids, from->grp_rids, size);
392 void copy_unix_sec_creds(CREDS_UNIX_SEC *to, const CREDS_UNIX_SEC *from)
394 if (from == NULL)
396 to->uid = -1;
397 to->gid = -1;
398 to->num_grps = 0;
399 to->grps = NULL;
400 return;
402 to->uid = from->uid;
403 to->gid = from->gid;
404 to->num_grps = 0;
405 to->grps = NULL;
407 if (from->num_grps != 0)
409 size_t size = from->num_grps * sizeof(from->grps[0]);
410 to->grps = (uint32*)malloc(size);
411 if (to->grps == NULL)
413 return;
415 to->num_grps = from->num_grps;
416 memcpy(to->grps, from->grps, size);
420 void create_ntc_from_cli_state (CREDS_NT *to, const struct cli_state *cli_from)
423 * NULL credentials --
424 * if this gets executed, it is a programming error.
425 * fall through to copy_nt_creds()
427 if (cli_from == NULL)
429 copy_nt_creds (to, NULL);
430 return;
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));
444 void copy_nt_creds(struct ntuser_creds *to,
445 const struct ntuser_creds *from)
447 if (from == NULL)
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;
455 return;
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));
466 void copy_user_creds(struct user_creds *to,
467 const struct user_creds *from)
469 ZERO_STRUCTP(to);
470 if (from == NULL)
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;
485 to->reuse = from->reuse;
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;
493 if (to->ptr_ntc != 0)
495 copy_nt_creds(&to->ntc, &from->ntc);
497 if (to->ptr_uxc != 0)
499 copy_unix_creds(&to->uxc, &from->uxc);
501 if (to->ptr_nts != 0)
503 copy_nt_sec_creds(&to->nts, &from->nts);
505 if (to->ptr_uxs != 0)
507 copy_unix_sec_creds(&to->uxs, &from->uxs);
509 if (to->ptr_ssk != 0)
511 memcpy(to->usr_sess_key, from->usr_sess_key,
512 sizeof(to->usr_sess_key));
516 void free_user_creds(struct user_creds *creds)
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);
524 /*******************************************************************
525 reads or writes a structure.
526 ********************************************************************/
527 BOOL creds_io_cmd(char *desc, CREDS_CMD *r_u, prs_struct *ps, int depth)
529 if (r_u == NULL) return False;
531 prs_debug(ps, depth, desc, "creds_io_cmd");
532 depth++;
534 prs_align(ps);
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 ));
540 prs_string("name ", ps, depth, r_u->name, strlen(r_u->name), sizeof(r_u->name));
541 prs_align(ps);
543 prs_uint32("ptr_creds", ps, depth, &(r_u->ptr_creds));
544 if (r_u->ptr_creds != 0)
546 if (!creds_io_hybrid("creds", r_u->cred, ps, depth))
548 return False;
553 return True;
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)
564 CREDS_CMD cmd;
565 struct user_creds usr;
567 ZERO_STRUCT(cmd);
568 ZERO_STRUCT(usr);
570 DEBUG(10,("create_user_creds: %s %d %d\n",
571 name, version, command));
573 usr.reuse = reuse;
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;
582 if (ntu != NULL)
584 copy_nt_creds(&usr.ntc, ntu);
585 usr.ptr_ntc = 1;
587 else
589 usr.ptr_ntc = 0;
592 prs_init(ps, 1024, NULL, MARSHALL);
594 ps->data_offset = 4;
595 return creds_io_cmd("creds", &cmd, ps, 0);
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)
604 CREDS_CMD cmd;
606 ZERO_STRUCT(cmd);
608 DEBUG(10,("create_user_creds: %s %d %d\n",
609 name, version, command));
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;
618 prs_init(ps, 1024, NULL, MARSHALL);
620 ps->data_offset = 4;
621 return creds_io_cmd("creds", &cmd, ps, 0);