2 * Unix SMB/Netbios implementation.
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.
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.
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,
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);
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");
57 prs_string("user_name", ps, depth, r_u->user_name, strlen(r_u->user_name), sizeof(r_u->user_name));
59 prs_string("requested_name", ps, depth, r_u->requested_name, strlen(r_u->requested_name), sizeof(r_u->requested_name));
61 prs_string("real_name", ps, depth, r_u->real_name, strlen(r_u->real_name), sizeof(r_u->real_name));
63 prs_uint32("guest", ps, depth, (uint32 *)&(r_u->guest));
68 /*******************************************************************
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)
82 if (r_u == NULL) return False;
84 DEBUG(5,("make_creds_unix_sec\n"));
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)
94 for (i = 0; i < num_grps; i++)
96 r_u->grps[i] = (gid_t)grps[i];
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)
109 if (r_u == NULL) return False;
111 prs_debug(ps, depth, desc, "creds_io_unix_sec");
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)
123 tgr = (uint32*)Realloc(r_u->grps,
124 sizeof(r_u->grps[0]) *
128 creds_free_unix_sec(r_u);
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]));
141 /*******************************************************************
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)
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)
168 for (i = 0; i < num_grps; i++)
170 r_u->grp_rids[i] = grps[i];
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)
182 if (r_u == NULL) return False;
184 prs_debug(ps, depth, desc, "creds_io_nt");
189 smb_io_dom_sid ("sid", &r_u->sid, ps, depth);
192 prs_uint32("num_grps", ps, depth, &(r_u->num_grps));
193 if (r_u->num_grps != 0)
197 tgrid = (uint32*)Realloc(r_u->grp_rids,
198 sizeof(r_u->grp_rids[0]) *
202 creds_free_nt_sec(r_u);
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]));
215 /*******************************************************************
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");
235 prs_uint32("nullpwd", ps, depth, (uint32 *)&(pwd->null_pwd));
241 prs_uint32("cleartext", ps, depth, (uint32 *)&(pwd->cleartext));
244 prs_string("password", ps, depth, pwd->password, strlen(pwd->password), sizeof(pwd->password));
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));
252 prs_uint8s(False, "smb_nt_pwd", ps, depth, (unsigned char*)&pwd->smb_nt_pwd, sizeof(pwd->smb_nt_pwd));
255 prs_uint8s(False, "smb_lm_owf", ps, depth, (unsigned char*)&pwd->smb_lm_owf, sizeof(pwd->smb_lm_owf));
257 prs_uint32("nt_owf_len", ps, depth, &(pwd->nt_owf_len));
258 if (pwd->nt_owf_len > sizeof(pwd->smb_nt_owf))
262 prs_uint8s(False, "smb_nt_owf", ps, depth, (unsigned char*)&pwd->smb_nt_owf, pwd->nt_owf_len);
265 prs_uint8s(False, "lm_cli_chal", ps, depth, (unsigned char*)&pwd->lm_cli_chal, sizeof(pwd->lm_cli_chal));
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))
273 prs_uint8s(False, "nt_cli_chal", ps, depth, (unsigned char*)&pwd->nt_cli_chal, pwd->nt_cli_chal_len);
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");
291 /* lkclXXXX CHEAT!!!!!!!! */
292 prs_string("user_name", ps, depth, r_u->user_name, strlen(r_u->user_name), sizeof(r_u->user_name));
294 prs_string("domain", ps, depth, r_u->domain, strlen(r_u->domain), sizeof(r_u->domain));
297 creds_io_pwd_info("pwd", &r_u->pwd, ps, depth);
300 prs_uint32("ntlmssp", ps, depth, &(r_u->ntlmssp_flags));
305 /*******************************************************************
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");
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));
353 memset(r_u->usr_sess_key, 0, sizeof(r_u->usr_sess_key));
358 void copy_unix_creds(CREDS_UNIX *to, const CREDS_UNIX *from)
362 to->user_name[0] = 0;
365 fstrcpy(to->user_name, from->user_name);
368 void copy_nt_sec_creds(CREDS_NT_SEC *to, const CREDS_NT_SEC *from)
375 sid_copy(&to->sid, &from->sid);
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)
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)
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)
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);
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,
444 void copy_nt_creds(struct ntuser_creds *to,
445 const struct ntuser_creds *from)
449 DEBUG(10,("copy_nt_creds: null creds\n"));
451 to->user_name[0] = 0;
452 pwd_set_nullpwd(&to->pwd);
453 to->ntlmssp_flags = 0;
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,
466 void copy_user_creds(struct user_creds *to,
467 const struct user_creds *from)
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);
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");
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));
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))
557 BOOL create_ntuser_creds( prs_struct *ps,
559 uint16 version, uint16 command,
561 const struct ntuser_creds *ntu,
565 struct user_creds usr;
570 DEBUG(10,("create_user_creds: %s %d %d\n",
571 name, version, command));
575 fstrcpy(cmd.name, name);
576 cmd.version = version;
577 cmd.command = command;
579 cmd.ptr_creds = ntu != NULL ? 1 : 0;
584 copy_nt_creds(&usr.ntc, ntu);
592 prs_init(ps, 1024, NULL, MARSHALL);
595 return creds_io_cmd("creds", &cmd, ps, 0);
598 BOOL create_user_creds( prs_struct *ps,
600 uint16 version, uint16 command,
602 struct user_creds *usr)
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;
615 cmd.ptr_creds = usr != NULL ? 1 : 0;
618 prs_init(ps, 1024, NULL, MARSHALL);
621 return creds_io_cmd("creds", &cmd, ps, 0);