forked from samba-team/samba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdb_interface.c
2682 lines (2172 loc) · 67 KB
/
pdb_interface.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) Andrew Bartlett 2002
Copyright (C) Jelmer Vernooij 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 "system/passwd.h"
#include "passdb.h"
#include "secrets.h"
#include "messages.h"
#include "../librpc/gen_ndr/samr.h"
#include "../librpc/gen_ndr/drsblobs.h"
#include "../librpc/gen_ndr/ndr_drsblobs.h"
#include "../librpc/gen_ndr/idmap.h"
#include "../lib/util/memcache.h"
#include "nsswitch/winbind_client.h"
#include "../libcli/security/security.h"
#include "../lib/util/util_pw.h"
#include "passdb/pdb_secrets.h"
#include "lib/util_sid_passdb.h"
#include "idmap_cache.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_PASSDB
static_decl_pdb;
static struct pdb_init_function_entry *backends = NULL;
static void lazy_initialize_passdb(void)
{
static bool initialized = False;
if(initialized) {
return;
}
static_init_pdb;
initialized = True;
}
static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32_t rid,
const char **name,
enum lsa_SidType *psid_name_use,
uid_t *uid, gid_t *gid);
NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
{
struct pdb_init_function_entry *entry = backends;
if(version != PASSDB_INTERFACE_VERSION) {
DEBUG(0,("Can't register passdb backend!\n"
"You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
"while this version of samba uses version %d\n",
version,PASSDB_INTERFACE_VERSION));
return NT_STATUS_OBJECT_TYPE_MISMATCH;
}
if (!name || !init) {
return NT_STATUS_INVALID_PARAMETER;
}
DEBUG(5,("Attempting to register passdb backend %s\n", name));
/* Check for duplicates */
if (pdb_find_backend_entry(name)) {
DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
return NT_STATUS_OBJECT_NAME_COLLISION;
}
entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
entry->name = smb_xstrdup(name);
entry->init = init;
DLIST_ADD(backends, entry);
DEBUG(5,("Successfully added passdb backend '%s'\n", name));
return NT_STATUS_OK;
}
struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
{
struct pdb_init_function_entry *entry = backends;
while(entry) {
if (strcmp(entry->name, name)==0) return entry;
entry = entry->next;
}
return NULL;
}
const struct pdb_init_function_entry *pdb_get_backends(void)
{
return backends;
}
/*
* The event context for the passdb backend. I know this is a bad hack and yet
* another static variable, but our pdb API is a global thing per
* definition. The first use for this is the LDAP idle function, more might be
* added later.
*
* I don't feel too bad about this static variable, it replaces the
* smb_idle_event_list that used to exist in lib/module.c. -- VL
*/
static struct tevent_context *pdb_tevent_ctx;
struct tevent_context *pdb_get_tevent_context(void)
{
return pdb_tevent_ctx;
}
/******************************************************************
Make a pdb_methods from scratch
*******************************************************************/
NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
{
char *module_name = smb_xstrdup(selected);
char *module_location = NULL, *p;
struct pdb_init_function_entry *entry;
NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
lazy_initialize_passdb();
p = strchr(module_name, ':');
if (p) {
*p = 0;
module_location = p+1;
trim_char(module_location, ' ', ' ');
}
trim_char(module_name, ' ', ' ');
DEBUG(5,("Attempting to find a passdb backend to match %s (%s)\n", selected, module_name));
entry = pdb_find_backend_entry(module_name);
/* Try to find a module that contains this module */
if (!entry) {
DEBUG(2,("No builtin backend found, trying to load plugin\n"));
if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
SAFE_FREE(module_name);
return NT_STATUS_UNSUCCESSFUL;
}
}
/* No such backend found */
if(!entry) {
DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
SAFE_FREE(module_name);
return NT_STATUS_INVALID_PARAMETER;
}
DEBUG(5,("Found pdb backend %s\n", module_name));
if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
selected, nt_errstr(nt_status)));
SAFE_FREE(module_name);
return nt_status;
}
SAFE_FREE(module_name);
DEBUG(5,("pdb backend %s has a valid init\n", selected));
return nt_status;
}
/******************************************************************
Return an already initialized pdb_methods structure
*******************************************************************/
static struct pdb_methods *pdb_get_methods_reload( bool reload )
{
static struct pdb_methods *pdb = NULL;
if ( pdb && reload ) {
if (pdb->free_private_data != NULL) {
pdb->free_private_data( &(pdb->private_data) );
}
if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
return NULL;
}
}
if ( !pdb ) {
if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
return NULL;
}
}
return pdb;
}
static struct pdb_methods *pdb_get_methods(void)
{
struct pdb_methods *pdb;
pdb = pdb_get_methods_reload(false);
if (!pdb) {
char *msg = NULL;
if (asprintf(&msg, "pdb_get_methods: "
"failed to get pdb methods for backend %s\n",
lp_passdb_backend()) > 0) {
smb_panic(msg);
} else {
smb_panic("pdb_get_methods");
}
}
return pdb;
}
struct pdb_domain_info *pdb_get_domain_info(TALLOC_CTX *mem_ctx)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->get_domain_info(pdb, mem_ctx);
}
/**
* @brief Check if the user account has been locked out and try to unlock it.
*
* If the user has been automatically locked out and a lockout duration is set,
* then check if we can unlock the account and reset the bad password values.
*
* @param[in] sampass The sam user to check.
*
* @return True if the function was successfull, false on an error.
*/
static bool pdb_try_account_unlock(struct samu *sampass)
{
uint32_t acb_info = pdb_get_acct_ctrl(sampass);
if ((acb_info & ACB_NORMAL) && (acb_info & ACB_AUTOLOCK)) {
uint32_t lockout_duration;
time_t bad_password_time;
time_t now = time(NULL);
bool ok;
ok = pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION,
&lockout_duration);
if (!ok) {
DEBUG(0, ("pdb_try_account_unlock: "
"pdb_get_account_policy failed.\n"));
return false;
}
if (lockout_duration == (uint32_t) -1 ||
lockout_duration == 0) {
DEBUG(9, ("pdb_try_account_unlock: No reset duration, "
"can't reset autolock\n"));
return false;
}
lockout_duration *= 60;
bad_password_time = pdb_get_bad_password_time(sampass);
if (bad_password_time == (time_t) 0) {
DEBUG(2, ("pdb_try_account_unlock: Account %s "
"administratively locked out "
"with no bad password "
"time. Leaving locked out.\n",
pdb_get_username(sampass)));
return true;
}
if ((bad_password_time +
convert_uint32_t_to_time_t(lockout_duration)) < now) {
NTSTATUS status;
pdb_set_acct_ctrl(sampass, acb_info & ~ACB_AUTOLOCK,
PDB_CHANGED);
pdb_set_bad_password_count(sampass, 0, PDB_CHANGED);
pdb_set_bad_password_time(sampass, 0, PDB_CHANGED);
become_root();
status = pdb_update_sam_account(sampass);
unbecome_root();
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("_samr_OpenUser: Couldn't "
"update account %s - %s\n",
pdb_get_username(sampass),
nt_errstr(status)));
return false;
}
}
}
return true;
}
/**
* @brief Get a sam user structure by the given username.
*
* This functions also checks if the account has been automatically locked out
* and unlocks it if a lockout duration time has been defined and the time has
* elapsed.
*
* @param[in] sam_acct The sam user structure to fill.
*
* @param[in] username The username to look for.
*
* @return True on success, false on error.
*/
bool pdb_getsampwnam(struct samu *sam_acct, const char *username)
{
struct pdb_methods *pdb = pdb_get_methods();
struct samu *for_cache;
const struct dom_sid *user_sid;
NTSTATUS status;
bool ok;
status = pdb->getsampwnam(pdb, sam_acct, username);
if (!NT_STATUS_IS_OK(status)) {
return false;
}
ok = pdb_try_account_unlock(sam_acct);
if (!ok) {
DEBUG(1, ("pdb_getsampwnam: Failed to unlock account %s\n",
username));
}
for_cache = samu_new(NULL);
if (for_cache == NULL) {
return False;
}
if (!pdb_copy_sam_account(for_cache, sam_acct)) {
TALLOC_FREE(for_cache);
return False;
}
user_sid = pdb_get_user_sid(for_cache);
memcache_add_talloc(NULL, PDB_GETPWSID_CACHE,
data_blob_const(user_sid, sizeof(*user_sid)),
&for_cache);
return True;
}
/**********************************************************************
**********************************************************************/
static bool guest_user_info( struct samu *user )
{
struct passwd *pwd;
NTSTATUS result;
const char *guestname = lp_guest_account();
pwd = Get_Pwnam_alloc(talloc_tos(), guestname);
if (pwd == NULL) {
DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
guestname));
return False;
}
result = samu_set_unix(user, pwd );
TALLOC_FREE( pwd );
return NT_STATUS_IS_OK( result );
}
/**
* @brief Get a sam user structure by the given username.
*
* This functions also checks if the account has been automatically locked out
* and unlocks it if a lockout duration time has been defined and the time has
* elapsed.
*
*
* @param[in] sam_acct The sam user structure to fill.
*
* @param[in] sid The user SDI to look up.
*
* @return True on success, false on error.
*/
bool pdb_getsampwsid(struct samu *sam_acct, const struct dom_sid *sid)
{
struct pdb_methods *pdb = pdb_get_methods();
uint32_t rid;
void *cache_data;
bool ok = false;
/* hard code the Guest RID of 501 */
if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
return False;
if ( rid == DOMAIN_RID_GUEST ) {
DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
return guest_user_info( sam_acct );
}
/* check the cache first */
cache_data = memcache_lookup_talloc(
NULL, PDB_GETPWSID_CACHE, data_blob_const(sid, sizeof(*sid)));
if (cache_data != NULL) {
struct samu *cache_copy = talloc_get_type_abort(
cache_data, struct samu);
ok = pdb_copy_sam_account(sam_acct, cache_copy);
} else {
ok = NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
}
if (!ok) {
return false;
}
ok = pdb_try_account_unlock(sam_acct);
if (!ok) {
DEBUG(1, ("pdb_getsampwsid: Failed to unlock account %s\n",
sam_acct->username));
}
return true;
}
static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
TALLOC_CTX *tmp_ctx, const char *name,
uint32_t acb_info, uint32_t *rid)
{
struct samu *sam_pass;
NTSTATUS status;
struct passwd *pwd;
if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
return NT_STATUS_NO_MEMORY;
}
if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
char *add_script = NULL;
int add_ret;
fstring name2;
if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
add_script = lp_add_user_script(tmp_ctx);
} else {
add_script = lp_add_machine_script(tmp_ctx);
}
if (!add_script || add_script[0] == '\0') {
DEBUG(3, ("Could not find user %s and no add script "
"defined\n", name));
return NT_STATUS_NO_SUCH_USER;
}
/* lowercase the username before creating the Unix account for
compatibility with previous Samba releases */
fstrcpy( name2, name );
if (!strlower_m( name2 )) {
return NT_STATUS_INVALID_PARAMETER;
}
add_script = talloc_all_string_sub(tmp_ctx,
add_script,
"%u",
name2);
if (!add_script) {
return NT_STATUS_NO_MEMORY;
}
add_ret = smbrun(add_script,NULL);
DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
add_script, add_ret));
if (add_ret == 0) {
smb_nscd_flush_user_cache();
}
flush_pwnam_cache();
pwd = Get_Pwnam_alloc(tmp_ctx, name);
if(pwd == NULL) {
DEBUG(3, ("Could not find user %s, add script did not work\n", name));
return NT_STATUS_NO_SUCH_USER;
}
}
/* we have a valid SID coming out of this call */
status = samu_alloc_rid_unix(methods, sam_pass, pwd);
TALLOC_FREE( pwd );
if (!NT_STATUS_IS_OK(status)) {
DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
return status;
}
if (!sid_peek_check_rid(get_global_sam_sid(),
pdb_get_user_sid(sam_pass), rid)) {
DEBUG(0, ("Could not get RID of fresh user\n"));
return NT_STATUS_INTERNAL_ERROR;
}
/* Use the username case specified in the original request */
pdb_set_username( sam_pass, name, PDB_SET );
/* Disable the account on creation, it does not have a reasonable password yet. */
acb_info |= ACB_DISABLED;
pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
status = methods->add_sam_account(methods, sam_pass);
TALLOC_FREE(sam_pass);
return status;
}
NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32_t flags,
uint32_t *rid)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->create_user(pdb, mem_ctx, name, flags, rid);
}
/****************************************************************************
Delete a UNIX user on demand.
****************************************************************************/
static int smb_delete_user(const char *unix_user)
{
char *del_script = NULL;
int ret;
/* safety check */
if ( strequal( unix_user, "root" ) ) {
DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
return -1;
}
del_script = lp_delete_user_script(talloc_tos());
if (!del_script || !*del_script) {
return -1;
}
del_script = talloc_all_string_sub(talloc_tos(),
del_script,
"%u",
unix_user);
if (!del_script) {
return -1;
}
ret = smbrun(del_script,NULL);
flush_pwnam_cache();
if (ret == 0) {
smb_nscd_flush_user_cache();
}
DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
return ret;
}
static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
TALLOC_CTX *mem_ctx,
struct samu *sam_acct)
{
NTSTATUS status;
fstring username;
status = methods->delete_sam_account(methods, sam_acct);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
/*
* Now delete the unix side ....
* note: we don't check if the delete really happened as the script is
* not necessary present and maybe the sysadmin doesn't want to delete
* the unix side
*/
/* always lower case the username before handing it off to
external scripts */
fstrcpy( username, pdb_get_username(sam_acct) );
if (!strlower_m( username )) {
return status;
}
smb_delete_user( username );
return status;
}
NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
{
struct pdb_methods *pdb = pdb_get_methods();
uid_t uid = -1;
NTSTATUS status;
const struct dom_sid *user_sid;
char *msg_data;
user_sid = pdb_get_user_sid(sam_acct);
/* sanity check to make sure we don't delete root */
if ( !sid_to_uid(user_sid, &uid ) ) {
return NT_STATUS_NO_SUCH_USER;
}
if ( uid == 0 ) {
return NT_STATUS_ACCESS_DENIED;
}
memcache_delete(NULL,
PDB_GETPWSID_CACHE,
data_blob_const(user_sid, sizeof(*user_sid)));
status = pdb->delete_user(pdb, mem_ctx, sam_acct);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
msg_data = talloc_asprintf(mem_ctx, "USER %s",
pdb_get_username(sam_acct));
if (!msg_data) {
/* not fatal, and too late to rollback,
* just return */
return status;
}
message_send_all(server_messaging_context(),
ID_CACHE_DELETE,
msg_data,
strlen(msg_data) + 1,
NULL);
TALLOC_FREE(msg_data);
return status;
}
NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->add_sam_account(pdb, sam_acct);
}
NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
{
struct pdb_methods *pdb = pdb_get_methods();
memcache_flush(NULL, PDB_GETPWSID_CACHE);
return pdb->update_sam_account(pdb, sam_acct);
}
NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
{
struct pdb_methods *pdb = pdb_get_methods();
const struct dom_sid *user_sid = pdb_get_user_sid(sam_acct);
memcache_delete(NULL,
PDB_GETPWSID_CACHE,
data_blob_const(user_sid, sizeof(*user_sid)));
return pdb->delete_sam_account(pdb, sam_acct);
}
NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
{
struct pdb_methods *pdb = pdb_get_methods();
uid_t uid;
NTSTATUS status;
memcache_flush(NULL, PDB_GETPWSID_CACHE);
/* sanity check to make sure we don't rename root */
if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
return NT_STATUS_NO_SUCH_USER;
}
if ( uid == 0 ) {
return NT_STATUS_ACCESS_DENIED;
}
status = pdb->rename_sam_account(pdb, oldname, newname);
/* always flush the cache here just to be safe */
flush_pwnam_cache();
return status;
}
NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->update_login_attempts(pdb, sam_acct, success);
}
bool pdb_getgrsid(GROUP_MAP *map, struct dom_sid sid)
{
struct pdb_methods *pdb = pdb_get_methods();
return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
}
bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
{
struct pdb_methods *pdb = pdb_get_methods();
return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
}
bool pdb_getgrnam(GROUP_MAP *map, const char *name)
{
struct pdb_methods *pdb = pdb_get_methods();
return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
}
static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
TALLOC_CTX *mem_ctx,
const char *name,
uint32_t *rid)
{
struct dom_sid group_sid;
struct group *grp;
fstring tmp;
grp = getgrnam(name);
if (grp == NULL) {
gid_t gid;
if (smb_create_group(name, &gid) != 0) {
return NT_STATUS_ACCESS_DENIED;
}
grp = getgrgid(gid);
}
if (grp == NULL) {
return NT_STATUS_ACCESS_DENIED;
}
if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
if (!pdb_new_rid(rid)) {
return NT_STATUS_ACCESS_DENIED;
}
} else {
*rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
}
sid_compose(&group_sid, get_global_sam_sid(), *rid);
return add_initial_entry(grp->gr_gid, sid_to_fstring(tmp, &group_sid),
SID_NAME_DOM_GRP, name, NULL);
}
NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
uint32_t *rid)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->create_dom_group(pdb, mem_ctx, name, rid);
}
static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
TALLOC_CTX *mem_ctx,
uint32_t rid)
{
struct dom_sid group_sid;
GROUP_MAP *map;
NTSTATUS status;
struct group *grp;
const char *grp_name;
map = talloc_zero(mem_ctx, GROUP_MAP);
if (!map) {
return NT_STATUS_NO_MEMORY;
}
/* coverity */
map->gid = (gid_t) -1;
sid_compose(&group_sid, get_global_sam_sid(), rid);
if (!get_domain_group_from_sid(group_sid, map)) {
DEBUG(10, ("Could not find group for rid %d\n", rid));
return NT_STATUS_NO_SUCH_GROUP;
}
/* We need the group name for the smb_delete_group later on */
if (map->gid == (gid_t)-1) {
return NT_STATUS_NO_SUCH_GROUP;
}
grp = getgrgid(map->gid);
if (grp == NULL) {
return NT_STATUS_NO_SUCH_GROUP;
}
TALLOC_FREE(map);
/* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
grp_name = talloc_strdup(mem_ctx, grp->gr_name);
if (grp_name == NULL) {
return NT_STATUS_NO_MEMORY;
}
status = pdb_delete_group_mapping_entry(group_sid);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
/* Don't check the result of smb_delete_group */
smb_delete_group(grp_name);
return NT_STATUS_OK;
}
NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32_t rid)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->delete_dom_group(pdb, mem_ctx, rid);
}
NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->add_group_mapping_entry(pdb, map);
}
NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->update_group_mapping_entry(pdb, map);
}
NTSTATUS pdb_delete_group_mapping_entry(struct dom_sid sid)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->delete_group_mapping_entry(pdb, sid);
}
bool pdb_enum_group_mapping(const struct dom_sid *sid,
enum lsa_SidType sid_name_use,
GROUP_MAP ***pp_rmap,
size_t *p_num_entries,
bool unix_only)
{
struct pdb_methods *pdb = pdb_get_methods();
return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
pp_rmap, p_num_entries, unix_only));
}
NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
const struct dom_sid *sid,
uint32_t **pp_member_rids,
size_t *p_num_members)
{
struct pdb_methods *pdb = pdb_get_methods();
NTSTATUS result;
result = pdb->enum_group_members(pdb, mem_ctx,
sid, pp_member_rids, p_num_members);
/* special check for rid 513 */
if ( !NT_STATUS_IS_OK( result ) ) {
uint32_t rid;
sid_peek_rid( sid, &rid );
if ( rid == DOMAIN_RID_USERS ) {
*p_num_members = 0;
*pp_member_rids = NULL;
return NT_STATUS_OK;
}
}
return result;
}
NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
struct dom_sid **pp_sids, gid_t **pp_gids,
uint32_t *p_num_groups)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->enum_group_memberships(
pdb, mem_ctx, user,
pp_sids, pp_gids, p_num_groups);
}
static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
TALLOC_CTX *mem_ctx,
struct samu *sampass)
{
struct group *grp;
gid_t gid;
if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
(grp = getgrgid(gid)) == NULL) {
return NT_STATUS_INVALID_PRIMARY_GROUP;
}
if (smb_set_primary_group(grp->gr_name,
pdb_get_username(sampass)) != 0) {
return NT_STATUS_ACCESS_DENIED;
}
return NT_STATUS_OK;
}
NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
{
struct pdb_methods *pdb = pdb_get_methods();
return pdb->set_unix_primary_group(pdb, mem_ctx, user);
}
/*
* Helper function to see whether a user is in a group. We can't use
* user_in_group_sid here because this creates dependencies only smbd can
* fulfil.
*/
static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
const struct dom_sid *group_sid)
{
struct dom_sid *sids;
gid_t *gids;
uint32_t i, num_groups;
if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
&sids, &gids,
&num_groups))) {
return False;
}
for (i=0; i<num_groups; i++) {
if (dom_sid_equal(group_sid, &sids[i])) {
return True;
}
}
return False;
}
static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
TALLOC_CTX *mem_ctx,
uint32_t group_rid,
uint32_t member_rid)
{
struct dom_sid group_sid, member_sid;
struct samu *account = NULL;
GROUP_MAP *map;
struct group *grp;
struct passwd *pwd;
const char *group_name;
uid_t uid;
map = talloc_zero(mem_ctx, GROUP_MAP);
if (!map) {
return NT_STATUS_NO_MEMORY;
}
/* coverity */
map->gid = (gid_t) -1;
sid_compose(&group_sid, get_global_sam_sid(), group_rid);
sid_compose(&member_sid, get_global_sam_sid(), member_rid);
if (!get_domain_group_from_sid(group_sid, map) ||
(map->gid == (gid_t)-1) ||
((grp = getgrgid(map->gid)) == NULL)) {
return NT_STATUS_NO_SUCH_GROUP;
}
TALLOC_FREE(map);
group_name = talloc_strdup(mem_ctx, grp->gr_name);