Merge tpot's Python patch. ./configure now writes to the Python
[samba.git] / source3 / sam / get_set_account.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SAM_ACCOUNT_HANDLE access routines
4    Copyright (C) Andrew Bartlett                        2002
5    Copyright (C) Stefan (metze) Metzmacher              2002
6    Copyright (C) Jelmer Vernooij                        2002
7       
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_SAM
27
28 NTSTATUS sam_get_account_domain_sid(const SAM_ACCOUNT_HANDLE *sampass, const DOM_SID **sid)
29 {
30         NTSTATUS status;
31         SAM_DOMAIN_HANDLE *domain;
32         SAM_ASSERT(!sampass || !sid);
33
34         if (!NT_STATUS_IS_OK(status = sam_get_account_domain(sampass, &domain))){
35                 DEBUG(0, ("sam_get_account_domain_sid: Can't get domain for account\n"));
36                 return status;
37         }
38
39         return sam_get_domain_sid(domain, sid);
40 }
41
42 NTSTATUS sam_get_account_domain_name(const SAM_ACCOUNT_HANDLE *sampass, const char **domain_name)
43 {
44         NTSTATUS status;
45         SAM_DOMAIN_HANDLE *domain;
46         SAM_ASSERT(sampass && domain_name);
47
48         if (!NT_STATUS_IS_OK(status = sam_get_account_domain(sampass, &domain))){
49                 DEBUG(0, ("sam_get_account_domain_name: Can't get domain for account\n"));
50                 return status;
51         }
52
53         return sam_get_domain_name(domain, domain_name);
54 }
55
56 NTSTATUS sam_get_account_acct_ctrl(const SAM_ACCOUNT_HANDLE *sampass, uint16 *acct_ctrl)
57 {
58         SAM_ASSERT(sampass && acct_ctrl);
59
60         *acct_ctrl = sampass->private.acct_ctrl;
61
62         return NT_STATUS_OK;
63 }
64
65 NTSTATUS sam_get_account_logon_time(const SAM_ACCOUNT_HANDLE *sampass, NTTIME *logon_time)
66 {
67         SAM_ASSERT(sampass && logon_time) ;
68
69         *logon_time = sampass->private.logon_time;
70
71         return NT_STATUS_OK;
72 }
73
74 NTSTATUS sam_get_account_logoff_time(const SAM_ACCOUNT_HANDLE *sampass, NTTIME *logoff_time)
75 {
76         SAM_ASSERT(sampass && logoff_time) ;
77
78         *logoff_time = sampass->private.logoff_time;
79
80         return NT_STATUS_OK;
81 }
82
83 NTSTATUS sam_get_account_kickoff_time(const SAM_ACCOUNT_HANDLE *sampass, NTTIME *kickoff_time)
84 {
85         SAM_ASSERT(sampass && kickoff_time);
86
87         *kickoff_time = sampass->private.kickoff_time;
88
89         return NT_STATUS_OK;
90 }
91
92 NTSTATUS sam_get_account_pass_last_set_time(const SAM_ACCOUNT_HANDLE *sampass, NTTIME *pass_last_set_time)
93 {
94         SAM_ASSERT(sampass && pass_last_set_time);
95
96         *pass_last_set_time = sampass->private.pass_last_set_time;
97
98         return NT_STATUS_OK;
99 }
100
101 NTSTATUS sam_get_account_pass_can_change_time(const SAM_ACCOUNT_HANDLE *sampass, NTTIME *pass_can_change_time)
102 {
103         SAM_ASSERT(sampass && pass_can_change_time);
104
105         *pass_can_change_time = sampass->private.pass_can_change_time;
106
107         return NT_STATUS_OK;
108 }
109
110 NTSTATUS sam_get_account_pass_must_change_time(const SAM_ACCOUNT_HANDLE *sampass, NTTIME *pass_must_change_time)
111 {
112         SAM_ASSERT(sampass && pass_must_change_time);
113
114         *pass_must_change_time = sampass->private.pass_must_change_time;
115
116         return NT_STATUS_OK;
117 }
118
119 NTSTATUS sam_get_account_logon_divs(const SAM_ACCOUNT_HANDLE *sampass, uint16 *logon_divs)
120 {
121         SAM_ASSERT(sampass && logon_divs);
122
123         *logon_divs = sampass->private.logon_divs;
124
125         return NT_STATUS_OK;
126 }
127
128 NTSTATUS sam_get_account_hours_len(const SAM_ACCOUNT_HANDLE *sampass, uint32 *hours_len)
129 {
130         SAM_ASSERT(sampass && hours_len);
131
132         *hours_len = sampass->private.hours_len;
133
134         return NT_STATUS_OK;
135 }
136
137 NTSTATUS sam_get_account_hours(const SAM_ACCOUNT_HANDLE *sampass, const uint8 **hours)
138 {
139         SAM_ASSERT(sampass && hours);
140
141         *hours = sampass->private.hours;
142
143         return NT_STATUS_OK;
144 }
145
146 NTSTATUS sam_get_account_nt_pwd(const SAM_ACCOUNT_HANDLE *sampass, DATA_BLOB *nt_pwd)
147 {
148         SAM_ASSERT(sampass);
149
150         SMB_ASSERT((!sampass->private.nt_pw.data) 
151                    || sampass->private.nt_pw.length == NT_HASH_LEN);
152
153         *nt_pwd = sampass->private.nt_pw;
154
155         return NT_STATUS_OK;
156 }
157
158 NTSTATUS sam_get_account_lm_pwd(const SAM_ACCOUNT_HANDLE *sampass, DATA_BLOB *lm_pwd)
159
160         SAM_ASSERT(sampass);
161
162         SMB_ASSERT((!sampass->private.lm_pw.data) 
163                    || sampass->private.lm_pw.length == LM_HASH_LEN);
164
165         *lm_pwd = sampass->private.lm_pw;
166
167         return NT_STATUS_OK;
168 }
169
170 /* Return the plaintext password if known.  Most of the time
171    it isn't, so don't assume anything magic about this function.
172    
173    Used to pass the plaintext to sam backends that might 
174    want to store more than just the NTLM hashes.
175 */
176
177 NTSTATUS sam_get_account_plaintext_pwd(const SAM_ACCOUNT_HANDLE *sampass, char **plain_pwd)
178 {
179         SAM_ASSERT(sampass && plain_pwd);
180
181         *plain_pwd = sampass->private.plaintext_pw;
182
183         return NT_STATUS_OK;
184 }
185
186 NTSTATUS sam_get_account_sid(const SAM_ACCOUNT_HANDLE *sampass, const DOM_SID **sid)
187 {
188         SAM_ASSERT(sampass);
189
190         *sid = &(sampass->private.account_sid);
191
192         return NT_STATUS_OK;
193 }
194
195 NTSTATUS sam_get_account_pgroup(const SAM_ACCOUNT_HANDLE *sampass, const DOM_SID **sid)
196 {
197         SAM_ASSERT(sampass);
198
199         *sid = &(sampass->private.group_sid);
200
201         return NT_STATUS_OK;
202 }
203
204 /**
205  * Get flags showing what is initalised in the SAM_ACCOUNT_HANDLE
206  * @param sampass the SAM_ACCOUNT_HANDLE in question
207  * @return the flags indicating the members initialised in the struct.
208  **/
209  
210 NTSTATUS sam_get_account_init_flag(const SAM_ACCOUNT_HANDLE *sampass, uint32 *initflag)
211 {
212         SAM_ASSERT(sampass);
213
214         *initflag = sampass->private.init_flag;
215
216         return NT_STATUS_OK;
217 }
218
219 NTSTATUS sam_get_account_name(const SAM_ACCOUNT_HANDLE *sampass, char **account_name)
220 {
221         SAM_ASSERT(sampass);
222
223         *account_name = sampass->private.account_name;
224
225         return NT_STATUS_OK;
226 }
227
228 NTSTATUS sam_get_account_domain(const SAM_ACCOUNT_HANDLE *sampass, SAM_DOMAIN_HANDLE **domain)
229 {
230         SAM_ASSERT(sampass);
231
232         *domain = sampass->private.domain;
233
234         return NT_STATUS_OK;
235 }
236
237 NTSTATUS sam_get_account_fullname(const SAM_ACCOUNT_HANDLE *sampass, char **fullname)
238 {
239         SAM_ASSERT(sampass);
240
241         *fullname = sampass->private.full_name;
242
243         return NT_STATUS_OK;
244 }
245
246 NTSTATUS sam_get_account_homedir(const SAM_ACCOUNT_HANDLE *sampass, char **homedir)
247 {
248         SAM_ASSERT(sampass);
249
250         *homedir = sampass->private.home_dir;
251
252         return NT_STATUS_OK;
253 }
254
255 NTSTATUS sam_get_account_unix_home_dir(const SAM_ACCOUNT_HANDLE *sampass, char **uhomedir)
256 {
257         SAM_ASSERT(sampass);
258
259         *uhomedir = sampass->private.unix_home_dir;
260
261         return NT_STATUS_OK;
262 }
263
264 NTSTATUS sam_get_account_dir_drive(const SAM_ACCOUNT_HANDLE *sampass, char **dirdrive)
265 {
266         SAM_ASSERT(sampass);
267
268         *dirdrive = sampass->private.dir_drive;
269
270         return NT_STATUS_OK;
271 }
272
273 NTSTATUS sam_get_account_logon_script(const SAM_ACCOUNT_HANDLE *sampass, char **logon_script)
274 {
275         SAM_ASSERT(sampass);
276
277         *logon_script = sampass->private.logon_script;
278
279         return NT_STATUS_OK;
280 }
281
282 NTSTATUS sam_get_account_profile_path(const SAM_ACCOUNT_HANDLE *sampass, char **profile_path)
283 {
284         SAM_ASSERT(sampass);
285
286         *profile_path = sampass->private.profile_path;
287
288         return NT_STATUS_OK;
289 }
290
291 NTSTATUS sam_get_account_description(const SAM_ACCOUNT_HANDLE *sampass, char **description)
292 {
293         SAM_ASSERT(sampass);
294
295         *description = sampass->private.acct_desc;
296
297         return NT_STATUS_OK;
298 }
299
300 NTSTATUS sam_get_account_workstations(const SAM_ACCOUNT_HANDLE *sampass, char **workstations)
301 {
302         SAM_ASSERT(sampass);
303
304         *workstations = sampass->private.workstations;
305
306         return NT_STATUS_OK;
307 }
308
309 NTSTATUS sam_get_account_unknown_str(const SAM_ACCOUNT_HANDLE *sampass, char **unknown_str)
310 {
311         SAM_ASSERT(sampass);
312
313         *unknown_str = sampass->private.unknown_str;
314
315         return NT_STATUS_OK;
316 }
317
318 NTSTATUS sam_get_account_munged_dial(const SAM_ACCOUNT_HANDLE *sampass, char **munged_dial)
319 {
320         SAM_ASSERT(sampass);
321
322         *munged_dial = sampass->private.munged_dial;
323
324         return NT_STATUS_OK;
325 }
326
327 NTSTATUS sam_get_account_unknown_1(const SAM_ACCOUNT_HANDLE *sampass, uint32 *unknown1)
328 {
329         SAM_ASSERT(sampass && unknown1);
330
331         *unknown1 = sampass->private.unknown_1;
332
333         return NT_STATUS_OK;
334 }
335
336 NTSTATUS sam_get_account_unknown_2(const SAM_ACCOUNT_HANDLE *sampass, uint32 *unknown2)
337 {
338         SAM_ASSERT(sampass && unknown2);
339
340         *unknown2 = sampass->private.unknown_2;
341
342         return NT_STATUS_OK;
343 }
344
345 NTSTATUS sam_get_account_unknown_3(const SAM_ACCOUNT_HANDLE *sampass, uint32 *unknown3)
346 {
347         SAM_ASSERT(sampass && unknown3);
348
349         *unknown3 = sampass->private.unknown_3;
350
351         return NT_STATUS_OK;
352 }
353
354 /*********************************************************************
355  Collection of set...() functions for SAM_ACCOUNT_HANDLE_INFO.
356  ********************************************************************/
357
358 NTSTATUS sam_set_account_acct_ctrl(SAM_ACCOUNT_HANDLE *sampass, uint16 flags)
359 {
360         SAM_ASSERT(sampass);
361                 
362         sampass->private.acct_ctrl = flags;
363
364         return NT_STATUS_OK;
365 }
366
367 NTSTATUS sam_set_account_logon_time(SAM_ACCOUNT_HANDLE *sampass, NTTIME mytime, BOOL store)
368 {
369         SAM_ASSERT(sampass);
370
371         sampass->private.logon_time = mytime;
372
373         if (store)
374                 sam_set_account_init_flag(sampass, FLAG_SAM_LOGONTIME); 
375
376         return NT_STATUS_UNSUCCESSFUL;
377 }
378
379 NTSTATUS sam_set_account_logoff_time(SAM_ACCOUNT_HANDLE *sampass, NTTIME mytime, BOOL store)
380 {
381         SAM_ASSERT(sampass);
382
383         sampass->private.logoff_time = mytime;
384
385         if (store)
386                 sam_set_account_init_flag(sampass, FLAG_SAM_LOGOFFTIME); 
387
388         return NT_STATUS_OK;
389 }
390
391 NTSTATUS sam_set_account_kickoff_time(SAM_ACCOUNT_HANDLE *sampass, NTTIME mytime, BOOL store)
392 {
393         SAM_ASSERT(sampass);
394
395         sampass->private.kickoff_time = mytime;
396
397         if (store)
398                 sam_set_account_init_flag(sampass, FLAG_SAM_KICKOFFTIME); 
399
400         return NT_STATUS_OK;
401 }
402
403 NTSTATUS sam_set_account_pass_can_change_time(SAM_ACCOUNT_HANDLE *sampass, NTTIME mytime, BOOL store)
404 {
405         SAM_ASSERT(sampass);
406
407         sampass->private.pass_can_change_time = mytime;
408
409         if (store)
410                 sam_set_account_init_flag(sampass, FLAG_SAM_CANCHANGETIME); 
411
412         return NT_STATUS_OK;
413 }
414
415 NTSTATUS sam_set_account_pass_must_change_time(SAM_ACCOUNT_HANDLE *sampass, NTTIME mytime, BOOL store)
416 {
417         SAM_ASSERT(sampass);
418
419         sampass->private.pass_must_change_time = mytime;
420
421         if (store)
422                 sam_set_account_init_flag(sampass, FLAG_SAM_MUSTCHANGETIME); 
423
424         return NT_STATUS_OK;
425 }
426
427 NTSTATUS sam_set_account_pass_last_set_time(SAM_ACCOUNT_HANDLE *sampass, NTTIME mytime)
428 {
429         SAM_ASSERT(sampass);
430
431         sampass->private.pass_last_set_time = mytime;
432
433         return NT_STATUS_OK;
434 }
435
436 NTSTATUS sam_set_account_hours_len(SAM_ACCOUNT_HANDLE *sampass, uint32 len)
437 {
438         SAM_ASSERT(sampass);
439
440         sampass->private.hours_len = len;
441         return NT_STATUS_OK;
442 }
443
444 NTSTATUS sam_set_account_logon_divs(SAM_ACCOUNT_HANDLE *sampass, uint16 hours)
445 {
446         SAM_ASSERT(sampass);
447
448         sampass->private.logon_divs = hours;
449         return NT_STATUS_OK;
450 }
451
452 /**
453  * Set flags showing what is initalised in the SAM_ACCOUNT_HANDLE
454  * @param sampass the SAM_ACCOUNT_HANDLE in question
455  * @param flag The *new* flag to be set.  Old flags preserved
456  *             this flag is only added.  
457  **/
458  
459 NTSTATUS sam_set_account_init_flag(SAM_ACCOUNT_HANDLE *sampass, uint32 flag)
460 {
461         SAM_ASSERT(sampass);
462
463         sampass->private.init_flag |= flag;
464
465         return NT_STATUS_OK;
466 }
467
468 NTSTATUS sam_set_account_sid(SAM_ACCOUNT_HANDLE *sampass, const DOM_SID *u_sid)
469 {
470         SAM_ASSERT(sampass && u_sid);
471         
472         sid_copy(&sampass->private.account_sid, u_sid);
473
474         DEBUG(10, ("sam_set_account_sid: setting account sid %s\n", 
475                     sid_string_static(&sampass->private.account_sid)));
476         
477         return NT_STATUS_OK;
478 }
479
480 NTSTATUS sam_set_account_sid_from_string(SAM_ACCOUNT_HANDLE *sampass, const char *u_sid)
481 {
482         DOM_SID new_sid;
483         SAM_ASSERT(sampass && u_sid);
484
485         DEBUG(10, ("sam_set_account_sid_from_string: setting account sid %s\n",
486                    u_sid));
487
488         if (!string_to_sid(&new_sid, u_sid)) { 
489                 DEBUG(1, ("sam_set_account_sid_from_string: %s isn't a valid SID!\n", u_sid));
490                 return NT_STATUS_UNSUCCESSFUL;
491         }
492          
493         if (!NT_STATUS_IS_OK(sam_set_account_sid(sampass, &new_sid))) {
494                 DEBUG(1, ("sam_set_account_sid_from_string: could not set sid %s on SAM_ACCOUNT_HANDLE!\n", u_sid));
495                 return NT_STATUS_UNSUCCESSFUL;
496         }
497
498         return NT_STATUS_OK;
499 }
500
501 NTSTATUS sam_set_account_pgroup_sid(SAM_ACCOUNT_HANDLE *sampass, const DOM_SID *g_sid)
502 {
503         SAM_ASSERT(sampass && g_sid);
504
505         sid_copy(&sampass->private.group_sid, g_sid);
506
507         DEBUG(10, ("sam_set_group_sid: setting group sid %s\n", 
508                     sid_string_static(&sampass->private.group_sid)));
509
510         return NT_STATUS_OK;
511 }
512
513 NTSTATUS sam_set_account_pgroup_string(SAM_ACCOUNT_HANDLE *sampass, const char *g_sid)
514 {
515         DOM_SID new_sid;
516         SAM_ASSERT(sampass && g_sid);
517
518         DEBUG(10, ("sam_set_group_sid_from_string: setting group sid %s\n",
519                    g_sid));
520
521         if (!string_to_sid(&new_sid, g_sid)) { 
522                 DEBUG(1, ("sam_set_group_sid_from_string: %s isn't a valid SID!\n", g_sid));
523                 return NT_STATUS_UNSUCCESSFUL;
524         }
525          
526         if (!NT_STATUS_IS_OK(sam_set_account_pgroup_sid(sampass, &new_sid))) {
527                 DEBUG(1, ("sam_set_group_sid_from_string: could not set sid %s on SAM_ACCOUNT_HANDLE!\n", g_sid));
528                 return NT_STATUS_UNSUCCESSFUL;
529         }
530         return NT_STATUS_OK;
531 }
532
533 /*********************************************************************
534  Set the domain name.
535  ********************************************************************/
536
537 NTSTATUS sam_set_account_domain(SAM_ACCOUNT_HANDLE *sampass, SAM_DOMAIN_HANDLE *domain)
538 {       
539         SAM_ASSERT(sampass);
540
541         sampass->private.domain = domain;
542
543         return NT_STATUS_OK;
544 }
545
546 /*********************************************************************
547  Set the account's NT name.
548  ********************************************************************/
549
550 NTSTATUS sam_set_account_name(SAM_ACCOUNT_HANDLE *sampass, const char *account_name)
551 {
552         SAM_ASSERT(sampass);
553
554         DEBUG(10, ("sam_set_account_name: setting nt account_name %s, was %s\n", account_name, sampass->private.account_name));
555
556         sampass->private.account_name = talloc_strdup(sampass->mem_ctx, account_name);
557
558         return NT_STATUS_OK;
559 }
560
561 /*********************************************************************
562  Set the account's full name.
563  ********************************************************************/
564
565 NTSTATUS sam_set_account_fullname(SAM_ACCOUNT_HANDLE *sampass, const char *full_name)
566 {
567         SAM_ASSERT(sampass);
568
569         DEBUG(10, ("sam_set_account_fullname: setting full name %s, was %s\n", full_name, sampass->private.full_name));
570
571         sampass->private.full_name = talloc_strdup(sampass->mem_ctx, full_name);
572
573         return NT_STATUS_OK;
574 }
575
576 /*********************************************************************
577  Set the account's logon script.
578  ********************************************************************/
579
580 NTSTATUS sam_set_account_logon_script(SAM_ACCOUNT_HANDLE *sampass, const char *logon_script, BOOL store)
581 {
582         SAM_ASSERT(sampass);
583
584         DEBUG(10, ("sam_set_logon_script: from %s to %s\n", logon_script, sampass->private.logon_script));
585
586         sampass->private.logon_script = talloc_strdup(sampass->mem_ctx, logon_script);
587         
588         sam_set_account_init_flag(sampass, FLAG_SAM_LOGONSCRIPT);
589
590         return NT_STATUS_OK;
591 }
592
593 /*********************************************************************
594  Set the account's profile path.
595  ********************************************************************/
596
597 NTSTATUS sam_set_account_profile_path(SAM_ACCOUNT_HANDLE *sampass, const char *profile_path, BOOL store)
598 {
599         SAM_ASSERT(sampass);
600
601         DEBUG(10, ("sam_set_profile_path: setting profile path %s, was %s\n", profile_path, sampass->private.profile_path));
602  
603         sampass->private.profile_path = talloc_strdup(sampass->mem_ctx, profile_path);
604                 
605         if (store) {
606                 DEBUG(10, ("sam_set_profile_path: setting profile path sam flag!\n"));
607                 sam_set_account_init_flag(sampass, FLAG_SAM_PROFILE);
608         }
609
610         return NT_STATUS_OK;
611 }
612
613 /*********************************************************************
614  Set the account's directory drive.
615  ********************************************************************/
616
617 NTSTATUS sam_set_account_dir_drive(SAM_ACCOUNT_HANDLE *sampass, const char *dir_drive, BOOL store)
618 {
619         SAM_ASSERT(sampass);
620
621         DEBUG(10, ("sam_set_dir_drive: setting dir drive %s, was %s\n", dir_drive,
622                         sampass->private.dir_drive));
623  
624         sampass->private.dir_drive = talloc_strdup(sampass->mem_ctx, dir_drive);
625                 
626         if (store) {
627                 DEBUG(10, ("sam_set_dir_drive: setting dir drive sam flag!\n"));
628                 sam_set_account_init_flag(sampass, FLAG_SAM_DRIVE);
629         }
630
631         return NT_STATUS_OK;
632 }
633
634 /*********************************************************************
635  Set the account's home directory.
636  ********************************************************************/
637
638 NTSTATUS sam_set_account_homedir(SAM_ACCOUNT_HANDLE *sampass, const char *home_dir, BOOL store)
639 {
640         SAM_ASSERT(sampass);
641
642         DEBUG(10, ("sam_set_homedir: setting home dir %s, was %s\n", home_dir,
643                 sampass->private.home_dir));
644  
645         sampass->private.home_dir = talloc_strdup(sampass->mem_ctx, home_dir);
646                 
647         if (store) {
648                 DEBUG(10, ("sam_set_homedir: setting home dir sam flag!\n"));
649                 sam_set_account_init_flag(sampass, FLAG_SAM_SMBHOME);
650         }
651
652         return NT_STATUS_OK;
653 }
654
655 /*********************************************************************
656  Set the account's unix home directory.
657  ********************************************************************/
658
659 NTSTATUS sam_set_account_unix_homedir(SAM_ACCOUNT_HANDLE *sampass, const char *unix_home_dir)
660 {
661         SAM_ASSERT(sampass);
662
663         DEBUG(10, ("sam_set_unix_homedir: setting home dir %s, was %s\n", unix_home_dir,
664                 sampass->private.unix_home_dir));
665  
666         sampass->private.unix_home_dir = talloc_strdup(sampass->mem_ctx, unix_home_dir);
667                 
668         return NT_STATUS_OK;
669 }
670
671 /*********************************************************************
672  Set the account's account description.
673  ********************************************************************/
674
675 NTSTATUS sam_set_account_acct_desc(SAM_ACCOUNT_HANDLE *sampass, const char *acct_desc)
676 {
677         SAM_ASSERT(sampass);
678
679         sampass->private.acct_desc = talloc_strdup(sampass->mem_ctx, acct_desc);
680
681         return NT_STATUS_OK;
682 }
683
684 /*********************************************************************
685  Set the account's workstation allowed list.
686  ********************************************************************/
687
688 NTSTATUS sam_set_account_workstations(SAM_ACCOUNT_HANDLE *sampass, const char *workstations)
689 {
690         SAM_ASSERT(sampass);
691
692         DEBUG(10, ("sam_set_workstations: setting workstations %s, was %s\n", workstations,
693                         sampass->private.workstations));
694  
695         sampass->private.workstations = talloc_strdup(sampass->mem_ctx, workstations);
696
697         return NT_STATUS_OK;
698 }
699
700 /*********************************************************************
701  Set the account's 'unknown_str', whatever the heck this actually is...
702  ********************************************************************/
703
704 NTSTATUS sam_set_account_unknown_str(SAM_ACCOUNT_HANDLE *sampass, const char *unknown_str)
705 {
706         SAM_ASSERT(sampass);
707
708         sampass->private.unknown_str = talloc_strdup(sampass->mem_ctx, unknown_str);
709                 
710         return NT_STATUS_OK;
711 }
712
713 /*********************************************************************
714  Set the account's dial string.
715  ********************************************************************/
716
717 NTSTATUS sam_set_account_munged_dial(SAM_ACCOUNT_HANDLE *sampass, const char *munged_dial)
718 {
719         SAM_ASSERT(sampass);
720
721         sampass->private.munged_dial = talloc_strdup(sampass->mem_ctx, munged_dial);
722         return NT_STATUS_OK;
723 }
724
725 /*********************************************************************
726  Set the account's NT hash.
727  ********************************************************************/
728
729 NTSTATUS sam_set_account_nt_pwd(SAM_ACCOUNT_HANDLE *sampass, const DATA_BLOB data)
730 {
731         SAM_ASSERT(sampass);
732
733         sampass->private.nt_pw = data;
734
735         return NT_STATUS_OK;
736 }
737
738 /*********************************************************************
739  Set the account's LM hash.
740  ********************************************************************/
741
742 NTSTATUS sam_set_account_lm_pwd(SAM_ACCOUNT_HANDLE *sampass, const DATA_BLOB data)
743 {
744         SAM_ASSERT(sampass);
745
746         sampass->private.lm_pw = data;
747
748         return NT_STATUS_OK;
749 }
750
751 /*********************************************************************
752  Set the account's plaintext password only (base procedure, see helper
753  below)
754  ********************************************************************/
755
756 NTSTATUS sam_set_account_plaintext_pwd(SAM_ACCOUNT_HANDLE *sampass, const char *plain_pwd)
757 {
758         SAM_ASSERT(sampass);
759
760         sampass->private.plaintext_pw = talloc_strdup(sampass->mem_ctx, plain_pwd);
761
762         return NT_STATUS_OK;
763 }
764
765 NTSTATUS sam_set_account_unknown_1(SAM_ACCOUNT_HANDLE *sampass, uint32 unkn)
766 {
767         SAM_ASSERT(sampass);
768
769         sampass->private.unknown_1 = unkn;
770
771         return NT_STATUS_OK;
772 }
773
774 NTSTATUS sam_set_account_unknown_2(SAM_ACCOUNT_HANDLE *sampass, uint32 unkn)
775 {
776         SAM_ASSERT(sampass);
777
778         sampass->private.unknown_2 = unkn;
779
780         return NT_STATUS_OK;
781 }
782
783 NTSTATUS sam_set_account_unknown_3(SAM_ACCOUNT_HANDLE *sampass, uint32 unkn)
784 {
785         SAM_ASSERT(sampass);
786
787         sampass->private.unknown_3 = unkn;
788         return NT_STATUS_OK;
789 }
790
791 NTSTATUS sam_set_account_hours(SAM_ACCOUNT_HANDLE *sampass, const uint8 *hours)
792 {
793         SAM_ASSERT(sampass);
794
795         if (!hours) {
796                 memset ((char *)sampass->private.hours, 0, MAX_HOURS_LEN);
797                 return NT_STATUS_OK;
798         }
799         
800         memcpy(sampass->private.hours, hours, MAX_HOURS_LEN);
801
802         return NT_STATUS_OK;
803 }
804
805 /* Helpful interfaces to the above */
806
807 /*********************************************************************
808  Sets the last changed times and must change times for a normal
809  password change.
810  ********************************************************************/
811
812 NTSTATUS sam_set_account_pass_changed_now(SAM_ACCOUNT_HANDLE *sampass)
813 {
814         uint32 expire;
815         NTTIME temptime;
816
817         SAM_ASSERT(sampass);
818         
819         unix_to_nt_time(&temptime, time(NULL));
820         if (!NT_STATUS_IS_OK(sam_set_account_pass_last_set_time(sampass, temptime)))
821                 return NT_STATUS_UNSUCCESSFUL;
822
823         if (!account_policy_get(AP_MAX_PASSWORD_AGE, &expire) 
824             || (expire==(uint32)-1)) {
825
826                 get_nttime_max(&temptime);
827                 if (!NT_STATUS_IS_OK(sam_set_account_pass_must_change_time(sampass, temptime, False)))
828                         return NT_STATUS_UNSUCCESSFUL;
829
830         } else {
831                 /* FIXME: Add expire to temptime */
832                 
833                 if (!NT_STATUS_IS_OK(sam_get_account_pass_last_set_time(sampass,&temptime)) || !NT_STATUS_IS_OK(sam_set_account_pass_must_change_time(sampass, temptime,True)))
834                         return NT_STATUS_UNSUCCESSFUL;
835         }
836         
837         return NT_STATUS_OK;
838 }
839
840 /*********************************************************************
841  Set the account's PLAINTEXT password.  Used as an interface to the above.
842  Also sets the last change time to NOW.
843  ********************************************************************/
844
845 NTSTATUS sam_set_account_passwd(SAM_ACCOUNT_HANDLE *sampass, const char *plaintext)
846 {
847         DATA_BLOB data;
848         uchar new_lanman_p16[16];
849         uchar new_nt_p16[16];
850
851         SAM_ASSERT(sampass && plaintext);
852         
853         nt_lm_owf_gen(plaintext, new_nt_p16, new_lanman_p16);
854
855         data = data_blob(new_nt_p16, 16);
856         if (!NT_STATUS_IS_OK(sam_set_account_nt_pwd(sampass, data)))
857                 return NT_STATUS_UNSUCCESSFUL;
858
859         data = data_blob(new_lanman_p16, 16);
860
861         if (!NT_STATUS_IS_OK(sam_set_account_lm_pwd(sampass, data)))
862                 return NT_STATUS_UNSUCCESSFUL;
863
864         if (!NT_STATUS_IS_OK(sam_set_account_plaintext_pwd(sampass, plaintext)))
865                 return NT_STATUS_UNSUCCESSFUL;
866         
867         if (!NT_STATUS_IS_OK(sam_set_account_pass_changed_now(sampass)))
868                 return NT_STATUS_UNSUCCESSFUL;
869
870         return NT_STATUS_OK;
871 }
872