@@ -737,6 +737,7 @@ function add_user_to_group($group, $user = 'admispconfig'){
737737 }
738738 }
739739
740+ /*
740741 function usermod($user, $groups){
741742 global $app;
742743 if($this->is_user($user)){
@@ -776,6 +777,7 @@ function usermod($user, $groups){
776777 return false;
777778 }
778779 }
780+ */
779781
780782 /**boot autostart etc
781783 *
@@ -1396,6 +1398,102 @@ function web_folder_protection($document_root,$protect) {
13961398 if ($ document_root != '' && $ document_root != '/ ' && strlen ($ document_root ) > 6 && !stristr ($ document_root ,'.. ' )) exec ('chattr -i ' .escapeshellcmd ($ document_root ));
13971399 }
13981400 }
1401+
1402+ function usermod ($ username , $ uid = 0 , $ gid = 0 , $ home = '' , $ shell = '' , $ password = '' , $ login = '' ) {
1403+ global $ app ;
1404+
1405+ if ($ login == '' ) $ login = $ username ;
1406+
1407+ //* Change values in /etc/passwd
1408+ $ passwd_file_array = file ('/etc/passwd ' );
1409+ if (is_array ($ passwd_file_array )) {
1410+ foreach ($ passwd_file_array as $ line ) {
1411+ $ line = trim ($ line );
1412+ $ parts = explode (': ' ,$ line );
1413+ if ($ parts [0 ] == $ username ) {
1414+ if (trim ($ login ) != '' && trim ($ login ) != trim ($ username )) $ parts [0 ] = trim ($ login );
1415+ if (!empty ($ uid )) $ parts [2 ] = trim ($ uid );
1416+ if (!empty ($ gid )) $ parts [3 ] = trim ($ gid );
1417+ if (trim ($ home ) != '' ) $ parts [5 ] = trim ($ home );
1418+ if (trim ($ shell ) != '' ) $ parts [6 ] = trim ($ shell );
1419+ $ new_line = implode (': ' ,$ parts );
1420+ copy ('/etc/passwd ' ,'/etc/passwd~ ' );
1421+ chmod ('/etc/passwd~ ' ,0600 );
1422+ $ app ->uses ('system ' );
1423+ $ app ->system ->replaceLine ('/etc/passwd ' ,$ line ,$ new_line ,1 ,0 );
1424+ }
1425+ }
1426+ unset($ passwd_file_array );
1427+ }
1428+
1429+ //* If username != login, change username in group and gshadow file
1430+ if ($ username != $ login ) {
1431+ $ group_file_array = file ('/etc/group ' );
1432+ if (is_array ($ group_file_array )) {
1433+ foreach ($ group_file_array as $ line ) {
1434+ $ line = trim ($ line );
1435+ $ parts = explode (': ' ,$ line );
1436+ if (strstr ($ parts [3 ],$ username )) {
1437+ $ uparts = explode (', ' ,$ parts [3 ]);
1438+ if (is_array ($ uparts )) {
1439+ foreach ($ uparts as $ key => $ val ) {
1440+ if ($ val == $ username ) $ uparts [$ key ] = $ login ;
1441+ }
1442+ }
1443+ $ parts [3 ] = implode (', ' ,$ uparts );
1444+ $ new_line = implode (': ' ,$ parts );
1445+ copy ('/etc/group ' ,'/etc/group~ ' );
1446+ chmod ('/etc/group~ ' ,0600 );
1447+ $ app ->system ->replaceLine ('/etc/group ' ,$ line ,$ new_line ,1 ,0 );
1448+ }
1449+ }
1450+ }
1451+ unset($ group_file_array );
1452+
1453+ $ gshadow_file_array = file ('/etc/gshadow ' );
1454+ if (is_array ($ gshadow_file_array )) {
1455+ foreach ($ gshadow_file_array as $ line ) {
1456+ $ line = trim ($ line );
1457+ $ parts = explode (': ' ,$ line );
1458+ if (strstr ($ parts [3 ],$ username )) {
1459+ $ uparts = explode (', ' ,$ parts [3 ]);
1460+ if (is_array ($ uparts )) {
1461+ foreach ($ uparts as $ key => $ val ) {
1462+ if ($ val == $ username ) $ uparts [$ key ] = $ login ;
1463+ }
1464+ }
1465+ $ parts [3 ] = implode (', ' ,$ uparts );
1466+ $ new_line = implode (': ' ,$ parts );
1467+ copy ('/etc/gshadow ' ,'/etc/gshadow~ ' );
1468+ chmod ('/etc/gshadow~ ' ,0600 );
1469+ $ app ->system ->replaceLine ('/etc/gshadow ' ,$ line ,$ new_line ,1 ,0 );
1470+ }
1471+ }
1472+ }
1473+ unset($ group_file_array );
1474+ }
1475+
1476+
1477+ //* When password or login name has been changed
1478+ if ($ password != '' || $ username != $ login ) {
1479+ $ shadow_file_array = file ('/etc/shadow ' );
1480+ if (is_array ($ shadow_file_array )) {
1481+ foreach ($ shadow_file_array as $ line ) {
1482+ $ line = trim ($ line );
1483+ $ parts = explode (': ' ,$ line );
1484+ if ($ parts [0 ] == $ username ) {
1485+ if (trim ($ login ) != '' && trim ($ login ) != trim ($ username )) $ parts [0 ] = trim ($ login );
1486+ if (trim ($ password ) != '' ) $ parts [1 ] = trim ($ password );
1487+ $ new_line = implode (': ' ,$ parts );
1488+ copy ('/etc/shadow ' ,'/etc/shadow~ ' );
1489+ chmod ('/etc/shadow~ ' ,0600 );
1490+ $ app ->system ->replaceLine ('/etc/shadow ' ,$ line ,$ new_line ,1 ,0 );
1491+ }
1492+ }
1493+ }
1494+ unset($ shadow_file_array );
1495+ }
1496+ }
13991497
14001498}
14011499?>
0 commit comments