Handle hostnames with upper-case letters
[webmin.git] / samba / useradmin_update.pl
1
2 do 'samba-lib.pl';
3
4 # useradmin_create_user(&details)
5 # Create a new samba user if sync is enabled
6 sub useradmin_create_user
7 {
8 &get_share("global");
9 if (&istrue("encrypt passwords") && ($config{'smb_passwd'} || $has_pdbedit) &&
10     $config{'sync_add'} && !&get_user($_[0]->{'user'})) {
11         # Add a user to smbpasswd
12         &lock_file($config{'smb_passwd'});
13         local $u = { 'name' => $_[0]->{'user'},
14                      'uid' => $_[0]->{'uid'} };
15         if ($samba_version >= 2) {
16                 local @opts = ("U");
17                 push(@opts, "N") if ($_[0]->{'passmode'} == 0);
18                 push(@opts, "D") if ($_[0]->{'passmode'} == 1);
19                 $u->{'opts'} = \@opts;
20                 }
21         else {
22                 $u->{'real'} = $_[0]->{'real'};
23                 $u->{'home'} = $_[0]->{'home'};
24                 $u->{'shell'} = $_[0]->{'shell'};
25                 }
26         $u->{'pass1'} = $u->{'pass2'} = ("X" x 32);
27         if ($_[0]->{'passmode'} == 0) {
28                 $u->{'pass1'} = "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX";
29                 $u->{'pass2'} = $u->{'pass1'};
30                 }
31         &create_user($u);
32         if ($_[0]->{'passmode'} == 3) {
33                 &set_password($_[0]->{'user'}, $_[0]->{'plainpass'});
34                 }
35         &unlock_file($config{'smb_passwd'});
36         }
37 }
38
39 # useradmin_delete_user(&details)
40 # Delete a samba user
41 sub useradmin_delete_user
42 {
43 &get_share("global");
44 if (&istrue("encrypt passwords") && ($config{'smb_passwd'} || $has_pdbedit) &&
45     $config{'sync_delete'} && ($u = &get_user($_[0]->{'user'}))) {
46         # Delete the user
47         &lock_file($config{'smb_passwd'});
48         &delete_user($u);
49         &unlock_file($config{'smb_passwd'});
50         }
51
52 if ($config{'sync_delete_profile'}) {
53         # Delete his roaming profile, if any
54         if (&get_share("Profiles")) {
55                 local $ppath = &getval("path");
56                 if ($ppath) {
57                         local $upath = "$ppath/$_[0]->{'user'}";
58                         if (-d $upath) {
59                                 &system_logged("rm -rf ".quotemeta($upath));
60                                 }
61                         else {
62                                 &lock_file($upath);
63                                 unlink($upath);
64                                 &unlock_file($upath);
65                                 }
66                         }
67                 }
68         }
69
70 }
71
72 # useradmin_modify_user(&details)
73 # Update a samba user
74 sub useradmin_modify_user
75 {
76 &get_share("global");
77 if (&istrue("encrypt passwords") && ($config{'smb_passwd'} || $has_pdbedit) &&
78     $config{'sync_change'} && ($u = &get_user($_[0]->{'olduser'}))) {
79         # Update details
80         &lock_file($config{'smb_passwd'});
81         $u->{'uid'} = $_[0]->{'uid'};
82         $u->{'name'} = $_[0]->{'user'};
83         if ($u->{'opts'}) {
84                 local @opts = grep { !/[ND]/ } @{$u->{'opts'}};
85                 push(@opts, "N") if ($_[0]->{'passmode'} == 0);
86                 push(@opts, "D") if ($_[0]->{'passmode'} == 1);
87                 $u->{'opts'} = \@opts;
88                 }
89         else {
90                 $u->{'real'} = $_[0]->{'real'};
91                 $u->{'home'} = $_[0]->{'home'};
92                 $u->{'shell'} = $_[0]->{'shell'};
93                 }
94         if ($_[0]->{'passmode'} == 0) {
95                 $u->{'pass1'} = "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX";
96                 $u->{'pass2'} = $u->{'pass1'};
97                 }
98         elsif ($_[0]->{'passmode'} == 1) {
99                 $u->{'pass1'} = $u->{'pass2'} = ("X" x 32);
100                 }
101         &modify_user($u);
102         if ($_[0]->{'passmode'} == 3) {
103                 &set_password($_[0]->{'user'}, $_[0]->{'plainpass'});
104                 }
105         &unlock_file($config{'smb_passwd'});
106         }
107
108 if ($config{'sync_change_profile'}) {
109         # Rename his roaming profile, if any
110         if (&get_share("Profiles")) {
111                 local $ppath = &getval("path");
112                 if ($ppath) {
113                         local $upath = "$ppath/$_[0]->{'olduser'}";
114                         local $newupath = "$ppath/$_[0]->{'user'}";
115                         if (-e $upath) {
116                                 &rename_logged($upath, $newupath);
117                                 }
118                         }
119                 }
120         }
121
122 }
123
124 sub get_user
125 {
126 local @ulist = &list_users();
127 local $u;
128 foreach $u (@ulist) {
129         return $u if ($u->{'name'} eq $_[0]);
130         }
131 return undef;
132 }
133
134
135 # When running Samba 3.x, these functions update the Samba groups file to
136 # match Unix groups
137
138 # useradmin_create_group(&group)
139 sub useradmin_create_group
140 {
141 return if (!$config{'gsync_add'});
142 return if ($samba_version < 3 || 
143            (!$has_smbgroupedit && !$has_net));
144 local $clash = &get_group($_[0]->{'group'});
145 return if ($clash);
146
147 local $group = { 'name' => $_[0]->{'group'},
148                  'unix' => $_[0]->{'group'},
149                  'type' => $config{'gsync_type'} };
150 if ($group->{'type'} eq 'l') {
151         $group->{'priv'} = $config{'gsync_priv'};
152         }
153 &create_group($group);
154 }
155
156 # useradmin_delete_group(&group)
157 sub useradmin_delete_group
158 {
159 return if (!$config{'gsync_delete'});
160 return if ($samba_version < 3 || 
161            (!$has_smbgroupedit && !$has_net));
162 local $group = &get_group($_[0]->{'group'});
163 return if (!$group);
164
165 &delete_group($group);
166 }
167
168 # useradmin_modify_group(&group, &oldgroup)
169 sub useradmin_modify_group
170 {
171 return if (!$config{'gsync_change'});
172 return if ($_[0]->{'group'} eq $_[1]->{'group'});
173 return if ($samba_version < 3 || 
174            (!$has_smbgroupedit && !$has_net));
175 local $group = &get_group($_[1]->{'group'});
176 return if (!$group);
177
178 $group->{'name'} = $_[0]->{'group'};
179 &modify_group($group);
180 # XXX clash?
181 }
182
183 sub get_group
184 {
185 local @glist = &list_groups();
186 local $g;
187 foreach $g (@glist) {
188         return $g if ($g->{'name'} eq $_[0]);
189         }
190 return undef;
191 }
192
193 1;
194