-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathusuarios.php
255 lines (227 loc) · 11.2 KB
/
usuarios.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
//error_reporting(-1);
//ini_set('display_errors', 'On');
session_start();
require_once 'classes/class.ldap.php';
$Ldap = new LDAP();
$current_page=basename(__FILE__);
$Ldap->check_login_or_redirect($current_page);
$permissions=$_SESSION["login"]["level"];
if ($Ldap->is_logged_in()&& $permissions != '10'){
$Ldap->redirect('404.php');
}
require_once('header.php');?>
<?php
//Set variables for ldap connection
$ldapconn=$Ldap->connect();
$psw=$Ldap->decrypt_psw();
if ($ldapconn){
$ldapbind=$Ldap->bind($ldapconn,$_SESSION["login"]["dn"],$psw);
}
$message='';
$ldaptree = LDAP_PEOPLE;
$groupinfo = posix_getgrnam("sftpusers");
$grid=$groupinfo["gid"];
//Only show sftpusers
$filtersftp="(&(objectClass=person)(uid=*)(!(gidnumber=27)))";
$filtersudo="(&(objectClass=person)(uid=*)(gidnumber=27))";
//Add new User
if(isset($_POST['adduser'])){
$entry=array();
$newuser=trim($_POST['username']);
$first_name=(isset($_POST['firstname']))?$_POST['firstname']:$newuser;
$second_name=(isset($_POST['surname']))?$_POST['surname']:$newuser;
$user_email=trim($_POST['usermail']);
$password=$_POST['pswd2'];
// SEt these variables to none in case in only vpn account
$entry['loginshell']='none';
$entry['homedirectory']='none';
$c=0;
if (isset($_POST['sshd'])){
$entry['gidnumber']=(int)$grid;
$entry['loginshell']='/bin/bash';
$entry['homedirectory']='/home/sftpusers/' . $newuser;
$entry['authorizedservice'][$c]='sshd';
$c++;
}
if (isset($_POST['vpn'])){
$entry['authorizedservice'][$c]='openvpn';
//$entry['homedirectory']='none';
$c++;
}
$entry['uid']=$newuser;
$entry['cn']=(!empty($first_name))?$first_name:$newuser;
$entry['sn']=(!empty($second_name))?$second_name:$newuser;
$entry['mail']=$user_email;
$entry['userpassword']=ldap_password_hash($password,'ssha');
$add_user=$Ldap->add_user($newuser,$entry);
if (isset($_POST["sendinstruction"]) && $add_user)$Ldap->send_vpn_instructions($user_email,$newuser);
$message=$add_user['message'];
}
//Modifiy Passord
if(isset($_POST['chpsw'])){
$modifydn='uid='. $_POST['userid']. ',' . $ldaptree;
$info['userpassword'][0]=ldap_password_hash($_POST['changepsw'],'md5crypt');
$info['shadowlastchange'][0] = floor(time()/86400);
$chpass=$Ldap->modifyRecord($ldapconn, $modifydn, $info );
$message=$chpass['message'];
}
//delete user
if(isset($_POST['deluser'])){
$deletedn='uid='. $_POST['userid']. ',' . $ldaptree;
$del_user=$Ldap->deleteRecord($ldapconn, $deletedn, $recursive = false);
$message=$del_user['message'];
### @TODO: if deleted user is webadmin for some domain, we need to update
# the adminID for this domain and set to default user
}
if ($ldapbind) {
//Get all sftpusers
$result=$Ldap->search($ldapconn,$ldaptree, $filtersftp);
$resultsudo=$Ldap->search($ldapconn,$ldaptree, $filtersudo);
}
?>
<div id="admin-content" class="content">
<?php echo $message;?>
<?/*php $commuid="getent passwd | awk -F: '{uid[$3]=1}END{for(x=10000; x<=40000; x++) {if(uid[x] != \"\"){}else{print x; exit;}}}'";
$firstuid_availabe=system($commuid);*/?>
<h1 class="navbar-nav"> <?php printf(_("Usuarios"));?></h1>
<span><button class="togglevisibility btn btn-small btn-secondary">Añadir usuario</button> </span>
<div class="clear"></div>
<div id="change">
<form autocomplete="off" action="" method="POST" class="form-signin jquery-check" id="adduser-form">
<hr>
<h2>Crea un nuevo usuario:</h2>
<label for="username"><h4><?php printf(_("Nombre de usuario"));?> *</h4></label>
<input id="username" type="text" name="username" required /><div id="result"></div>
<label for="firstname"><?php printf(_("Nombre"));?></label>
<input id="firstname" type="text" name="firstname" />
<label for="surname"><?php printf(_("Apeliidos"));?></label>
<input id="surname" type="text" name="surname" />
<label for="usermail"><?php printf(_("Correo electrónico"));?> *</label>
<p class="little"><?php printf(_("Puedes insertar un correo electrónico externo o elegir una entre las cuentas creadas en el servidor"));?></p>
<input id="usermail" class="usermail" type="mail" name="usermail" required />
<?php $resultmail = $Ldap->search($ldapconn,LDAP_BASE,'(&(objectClass=VirtualMailAccount)(!(cn=postmaster))(!(mail=abuse@*)))');
$mailcount = $resultmail["count"];
if($mailcount>0) {
echo '<select id="selmail">';
echo '<option value="">Seleccionar cuenta existente</option>';
for ($c=0; $c<$resultmail["count"]; $c++) {
echo '<option value="' . $resultmail[$c]["mail"][0] .'">' . $resultmail[$c]["mail"][0] . '</option>';
}
echo '</select>';
};?>
<div id="emailresult"></div>
<hr>
<h4><?php printf(_("Acceso Sftp"));?></h4>
<input type="checkbox" name="sshd" id="sshd" />
<label for="sshd"> </label>
<hr>
<?php if ($Ldap->check_installed_service('openvpn')){?>
<h4><?php printf(_("Cuenta VPN"));?></h4>
<input type="checkbox" name="vpn" id="vpn" />
<label for="vpn" class="togglehidden" > </label></h4>
<div id="hidden">
<h4><?php printf(_("Instrucciones"));?></h4>
<p><?php printf(_("Puedes enviar al usuario un email con instrucciones para configurar el cliente VPN"));?></p>
<p><?php printf(_("NOTA: Las instrucciones incluyen todos los datos necesarios menos la contraseña. Por razones de seguridad proporciona al usuario la contraseña por otro canal"));?></p>
<input type="checkbox" name="sendinstruction" id="sendinstruction" />
<label for="sendinstruction" class="left small"> </label> <span><?php printf(_("Enviar instrucciones"));?></span></h4>
</div>
<?php } ?>
<div class="clear"></div>
<hr>
<label for="pswd1"><?php printf(_("Contraseña"));?> *</label>
<div id="pswcheck"></div>
<input id="pswd1" type="password" name="pswd1" required />
<label for="pswd2"><?php printf(_("Confirma contraseña"));?> *</label><input id="pswd2" type="password" name="pswd2" required />
<div id="pswresult"></div>
<hr>
<input type="submit" name="adduser" value="Guardar" class="btn btn-small btn-primary" />
</form>
</div><!--change-->
<table id="users">
<thead>
<tr>
<th><?php printf (_('Usuario'));?></th>
<th><?php printf (_('Acceso sftp'));?></th>
<th><?php printf (_('Acceso VPN'));?></th>
<th><?php printf (_('Editar'));?></th>
<th><?php printf (_('Borrar'))?></th>
</tr>
</thead>
<tbody>
<?php
#list sudo user without pssword change option
for ($i=0; $i<$resultsudo["count"]; $i++) {
$username = $resultsudo[$i]["uid"][0];
$services=$resultsudo[$i]["authorizedservice"];
$issftp=(in_array('sshd',$services)&& (!empty ($services)))?'<i class="fa fa-check-circle-o icon checkok"></i>':'<i class="fa fa-exclamation-triangle icon checkko"></i>';
$isvpn=(in_array('openvpn',$services)&& (!empty ($services)))?'<i class="fa fa-check-circle-o icon checkok"></i>':'<i class="fa fa-exclamation-triangle icon checkko"></i>';
echo "<tr>";
echo "<td>";
echo $username;
echo "</td>";
echo "<td class='center'>";
echo $issftp;
echo "</td>";
echo "<td class='center'>";
echo $isvpn;
echo "</td>";
echo "<td>";
echo "<a href='edit-supuser.php?user=". $username ."'><button class='btn btn-small'><i class='fa fa-cogs' aria-hidden='true'></i> Editar</button></a>";
echo "</td>";
echo "<td>";
echo "No disponible";
echo "</td>";
echo "</tr>";
}
#List sftpusers with edit options
for ($i=0; $i<$result["count"]; $i++) {
$oldpsw=$result[$i]['userpassword'][0];
$username = $result[$i]["uid"][0];
$services=(isset($result[$i]["authorizedservice"]))?$result[$i]["authorizedservice"]:array();
$issftp=(in_array('sshd',$services) && (is_array($services)))?'<i class="fa fa-check-circle-o icon checkok"></i>':'<i class="fa fa-exclamation-triangle icon checkko"></i>';
$isvpn=(in_array('openvpn',$services)&& (is_array($services)))?'<i class="fa fa-check-circle-o icon checkok"></i>':'<i class="fa fa-exclamation-triangle icon checkko"></i>';
echo "<tr>";
echo "<td>";
echo $username;
echo "</td>";
echo "<td class='center'>";
echo $issftp;
echo "</td>";
echo "<td class='center'>";
echo $isvpn;
echo "</td>";
echo "<td>";
echo "<a href='edit-user.php?user=". $username ."'><button class='btn btn-small'><i class='fa fa-cogs' aria-hidden='true'></i> ". sprintf(_('Editar')) ."</button></a>";
echo "</td>";
echo "<td>";
$deletestring=sprintf(_('¿Quieres borrar la cuenta para el usuario %s? Esto eliminará su acceso al servidor'),$username);
// echo "<form action='' method='POST' class='form-table'><input type='hidden' name='userid' value='". $username ."' /> <input type='submit' name='deluser' value='". sprintf(_('Borrar')) ."' class='btn btn-small btn-primary' onclick=\"return confirm('" . $deletestring ."');\" /></form>";
echo '<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#userModal" data-user="' . $username . '">' . sprintf (_('Eliminar')) . '</button>';
echo "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
<div class="bd-example">
<div class="modal fade" id="userModal" tabindex="-1" role="dialog" aria-labelledby="userModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="userModalLabel"></h4>
</div>
<div class="modal-body" id="modal-body">
</div>
</div><!--modal-content-->
</div><!--modal-dialog-->
</div><!--exampleModal-->
</div><!--bd-example-->
</div><!--admin-content-->
<?php ldap_close($ldapconn);
require_once('footer.php');?>
</script>