-
Notifications
You must be signed in to change notification settings - Fork 16
/
UsersSimple.pm
1331 lines (1122 loc) · 35.4 KB
/
UsersSimple.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/UsersSimple.pm
# Package: Configuration of users and groups
# Summary: module for first stage user configuration
#
# $Id$
#
package UsersSimple;
use strict;
use Data::Dumper;
use YaST::YCP qw(:LOGGING sformat);
use YaPI;
textdomain("users");
our %TYPEINFO;
# What client to call after authentication dialog during installation:
# could be "users","nis" or "ldap", for more see inst_auth.ycp
my $after_auth = "users";
# If kerberos configuration should be called after authentication
# during installation (F120214)
my $run_krb_config = 0;
my $root_password = "";
# root password already written
my $root_password_written = 0;
# users from 1st stage already written
my $users_written = 0;
# only for first stage, remember if root pw dialog should be skipped
my $skip_root_dialog = 0;
# data of user configured during installation
#my %user = ();
# data of users configured during installation
my @users = ();
# password encryption method
my $encryption_method = "sha512";
# mail alias for root
my $root_alias = "";
my %min_pass_length = (
"local" => 5,
"system" => 5,
"ldap" => 5
);
my %max_pass_length = (
"local" => 72,
"system" => 72,
"ldap" => 72
);
# Number of sigificant characters in the password for given encryption method
my %max_lengths = (
"des" => 8,
"md5" => 127,
"blowfish" => 72,
"sha256" => 127, # arbitrary high number, there's probably no limit
"sha512" => 127
);
# name of user that should be logged in automatically
my $autologin_user = "";
# path to cracklib dictionary
my $cracklib_dictpath = "";
# if cracklib is used for password checking
my $use_cracklib = 1;
# this is yast2-users internal check now, feature was removed from PAM
my $obscure_checks = 1;
# User/group names must match the following regex expression. (/etc/login.defs)
my $character_class = "[[:alpha:]_][[:alnum:]_.-]*[[:alnum:]_.\$-]\\?";
my $max_length_login = 32; # reason: see for example man utmp, UT_NAMESIZE
my $min_length_login = 2;
# see SYS_UID_MAX and SYS_GID_MAX in /etc/login.defs
my $max_system_uid = 499;
# maps for user data read in 1st stage ('from previous installation')
my %imported_users = ();
my %imported_shadow = ();
# if importing users during installation is possible
my $import_available;
# if check for LDAP/Kerberos via DNS was done
my $network_methods_checked = 0;
# note if users configuration should be run or skipped in 2nd stage (bnc#450231)
my $skip_user_creation = 0;
##------------------------------------
##------------------- global imports
YaST::YCP::Import ("Directory");
YaST::YCP::Import ("FileUtils");
YaST::YCP::Import ("Hostname");
YaST::YCP::Import ("InstExtensionImage");
YaST::YCP::Import ("Language");
YaST::YCP::Import ("Mode");
YaST::YCP::Import ("NetworkService");
YaST::YCP::Import ("ProductControl");
YaST::YCP::Import ("SCR");
YaST::YCP::Import ("Stage");
YaST::YCP::Import ("SystemFilesCopy");
YaST::YCP::Import ("UsersUI");
# known system users (hard-written here to check user name conflicts)
# number may mean the UID (but it don't have to be defined)
my %system_users = (
"root" => 0,
"bin" => 1,
"uucp" => 10,
"daemon" => 2,
"lp" => 4,
"mail" => 8,
"news" => 9,
"uucp" => 10,
"games" => 12,
"man" => 13,
"at" => 25,
"wwwrun" => 30,
"ftp" => 40,
"named" => 0,
"gdm" => 0,
"postfix" => 51,
"sshd" => 71,
"ntp" => 74,
"ldap" => 76,
"nobody" => 65534,
"amanda" => 0,
"vscan" => 0,
"bigsister" => 0,
"wnn" => 0,
"cyrus" => 0,
"dpbox" => 0,
"gnats" => 0,
"gnump3d" => 0,
"hacluster" => 0,
"irc" => 0,
"mailman" => 0,
"mdom" => 0,
"mysql" => 0,
"oracle" => 0,
"postgres" => 0,
"pop" => 0,
"sapdb" => 0,
"snort" => 0,
"squid" => 31,
"stunnel" => 0,
"zope" => 0,
"radiusd" => 0,
"otrs" => 0,
"privoxy" => 0,
"vdr" => 0,
"icecream" => 0,
"bitlbee" => 0,
"dhcpd" => 0,
"distcc" => 0,
"dovecot" => 0,
"fax" => 0,
"partimag" => 0,
"avahi" => 0,
"beagleindex" => 0,
"casaauth" => 0,
"dvbdaemon" => 0,
"festival" => 0,
"haldaemon" => 0,
"icecast" => 0,
"lighttpd" => 0,
"nagios" => 0,
"pdns" => 0,
"polkituser" => 0,
"pound" => 0,
"pulse" => 0,
"quagga" => 0,
"sabayon-admin" => 0,
"tomcat" => 0,
"pegasus" => 0,
"cimsrvr" => 0,
"ulogd" => 0,
"uuidd" => 0,
"suse-ncc" => 0,
"messagebus" => 0,
"nx" => 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;
}
##------------------------------------
# set new value for $character_class
BEGIN { $TYPEINFO{SetCharacterClass} = ["function", "void", "string"];}
sub SetCharacterClass {
my $self = shift;
$character_class = shift;
}
##------------------------------------
# set new cracklib dictionary path
BEGIN { $TYPEINFO{SetCrackLibDictPath} = ["function", "void", "string"];}
sub SetCrackLibDictPath {
my $self = shift;
$cracklib_dictpath = shift;
}
##------------------------------------
# return the value of current encryption method
BEGIN { $TYPEINFO{EncryptionMethod} = ["function", "string"];}
sub EncryptionMethod {
return $encryption_method;
}
# is cracklib used for password checking?
BEGIN { $TYPEINFO{CrackLibUsed} = ["function", "boolean"]; }
sub CrackLibUsed {
return $use_cracklib;
}
# set the new value of cracklib usage for password checking
BEGIN { $TYPEINFO{UseCrackLib} = ["function", "void", "boolean"]; }
sub UseCrackLib {
my $self = shift;
my $crack = shift;
$use_cracklib = bool ($crack) if (defined $crack);
}
# are 'obscure checks' used for password checking?
BEGIN { $TYPEINFO{ObscureChecksUsed} = ["function", "boolean"]; }
sub ObscureChecksUsed {
return $obscure_checks;
}
# set the new value of 'obscure checks' usage for password checking
BEGIN { $TYPEINFO{UseObscureChecks} = ["function", "void", "boolean"]; }
sub UseObscureChecks {
my $self = shift;
my $checks = shift;
$obscure_checks = bool ($checks) if (defined $checks);
}
##------------------------------------
# set new encryption method
BEGIN { $TYPEINFO{SetEncryptionMethod} = ["function", "void", "string"];}
sub SetEncryptionMethod {
my $self = shift;
my $method = shift;
if ($encryption_method ne $method) {
$encryption_method = $method;
if (defined $max_lengths{$encryption_method}) {
$max_pass_length{"local"} = $max_lengths{$encryption_method};
$max_pass_length{"system"} = $max_lengths{$encryption_method};
}
}
}
BEGIN { $TYPEINFO{GetAutologinUser} = ["function", "string"]; }
sub GetAutologinUser {
return $autologin_user;
}
BEGIN { $TYPEINFO{AutologinUsed} = ["function", "boolean"]; }
sub AutologinUsed {
return bool ($autologin_user ne "");
}
BEGIN { $TYPEINFO{SetAutologinUser} = ["function", "void", "string"]; }
sub SetAutologinUser {
my $self = shift;
$autologin_user = shift;
}
BEGIN { $TYPEINFO{GetRootAlias} = ["function", "string"]; }
sub GetRootAlias {
return $root_alias;
}
BEGIN { $TYPEINFO{SetRootAlias} = ["function", "void", "string"]; }
sub SetRootAlias {
my $self = shift;
$root_alias = shift;
}
BEGIN { $TYPEINFO{AfterAuth} = ["function", "string"];}
sub AfterAuth {
return $after_auth;
}
BEGIN { $TYPEINFO{SetAfterAuth} = ["function", "void", "string"];}
sub SetAfterAuth {
my $self = shift;
$after_auth = $_[0];
}
# return the value of run_krb_config (should the kerberos config be run?)
BEGIN { $TYPEINFO{KerberosConfiguration} = ["function", "boolean"];}
sub KerberosConfiguration {
return bool ($run_krb_config);
}
# set the new value for run_krb_config
BEGIN { $TYPEINFO{SetKerberosConfiguration} = ["function", "void", "boolean"];}
sub SetKerberosConfiguration {
my ($self, $krb) = @_;
$run_krb_config = bool ($krb) if (defined $krb);
}
# note if users configuration should be run in 2nd stage
BEGIN { $TYPEINFO{SkipUserCreation} = ["function", "void", "boolean"];}
sub SkipUserCreation {
my ($self, $skip) = @_;
$skip_user_creation = bool ($skip);
}
# return the value of skip_user_creation
BEGIN { $TYPEINFO{UserCreationSkipped} = ["function", "boolean"];}
sub UserCreationSkipped {
return bool ($skip_user_creation);
}
##------------------------------------
# Returns the map of user configured during installation
# @return the map of user
BEGIN { $TYPEINFO{GetUser} = [ "function",
["map", "string", "any" ]];
}
sub GetUser {
my %ret = ();
%ret = %{$users[0]} if (defined $users[0]);
return \%ret;
}
##------------------------------------
# Returns the list users configured during installation
# @return the list of user maps
BEGIN { $TYPEINFO{GetUsers} = [ "function", ["list", "any" ]]; }
sub GetUsers {
return \@users;
}
##------------------------------------
# Saves the user data into the map
# @param data user initial data (could be an empty map)
BEGIN { $TYPEINFO{SetUser} = ["function",
"string",
["map", "string", "any" ]]; # data to fill in
}
sub SetUser {
my $self = shift;
my $data = shift;
if (defined $data && (ref ($data) eq "HASH")) {
my %user = %{$data};
@users = ();
push @users, %user;
}
return "";
}
##------------------------------------
# Saves the user data into the list
# @param list with user data maps (could be empty)
BEGIN { $TYPEINFO{SetUsers} = ["function",
"string",
["list", "any" ]]; # data to fill in
}
sub SetUsers {
my $self = shift;
my $data = shift;
if (defined $data && (ref ($data) eq "ARRAY")) {
@users = @{$data};
}
return "";
}
# were users from 1st stage already written?
BEGIN { $TYPEINFO{UsersWritten} = ["function", "boolean"];}
sub UsersWritten {
return bool ($users_written);
}
# was root password written in 1st stage?
BEGIN { $TYPEINFO{RootPasswordWritten} = ["function", "boolean"];}
sub RootPasswordWritten {
return bool ($root_password_written);
}
##------------------------------------
# save the root password into variable
BEGIN { $TYPEINFO{SetRootPassword} = ["function", "void", "string"];}
sub SetRootPassword {
my $self = shift;
$root_password = $_[0];
}
##------------------------------------
BEGIN { $TYPEINFO{GetRootPassword} = ["function", "string"];}
sub GetRootPassword {
return $root_password;
}
# remember if the checkbox 'Use this password for root' was checked
BEGIN { $TYPEINFO{SkipRootPasswordDialog} = ["function", "void", "boolean"];}
sub SkipRootPasswordDialog {
my $self = shift;
my $skip = shift;
$skip_root_dialog = bool ($skip) if (defined $skip);
}
# was the checkbox 'Use this password for root' was checked
BEGIN { $TYPEINFO{RootPasswordDialogSkipped} = ["function", "boolean"];}
sub RootPasswordDialogSkipped {
return bool ($skip_root_dialog);
}
##------------------------------------
# crypt given password
BEGIN { $TYPEINFO{CryptPassword} = ["function",
"string", "string"];
}
sub CryptPassword {
my $self = shift;
my $pw = shift;
return $pw if (!defined $pw);
return UsersUI->HashPassword (lc ($encryption_method), $pw);
}
##------------------------------------
# Writes password of superuser
# This is called during install
# @return true on success
BEGIN { $TYPEINFO{WriteRootPassword} = ["function", "boolean"];}
sub WriteRootPassword {
my $self = shift;
my $crypted = $self->CryptPassword ($root_password, "system");
return SCR->Write (".target.passwd.root", $crypted);
}
# "-" means range! -> at the begining or at the end!
# now CHARACTER_CLASS from /etc/login.defs is used
my $valid_logname_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-";
my $valid_password_chars = "[-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#\$%^&*() ,;:._+/|?{}=\['\"`~<>]|]";# the ']' is or-ed...
# error popup
my $valid_password_message = __("The password may only contain the following characters:
0-9, a-z, A-Z, and any of \"`~!\@#\$%^&* ,.;:._-+/|\?='{[(<>)]}\\\".
Try again.");
my $valid_home_chars = "[0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@/_.-]";
##------------------------------------
BEGIN { $TYPEINFO{ValidLognameChars} = ["function", "string"]; }
sub ValidLognameChars {
return $valid_logname_chars;
}
##------------------------------------
BEGIN { $TYPEINFO{ValidPasswordChars} = ["function", "string"]; }
sub ValidPasswordChars {
return $valid_password_chars;
}
##------------------------------------
BEGIN { $TYPEINFO{ValidHomeChars} = ["function", "string"]; }
sub ValidHomeChars {
return $valid_home_chars;
}
##------------------------------------
BEGIN { $TYPEINFO{ValidPasswordMessage} = ["function", "string"]; }
sub ValidPasswordMessage {
return $valid_password_message;
}
##------------------------------------
# Return the part of help text about valid password characters
BEGIN { $TYPEINFO{ValidPasswordHelptext} = ["function", "string"]; }
sub ValidPasswordHelptext {
# help text (default part shown in more places)
return __("<p>
For the password, use only characters that can be found on an English keyboard
layout. In cases of system error, it may be necessary to log in without a
localized keyboard layout.
</p>");
}
##------------------------------------
BEGIN { $TYPEINFO{GetMinPasswordLength} = ["function", "integer", "string"]; }
sub GetMinPasswordLength {
my $self = shift;
my $type = shift;
if (defined $type && defined ($min_pass_length{$type})) {
return $min_pass_length{$type};
}
else { return 5;}
}
##------------------------------------
# Set the minimum password length for given user type
# @param type
# @param length
BEGIN { $TYPEINFO{SetMinPasswordLength} = ["function",
"void", "string", "integer"];
}
sub SetMinPasswordLength {
my ($self, $type, $len) = @_;
$min_pass_length{$type} = $len;
}
##------------------------------------
BEGIN { $TYPEINFO{GetMaxPasswordLength} = ["function", "integer", "string"]; }
sub GetMaxPasswordLength {
my $self = shift;
if (defined ($max_pass_length{$_[0]})) {
return $max_pass_length{$_[0]};
}
else { return 8; }
}
##------------------------------------
# Set the maximum password length for given user type
# @param type
# @param length
BEGIN { $TYPEINFO{SetMaxPasswordLength} = ["function",
"void", "string", "integer"];
}
sub SetMaxPasswordLength {
my ($self, $type, $len) = @_;
$max_pass_length{$type} = $len;
}
##------------------------------------
BEGIN { $TYPEINFO{GetMinLoginLength} = ["function", "integer" ]; }
sub GetMinLoginLength {
my $self = shift;
return $min_length_login;
}
##------------------------------------
BEGIN { $TYPEINFO{GetMaxLoginLength} = ["function", "integer" ]; }
sub GetMaxLoginLength {
my $self = shift;
return $max_length_login;
}
##---------------------------------------------------------------------------
## check functions
##------------------------------------
# check fullname contents
BEGIN { $TYPEINFO{CheckFullname} = ["function", "string", "string"]; }
sub CheckFullname {
my ($self, $fullname) = @_;
if (defined $fullname && $fullname =~ m/[:,]/) {
# error popup
return __("The user's full name cannot contain
\":\" or \",\" characters.
Try again.");
}
return "";
}
##------------------------------------
# Just some simple checks for password contens
# @param usernames list of user or group names
# @param pw password
# @param user/group
# @return error message (password too simple) or empty string (OK)
BEGIN { $TYPEINFO{CheckObscurity} = ["function", "string",
["list", "string"],
"string", "string"];}
sub CheckObscurity {
my $self = shift;
my $names = shift;
my $pw = shift;
my $what = shift;
foreach my $name (@$names) {
if ($pw =~ m/$name/) {
if ($what eq "groups") {
# popup question
return __("You have used the group name as a part of the password.");
}
# popup question
return __("You have used the username as a part of the password.");
}
}
# check for lowercase
my $filtered = $pw;
$filtered =~ s/[[:lower:]]//g;
if ($filtered eq "") {
# popup question
return __("You have used only lowercase letters for the password.");
}
# check for uppercase
$filtered = $pw;
$filtered =~ s/[[:upper:]]//g;
if ($filtered eq "") {
# popup question
return __("You have used only uppercase letters for the password.");
}
# check for palindroms
$filtered = reverse $pw;
if ($filtered eq $pw) {
# popup question
return __("You have used a palindrome for the password.");
}
# check for numbers
$filtered = $pw;
$filtered =~ s/[0-9]//g;
if ($filtered eq "") {
# popup question
return __("You have used only digits for the password.");
}
return "";
}
##------------------------------------
# Checks if password is not too long
# @param pw password
# @param user/group type
BEGIN { $TYPEINFO{CheckPasswordMaxLength} = ["function",
"string", "string", "string"];
}
sub CheckPasswordMaxLength {
my $self = shift;
my $pw = shift;
my $type = shift;
my $max_length = $self->GetMaxPasswordLength ($type);
my $ret = "";
if (length ($pw) > $max_length) {
# popup question
$ret = sprintf (__("The password is too long for the current encryption method.
It will be truncated to %s characters."), $max_length);
}
return $ret;
}
##------------------------------------
# Try to crack password using cracklib
# @param pw password
# @return utility output: either "" or error message
BEGIN { $TYPEINFO{CrackPassword} = ["function", "string", "string"];}
sub CrackPassword {
my $self = shift;
my $pw = shift;
my $ret = "";
if (!defined $pw || $pw eq "") {
return $ret;
}
if (!defined $cracklib_dictpath || $cracklib_dictpath eq "" ||
!FileUtils->Exists ("$cracklib_dictpath.pwd")) {
$ret = SCR->Execute (".crack", $pw);
}
else {
$ret = SCR->Execute (".crack", $pw, $cracklib_dictpath);
}
if (!defined ($ret)) { $ret = ""; }
return $ret if ($ret eq "");
return UsersUI->RecodeUTF ($ret);
}
##------------------------------------
# check the password of given user
# @param password
# @param user type
# return value is error message
BEGIN { $TYPEINFO{CheckPassword} = ["function", "string", "string", "string"]; }
sub CheckPassword {
my ($self, $pw, $type) = @_;
my $min_length = $self->GetMinPasswordLength ($type);
if ((!defined $pw) || ($pw eq "" && $min_length > 0)) {
# error popup
return __("No password entered.
Try again.");
}
my $filtered = $pw;
$filtered =~ s/$valid_password_chars//g;
$filtered =~ s/\\//g; # bug 175706
if ($filtered ne "") {
return $self->ValidPasswordMessage ();
}
return "";
}
# Check the password of given user or group: part 2, checking for
# problems that may be skipped (accepted) by user
# @param data map containing user/group name, password and type
#
# Merges all error reports and returns them in the list
BEGIN { $TYPEINFO{CheckPasswordUI} = ["function",
["list", "string"],
["map", "string", "any"]];
}
sub CheckPasswordUI {
my ($self, $data) = @_;
my $pw = $data->{"userPassword"} || "";
my $name = $data->{"uid"};
$name = ($data->{"cn"} || "") if (!defined $name);
my $type = $data->{"type"} || "local";
my $min_length = $self->GetMinPasswordLength ($type);
my @ret = ();
if ($pw eq "") {
return \@ret;
}
if ($self->CrackLibUsed ()) {
my $error = $self->CrackPassword ($pw);
if ($error ne "") {
# error message
push @ret, sprintf (__("The password is too simple:
%s."), $error);
}
}
my $what = "users";
$what = "groups" if (! defined $data->{"uid"});
my @names = ( $name );
push @names, "root" if $data->{"root"} || 0;
my $error = $self->CheckObscurity (\@names, $pw, $what);
push @ret, $error if $error;
if (length ($pw) < $min_length) {
# popup error, %i is number
push @ret, sprintf (__("The password should have at least %i characters."), $min_length);
}
$error = $self->CheckPasswordMaxLength ($pw, $type);
push @ret, $error if $error;
return \@ret;
}
##------------------------------------
# Check the length of given user name
# @param user name
# @return error message
BEGIN { $TYPEINFO{CheckUsernameLength} = ["function", "string", "string"]; }
sub CheckUsernameLength {
my $self = shift;
my $username = shift;
if (!defined $username || $username eq "") {
# error popup
return __("No username entered.
Try again.");
}
my $min = $self->GetMinLoginLength ();
my $max = $self->GetMaxLoginLength ();
if (length ($username) < $min || length ($username) > $max) {
# error popup
return sprintf (__("The username must be between %i and %i characters in length.
Try again."), $min, $max);
}
return "";
}
##------------------------------------
# check given user name for valid contents
# @param user name
# @param user type (local/ldap etc.)
# @return error message
BEGIN { $TYPEINFO{CheckUsernameContents} = ["function",
"string", "string", "string"];
}
sub CheckUsernameContents {
my ($self, $username, $type) = @_;
my $filtered = $username;
# Samba users may need to have '$' at the end of username (#40433)
if ($type eq "ldap") {
$filtered =~ s/\$$//g;
}
my $grep = SCR->Execute (".target.bash_output", "echo '$filtered' | grep '\^$character_class\$'", { "LANG" => "C" });
my $stdout = $grep->{"stdout"} || "";
$stdout =~ s/\n//g;
if ($stdout ne $filtered) {
y2warning ("username $username doesn't match to $character_class");
# error popup
return __("The username may contain only
letters, digits, \"-\", \".\", and \"_\"
and must begin with a letter or \"_\".
Try again.");
}
return "";
}
##------------------------------------
# check given user name for a conflict with a (fixed) set of system users
# @param user name
# @return error message
BEGIN { $TYPEINFO{CheckUsernameConflicts} = ["function", "string", "string" ]; }
sub CheckUsernameConflicts {
my ($self, $username) = @_;
if (defined $system_users{$username}) {
# error popup
return __("There is a conflict between the entered
username and an existing username.
Try another one.");
}
}
# return the %system_users map = these are NOT the current system users,
# but the names that could be used for system users by packages
BEGIN { $TYPEINFO{GetSystemUserNames} = ["function", ["map", "string", "integer"]];}
sub GetSystemUserNames {
return \%system_users;
}
##---------------------------------------------------------------------------
##---------------------------------------------------------------------------
## Read/Write functions
# Writes the root password configured in the 1st stage
BEGIN { $TYPEINFO{Write} = ["function", "boolean"];}
sub Write {
my $self = shift;
if ($root_password ne "") {
# write root password now
return $self->WriteRootPassword ();
}
return bool (1);
}
# Empty function (kept for backward compatibility)
BEGIN { $TYPEINFO{Read} = ["function", "boolean", "boolean"];}
sub Read {
my $self = shift;
my $force = shift;
return bool (1);
}
# Remove the private data from users_first_stage.ycp after they were
# written (bnc#395796)
BEGIN { $TYPEINFO{RemoveUserData} = ["function", "boolean"];}
sub RemoveUserData {
my $self = shift;
my $file = Directory->vardir()."/users_first_stage.ycp";
my $ret = 1;
if (FileUtils->Exists ($file)) {
my $data = SCR->Read (".target.ycp", $file);
if (defined $data && ref ($data) eq "HASH") {
if (defined $data->{"users"}) {
delete $data->{"users"};
}
$data->{"users_written"} = YaST::YCP::Integer (1);
$ret = SCR->Write (".target.ycp", $file, $data);
}
}
return bool ($ret);
}
##---------------------------------------------------------------------------
## functions for handling passwd/shadow files in the 1st stage
## (simplified version of functions from UsersPasswd and Users)
# read 'shadow' file from a given directory
# return hash with shadow description
sub read_shadow {
my $base_directory = shift;
my $file = "$base_directory/shadow";
my %shadow_tmp = ();
my $in = SCR->Read (".target.string", $file);
if (! defined $in) {
y2warning ("$file cannot be opened for reading!");
return undef;
}
foreach my $shadow_entry (split (/\n/,$in)) {
chomp $shadow_entry;
next if ($shadow_entry eq "");
my ($uname,$pass,$last_change,$min, $max, $warn, $inact, $expire, $flag)
= split(/:/,$shadow_entry);
my $first = substr ($uname, 0, 1);
if ($first ne "#" && $first ne "+" && $first ne "-")
{
if (!defined $uname || $uname eq "") {
y2error ("strange line in shadow file: '$shadow_entry'");
return undef;
}
if (defined $shadow_tmp{$uname})
{
y2error ("duplicated username in /etc/shadow! Exiting...");
return undef;
}
$shadow_tmp{$uname} = {
"shadowLastChange" => $last_change,
"shadowWarning" => $warn,
"shadowInactive" => $inact,
"shadowExpire" => $expire,
"shadowMin" => $min,
"shadowMax" => $max,
"shadowFlag" => $flag,
"userPassword" => $pass
};
}
}
return \%shadow_tmp;
}