Handle hostnames with upper-case letters
[webmin.git] / rbac / save_prof.cgi
1 #!/usr/local/bin/perl
2 # Create, update or delete one RBAC profile
3
4 require './rbac-lib.pl';
5 &ReadParse();
6 $access{'profs'} == 1 || &error($text{'profs_ecannot'});
7 &error_setup($text{'prof_err'});
8
9 &lock_rbac_files();
10 $profs = &list_prof_attrs();
11 if (!$in{'new'}) {
12         $prof = $profs->[$in{'idx'}];
13         $logname = $prof->{'name'};
14         }
15 else {
16         $prof = { 'attr' => { } };
17         $logname = $in{'name'};
18         }
19
20 if (!$in{'new'}) {
21         # Find users of this profile
22         $users = &list_user_attrs();
23         foreach $u (@$users) {
24                 local @profiles =
25                     split(/,/, $u->{'attr'}->{'profiles'});
26                 $idx = &indexof($logname, @profiles);
27                 if ($idx >= 0) {
28                         push(@profusers, [ $u, $idx, \@profiles ]);
29                         }
30                 }
31         foreach $p (@$profs) {
32                 local @profiles =
33                     split(/,/, $p->{'attr'}->{'profs'});
34                 $idx = &indexof($logname, @profiles);
35                 if ($idx >= 0) {
36                         push(@profprofs, [ $p, $idx, \@profiles ]);
37                         }
38                 }
39         $execs = &list_exec_attrs();
40         foreach $e (@$execs) {
41                 if ($e->{'name'} eq $logname) {
42                         push(@profexecs, [ $e ]);
43                         }
44                 }
45         }
46
47 if ($in{'delete'}) {
48         # Just delete this prof
49         @profusers && &error(&text('prof_einuseu',
50                                    $profusers[0]->[0]->{'user'}));
51         @profprofs && &error(&text('prof_einusep',
52                                    $profprofs[0]->[0]->{'name'}));
53         @profexecs && &error(&text('prof_einusee', scalar(@profexecs)));
54         &delete_prof_attr($prof);
55         }
56 else {
57         # Check for clash
58         if ($in{'new'} || $logname ne $in{'name'}) {
59                 ($clash) = grep { $_->{'name'} eq $in{'name'} } @$profs;
60                 $clash && &error($text{'prof_eclash'});
61                 }
62
63         # Validate and store inputs
64         $in{'name'} =~ /^[^:,]+$/ || &error($text{'prof_ename'});
65         $prof->{'name'} = $in{'name'};
66         $in{'desc'} =~ /^[^:]*$/ || &error($text{'prof_edesc'});
67         $prof->{'desc'} = $in{'desc'};
68         $profiles = &profiles_parse("profiles");
69         if ($profiles) {
70                 @profiles = split(/,/, $profiles);
71                 &indexof($in{'name'}, @profiles) < 0 ||
72                         &error($text{'prof_esub'});
73                 $prof->{'attr'}->{'profs'} = $profiles;
74                 }
75         else {
76                 delete($prof->{'attr'}->{'profs'});
77                 }
78         $auths = &auths_parse("auths");
79         if ($auths) {
80                 $prof->{'attr'}->{'auths'} = $auths;
81                 }
82         else {
83                 delete($prof->{'attr'}->{'auths'});
84                 }
85
86         # Save or update profile
87         if ($in{'new'}) {
88                 &create_prof_attr($prof);
89                 }
90         else {
91                 &modify_prof_attr($prof);
92
93                 # Update other users of this profile, if renamed
94                 if ($logname ne $in{'name'}) {
95                         foreach $pu (@profusers) {
96                                 $pu->[2]->[$pu->[1]] = $in{'name'};
97                                 $pu->[0]->{'attr'}->{'profiles'} =
98                                         join(",", @{$pu->[2]});
99                                 &modify_user_attr($pu->[0]);
100                                 }
101                         foreach $pp (@profprofs) {
102                                 $pp->[2]->[$pp->[1]] = $in{'name'};
103                                 $pp->[0]->{'attr'}->{'profiles'} =
104                                         join(",", @{$pp->[2]});
105                                 &modify_prof_attr($pp->[0]);
106                                 }
107                         foreach $pe (@profexecs) {
108                                 $pe->[0]->{'name'} = $in{'name'};
109                                 &modify_exec_attr($pe->[0]);
110                                 }
111                         }
112                 }
113         }
114
115 &unlock_rbac_files();
116 &webmin_log($in{'delete'} ? "delete" : $in{'new'} ? "create" : "modify",
117             "prof", $logname, $prof);
118 &redirect("list_profs.cgi");
119