3 # $Id: update.cgi,v 1.13 2006/12/28 02:44:02 rmurray Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5 # (c) 2006 Ryan Murray. Licensed under the GPL. <rmurray@debian.org>
12 use Digest::HMAC_SHA1 qw(hmac_sha1_hex);
13 use Digest::MD5 qw(md5_hex);
18 use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR);
19 use Net::LDAP::Util qw(ldap_explode_dn);
21 my %config = &Util::ReadConfigFile;
24 my $proto = ($ENV{HTTPS} ? "https" : "http");
26 my $id = $query->param('id');
27 my $authtoken = $query->param('authtoken');
28 my $password = &Util::CheckAuthToken($authtoken);
29 my $editdn = $query->param('editdn');
31 if ($proto eq "http" || !($id && $password)) {
32 print "Location: https://$ENV{SERVER_NAME}/$config{webloginhtml}\n\n";
38 open (F, "<$config{passdir}/key-hmac-$UID") || &Util::HTMLError($!);
44 $ldap->unbind if (defined($ldap));
47 #$SIG{__DIE__} = \&DieHandler;
49 $ldap = Net::LDAP->new($config{ldaphost});
50 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
53 $mesg = $ldap->bind($editdn, password => $password);
55 $auth = ($mesg->code == LDAP_SUCCESS);
59 &Util::HTMLError("You have not been authenticated. Please <a href=\"https://$ENV{SERVER_NAME}/$config{webloginhtml}\">Login</a>");
64 $mesg = $ldap->search(base => $editdn,
66 $mesg->code && &Util::HTMLError($mesg->error);
68 my $entries = $mesg->as_struct;
69 if ($mesg->count != 1) {
73 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
74 $mesg->code && &Util::HTMLError($mesg->error);
75 my $host_entries = $mesg->as_struct;
77 my @dns = keys(%$entries);
78 my $entry = $entries->{$dns[0]};
80 if (!($query->param('doupdate'))) {
81 # Not yet update, just fill in the form with the current values
85 # First do the easy stuff - this catches most of the cases
86 foreach (keys(%$entry)) {
87 $data{$_} = $entry->{$_}->[0];
88 $data{$_} = CGI::escapeHTML($data{$_}) if defined $data{$_};
91 $data{gender} = 9 if not $data{gender};
93 # Now we have to fill in the rest that needs some processing...
95 $data{authtoken} = $authtoken;
96 $data{editdn} = $editdn;
97 $data{staddress} = $entry->{postaladdress}->[0];
98 $data{staddress} =~ s/\$/\n/;
99 $data{staddress} = CGI::escapeHTML($data{staddress});
100 $data{countryname} = &Util::LookupCountry($data{c});
102 if ($data{mailgreylisting} eq "TRUE") {
103 $data{mailgreylisting} = " checked";
105 $data{mailgreylisting} = "";
108 if ($data{mailcallout} eq "TRUE") {
109 $data{mailcallout} = " checked";
111 $data{mailcallout} = "";
114 $data{maildefaultoptions} ||= 'TRUE';
116 if ($data{maildefaultoptions} eq "TRUE") {
117 $data{maildefaultoptions} = " checked";
119 $data{maildefaultoptions} = "";
122 $data{mailcontentinspectionaction} ||= 'reject';
124 $data{email} = CGI::escapeHTML(join(", ", @{$entry->{emailforward}}));
126 my $genderselect = '<select name="gender">'
127 . '<option value="9"'
128 . ($data{gender} == 9 ? ' selected' : '')
130 . '<option value="1"'
131 . ($data{gender} == 1 ? ' selected' : '')
132 . '>male<option value="2"'
133 . ($data{gender} == 2 ? ' selected' : '')
134 . '>female</select>';
135 my $mailcontentselect = '<select name="mailcontentinspection">'
136 . '<option value="blackhole"'
137 . ($data{mailcontentinspectionaction} eq 'blackhole' ? ' selected' : '')
139 . '<option value="markup"'
140 . ($data{mailcontentinspectionaction} eq 'markup' ? ' selected' : '')
142 . '<option value="reject"'
143 . ($data{mailcontentinspectionaction} eq 'reject' ? ' selected' : '')
146 my $confirmstring = '';
147 my $sudopassword = '';
148 for my $e(@{$entry->{'sudopassword'}}) {
149 my ($uuid, $status, $hosts, $crypted) = ($e =~ /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}) (confirmed:[0-9a-f]{40}|unconfirmed) ([a-z0-9.,*]+) ([^ ]+)$/);
150 unless (defined $uuid) {
151 $sudopassword .= "<tr><td>Unparseable line!</td></tr>\n";
154 if ($status =~ /^confirmed:/) {
155 my $data = join(':', 'password-is-confirmed', 'sudo', $data{'uid'}, $uuid, $hosts, $crypted);
156 my $hmac = hmac_sha1_hex( $data, $hmac_key);
157 if ($status eq "confirmed:$hmac") {
158 $status = 'confirmed';
163 my $e = "<tr><td>".CGI::escapeHTML($hosts)."</td>
164 <td>".CGI::escapeHTML($status)."</td>
165 <td><small>not shown</small></td>
166 <!--<td><small><code>".CGI::escapeHTML($uuid)."</code></small></td>-->
167 <td><input name=\"sudopassword-delete-".CGI::escapeHTML($uuid)."\" type=\"checkbox\" value=\"delete\"> (delete)</td></tr>\n";
169 if ($status eq 'unconfirmed') {
170 my $data = join(':', 'confirm-new-password', 'sudo', $data{'uid'}, $uuid, $hosts, $crypted);
171 my $hmac = hmac_sha1_hex( $data, $hmac_key);
172 $confirmstring .= CGI::escapeHTML("confirm sudopassword $uuid $hosts $hmac\n");
175 if ($confirmstring ne '') {
176 $confirmstring = "<br>To confirm your new sudo passwords send signed mail to changes\@db.debian.org with a signed body containing these lines:<br><pre>$confirmstring</pre>";
179 my $sudopasswordhosts = '<select name="newsudopass-host"> <option value="*">ALL';
180 for my $dn (sort {$host_entries->{$a}->{host}->[0] cmp $host_entries->{$b}->{host}->[0]} keys(%$host_entries)) {
181 my $data = $host_entries->{$dn};
182 my $host = $data->{'host'}->[0];
183 my $hostname = $data->{'hostname'}->[0];
184 $sudopasswordhosts .= "<option value=\"$hostname\">$host\n";
186 $sudopasswordhosts .= '</select>';
188 # finally we can send output...
190 &Util::HTMLSendHeader;
191 open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
193 s/~(.+?)~/$data{$1}/g;
194 s/<\?genderselect>/$genderselect/;
195 s/<\?mailcontentselect>/$mailcontentselect/;
196 s/<\?sudopassword>/$sudopassword/;
197 s/<\?sudopasswordhosts>/$sudopasswordhosts/;
198 s/<\?confirmstring>/$confirmstring/;
203 my @ldapinfo_for_pwcheck;
204 for my $a (qw{cn sn md gecos uid}) {
205 for my $e (@{$entry->{$a}}) {
206 push @ldapinfo_for_pwcheck, $e;
211 # Actually update stuff...
212 my ($newpassword, $newstaddress, $newwebpassword, $newrtcpassword);
214 # Good god, why would we want to do that here? it breaks password setting
215 # etc, and it doesn't prevent people from setting eveil stuff in ldap
217 # &Util::FixParams($query);
219 if (($query->param('labeleduri')) &&
220 ($query->param('labeleduri') !~ /^https?:\/\//i)) {
221 &Util::HTMLError("Your homepage URL is invalid");
224 if ($query->param('newpass') && $query->param('newpassvrfy')) {
225 if ($query->param('newpass') ne $query->param('newpassvrfy')) {
226 # passwords don't match...
227 &Util::HTMLError("The passwords you specified do not match. Please go back and try again.");
230 my ($r, $msg) = &Util::checkPasswordQuality($query->param('newpass'), undef, [@ldapinfo_for_pwcheck]);
232 &Util::HTMLError("Password check failed: $msg. Please go back and try again.");
235 # create a md5 crypted password
236 $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
238 &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
239 &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
242 if ($query->param('newwebpass') && $query->param('newwebpassvrfy')) {
243 if ($query->param('newwebpass') ne $query->param('newwebpassvrfy')) {
244 # passwords don't match...
245 &Util::HTMLError("The web-passwords you specified do not match. Please go back and try again.");
248 my ($r, $msg) = &Util::checkPasswordQuality($query->param('newwebpass'), undef, [@ldapinfo_for_pwcheck]);
250 &Util::HTMLError("Password check failed for web-password: $msg. Please go back and try again.");
253 # create a md5 crypted password
254 $newwebpassword = apache_md5_crypt($query->param('newwebpass'), &Util::CreateCryptSalt(1, 1));
256 &Util::LDAPUpdate($ldap, $editdn, 'webPassword', $newwebpassword);
258 if ($query->param('newrtcpass') && $query->param('newrtcpassvrfy')) {
259 if ($query->param('newrtcpass') ne $query->param('newrtcpassvrfy')) {
260 # passwords don't match...
261 &Util::HTMLError("The rtc-passwords you specified do not match. Please go back and try again.");
264 my ($r, $msg) = &Util::checkPasswordQuality($query->param('newrtcpass'), undef, [@ldapinfo_for_pwcheck]);
266 &Util::HTMLError("Password check failed for rtc-password: $msg. Please go back and try again.");
268 # create a md5 crypted password
269 $newrtcpassword = &md5_hex( ldap_explode_dn($editdn)->[0]{UID} . '@debian.org:rtc.debian.org:' . $query->param('newrtcpass') );
271 &Util::LDAPUpdate($ldap, $editdn, 'rtcPassword', $newrtcpassword);
274 $newstaddress = $query->param('staddress');
275 $newstaddress =~ s/\n/\$/m;
277 my $gender = $query->param('gender');
278 if ($gender != 1 && $gender != 2) {
279 $gender = 9; # unspecified
282 my ($bd_ok, $bd_yr, $bd_mo, $bd_day);
284 if ($query->param('birthdate') =~ /^([1-9][0-9]{3})([01][0-9])([0-3][0-9])$/) {
285 $bd_yr = $1; $bd_mo = $2; $bd_day = $3;
286 if ($bd_yr > 1850 and $bd_mo > 0 and $bd_mo <= 12 and $bd_day > 0) {
288 if ($bd_day == 29 and ($bd_yr % 4 == 0 && ($bd_yr % 100 != 0 || $bd_yr % 400 == 0))) {
290 } elsif ($bd_day <= 28) {
293 } elsif ($bd_mo == 4 or $bd_mo == 6 or $bd_mo == 9 or $bd_mo == 11) {
303 } elsif (not defined($query->param('birthdate')) or $query->param('birthdate') =~ /^\s*$/) {
307 ($lat, $long) = &Util::CheckLatLong($query->param('latitude'),
308 $query->param('longitude'));
309 my ($greylisting, $callout, $mailcontentinspection, $defaultoptions);
311 $greylisting = $query->param('mailgreylisting');
312 if (!$greylisting or $greylisting ne "TRUE") {
313 $greylisting = "FALSE";
316 $callout = $query->param('mailcallout');
317 if (!$callout or $callout ne "TRUE") {
321 $mailcontentinspection = $query->param('mailcontentinspection');
322 if (!$mailcontentinspection or ($mailcontentinspection ne "blackhole" and $mailcontentinspection ne "markup")) {
323 $mailcontentinspection = "reject";
326 $defaultoptions = $query->param('maildefaultoptions');
327 if (!$defaultoptions or $defaultoptions ne "TRUE") {
328 $defaultoptions = "FALSE";
333 if ($query->param('newsudopass') && $query->param('newsudopassvrfy')) {
334 my $host = $query->param('newsudopass-host');
335 if ($host =~ /[^a-z0-9.-]/ and $host ne '*') {
336 &Util::HTMLError("The sudo host has weird characters '$host'.");
339 if ($query->param('newsudopass') ne $query->param('newsudopassvrfy')) {
340 &Util::HTMLError("The sudo passwords you specified do not match. Please go back and try again.");
343 my $ldappass = $password;
344 $ldappass = $query->param('newpass') if $query->param('newpass');
345 push @ldapinfo_for_pwcheck, $host, split(/\./, $host);
346 my ($r, $msg) = &Util::checkPasswordQuality($query->param('newsudopass'), $ldappass, [@ldapinfo_for_pwcheck]);
348 &Util::HTMLError("Password check failed for new sudo pass: $msg. Please go back and try again.");
351 # create a md5 crypted password
352 my $newsudopassword = crypt($query->param('newsudopass'), &Util::CreateCryptSalt(1));
353 my $ug = new Data::UUID;
354 my $uuid = $ug->create_str();
356 $newsudo = "$uuid unconfirmed $host $newsudopassword";
357 $newsudo_hosts = $host;
360 my %delete_uuids = map { s/^sudopassword-delete-//; $_ => 1} grep { $query->param($_) eq 'delete' } grep { /^sudopassword-delete-/ } $query->param;
362 for my $entry (@{$entry->{'sudopassword'}}) {
363 my ($uuid, $status, $hosts, $crypted) = ($entry =~ /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}) (confirmed:[0-9a-f]{40}|unconfirmed) ([a-z0-9.,*]+) ([^ ]+)$/);
364 next unless defined ($uuid);
365 next if (defined $delete_uuids{$uuid});
366 my %hosts = map { $_ => 1 } split(/,/, $hosts);
367 next if ($hosts{$newsudo_hosts});
368 push @keepsudo, $entry;
370 if ($newsudo ne '') {
371 push @keepsudo, $newsudo;
374 &Util::LDAPUpdate($ldap, $editdn, 'postalAddress', $newstaddress);
375 &Util::LDAPUpdate($ldap, $editdn, 'l', $query->param('l'));
376 &Util::LDAPUpdate($ldap, $editdn, 'latitude', $lat);
377 &Util::LDAPUpdate($ldap, $editdn, 'longitude', $long);
378 &Util::LDAPUpdate($ldap, $editdn, 'c', $query->param('country'));
379 &Util::LDAPUpdate($ldap, $editdn, 'postalCode', $query->param('postalcode'));
380 &Util::LDAPUpdate($ldap, $editdn, 'telephoneNumber', $query->param('telephonenumber'));
381 &Util::LDAPUpdate($ldap, $editdn, 'facsimileTelephoneNumber', $query->param('facsimiletelephonenumber'));
382 &Util::LDAPUpdate($ldap, $editdn, 'bATVToken', $query->param('bATVToken'));
383 &Util::LDAPUpdate($ldap, $editdn, 'VoIP', $query->param('VoIP'));
384 &Util::LDAPUpdate($ldap, $editdn, 'loginShell', $query->param('loginshell'));
385 &Util::LDAPUpdate($ldap, $editdn, 'emailForward', $query->param('email'));
386 &Util::LDAPUpdate($ldap, $editdn, 'privateSub', $query->param('privatesub'));
387 &Util::LDAPUpdate($ldap, $editdn, 'ircNick', $query->param('ircnick'));
388 &Util::LDAPUpdate($ldap, $editdn, 'icqUin', $query->param('icquin'));
389 &Util::LDAPUpdate($ldap, $editdn, 'jabberJID', $query->param('jabberjid'));
390 &Util::LDAPUpdate($ldap, $editdn, 'labeledURI', $query->param('labeleduri'));
391 &Util::LDAPUpdate($ldap, $editdn, 'onVacation', $query->param('onvacation'));
392 &Util::LDAPUpdate($ldap, $editdn, 'gender', $gender);
393 &Util::LDAPUpdate($ldap, $editdn, 'birthDate', $query->param('birthdate')) if $bd_ok;
394 &Util::LDAPUpdate($ldap, $editdn, 'mailDisableMessage', $query->param('maildisablemessage'));
395 &Util::LDAPUpdate($ldap, $editdn, 'mailCallout', $callout);
396 &Util::LDAPUpdate($ldap, $editdn, 'mailContentInspectionAction', $mailcontentinspection);
397 &Util::LDAPUpdate($ldap, $editdn, 'mailGreylisting', $greylisting);
398 &Util::LDAPUpdate($ldap, $editdn, 'mailDefaultOptions', $defaultoptions);
399 &Util::LDAPUpdate($ldap, $editdn, 'sudoPassword', \@keepsudo);
401 # when we are done, reload the page with the updated details.
402 my $url = "https://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id&authtoken=$authtoken&editdn=";
403 $url .= uri_escape($editdn, "\x00-\x40\x7f-\xff");
404 print "Location: $url\n\n";