/
Users.pm
6694 lines (5839 loc) · 190 KB
/
Users.pm
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
#! /usr/bin/perl -w
# ------------------------------------------------------------------------------
# Copyright (c) 2006-2012 Novell, Inc. All Rights Reserved.
#
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# 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, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail, you may find
# current contact information at www.novell.com.
# ------------------------------------------------------------------------------
#
#
# File: modules/Users.pm
# Package: Configuration of users and groups
# Summary: I/O routines + main data structures
#
# $Id$
#
package Users;
use strict;
use YaST::YCP qw(:LOGGING sformat);
use YaPI;
textdomain("users");
# for password encoding:
use MIME::Base64 qw(encode_base64);
use Digest::MD5;
use Digest::SHA1 qw(sha1);
use Data::Dumper;
our %TYPEINFO;
# If YaST UI (Qt,ncurses) should be used. When this is off, some helper
# UI-related structures won't be generated.
my $use_gui = 1;
# what should be imported during installation (F120103)
my %installation_import = (
"users" => (),
"groups" => (),
);
# Write only, keep progress turned off
my $write_only = 0;
# Export all users and groups (for autoinstallation purposes)
# (used also for indication of cloning: #94340
my $export_all = 0;
# Where the user/group/password data are stored (can be different on NIS server)
my $base_directory = "/etc";
my $root_password = "";
my %default_groupname = ();
my %users = (
"system" => {},
"local" => {},
);
my %shadow = (
"system" => {},
"local" => {},
);
my %groups = (
"system" => {},
"local" => {},
);
my %users_by_uidnumber = (
"system" => {},
"local" => {},
);
my %groups_by_gidnumber = (
"system" => {},
"local" => {},
);
my %removed_homes = ();
my %removed_users = ();
my %removed_groups = ();
my %modified_users = (
"local" => {},
"system" => {}
);
my %modified_groups = ();
my %user_in_work = ();
my %group_in_work = ();
# the first user, added during installation
my %saved_user = ();
my %useradd_defaults = (
"group" => "500",
"home" => "/home",
"inactive" => "",
"expire" => "",
"shell" => "",
"skel" => "",
"groups" => "",
"umask" => "022"
);
# which sets of users are available:
my @available_usersets = ();
my @available_groupsets = ();
# available shells (read from /etc/shells)
my %all_shells = ();
# if passwd/group/shadow entries should be read
my $read_local = 1;
my $users_modified = 0;
my $groups_modified = 0;
my $ldap_modified = 0;
my $customs_modified = 0;
my $defaults_modified = 0;
my $security_modified = 0;
my $sysconfig_ldap_modified = 0;
my $ldap_settings_read = 0;
# variables describing available users sets:
my $nis_available = 1;
my $ldap_available = 1;
my $nis_master = 0;
# nis users are not read by default, but could be read on demand:
my $nis_not_read = 1;
# ldap users are not read by default, but could be read on demand:
my $ldap_not_read = 1;
# check if config files were read before w try to write them
my $passwd_not_read = 1;
my $shadow_not_read = 1;
my $group_not_read = 1;
# paths to commands that should be run before (after) adding (deleting) a user
my $useradd_cmd = "";
my $userdel_precmd = "";
my $userdel_postcmd = "";
my $groupadd_cmd = "";
my $pass_warn_age = "7";
my $pass_min_days = "0";
my $pass_max_days = "99999";
# password encryption method
my $encryption_method = "des";
my $group_encryption_method = "des";
# User/group names must match the following regex expression. (/etc/login.defs)
my $character_class = "[[:alpha:]_][[:alnum:]_.-]*[[:alnum:]_.\$-]\\?";
# the +/- entries in config files:
my @pluses_passwd = ();
my @pluses_group = ();
my @pluses_shadow = ();
# starting dialog for installation mode
my $start_dialog = "summary";
my $use_next_time = 0;
# if user should be warned when using uppercase letters in login name
my $not_ask_uppercase = 0;
# if user should be warned when using blowfish/md5 encryption on NIS server
my $not_ask_nisserver_notdes = 0;
# which sets of users are we working with:
my @current_users = ();
my @current_groups = ();
# mail alias for root (helper; see root_aliases for current values)
my $root_mail = "";
# hash with user names that should get root's mail
my %root_aliases = ();
my %root_aliases_orig = ();
# users sets in "Custom" selection:
my @user_custom_sets = ("local");
my @group_custom_sets = ("local");
# list of available plugin modules for local and system users (groups)
my @local_plugins = ();
# if system settings already read by ReadSystemDefaults
my $system_defaults_read = 0;
# if new value of root password should be written at the end
my $save_root_password = 0;
##------------------------------------
##------------------- global imports
YaST::YCP::Import ("SCR");
YaST::YCP::Import ("Autologin");
YaST::YCP::Import ("Call");
YaST::YCP::Import ("Directory");
YaST::YCP::Import ("FileUtils");
YaST::YCP::Import ("Ldap");
YaST::YCP::Import ("Linuxrc");
YaST::YCP::Import ("Installation");
YaST::YCP::Import ("MailAliases");
YaST::YCP::Import ("Message");
YaST::YCP::Import ("Mode");
YaST::YCP::Import ("Package");
YaST::YCP::Import ("Popup");
YaST::YCP::Import ("ProductFeatures");
YaST::YCP::Import ("Progress");
YaST::YCP::Import ("Report");
YaST::YCP::Import ("Security");
YaST::YCP::Import ("Service");
YaST::YCP::Import ("Stage");
YaST::YCP::Import ("String");
YaST::YCP::Import ("Syslog");
YaST::YCP::Import ("UsersCache");
YaST::YCP::Import ("UsersLDAP");
YaST::YCP::Import ("UsersPasswd");
YaST::YCP::Import ("UsersPlugins");
YaST::YCP::Import ("UsersRoutines");
YaST::YCP::Import ("UsersSimple");
YaST::YCP::Import ("UsersUI");
YaST::YCP::Import ("SSHAuthorizedKeys");
##-------------------------------------------------------------------------
##----------------- various routines --------------------------------------
sub contains {
my ( $list, $key, $ignorecase ) = @_;
if (!defined $list || ref ($list) ne "ARRAY" || @{$list} == 0) {
return 0;
}
if ( $ignorecase ) {
if ( grep /^\Q$key\E$/i, @{$list} ) {
return 1;
}
} else {
if ( grep /^\Q$key\E$/, @{$list} ) {
return 1;
}
}
return 0;
}
# check the boolean value, return 0 or 1
sub bool {
my $param = $_[0];
if (!defined $param) {
return 0;
}
if (ref ($param) eq "YaST::YCP::Boolean") {
return $param->value();
}
return $param;
}
sub DebugMap {
UsersCache->DebugMap ($_[0]);
}
##------------------------------------
BEGIN { $TYPEINFO{LastChangeIsNow} = ["function", "string"]; }
sub LastChangeIsNow {
if (Mode->test ()) { return "0";}
my %out = %{SCR->Execute (".target.bash_output", "date +%s")};
my $seconds = $out{"stdout"} || "0";
chomp $seconds;
return sprintf ("%u", $seconds / (60*60*24));
}
BEGIN { $TYPEINFO{Modified} = ["function", "boolean"];}
sub Modified {
my $ret = $users_modified ||
$groups_modified ||
$ldap_modified ||
$customs_modified ||
$defaults_modified ||
$security_modified;
return $ret;
}
# if root password value should be explicitely written
BEGIN { $TYPEINFO{SaveRootPassword} = ["function", "void", "boolean"]; }
sub SaveRootPassword {
my $self = shift;
$save_root_password = bool ($_[0]);
}
BEGIN { $TYPEINFO{NISAvailable} = ["function", "boolean"]; }
sub NISAvailable {
return $nis_available;
}
BEGIN { $TYPEINFO{NISMaster} = ["function", "boolean"]; }
sub NISMaster {
return $nis_master;
}
BEGIN { $TYPEINFO{NISNotRead} = ["function", "boolean"]; }
sub NISNotRead {
return $nis_not_read;
}
BEGIN { $TYPEINFO{LDAPAvailable} = ["function", "boolean"]; }
sub LDAPAvailable {
return $ldap_available;
}
BEGIN { $TYPEINFO{LDAPModified} = ["function", "boolean"]; }
sub LDAPModified {
return $ldap_modified;
}
BEGIN { $TYPEINFO{LDAPNotRead} = ["function", "boolean"]; }
sub LDAPNotRead {
return $ldap_not_read;
}
BEGIN { $TYPEINFO{SetLDAPNotRead} = ["function", "void", "boolean"]; }
sub SetLDAPNotRead {
my $self = shift;
$ldap_not_read = $_[0];
}
# OBSOLETE
BEGIN { $TYPEINFO{GetRootMail} = ["function", "string"]; }
sub GetRootMail {
y2warning ("this function is obsolete, use GetRootAliases instead");
return join (",", keys %root_aliases);
}
# OBSOLETE
BEGIN { $TYPEINFO{SetRootMail} = ["function", "void", "string"]; }
sub SetRootMail {
my $self = shift;
my $root_a = shift;
y2warning ("this function is obsolete, use RemoveRootAlias/AddRootAlias instead");
foreach my $alias (split (/,/, $root_a)) {
$alias =~ s/[ \t]//g;
$root_aliases{$alias} = 1;
}
}
# return the map with root aliases
BEGIN { $TYPEINFO{GetRootAliases} = ["function", ["map", "string", "integer"]];}
sub GetRootAliases {
return \%root_aliases;
}
# remove the given user from root's aliases set
BEGIN { $TYPEINFO{RemoveRootAlias} = ["function", "void", "string"]; }
sub RemoveRootAlias {
my $self = shift;
my $u = shift;
delete $root_aliases{$u} if (defined $root_aliases{$u});
}
# add the given user to root's aliases set
BEGIN { $TYPEINFO{AddRootAlias} = ["function", "void", "string"]; }
sub AddRootAlias {
my $self = shift;
my $u = shift;
$root_aliases{$u} = 1;
}
# return the value of base directory
BEGIN { $TYPEINFO{GetBaseDirectory} = ["function", "string"]; }
sub GetBaseDirectory {
return $base_directory;
}
# set the new the value of base directory
BEGIN { $TYPEINFO{SetBaseDirectory} = ["function", "void", "string"]; }
sub SetBaseDirectory {
my $self = shift;
my $dir = shift;
$base_directory = $dir if (defined $dir);
}
BEGIN { $TYPEINFO{GetStartDialog} = ["function", "string"]; }
sub GetStartDialog {
return $start_dialog;
}
BEGIN { $TYPEINFO{StartDialog} = ["function", "boolean", "string"]; }
sub StartDialog {
my $self = shift;
return $start_dialog eq $_[0];
}
BEGIN { $TYPEINFO{SetStartDialog} = ["function", "void", "string"]; }
sub SetStartDialog {
my $self = shift;
$start_dialog = $_[0];
}
BEGIN { $TYPEINFO{UseNextTime} = ["function", "boolean"]; }
sub UseNextTime {
return $use_next_time;
}
BEGIN { $TYPEINFO{SetUseNextTime} = ["function", "void", "boolean"]; }
sub SetUseNextTime {
my $self = shift;
$use_next_time = $_[0];
}
BEGIN { $TYPEINFO{GetAvailableUserSets} = ["function", ["list", "string"]]; }
sub GetAvailableUserSets {
return \@available_usersets;
}
BEGIN { $TYPEINFO{GetAvailableGroupSets} = ["function", ["list", "string"]]; }
sub GetAvailableGroupSets {
return \@available_groupsets;
}
# return the global $umask value
BEGIN { $TYPEINFO{GetUmask} = ["function", "string"]; }
sub GetUmask {
my $umask = $useradd_defaults{"umask"};
$umask = "022" unless $umask;
return $umask;
}
##------------------------------------
# current users = name of user sets currently shown in table ("local", "nis")
BEGIN { $TYPEINFO{GetCurrentUsers} = ["function", ["list", "string"]]; }
sub GetCurrentUsers {
return \@current_users;
}
BEGIN { $TYPEINFO{SetCurrentUsers} = ["function", "void", ["list", "string"]]; }
sub SetCurrentUsers {
my $self = shift;
my $new = shift;
if (ref ($new) eq "ARRAY") {
@current_users = @$new;
}
}
BEGIN { $TYPEINFO{GetCurrentGroups} = ["function", ["list", "string"]]; }
sub GetCurrentGroups {
return \@current_groups;
}
BEGIN { $TYPEINFO{SetCurrentGroups} = ["function", "void", ["list", "string"]];}
sub SetCurrentGroups {
my $self = shift;
my $new = shift;
if (ref ($new) eq "ARRAY") {
@current_groups = @$new;
}
}
##------------------------------------
# Change the current users set, additional reading could be necessary
# @param new the new current set
BEGIN { $TYPEINFO{ChangeCurrentUsers} = ["function", "boolean", "string"];}
sub ChangeCurrentUsers {
my $self = shift;
my $new = $_[0];
my @backup = @current_users;
if ($new eq "custom") {
@current_users = @user_custom_sets;
}
else {
@current_users = ( $new );
}
if (contains (\@current_users, "ldap") && $ldap_not_read) {
if (!$self->ReadNewSet ("ldap")) {
@current_users = @backup;
return 0;
}
}
if (contains (\@current_users, "nis") && $nis_not_read) {
if (!$self->ReadNewSet ("nis")) {
@current_users = @backup;
return 0;
}
}
# correct also possible change in custom itemlist
if ($new eq "custom") {
UsersCache->SetCustomizedUsersView (1);
}
UsersCache->SetCurrentUsers (\@current_users);
return 1;
}
##------------------------------------
# Change the current group set, additional reading could be necessary
# @param new the new current set
BEGIN { $TYPEINFO{ChangeCurrentGroups} = ["function", "boolean", "string"];}
sub ChangeCurrentGroups {
my $self = shift;
my $new = $_[0];
my @backup = @current_groups;
if ($new eq "custom") {
@current_groups = @group_custom_sets;
}
else {
@current_groups = ( $new );
}
if (contains (\@current_groups, "ldap") && $ldap_not_read) {
if (!$self->ReadNewSet ("ldap")) {
@current_groups = @backup;
return 0;
}
}
if (contains (\@current_groups, "nis") && $nis_not_read) {
if (!$self->ReadNewSet ("nis")) {
@current_groups = @backup;
return 0;
}
}
# correct also possible change in custom itemlist
if ($new eq "custom") {
UsersCache->SetCustomizedGroupsView (1);
}
UsersCache->SetCurrentGroups (\@current_groups);
return 1;
}
##------------------------------------
BEGIN { $TYPEINFO{ChangeCustoms} = ["function",
"boolean",
"string", ["list","string"]];
}
sub ChangeCustoms {
my $self = shift;
my @new = @{$_[1]};
if ($_[0] eq "users") {
my @old = @user_custom_sets;
@user_custom_sets = @new;
$customs_modified = $self->ChangeCurrentUsers ("custom");
if (!$customs_modified) {
@user_custom_sets = @old;
}
}
else
{
my @old = @group_custom_sets;
@group_custom_sets = @new;
$customs_modified = $self->ChangeCurrentGroups ("custom");
if (!$customs_modified) {
@group_custom_sets = @old;
}
}
return $customs_modified;
}
BEGIN { $TYPEINFO{AllShells} = ["function", ["list", "string"]];}
sub AllShells {
my @shells = sort keys %all_shells;
return \@shells;
}
# set the list of users to be imported during installation
BEGIN { $TYPEINFO{SetUsersForImport} = ["function", "void", ["list","any"]];}
sub SetUsersForImport {
my $self = shift;
my $to_import = shift;
return if (! defined ($to_import) || ref ($to_import) ne "ARRAY");
my @u = @$to_import;
$installation_import{"users"} = \@u;
}
# return the data of users to be imported during installation
BEGIN { $TYPEINFO{GetUsersForImport} = ["function", ["list","any"]];}
sub GetUsersForImport {
my $self = shift;
my @ret = ();
if (defined ($installation_import{"users"}) &&
ref ($installation_import{"users"}) eq "ARRAY") {
@ret = @{$installation_import{"users"}};
}
return \@ret;
}
# return the data of users and groups to be imported during installation
BEGIN { $TYPEINFO{GetDataForImport} = ["function", ["map", "string", "any"]];}
sub GetDataForImport {
my $self = shift;
return \%installation_import;
}
BEGIN { $TYPEINFO{NotAskUppercase} = ["function", "boolean"];}
sub NotAskUppercase {
return $not_ask_uppercase;
}
BEGIN { $TYPEINFO{SetAskUppercase} = ["function", "void", "boolean"];}
sub SetAskUppercase {
my $self = shift;
if ($not_ask_uppercase != bool ($_[0])) {
$not_ask_uppercase = bool ($_[0]);
$customs_modified = 1;
}
}
BEGIN { $TYPEINFO{NotAskNISServerNotDES} = ["function", "boolean"];}
sub NotAskNISServerNotDES {
return $not_ask_nisserver_notdes;
}
BEGIN { $TYPEINFO{SetAskNISServerNotDES} = ["function", "void", "boolean"];}
sub SetAskNISServerNotDES {
my $self = shift;
if ($not_ask_nisserver_notdes != bool ($_[0])) {
$not_ask_nisserver_notdes = bool ($_[0]);
$customs_modified = 1;
}
}
##------------------------------------
BEGIN { $TYPEINFO{CheckHomeMounted} = ["function", "void"]; }
# Checks if the home directory is properly mounted (bug #20365)
sub CheckHomeMounted {
if (Mode->test() || Mode->config()) {
return "";
}
my $self = shift;
my $ret = "";
my $mountpoint_in = "";
my $home = $self->GetDefaultHome ("local");
if (substr ($home, -1, 1) eq "/") {
chop $home;
}
my $fstab = SCR->Read (".etc.fstab");
if (defined $fstab && ref ($fstab) eq "ARRAY") {
foreach my $line (@{$fstab}) {
my %line = %{$line};
if ($line{"file"} eq $home) {
$mountpoint_in = "/etc/fstab";
}
}
}
if (FileUtils->Exists ("/etc/cryptotab")) {
my $cryptotab = SCR->Read (".etc.cryptotab");
if (defined $cryptotab && ref ($cryptotab) eq "ARRAY") {
foreach my $line (@{$cryptotab}) {
my %line = %{$line};
if ($line{"mount"} eq $home) {
$mountpoint_in = "/etc/cryptotab";
}
}
}
}
if ($mountpoint_in ne "") {
my $dest_home_mountpoint = Installation->destdir() . $home;
y2milestone("home mount points are:", $dest_home_mountpoint, ",", $home);
my $mounted = 0;
my $mtab = SCR->Read (".etc.mtab");
if (defined $mtab && ref ($mtab) eq "ARRAY") {
foreach my $line (@{$mtab}) {
my %line = %{$line};
# While installing package "systemd" the /etc/mtab entries will be
# changed from e.g. /home to /mnt/home. This will be done in SLES12
# only and not for e.g. LEAP. So we have to check both.
# (bnc#995299, bnc#980878)
if ($line{"file"} eq $dest_home_mountpoint || $line{"file"} eq $home ) {
$mounted = 1;
}
}
}
if (!$mounted) {
return sformat (
# Popup text: %1 is the file name (e.g. /etc/fstab),
# %2 is the directory (e.g. /home),
__("In %1, there is a mount point for the directory
%2, which is used as a default home directory for new
users, but this directory is not currently mounted.
If you add new users using the default values,
their home directories will be created in the current %2.
This can result in these directories not being accessible
after you mount correctly. Continue user configuration?"),
$mountpoint_in, $home);
}
}
return $ret;
}
##-------------------------------------------------------------------------
##----------------- get routines ------------------------------------------
##------------------------------------
BEGIN { $TYPEINFO{GetMinPasswordLength} = ["function", "integer", "string"]; }
sub GetMinPasswordLength {
my ($self, $type) = @_;
return UsersSimple->GetMinPasswordLength ($type);
}
##------------------------------------
BEGIN { $TYPEINFO{GetMaxPasswordLength} = ["function", "integer", "string"]; }
sub GetMaxPasswordLength {
my ($self, $type) = @_;
return UsersSimple->GetMaxPasswordLength ($type);
}
##------------------------------------
BEGIN { $TYPEINFO{GetDefaultGrouplist} = ["function",
["map", "string", "integer"],
"string"];
}
sub GetDefaultGrouplist {
my $self = shift;
my $type = $_[0];
my %grouplist = ();
my $grouplist = "";
if ($type eq "ldap") {
$grouplist = UsersLDAP->GetDefaultGrouplist ();
}
else {
$grouplist = $useradd_defaults{"groups"};
}
foreach my $group (split (/,/, $grouplist)) {
$grouplist{$group} = 1;
}
return \%grouplist;
}
##------------------------------------
BEGIN { $TYPEINFO{GetDefaultGID} = ["function", "integer", "string"]; }
sub GetDefaultGID {
my $self = shift;
my $type = $_[0];
my $gid = $useradd_defaults{"group"};
if ($type eq "ldap") {
$gid = UsersLDAP->GetDefaultGID ();
}
return $gid;
}
##------------------------------------
BEGIN { $TYPEINFO{GetDefaultShell} = ["function", "string", "string"]; }
sub GetDefaultShell {
my $self = shift;
my $type = $_[0];
if ($type eq "ldap") {
return UsersLDAP->GetDefaultShell ();
}
else {
return $useradd_defaults{"shell"};
}
}
##------------------------------------
BEGIN { $TYPEINFO{GetDefaultHome} = ["function", "string", "string"]; }
sub GetDefaultHome {
my $self = shift;
my $home = $useradd_defaults{"home"} || "";
if ($_[0] eq "ldap") {
$home = UsersLDAP->GetDefaultHome ();
}
if (substr ($home, -1, 1) ne "/") {
$home.="/";
}
return $home;
}
##------------------------------------
BEGIN { $TYPEINFO{GetDefaultShadow} = ["function",
[ "map", "string", "string"],
"string"];
}
sub GetDefaultShadow {
my $self = shift;
my $type = $_[0];
my %ret = (
"shadowInactive" => $useradd_defaults{"inactive"},
"shadowExpire" => $useradd_defaults{"expire"},
"shadowWarning" => $pass_warn_age,
"shadowMin" => $pass_min_days,
"shadowMax" => $pass_max_days,
"shadowFlag" => "",
"shadowLastChange" => "",
"userPassword" => undef
);
if ($type eq "ldap") {
%ret = %{UsersLDAP->GetDefaultShadow()};
}
return \%ret;
}
##------------------------------------
BEGIN { $TYPEINFO{GetDefaultGroupname} = ["function", "string", "string"]; }
sub GetDefaultGroupname {
my $self = shift;
my $type = $_[0];
if (defined $default_groupname{$type}) {
return $default_groupname{$type};
}
my $gid = $self->GetDefaultGID ($type);
my %group = ();
if ($type eq "ldap") {
%group = %{$self->GetGroup ($gid, "ldap")};
}
if (!%group) {
%group = %{$self->GetGroup ($gid, "local")};
}
if (!%group) {
%group = %{$self->GetGroup ($gid, "system")};
}
if (%group) {
$default_groupname{$type} = $group{"cn"};
return $default_groupname{$type};
}
}
##------------------------------------
BEGIN { $TYPEINFO{GetLoginDefaults} = ["function",
["map", "string", "string"]];
}
sub GetLoginDefaults {
return \%useradd_defaults;
}
##------------------------------------
# Change the structure with default values (/etc/defaults/useradd)
# @param new_defaults new values
# @param groupname the name of dew default group
BEGIN { $TYPEINFO{SetLoginDefaults} = ["function",
"void",
["map", "string", "string"], "string"];
}
sub SetLoginDefaults {
my $self = shift;
my %data = %{$_[0]};
$default_groupname{"local"} = $_[1];
$default_groupname{"system"}= $_[1];
foreach my $key (keys %data) {
if ($data{$key} ne $useradd_defaults{$key}) {
$useradd_defaults{$key} = $data{$key};
$defaults_modified = 1;
}
};
if (substr ($useradd_defaults{"home"}, -1, 1) eq "/") {
chop $useradd_defaults{"home"};
}
}
##------------------------------------
# Returns the map of user specified by its UID
# @param uid user's identification number (UID) or uidnumber attribute for LDAP users
# @param type "local"/"system"/"nis"/"ldap"; if empty, all types are searched
# @return the map of _first_ user matching the parameters
BEGIN { $TYPEINFO{GetUser} = [ "function",
["map", "string", "any" ],
"integer", "string"];
}
sub GetUser {
my $self = shift;
my $uid = $_[0];
my @types_to_look = ($_[1]);
if ($_[1] eq "") {
@types_to_look = keys %users_by_uidnumber;
unshift @types_to_look, UsersCache->GetUserType ();
}
foreach my $type (@types_to_look) {
next if (!$type);
if (defined $users_by_uidnumber{$type}{$uid} &&
(ref ($users_by_uidnumber{$type}{$uid}) eq "HASH") &&
%{$users_by_uidnumber{$type}{$uid}})
{
my $first_username = (keys %{$users_by_uidnumber{$type}{$uid}})[0];
return $self->GetUserByName ($first_username, $type);
}
}
return {};
}
##------------------------------------
# get set of users of given type indexed by given key
BEGIN { $TYPEINFO{GetUsers} = [ "function",
["map", "any", "any" ],
"string", "string"]; # index key, type
}
sub GetUsers {
my $self = shift;
my $key = shift;
my $type = shift;
my $ret = {};
foreach my $id (keys %{$users{$type}}) {
my $user = $users{$type}{$id};
if (ref ($user) eq "HASH" && defined $user->{$key}) {
if (defined $ret->{$user->{$key}}) {
y2warning("Multiple users satisfy the input conditions: user $id, key ". $user->{$key});
}
else {
$ret->{$user->{$key}} = $user;
}
}
}
return $ret;
# error message
my $msg = __("Multiple users satisfy the input conditions.");
}
##------------------------------------
BEGIN { $TYPEINFO{GetCurrentUser} = [ "function", ["map", "string", "any" ]];}
sub GetCurrentUser {
return \%user_in_work;
}
##------------------------------------
# must be called before each Add...
BEGIN { $TYPEINFO{ResetCurrentUser} = [ "function", "void" ];}
sub ResetCurrentUser {
undef %user_in_work;
}
##------------------------------------
BEGIN { $TYPEINFO{SaveCurrentUser} = [ "function", "void" ];}
sub SaveCurrentUser {
%saved_user = %user_in_work;
}
##------------------------------------
BEGIN { $TYPEINFO{RestoreCurrentUser} = [ "function", "void" ];}
sub RestoreCurrentUser {
%user_in_work = %saved_user;
}