Debian Codegraph @

lib/lamdaemon.pl

raw
1 #! /usr/bin/perl
2
3 # This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
4 # Copyright (C) 2003 - 2006 Tilo Lutz
5 # Copyright (C) 2006 - 2023 Roland Gruber
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #
21 #
22 # LDAP Account Manager daemon to create and delete homedirecotries and quotas
23
24 use Sys::Syslog;
25
26 # Defines the protocol version of the lamdaemon script.
27 # This will only be changed when additional commands are added etc.
28 my $LAMDAEMON_PROTOCOL_VERSION = 5;
29
30 my $SPLIT_DELIMITER = "###x##y##x###";
31
32 # set a known path
33 my $path = "";
34 if (-d "/sbin") {
35 if ($path eq "") { $path = "/sbin"; }
36 else { $path = "$path:/sbin"; }
37 }
38 if (-d "/usr/sbin") {
39 if ($path eq "") { $path = "/usr/sbin"; }
40 else { $path = "$path:/usr/sbin"; }
41 }
42 if (-l "/bin") {
43 if ($path eq "") { $path = "/usr/bin"; }
44 else { $path = "$path:/usr/bin"; }
45 }
46 else {
47 if ($path eq "") { $path = "/bin:/usr/bin"; }
48 else { $path = "$path:/bin:/usr/bin"; }
49 }
50 if (-d "/opt/sbin") { $path = "$path:/opt/sbin"; }
51 if (-d "/opt/bin") { $path = "$path:/opt/bin"; }
52 $ENV{"PATH"} = $path;
53
54 # get hostname
55 $hostname = `hostname`;
56 chop($hostname);
57
58 #use strict; # Use strict for security reasons
59
60 @quota_grp;
61 @quota_usr; # Filesystems with enabled user quotas
62 # vals = DN, PAssword, user, home, (add|rem),
63 # quota, (set|get),(u|g), (mountpoint,blocksoft,blockhard,filesoft,filehard)+
64 # chown options
65 $|=1; # Disable buffering
66
67 sub get_fs { # Load mount points from mtab if enabled quotas
68 Quota::setmntent();
69 my $i=0;
70 my @args;
71 my %deviceList;
72 while (my @temp = Quota::getmntent()) {
73 if (exists($deviceList{$temp[0]})) {
74 # skip duplicate devices
75 next;
76 }
77 $args[$i][0] = $temp[0];
78 $args[$i][1] = $temp[1];
79 $args[$i][2] = $temp[2];
80 $args[$i][3] = $temp[3];
81 $deviceList{$temp[0]} = 1;
82 $i++;
83 }
84 Quota::endmntent();
85 my $j=0; my $k=0; $i=0;
86 while ($args[$i][0]) {
87 if ( $args[$i][3] =~ m/usr[j]?quota/ ) {
88 $quota_usr[$j][0] = $args[$i][0];
89 $quota_usr[$j][1] = $args[$i][1];
90 $quota_usr[$j][2] = $args[$i][2];
91 $quota_usr[$j][3] = $args[$i][3];
92 $j++;
93 }
94 if ( $args[$i][3] =~ m/grp[j]?quota/ ) {
95 $quota_grp[$k][0] = $args[$i][0];
96 $quota_grp[$k][1] = $args[$i][1];
97 $quota_grp[$k][2] = $args[$i][2];
98 $quota_grp[$k][3] = $args[$i][3];
99 $k++;
100 }
101 $i++;
102 }
103 }
104
105 # ***************** Check values
106
107 # check if script runs as root
108 if ($< != 0 ) {
109 print "ERROR,Lamdaemon ($hostname),Not called as root (user id " . $< . ").\n";
110 logMessage(LOG_ERR, "Not called as root (user id " . $< . ").");
111 exit 1;
112 }
113
114 # Drop root privileges
115 ($<, $>) = ($>, $<);
116 my $input = $ARGV[0];
117 $return = "";
118 @vals = split ($SPLIT_DELIMITER, $input);
119 # Get user information
120 if (($vals[3] eq 'user') || ($vals[1] eq 'home')) { @user = getpwnam($vals[0]); }
121 else { @user = getgrnam($vals[0]); }
122 if ($vals[1] eq '') {
123 # empty line, nothing to do
124 }
125 elsif (($vals[1] eq 'test')) {
126 # run tests
127 runTest();
128 }
129 elsif ($vals[1] eq 'home') {
130 manageHomedirs();
131 }
132 elsif ($vals[1] eq 'directory') {
133 manageDirectories();
134 }
135 elsif ($vals[1] eq 'quota') {
136 manageQuotas();
137 }
138 else {
139 $return = "ERROR,Lamdaemon ($hostname),Unknown command $vals[1].";
140 logMessage(LOG_ERR, "Unknown command $vals[1].");
141 }
142 print "$return\n";
143
144 #
145 # Runs tests to check the environment
146 #
147 sub runTest {
148 # protocol version check
149 if ($vals[2] eq 'version') {
150 if ($vals[3] eq $LAMDAEMON_PROTOCOL_VERSION) {
151 $return = "INFO,Version check ok";
152 }
153 else {
154 $return = "ERROR,Version check failed. Please upgrade the lamdaemon script to the same version as your main LAM installation.";
155 }
156 }
157 # basic test
158 elsif ($vals[2] eq 'basic') {
159 $return = "INFO,Basic test ok";
160 }
161 # quota test
162 elsif ($vals[2] eq 'quota') {
163 require Quota;
164 $return = "INFO,Quota test ok";
165 }
166 # NSS LDAP
167 elsif ($vals[2] eq 'nss') {
168 $userName = $vals[3];
169 # check if the user exists in /etc/passwd
170 system("grep", "-q", "^" . $userName . ":", "/etc/passwd");
171 if ( $? == 0 ) {
172 $error = "User $userName is a local user (/etc/passwd) but should be LDAP only.";
173 $return = "ERROR,$error";
174 logMessage(LOG_ERR, $error);
175 }
176 else {
177 # check if home directory is readable
178 @user = getpwnam($userName);
179 if ($user[7] eq '') {
180 $return = "ERROR,Unable to determine home directory of user $userName. Please check that NSS LDAP is correctly configured.";
181 logMessage(LOG_ERR, "Unable to determine home directory of user $userName. Please check that NSS LDAP is correctly configured.");
182 }
183 else {
184 $return = "INFO,NSS test ok";
185 }
186 }
187 }
188 else {
189 $return = "ERROR,Unknown test: $vals[2]";
190 }
191 }
192
193 #
194 # Handles all homedir related commands
195 #
196 sub manageHomedirs {
197 if ($vals[2] eq 'add') {
198 createHomedir();
199 }
200 elsif ($vals[2] eq 'rem') {
201 removeHomedir();
202 }
203 elsif ($vals[2] eq 'chgrp') {
204 chgrpHomedir();
205 }
206 elsif ($vals[2] eq 'move') {
207 moveHomedir();
208 }
209 elsif ($vals[2] eq 'check') {
210 checkHomedir();
211 }
212 else {
213 # Show error if undefined command is used
214 $return = "ERROR,Lamdaemon ($hostname),Unknown home command $vals[2].";
215 logMessage(LOG_ERR, "Unknown command $vals[2]");
216 }
217 }
218
219 #
220 # Creates the home directory of the user
221 #
222 sub createHomedir {
223 my $homedir = $vals[3];
224 if ($homedir eq '') {
225 $return = "ERROR,Lamdaemon ($hostname),No home directory specified.";
226 logMessage(LOG_ERR, "No home directory specified to create.");
227 return;
228 }
229 my $path = $homedir;
230 # split homedir to set all directories below the last dir. to 0755
231 $path =~ s,/(?:[^/]*)$,,;
232 ($<, $>) = ($>, $<); # Get root privileges
233 if (! -e $path) {
234 system 'mkdir', '-m', '0755', '-p', $path; # Create paths to homedir
235 }
236 if (! -e $homedir) {
237 system 'mkdir', '-m', $vals[4], $homedir; # Create homedir itself
238 system ("(cd /etc/skel && tar cf - .) | (cd $homedir && tar xmf -)"); # Copy /etc/skel into homedir
239 system 'chown', '-hR', "$vals[5]:$vals[6]" , $homedir; # Change owner to new user
240 if (-e '/usr/sbin/useradd.local') {
241 system '/usr/sbin/useradd.local', $vals[0]; # run useradd-script
242 system 'chmod', '-R', $vals[4], $homedir; # Edit chmod rights
243 }
244 system 'chmod', $vals[4], $homedir; # Edit chmod rights
245 $return = "INFO,Lamdaemon ($hostname),Home directory created (" . $homedir . ").";
246 logMessage(LOG_INFO, "Home directory created (" . $homedir . ")");
247 }
248 else {
249 $return = "ERROR,Lamdaemon ($hostname),Home directory already exists (" . $homedir . ").";
250 logMessage(LOG_ERR, "Home directory already exists (" . $homedir . ")");
251 }
252 ($<, $>) = ($>, $<); # Give up root privileges
253 }
254
255 #
256 # Removes the home directory of the user
257 #
258 sub removeHomedir {
259 if ($vals[3] eq '') {
260 $return = "ERROR,Lamdaemon ($hostname),No home directory specified to delete.";
261 logMessage(LOG_ERR, "No home directory specified to delete.");
262 return;
263 }
264 ($<, $>) = ($>, $<); # Get root privileges
265 if (-d $vals[3] && $vals[3] ne '/') {
266 if ((stat($vals[3]))[4] eq $vals[4]) {
267 if (-e '/usr/sbin/userdel.local') {
268 system '/usr/sbin/userdel.local', $vals[0];
269 }
270 system 'rm', '-Rf', $vals[3]; # delete home directory
271 $return = "Ok";
272 logMessage(LOG_INFO, "Home directory removed (" . $vals[3] . ")");
273 }
274 else {
275 $return = "ERROR,Lamdaemon ($hostname),Home directory not owned by $vals[4].";
276 logMessage(LOG_ERR, "Home directory owned by wrong user (" . $vals[4] . ")");
277 }
278 }
279 else {
280 $return = "Ok";
281 logMessage(LOG_INFO, "The directory " . $vals[3] . " which should be deleted was not found (skipped).");
282 }
283 ($<, $>) = ($>, $<); # Give up root privileges
284 }
285
286 #
287 # Moves the home directory of the user
288 #
289 sub moveHomedir {
290 my $homedir = $vals[3];
291 my $owner = $vals[4];
292 my $homedirNew = $vals[5];
293 if ($homedir eq '') {
294 $return = "ERROR,Lamdaemon ($hostname),No home directory specified to move.";
295 logMessage(LOG_ERR, "No home directory specified to move.");
296 return;
297 }
298 if (-d $homedirNew) {
299 $return = "ERROR,Lamdaemon ($hostname),Directory $homedirNew already exists.";
300 logMessage(LOG_ERR, "Directory $homedirNew already exists.");
301 return;
302 }
303 ($<, $>) = ($>, $<); # Get root privileges
304 if (-d $homedir && $homedir ne '/') {
305 if ((stat($homedir))[4] eq $owner) {
306 system 'mv', $homedir, $homedirNew; # move home directory
307 $return = "Ok";
308 logMessage(LOG_INFO, "Home directory moved ($homedir - $homedirNew)");
309 }
310 else {
311 $return = "ERROR,Lamdaemon ($hostname),Home directory not owned by $owner.";
312 logMessage(LOG_ERR, "Home directory owned by wrong user (" . $owner . ")");
313 }
314 }
315 else {
316 $return = "Ok";
317 logMessage(LOG_INFO, "The directory " . $homedir . " which should be moved was not found (skipped).");
318 }
319 ($<, $>) = ($>, $<); # Give up root privileges
320 }
321
322 #
323 # Changes the group of the home directory of the user.
324 #
325 sub chgrpHomedir {
326 my $homedir = $vals[3];
327 my $owner = $vals[4];
328 my $group = $vals[5];
329 if ($homedir eq '') {
330 $return = "ERROR,Lamdaemon ($hostname),No home directory specified to move.";
331 logMessage(LOG_ERR, "No home directory specified to move.");
332 return;
333 }
334 ($<, $>) = ($>, $<); # Get root privileges
335 if (-d $homedir && $homedir ne '/') {
336 if ((stat($homedir))[4] eq $owner) {
337 system 'chgrp', $group, $homedir; # change group
338 $return = "Ok";
339 logMessage(LOG_INFO, "Home directory changed to new group ($homedir - $group)");
340 }
341 else {
342 $return = "ERROR,Lamdaemon ($hostname),Home directory not owned by $owner.";
343 logMessage(LOG_ERR, "Home directory owned by wrong user (" . $owner . ")");
344 }
345 }
346 else {
347 $return = "Ok";
348 logMessage(LOG_INFO, "The directory " . $homedir . " which should be changed was not found (skipped).");
349 }
350 ($<, $>) = ($>, $<); # Give up root privileges
351 }
352
353 #
354 # Checks if the home directory of the user already exists.
355 #
356 sub checkHomedir {
357 my $homedir = $vals[3];
358 if ($homedir eq '') {
359 $return = "ERROR,Lamdaemon ($hostname),No home directory specified to check.";
360 logMessage(LOG_ERR, "No home directory specified to check.");
361 return;
362 }
363 if (-d $homedir) {
364 $return = "ok";
365 }
366 else {
367 $return = "missing";
368 }
369 }
370
371 #
372 # Handles all directory related commands
373 #
374 sub manageDirectories {
375 if ($vals[2] eq 'add') {
376 createDirectory();
377 }
378 else {
379 # Show error if undefined command is used
380 $return = "ERROR,Lamdaemon ($hostname),Unknown home command $vals[2].";
381 logMessage(LOG_ERR, "Unknown command $vals[2]");
382 }
383 }
384
385 #
386 # Creates a directory of the user
387 #
388 sub createDirectory {
389 my $homedir = $vals[3];
390 if ($homedir eq '') {
391 $return = "ERROR,Lamdaemon ($hostname),No directory specified.";
392 logMessage(LOG_ERR, "No directory specified to create.");
393 return;
394 }
395 my $path = $homedir;
396 # split homedir to set all directories below the last dir. to 0755
397 $path =~ s,/(?:[^/]*)$,,;
398 ($<, $>) = ($>, $<); # Get root privileges
399 if (! -e $path) {
400 system 'mkdir', '-m', '0755', '-p', $path; # Create paths to homedir
401 }
402 if (! -e $homedir) {
403 system 'mkdir', '-m', $vals[4], $homedir; # Create homedir itself
404 system 'chown', '-hR', "$vals[5]:$vals[6]" , $homedir; # Change owner to new user
405 system 'chmod', $vals[4], $homedir; # Edit chmod rights
406 $return = "INFO,Lamdaemon ($hostname),Directory created (" . $homedir . ").";
407 logMessage(LOG_INFO, "Directory created (" . $homedir . ")");
408 }
409 else {
410 $return = "ERROR,Lamdaemon ($hostname),Directory already exists (" . $homedir . ").";
411 logMessage(LOG_ERR, "Directory already exists (" . $homedir . ")");
412 }
413 ($<, $>) = ($>, $<); # Give up root privileges
414 }
415
416 #
417 # Handles all quota related commands
418 #
419 sub manageQuotas {
420 require Quota; # Needed to get and set quotas
421 get_fs(); # Load list of devices with enabled quotas
422 # Store quota information in array
423 @quota_temp1 = split (':', $vals[4]);
424 $group=0;
425 $i=0;
426 foreach my $quota_element (@quota_temp1) {
427 if (length ($quota_element) >= 2) {
428 @temp = split (',', $quota_element);
429 $j=0;
430 foreach my $temp_element (@temp) {
431 $quota[$i][$j] = $temp_element;
432 $j++;
433 }
434 $i++;
435 }
436 }
437 if ($vals[3] eq 'user') { $group=false; }
438 else {
439 $group=1;
440 @quota_usr = @quota_grp;
441 }
442 if ($vals[2] eq 'rem') {
443 remQuotas();
444 }
445 elsif ($vals[2] eq 'set') {
446 setQuotas();
447 }
448 elsif ($vals[2] eq 'get') {
449 getQuotas();
450 }
451 else {
452 $return = "ERROR,Lamdaemon ($hostname),Unknown quota command $vals[2].";
453 logMessage(LOG_ERR, "Unknown command $vals[2].");
454 }
455 }
456
457 #
458 # Removes the quotas of a user or group
459 #
460 sub remQuotas {
461 $i=0;
462 ($<, $>) = ($>, $<); # Get root privileges
463 while ($quota_usr[$i][0]) {
464 $dev = Quota::getqcarg($quota_usr[$i][1]);
465 $return = Quota::setqlim($dev,$user[2],0,0,0,0,1,$group);
466 $i++;
467 }
468 ($<, $>) = ($>, $<); # Give up root privileges
469 }
470
471 #
472 # Sets the quota values
473 #
474 sub setQuotas {
475 $i=0;
476 ($<, $>) = ($>, $<); # Get root privileges
477 while ($quota[$i][0]) {
478 $dev = Quota::getqcarg($quota[$i][0]);
479 last if ($dev eq '');
480 $return = Quota::setqlim($dev,$user[2],$quota[$i][1],$quota[$i][2],$quota[$i][3],$quota[$i][4],1,$group);
481 if ($return == -1) {
482 $return = "ERROR,Lamdaemon ($hostname),Unable to set quota!";
483 logMessage(LOG_ERR, "Unable to set quota for $user[0] on " . $quota[$i][0] . ".");
484 }
485 else {
486 logMessage(LOG_INFO, "Set quota for $user[0].");
487 }
488 $i++;
489 }
490 ($<, $>) = ($>, $<); # Give up root privileges
491 }
492
493 #
494 # Reads the quota values
495 #
496 sub getQuotas {
497 $i=0;
498 ($<, $>) = ($>, $<); # Get root privileges
499 while ($quota_usr[$i][0]) {
500 if ($vals[0]ne'+') {
501 $dev = Quota::getqcarg($quota_usr[$i][1]);
502 @temp = Quota::query($dev,$user[2],$group);
503 if ($temp[0]ne'') {
504 if ($temp == -1) {
505 $return = "ERROR,Lamdaemon ($hostname),Unable to read quota!";
506 logMessage(LOG_ERR, "Unable to read quota for $user[0].");
507 }
508 else {
509 $return = "QUOTA_ENTRY $quota_usr[$i][1],$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$temp[7]:$return";
510 }
511 }
512 else { $return = "QUOTA_ENTRY $quota_usr[$i][1],0,0,0,0,0,0,0,0:$return"; }
513 }
514 else { $return = "QUOTA_ENTRY $quota_usr[$i][1],0,0,0,0,0,0,0,0:$return"; }
515 $i++;
516 }
517 ($<, $>) = ($>, $<); # Give up root privileges
518 }
519
520 #
521 # Logs a message to the syslog.
522 #
523 # Parameters: level message
524 #
525 # level: error level
526 # message: message text
527 #
528 sub logMessage {
529 my $level = $_[0];
530 my $message = $_[1];
531 openlog('LAM - lamdaemon','','user');
532 syslog($level, $message);
533 closelog;
534 }
535