forked from smillie/gas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
executable file
·227 lines (187 loc) · 7.64 KB
/
functions.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
<?php
function computeExpiry($date) {
$nextExpiry = strtotime('first Friday of October', $date);
$threshold = strtotime('last Friday of May', $date);
if ($date < $threshold)
{
$expiry = $nextExpiry;
}
else
{
$expiry = strtotime('first Friday of October', $date + 365 * 24 * 60 * 60);
}
$expiry = $expiry / (24 * 60 * 60);
return $expiry;
}
function generatePassword ($length = 8) {
$password = "";
$possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";
$maxlength = strlen($possible);
if ($length > $maxlength) {
$length = $maxlength;
}
$i = 0;
while ($i < $length) {
$char = substr($possible, mt_rand(0, $maxlength-1), 1);
if (!strstr($password, $char)) {
$password .= $char;
$i++;
}
}
return $password;
}
function isUserInGroup($con, $user, $group) {
$group_search = ldap_search($con, "cn=$group,ou=groups,dc=geeksoc,dc=org", "(memberUid=$user)");
if (ldap_count_entries($con, $group_search) >= 1) {
return true;
} else {
return false;
}
}
function getAllGroups($con) {
$group_search = ldap_search($con, "ou=groups,dc=geeksoc,dc=org", "(objectClass=posixGroup)");
ldap_sort($con, $group_search, 'cn');
$results = ldap_get_entries($con, $group_search);
return array_slice($results, 1);
}
function getGroupsForUser($con, $user) {
$groups = array();
foreach (getAllGroups($con) as $groupEntry) {
if (isUserInGroup($con, $user, $groupEntry['cn'][0])) {
$groups[] = $groupEntry['cn'][0];
}
}
return $groups;
}
function getStatus($expiry, $paid) {
$day = intval(time()/(60*60*24));
$i = (int)$expiry;
if (!isset($expiry)) $i=99999;
if ($i == 1) {
$status = "Administratively Disabled";
$stat_icon = "icon-ban-circle";
} elseif ($i <= $day) {
$status = "Expired";
$stat_icon = "icon-time";
} elseif ($i <= ($day+60) && $paid == "FALSE") {
$status = "Expiring (Not Paid)";
$stat_icon = "icon-time";
} elseif ($i <= ($day+60)) {
$status = "Expiring";
$stat_icon = "icon-time";
} elseif ($paid == "FALSE") {
$status = "Not Paid";
$stat_icon = "icon-exclamation-sign";
} else {
$status = "Active";
$stat_icon = "icon-ok";
}
return "<i class='$stat_icon'></i> $status";
}
function ircNotify($message) {
global $conf;
if ($conf['ircNotifications']) {
$ircmessage = $conf['ircChannel']." [GAS] $message";
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock, $conf['ircServer'], $conf['ircBotPort']);
socket_write($sock, $ircmessage, strlen($ircmessage));
socket_close($sock);
}
}
function mailNotify($to, $subject, $message) {
global $conf;
$from = $conf['mailFrom'];
if ($conf['mailNotifications']) {
mail($to, $subject, $message, "From: $from");
}
}
function addUser($uid, $first, $last, $stuno, $email) {
global $con;
global $dn;
global $user;
// echo "$uid, $first, $last, $stuno, $email";
if (ldap_count_entries($con, ldap_search($con, $dn, "(uid=$uid)")) >= 1) {
$return = "Username '$uid' already exists.";
} else {
//compute uid
$users = ldap_get_entries($con, ldap_search($con, $dn, "(objectclass=posixaccount)"));
$uidno = 10000;
foreach($users as $u) {
if ($u['uidnumber'][0] > $uidno)
$uidno = $u['uidnumber'][0];
}
$uidno += 1;
//compute expiry date
$expiry = computeExpiry(date());
//generate password
$pass = generatePassword();
mt_srand((double)microtime()*1000000);
$salt = pack("CCCCCCCC", mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand());
$hashedpass = "{SSHA}" . base64_encode( sha1( $pass . $salt, true) . $salt );
//assemble object
//$newuser['dn'] = "uid=$uid,$dn";
$newuser['objectclass'][0] = "inetOrgPerson";
$newuser['objectclass'][1] = "organizationalPerson";
$newuser['objectclass'][2] = "person";
$newuser['objectclass'][3] = "top";
$newuser['objectclass'][4] = "posixAccount";
$newuser['objectclass'][5] = "shadowAccount";
$newuser['objectclass'][6] = "gsAccount";
$newuser['cn'] = "$first $last";
$newuser['sn'] = $last;
$newuser['givenName'] = $first;
$newuser['title'] = "Member";
$newuser['uid'] = $uid;
$newuser['uidnumber'] = (int) $uidno;
$newuser['gidNumber'] = (int) 500;
$newuser['homeDirectory'] = "/home/$uid";
$newuser['loginShell'] = "/bin/bash";
$newuser['gecos'] = "$first $last,,,";
$newuser['shadowLastChange'] = (int) 10877;
$newuser['shadowMax'] = (int) 99999;
$newuser['shadowWarning'] = (int) 7;
$newuser['mail'] = $email;
$newuser['studentNumber'] = (int) $stuno;
$newuser['hasPaid'] = "TRUE";
$newuser['hasSignedTOS'] = "TRUE";
$newuser['shadowExpire'] = (int) $expiry;
$newuser['userpassword'] = $hashedpass;
//add to directory
ldap_add($con,"uid=$uid,$dn", $newuser);
if (ldap_error($con) != "Success") {
$return = ldap_error($con);
}
//adduser to members group
$newmember['memberUid'] = $uid;
ldap_mod_add($con, "cn=members,ou=groups,dc=geeksoc,dc=org", $newmember);
if (ldap_error($con) != "Success") {
$return = ldap_error($con);
}
//email user confirmation
$userEmail = <<<EOT
Welcome to GeekSoc $first!
You may find your new account details below, but please change your password at http://accounts.geeksoc.org/ as soon as possible.
Username: $uid
Password: $pass
You may login to the shell server via SSH at shell.geeksoc.org on port 22. IRC may be found at irc.geeksoc.org on port 6667 - #geeksoc is the official channel.
On Windows the program PuTTY may be used to login to the SSH server, while Mac/Linux users will already have SSH installed and may connect using the 'ssh' command from a terminal.
The recommended way of accessing IRC is setting up a persistent connection on Shell using screen and irssi, see http://quadpoint.org/articles/irssi for details on how to set this up.
Have fun, but please be responsible and abide with the terms of service.
GeekSoc
http://www.geeksoc.org/
EOT;
mailNotify($email, "[GeekSoc] Your account has been created", $userEmail);
//email creation notice to gsag
$adminEmail = <<<EOT
An account has been created by $user for $first $last:
Username: $uid
Email: $email
EOT;
mailNotify("gsag@geeksoc.org", "[GeekSoc] New account created", $adminEmail);
//irc creation notice (#gsag)
ircNotify("Account created for $first $last: Username: $uid, Email: $email (by $user)");
$return = "S Created user '$uid' with password '$pass'.";
}
return $return;
}
?>