|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright (c) 2024, Till Brehm, ISPConfig UG |
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without modification, |
| 8 | +are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | + * Redistributions of source code must retain the above copyright notice, |
| 11 | + this list of conditions and the following disclaimer. |
| 12 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + this list of conditions and the following disclaimer in the documentation |
| 14 | + and/or other materials provided with the distribution. |
| 15 | + * Neither the name of ISPConfig nor the names of its contributors |
| 16 | + may be used to endorse or promote products derived from this software without |
| 17 | + specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 | +IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 23 | +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 26 | +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 | +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +*/ |
| 30 | + |
| 31 | +class user_cli extends cli { |
| 32 | + |
| 33 | + function __construct() { |
| 34 | + $cmd_opt = []; |
| 35 | + $cmd_opt['user'] = 'showHelp'; |
| 36 | + $cmd_opt['user:set-password'] = 'setPassword'; |
| 37 | + $this->addCmdOpt($cmd_opt); |
| 38 | + } |
| 39 | + |
| 40 | + public function setPassword($arg) { |
| 41 | + global $app, $conf; |
| 42 | + |
| 43 | + // Get username |
| 44 | + $username = $arg[0]; |
| 45 | + |
| 46 | + // Check empty username |
| 47 | + if(empty($username)) { |
| 48 | + $this->swriteln(); |
| 49 | + $this->swriteln('Error: Username may not be empty.'); |
| 50 | + $this->swriteln(); |
| 51 | + $this->showHelp($arg); |
| 52 | + die(); |
| 53 | + } |
| 54 | + |
| 55 | + // Check for invalid chars |
| 56 | + if(!preg_match('/^[\w\.\-\_]{1,64}$/',$username)) { |
| 57 | + $this->swriteln(); |
| 58 | + $this->swriteln('Error: Username contains invalid characters.'); |
| 59 | + $this->swriteln(); |
| 60 | + $this->showHelp($arg); |
| 61 | + die(); |
| 62 | + } |
| 63 | + |
| 64 | + // Get user from ISPConfig database |
| 65 | + $user = $app->db->queryOneRecord("SELECT * FROM `sys_user` WHERE `username` = ?",$username); |
| 66 | + |
| 67 | + // Check if user exists |
| 68 | + if(empty($user)) { |
| 69 | + $this->swriteln(); |
| 70 | + $this->swriteln('Error: Username does not exist.'); |
| 71 | + $this->swriteln(); |
| 72 | + $this->showHelp($arg); |
| 73 | + die(); |
| 74 | + } |
| 75 | + |
| 76 | + // Include auth class from interface |
| 77 | + include_once '/usr/local/ispconfig/interface/lib/classes/auth.inc.php'; |
| 78 | + $app->auth = new auth; |
| 79 | + |
| 80 | + $ok = false; |
| 81 | + |
| 82 | + while ($ok == false) { |
| 83 | + |
| 84 | + $ok = true; |
| 85 | + // Ask for new password |
| 86 | + $min_password_len = $app->auth->get_min_password_length(); |
| 87 | + $new_password = $this->free_query('Enter new password for the user '.$username .' or quit', $app->auth->get_random_password($min_password_len)); |
| 88 | + |
| 89 | + if(strlen($new_password) < $min_password_len) { |
| 90 | + $this->swriteln('The minimum password length is '. $min_password_len); |
| 91 | + $this->swriteln(); |
| 92 | + $ok = false; |
| 93 | + } |
| 94 | + |
| 95 | + if($ok) { |
| 96 | + $new_password2 = $this->free_query('Repeat the password', ''); |
| 97 | + } |
| 98 | + |
| 99 | + if($ok && $new_password != $new_password2) { |
| 100 | + $this->swriteln('Passwords do not match.'); |
| 101 | + $this->swriteln(); |
| 102 | + $ok = false; |
| 103 | + } |
| 104 | + |
| 105 | + if($ok) { |
| 106 | + $crypted_password = $app->auth->crypt_password($new_password); |
| 107 | + $app->db->query("UPDATE `sys_user` SET `passwort` = ? WHERE `username` = ?",$crypted_password,$username); |
| 108 | + $this->swriteln('Password for user '.$username.' has been changed.'); |
| 109 | + $this->swriteln(); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public function showHelp($arg) { |
| 115 | + global $conf; |
| 116 | + |
| 117 | + $this->swriteln("---------------------------------"); |
| 118 | + $this->swriteln("- Available commandline options -"); |
| 119 | + $this->swriteln("---------------------------------"); |
| 120 | + $this->swriteln("ispc user set-password <username> - Set a new password for the ISPConfig user <username>."); |
| 121 | + $this->swriteln("---------------------------------"); |
| 122 | + $this->swriteln(); |
| 123 | + } |
| 124 | + |
| 125 | +} |
| 126 | + |
0 commit comments