forked from samba-team/samba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passdb.c
2755 lines (2366 loc) · 74 KB
/
passdb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Unix SMB/CIFS implementation.
Password and authentication handling
Copyright (C) Jeremy Allison 1996-2001
Copyright (C) Luke Kenneth Casson Leighton 1996-1998
Copyright (C) Gerald (Jerry) Carter 2000-2006
Copyright (C) Andrew Bartlett 2001-2002
Copyright (C) Simo Sorce 2003
Copyright (C) Volker Lendecke 2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "passdb.h"
#include "system/passwd.h"
#include "../libcli/auth/libcli_auth.h"
#include "secrets.h"
#include "../libcli/security/security.h"
#include "../lib/util/util_pw.h"
#include "util_tdb.h"
#include "auth/credentials/credentials.h"
#include "lib/param/param.h"
#include "lib/util/string_wrappers.h"
#include "source3/lib/substitute.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_PASSDB
/**********************************************************************
***********************************************************************/
static int samu_destroy(struct samu *user)
{
data_blob_clear_free( &user->lm_pw );
data_blob_clear_free( &user->nt_pw );
if ( user->plaintext_pw )
memset( user->plaintext_pw, 0x0, strlen(user->plaintext_pw) );
return 0;
}
/**********************************************************************
generate a new struct samuser
***********************************************************************/
struct samu *samu_new( TALLOC_CTX *ctx )
{
struct samu *user;
if ( !(user = talloc_zero( ctx, struct samu )) ) {
DEBUG(0,("samuser_new: Talloc failed!\n"));
return NULL;
}
talloc_set_destructor( user, samu_destroy );
/* no initial methods */
user->methods = NULL;
/* Don't change these timestamp settings without a good reason.
They are important for NT member server compatibility. */
user->logon_time = (time_t)0;
user->pass_last_set_time = (time_t)0;
user->pass_can_change_time = (time_t)0;
user->logoff_time = get_time_t_max();
user->kickoff_time = get_time_t_max();
user->fields_present = 0x00ffffff;
user->logon_divs = 168; /* hours per week */
user->hours_len = 21; /* 21 times 8 bits = 168 */
memset(user->hours, 0xff, user->hours_len); /* available at all hours */
user->bad_password_count = 0;
user->logon_count = 0;
user->unknown_6 = 0x000004ec; /* don't know */
/* Some parts of samba strlen their pdb_get...() returns,
so this keeps the interface unchanged for now. */
user->username = "";
user->domain = "";
user->nt_username = "";
user->full_name = "";
user->home_dir = "";
user->logon_script = "";
user->profile_path = "";
user->acct_desc = "";
user->workstations = "";
user->comment = "";
user->munged_dial = "";
user->plaintext_pw = NULL;
/* Unless we know otherwise have a Account Control Bit
value of 'normal user'. This helps User Manager, which
asks for a filtered list of users. */
user->acct_ctrl = ACB_NORMAL;
return user;
}
static int count_commas(const char *str)
{
int num_commas = 0;
const char *comma = str;
while ((comma = strchr(comma, ',')) != NULL) {
comma += 1;
num_commas += 1;
}
return num_commas;
}
/*********************************************************************
Initialize a struct samu from a struct passwd including the user
and group SIDs. The *user structure is filled out with the Unix
attributes and a user SID.
*********************************************************************/
static NTSTATUS samu_set_unix_internal(struct pdb_methods *methods,
struct samu *user, const struct passwd *pwd, bool create)
{
const char *guest_account = lp_guest_account();
const char *domain = lp_netbios_name();
char *fullname;
uint32_t urid;
bool ok;
if ( !pwd ) {
return NT_STATUS_NO_SUCH_USER;
}
/* Basic properties based upon the Unix account information */
ok = pdb_set_username(user, pwd->pw_name, PDB_SET);
if (!ok) {
return NT_STATUS_NO_MEMORY;
}
fullname = NULL;
if (count_commas(pwd->pw_gecos) == 3) {
/*
* Heuristic: This seems to be a gecos field that has been
* edited by chfn(1). Only use the part before the first
* comma. Fixes bug 5198.
*/
fullname = talloc_strndup(
talloc_tos(), pwd->pw_gecos,
strchr(pwd->pw_gecos, ',') - pwd->pw_gecos);
if (fullname == NULL) {
return NT_STATUS_NO_MEMORY;
}
}
if (fullname != NULL) {
ok = pdb_set_fullname(user, fullname, PDB_SET);
} else {
ok = pdb_set_fullname(user, pwd->pw_gecos, PDB_SET);
}
TALLOC_FREE(fullname);
if (!ok) {
return NT_STATUS_NO_MEMORY;
}
ok = pdb_set_domain(user, get_global_sam_name(), PDB_DEFAULT);
if (!ok) {
return NT_STATUS_NO_MEMORY;
}
#if 0
/* This can lead to a primary group of S-1-22-2-XX which
will be rejected by other parts of the Samba code.
Rely on pdb_get_group_sid() to "Do The Right Thing" (TM)
--jerry */
gid_to_sid(&group_sid, pwd->pw_gid);
pdb_set_group_sid(user, &group_sid, PDB_SET);
#endif
/* save the password structure for later use */
user->unix_pw = tcopy_passwd( user, pwd );
if (user->unix_pw == NULL) {
return NT_STATUS_NO_MEMORY;
}
/* Special case for the guest account which must have a RID of 501 */
if ( strequal( pwd->pw_name, guest_account ) ) {
if ( !pdb_set_user_sid_from_rid(user, DOMAIN_RID_GUEST, PDB_DEFAULT)) {
return NT_STATUS_NO_SUCH_USER;
}
return NT_STATUS_OK;
}
/* Non-guest accounts...Check for a workstation or user account */
if (pwd->pw_name[strlen(pwd->pw_name)-1] == '$') {
/* workstation */
if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_DEFAULT)) {
DEBUG(1, ("Failed to set 'workstation account' flags for user %s.\n",
pwd->pw_name));
return NT_STATUS_INVALID_COMPUTER_NAME;
}
}
else {
/* user */
if (!pdb_set_acct_ctrl(user, ACB_NORMAL, PDB_DEFAULT)) {
DEBUG(1, ("Failed to set 'normal account' flags for user %s.\n",
pwd->pw_name));
return NT_STATUS_INVALID_ACCOUNT_NAME;
}
/* set some basic attributes */
ok = pdb_set_profile_path(
user,
talloc_sub_specified(
user,
lp_logon_path(),
pwd->pw_name,
NULL,
domain,
pwd->pw_uid,
pwd->pw_gid),
PDB_DEFAULT);
ok &= pdb_set_homedir(
user,
talloc_sub_specified(
user,
lp_logon_home(),
pwd->pw_name,
NULL,
domain,
pwd->pw_uid,
pwd->pw_gid),
PDB_DEFAULT);
ok &= pdb_set_dir_drive(
user,
talloc_sub_specified(
user,
lp_logon_drive(),
pwd->pw_name,
NULL,
domain,
pwd->pw_uid,
pwd->pw_gid),
PDB_DEFAULT);
ok &= pdb_set_logon_script(
user,
talloc_sub_specified(
user,
lp_logon_script(),
pwd->pw_name,
NULL,
domain,
pwd->pw_uid,
pwd->pw_gid),
PDB_DEFAULT);
if (!ok) {
return NT_STATUS_NO_MEMORY;
}
}
/* Now deal with the user SID. If we have a backend that can generate
RIDs, then do so. But sometimes the caller just wanted a structure
initialized and will fill in these fields later (such as from a
netr_SamInfo3 structure) */
if ( create && (methods->capabilities(methods) & PDB_CAP_STORE_RIDS)) {
uint32_t user_rid;
struct dom_sid user_sid;
if ( !methods->new_rid(methods, &user_rid) ) {
DEBUG(3, ("Could not allocate a new RID\n"));
return NT_STATUS_ACCESS_DENIED;
}
sid_compose(&user_sid, get_global_sam_sid(), user_rid);
if ( !pdb_set_user_sid(user, &user_sid, PDB_SET) ) {
DEBUG(3, ("pdb_set_user_sid failed\n"));
return NT_STATUS_INTERNAL_ERROR;
}
return NT_STATUS_OK;
}
/* generate a SID for the user with the RID algorithm */
urid = algorithmic_pdb_uid_to_user_rid( user->unix_pw->pw_uid );
if ( !pdb_set_user_sid_from_rid( user, urid, PDB_SET) ) {
return NT_STATUS_INTERNAL_ERROR;
}
return NT_STATUS_OK;
}
/********************************************************************
Set the Unix user attributes
********************************************************************/
NTSTATUS samu_set_unix(struct samu *user, const struct passwd *pwd)
{
return samu_set_unix_internal( NULL, user, pwd, False );
}
NTSTATUS samu_alloc_rid_unix(struct pdb_methods *methods,
struct samu *user, const struct passwd *pwd)
{
return samu_set_unix_internal( methods, user, pwd, True );
}
/**********************************************************
Encode the account control bits into a string.
length = length of string to encode into (including terminating
null). length *MUST BE MORE THAN 2* !
**********************************************************/
char *pdb_encode_acct_ctrl(uint32_t acct_ctrl, size_t length)
{
fstring acct_str;
char *result;
size_t i = 0;
SMB_ASSERT(length <= sizeof(acct_str));
acct_str[i++] = '[';
if (acct_ctrl & ACB_PWNOTREQ ) acct_str[i++] = 'N';
if (acct_ctrl & ACB_DISABLED ) acct_str[i++] = 'D';
if (acct_ctrl & ACB_HOMDIRREQ) acct_str[i++] = 'H';
if (acct_ctrl & ACB_TEMPDUP ) acct_str[i++] = 'T';
if (acct_ctrl & ACB_NORMAL ) acct_str[i++] = 'U';
if (acct_ctrl & ACB_MNS ) acct_str[i++] = 'M';
if (acct_ctrl & ACB_WSTRUST ) acct_str[i++] = 'W';
if (acct_ctrl & ACB_SVRTRUST ) acct_str[i++] = 'S';
if (acct_ctrl & ACB_AUTOLOCK ) acct_str[i++] = 'L';
if (acct_ctrl & ACB_PWNOEXP ) acct_str[i++] = 'X';
if (acct_ctrl & ACB_DOMTRUST ) acct_str[i++] = 'I';
for ( ; i < length - 2 ; i++ )
acct_str[i] = ' ';
i = length - 2;
acct_str[i++] = ']';
acct_str[i++] = '\0';
result = talloc_strdup(talloc_tos(), acct_str);
SMB_ASSERT(result != NULL);
return result;
}
/**********************************************************
Decode the account control bits from a string.
**********************************************************/
uint32_t pdb_decode_acct_ctrl(const char *p)
{
uint32_t acct_ctrl = 0;
bool finished = false;
/*
* Check if the account type bits have been encoded after the
* NT password (in the form [NDHTUWSLXI]).
*/
if (*p != '[')
return 0;
for (p++; *p && !finished; p++) {
switch (*p) {
case 'N': { acct_ctrl |= ACB_PWNOTREQ ; break; /* 'N'o password. */ }
case 'D': { acct_ctrl |= ACB_DISABLED ; break; /* 'D'isabled. */ }
case 'H': { acct_ctrl |= ACB_HOMDIRREQ; break; /* 'H'omedir required. */ }
case 'T': { acct_ctrl |= ACB_TEMPDUP ; break; /* 'T'emp account. */ }
case 'U': { acct_ctrl |= ACB_NORMAL ; break; /* 'U'ser account (normal). */ }
case 'M': { acct_ctrl |= ACB_MNS ; break; /* 'M'NS logon user account. What is this ? */ }
case 'W': { acct_ctrl |= ACB_WSTRUST ; break; /* 'W'orkstation account. */ }
case 'S': { acct_ctrl |= ACB_SVRTRUST ; break; /* 'S'erver account. */ }
case 'L': { acct_ctrl |= ACB_AUTOLOCK ; break; /* 'L'ocked account. */ }
case 'X': { acct_ctrl |= ACB_PWNOEXP ; break; /* No 'X'piry on password */ }
case 'I': { acct_ctrl |= ACB_DOMTRUST ; break; /* 'I'nterdomain trust account. */ }
case ' ': { break; }
case ':':
case '\n':
case '\0':
case ']':
default: { finished = true; }
}
}
return acct_ctrl;
}
/*************************************************************
Routine to set 32 hex password characters from a 16 byte array.
**************************************************************/
void pdb_sethexpwd(char p[33], const unsigned char *pwd, uint32_t acct_ctrl)
{
if (pwd != NULL) {
hex_encode_buf(p, pwd, 16);
} else {
if (acct_ctrl & ACB_PWNOTREQ)
strlcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
else
strlcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
}
}
/*************************************************************
Routine to get the 32 hex characters and turn them
into a 16 byte array.
**************************************************************/
bool pdb_gethexpwd(const char *p, unsigned char *pwd)
{
int i;
unsigned char lonybble, hinybble;
const char *hexchars = "0123456789ABCDEF";
char *p1, *p2;
if (!p)
return false;
for (i = 0; i < 32; i += 2) {
hinybble = toupper_m(p[i]);
lonybble = toupper_m(p[i + 1]);
p1 = strchr(hexchars, hinybble);
p2 = strchr(hexchars, lonybble);
if (!p1 || !p2)
return false;
hinybble = PTR_DIFF(p1, hexchars);
lonybble = PTR_DIFF(p2, hexchars);
pwd[i / 2] = (hinybble << 4) | lonybble;
}
return true;
}
/*************************************************************
Routine to set 42 hex hours characters from a 21 byte array.
**************************************************************/
void pdb_sethexhours(char *p, const unsigned char *hours)
{
if (hours != NULL) {
hex_encode_buf(p, hours, 21);
} else {
strlcpy(p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 44);
}
}
/*************************************************************
Routine to get the 42 hex characters and turn them
into a 21 byte array.
**************************************************************/
bool pdb_gethexhours(const char *p, unsigned char *hours)
{
int i;
unsigned char lonybble, hinybble;
const char *hexchars = "0123456789ABCDEF";
char *p1, *p2;
if (!p) {
return (False);
}
for (i = 0; i < 42; i += 2) {
hinybble = toupper_m(p[i]);
lonybble = toupper_m(p[i + 1]);
p1 = strchr(hexchars, hinybble);
p2 = strchr(hexchars, lonybble);
if (!p1 || !p2) {
return (False);
}
hinybble = PTR_DIFF(p1, hexchars);
lonybble = PTR_DIFF(p2, hexchars);
hours[i / 2] = (hinybble << 4) | lonybble;
}
return (True);
}
/********************************************************************
********************************************************************/
int algorithmic_rid_base(void)
{
int rid_offset;
rid_offset = lp_algorithmic_rid_base();
if (rid_offset < BASE_RID) {
/* Try to prevent admin foot-shooting, we can't put algorithmic
rids below 1000, that's the 'well known RIDs' on NT */
DEBUG(0, ("'algorithmic rid base' must be equal to or above %ld\n", BASE_RID));
rid_offset = BASE_RID;
}
if (rid_offset & 1) {
DEBUG(0, ("algorithmic rid base must be even\n"));
rid_offset += 1;
}
return rid_offset;
}
/*******************************************************************
Converts NT user RID to a UNIX uid.
********************************************************************/
uid_t algorithmic_pdb_user_rid_to_uid(uint32_t user_rid)
{
int rid_offset = algorithmic_rid_base();
return (uid_t)(((user_rid & (~USER_RID_TYPE)) - rid_offset)/RID_MULTIPLIER);
}
uid_t max_algorithmic_uid(void)
{
return algorithmic_pdb_user_rid_to_uid(0xfffffffe);
}
/*******************************************************************
converts UNIX uid to an NT User RID.
********************************************************************/
uint32_t algorithmic_pdb_uid_to_user_rid(uid_t uid)
{
int rid_offset = algorithmic_rid_base();
return (((((uint32_t)uid)*RID_MULTIPLIER) + rid_offset) | USER_RID_TYPE);
}
/*******************************************************************
Converts NT group RID to a UNIX gid.
********************************************************************/
gid_t pdb_group_rid_to_gid(uint32_t group_rid)
{
int rid_offset = algorithmic_rid_base();
return (gid_t)(((group_rid & (~GROUP_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
}
gid_t max_algorithmic_gid(void)
{
return pdb_group_rid_to_gid(0xffffffff);
}
/*******************************************************************
converts NT Group RID to a UNIX uid.
warning: you must not call that function only
you must do a call to the group mapping first.
there is not anymore a direct link between the gid and the rid.
********************************************************************/
uint32_t algorithmic_pdb_gid_to_group_rid(gid_t gid)
{
int rid_offset = algorithmic_rid_base();
return (((((uint32_t)gid)*RID_MULTIPLIER) + rid_offset) | GROUP_RID_TYPE);
}
/*******************************************************************
Decides if a RID is a well known RID.
********************************************************************/
static bool rid_is_well_known(uint32_t rid)
{
/* Not using rid_offset here, because this is the actual
NT fixed value (1000) */
return (rid < BASE_RID);
}
/*******************************************************************
Decides if a RID is a user or group RID.
********************************************************************/
bool algorithmic_pdb_rid_is_user(uint32_t rid)
{
if ( rid_is_well_known(rid) ) {
/*
* The only well known user RIDs are DOMAIN_RID_ADMINISTRATOR
* and DOMAIN_RID_GUEST.
*/
if(rid == DOMAIN_RID_ADMINISTRATOR || rid == DOMAIN_RID_GUEST)
return True;
} else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
return True;
}
return False;
}
/*******************************************************************
Convert a name into a SID. Used in the lookup name rpc.
********************************************************************/
bool lookup_global_sam_name(const char *name, int flags, uint32_t *rid,
enum lsa_SidType *type)
{
GROUP_MAP *map;
bool ret;
/* Windows treats "MACHINE\None" as a special name for
rid 513 on non-DCs. You cannot create a user or group
name "None" on Windows. You will get an error that
the group already exists. */
if ( strequal( name, "None" ) ) {
*rid = DOMAIN_RID_USERS;
*type = SID_NAME_DOM_GRP;
return True;
}
/* LOOKUP_NAME_GROUP is a hack to allow valid users = @foo to work
* correctly in the case where foo also exists as a user. If the flag
* is set, don't look for users at all. */
if ((flags & LOOKUP_NAME_GROUP) == 0) {
struct samu *sam_account = NULL;
struct dom_sid user_sid;
if ( !(sam_account = samu_new( NULL )) ) {
return False;
}
become_root();
ret = pdb_getsampwnam(sam_account, name);
unbecome_root();
if (ret) {
sid_copy(&user_sid, pdb_get_user_sid(sam_account));
}
TALLOC_FREE(sam_account);
if (ret) {
if (!sid_check_is_in_our_sam(&user_sid)) {
struct dom_sid_buf buf;
DBG_ERR("User %s with invalid SID %s"
" in passdb\n",
name,
dom_sid_str_buf(&user_sid, &buf));
return False;
}
sid_peek_rid(&user_sid, rid);
*type = SID_NAME_USER;
return True;
}
}
/*
* Maybe it is a group ?
*/
map = talloc_zero(NULL, GROUP_MAP);
if (!map) {
return false;
}
become_root();
ret = pdb_getgrnam(map, name);
unbecome_root();
if (!ret) {
TALLOC_FREE(map);
return False;
}
/* BUILTIN groups are looked up elsewhere */
if (!sid_check_is_in_our_sam(&map->sid)) {
struct dom_sid_buf buf;
DEBUG(10, ("Found group %s (%s) not in our domain -- "
"ignoring.\n",
name,
dom_sid_str_buf(&map->sid, &buf)));
TALLOC_FREE(map);
return False;
}
/* yes it's a mapped group */
sid_peek_rid(&map->sid, rid);
*type = map->sid_name_use;
TALLOC_FREE(map);
return True;
}
/*************************************************************
Change a password entry in the local passdb backend.
Assumptions:
- always called as root
- ignores the account type except when adding a new account
- will create/delete the unix account if the relative
add/delete user script is configured
*************************************************************/
NTSTATUS local_password_change(const char *user_name,
int local_flags,
const char *new_passwd,
char **pp_err_str,
char **pp_msg_str)
{
TALLOC_CTX *tosctx;
struct samu *sam_pass;
uint32_t acb;
uint32_t rid;
NTSTATUS result;
bool user_exists;
int ret = -1;
*pp_err_str = NULL;
*pp_msg_str = NULL;
tosctx = talloc_tos();
sam_pass = samu_new(tosctx);
if (!sam_pass) {
result = NT_STATUS_NO_MEMORY;
goto done;
}
/* Get the smb passwd entry for this user */
user_exists = pdb_getsampwnam(sam_pass, user_name);
/* Check delete first, we don't need to do anything else if we
* are going to delete the account */
if (user_exists && (local_flags & LOCAL_DELETE_USER)) {
result = pdb_delete_user(tosctx, sam_pass);
if (!NT_STATUS_IS_OK(result)) {
ret = asprintf(pp_err_str,
"Failed to delete entry for user %s.\n",
user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
result = NT_STATUS_UNSUCCESSFUL;
} else {
ret = asprintf(pp_msg_str,
"Deleted user %s.\n",
user_name);
if (ret < 0) {
*pp_msg_str = NULL;
}
}
goto done;
}
if (user_exists && (local_flags & LOCAL_ADD_USER)) {
/* the entry already existed */
local_flags &= ~LOCAL_ADD_USER;
}
if (!user_exists && !(local_flags & LOCAL_ADD_USER)) {
ret = asprintf(pp_err_str,
"Failed to find entry for user %s.\n",
user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
result = NT_STATUS_NO_SUCH_USER;
goto done;
}
/* First thing add the new user if we are required to do so */
if (local_flags & LOCAL_ADD_USER) {
if (local_flags & LOCAL_TRUST_ACCOUNT) {
acb = ACB_WSTRUST;
} else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
acb = ACB_DOMTRUST;
} else {
acb = ACB_NORMAL;
}
result = pdb_create_user(tosctx, user_name, acb, &rid);
if (!NT_STATUS_IS_OK(result)) {
ret = asprintf(pp_err_str,
"Failed to add entry for user %s.\n",
user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
result = NT_STATUS_UNSUCCESSFUL;
goto done;
}
sam_pass = samu_new(tosctx);
if (!sam_pass) {
result = NT_STATUS_NO_MEMORY;
goto done;
}
/* Now get back the smb passwd entry for this new user */
user_exists = pdb_getsampwnam(sam_pass, user_name);
if (!user_exists) {
ret = asprintf(pp_err_str,
"Failed to add entry for user %s.\n",
user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
result = NT_STATUS_UNSUCCESSFUL;
goto done;
}
}
acb = pdb_get_acct_ctrl(sam_pass);
/*
* We are root - just write the new password
* and the valid last change time.
*/
if ((local_flags & LOCAL_SET_NO_PASSWORD) && !(acb & ACB_PWNOTREQ)) {
acb |= ACB_PWNOTREQ;
if (!pdb_set_acct_ctrl(sam_pass, acb, PDB_CHANGED)) {
ret = asprintf(pp_err_str,
"Failed to set 'no password required' "
"flag for user %s.\n", user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
result = NT_STATUS_UNSUCCESSFUL;
goto done;
}
}
if (local_flags & LOCAL_SET_PASSWORD) {
/*
* If we're dealing with setting a completely empty user account
* ie. One with a password of 'XXXX', but not set disabled (like
* an account created from scratch) then if the old password was
* 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
* We remove that as we're giving this user their first password
* and the decision hasn't really been made to disable them (ie.
* don't create them disabled). JRA.
*/
if ((pdb_get_lanman_passwd(sam_pass) == NULL) &&
(acb & ACB_DISABLED)) {
acb &= (~ACB_DISABLED);
if (!pdb_set_acct_ctrl(sam_pass, acb, PDB_CHANGED)) {
ret = asprintf(pp_err_str,
"Failed to unset 'disabled' "
"flag for user %s.\n",
user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
result = NT_STATUS_UNSUCCESSFUL;
goto done;
}
}
acb &= (~ACB_PWNOTREQ);
if (!pdb_set_acct_ctrl(sam_pass, acb, PDB_CHANGED)) {
ret = asprintf(pp_err_str,
"Failed to unset 'no password required'"
" flag for user %s.\n", user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
result = NT_STATUS_UNSUCCESSFUL;
goto done;
}
if (!pdb_set_plaintext_passwd(sam_pass, new_passwd)) {
ret = asprintf(pp_err_str,
"Failed to set password for "
"user %s.\n", user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
result = NT_STATUS_UNSUCCESSFUL;
goto done;
}
}
if ((local_flags & LOCAL_DISABLE_USER) && !(acb & ACB_DISABLED)) {
acb |= ACB_DISABLED;
if (!pdb_set_acct_ctrl(sam_pass, acb, PDB_CHANGED)) {
ret = asprintf(pp_err_str,
"Failed to set 'disabled' flag for "
"user %s.\n", user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
result = NT_STATUS_UNSUCCESSFUL;
goto done;
}
}
if ((local_flags & LOCAL_ENABLE_USER) && (acb & ACB_DISABLED)) {
acb &= (~ACB_DISABLED);
if (!pdb_set_acct_ctrl(sam_pass, acb, PDB_CHANGED)) {
ret = asprintf(pp_err_str,
"Failed to unset 'disabled' flag for "
"user %s.\n", user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
result = NT_STATUS_UNSUCCESSFUL;
goto done;
}
}
/* now commit changes if any */
result = pdb_update_sam_account(sam_pass);
if (!NT_STATUS_IS_OK(result)) {
ret = asprintf(pp_err_str,
"Failed to modify entry for user %s.\n",
user_name);
if (ret < 0) {
*pp_err_str = NULL;
}
goto done;
}
if (local_flags & LOCAL_ADD_USER) {
ret = asprintf(pp_msg_str, "Added user %s.\n", user_name);
} else if (local_flags & LOCAL_DISABLE_USER) {
ret = asprintf(pp_msg_str, "Disabled user %s.\n", user_name);
} else if (local_flags & LOCAL_ENABLE_USER) {
ret = asprintf(pp_msg_str, "Enabled user %s.\n", user_name);
} else if (local_flags & LOCAL_SET_NO_PASSWORD) {
ret = asprintf(pp_msg_str,
"User %s password set to none.\n", user_name);
}
if (ret < 0) {
*pp_msg_str = NULL;
}
result = NT_STATUS_OK;
done:
TALLOC_FREE(sam_pass);
return result;
}
/**********************************************************************
Marshall/unmarshall struct samu structs.
*********************************************************************/
#define SAMU_BUFFER_FORMAT_V0 "ddddddBBBBBBBBBBBBddBBwdwdBwwd"
#define SAMU_BUFFER_FORMAT_V1 "dddddddBBBBBBBBBBBBddBBwdwdBwwd"
#define SAMU_BUFFER_FORMAT_V2 "dddddddBBBBBBBBBBBBddBBBwwdBwwd"
#define SAMU_BUFFER_FORMAT_V3 "dddddddBBBBBBBBBBBBddBBBdwdBwwd"
/* nothing changed between V3 and V4 */
/*********************************************************************
*********************************************************************/
static bool init_samu_from_buffer_v0(struct samu *sampass, uint8_t *buf, uint32_t buflen)
{
/* times are stored as 32bit integer
take care on system with 64bit wide time_t
--SSS */
uint32_t logon_time,
logoff_time,
kickoff_time,
pass_last_set_time,
pass_can_change_time,
pass_must_change_time;
char *username = NULL;
char *domain = NULL;
char *nt_username = NULL;
char *dir_drive = NULL;
char *unknown_str = NULL;
char *munged_dial = NULL;
char *fullname = NULL;