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);
16 use Net::LDAP qw(:all);
18 my %config = &Util::ReadConfigFile;
21 my $proto = ($ENV{HTTPS} ? "https" : "http");
23 my $id = $query->param('id');
24 my $authtoken = $query->param('authtoken');
25 my $password = &Util::CheckAuthToken($authtoken);
26 my $editdn = $query->param('editdn');
28 if ($proto eq "http" || !($id && $password)) {
29 print "Location: https://$ENV{SERVER_NAME}/$config{webloginhtml}\n\n";
35 open (F, "<$config{passdir}/key-hmac-$UID") || &Util::HTMLError($!);
41 $ldap->unbind if (defined($ldap));
44 $SIG{__DIE__} = \&DieHandler;
46 $ldap = Net::LDAP->new($config{ldaphost});
47 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
50 $mesg = $ldap->bind($editdn, password => $password);
52 $auth = ($mesg->code == LDAP_SUCCESS);
56 &Util::HTMLError("You have not been authenticated. Please <a href=\"https://$ENV{SERVER_NAME}/$config{webloginhtml}\">Login</a>");
61 $mesg = $ldap->search(base => $editdn,
63 $mesg->code && &Util::HTMLError($mesg->error);
65 my $entries = $mesg->as_struct;
66 if ($mesg->count != 1) {
70 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
71 $mesg->code && &Util::HTMLError($mesg->error);
72 my $host_entries = $mesg->as_struct;
74 my @dns = keys(%$entries);
75 my $entry = $entries->{$dns[0]};
77 if (!($query->param('doupdate'))) {
78 # Not yet update, just fill in the form with the current values
82 # First do the easy stuff - this catches most of the cases
83 foreach (keys(%$entry)) {
84 $data{$_} = $entry->{$_}->[0];
85 $data{$_} = CGI::escapeHTML($data{$_}) if defined $data{$_};
88 $data{gender} = 9 if not $data{gender};
90 # Now we have to fill in the rest that needs some processing...
92 $data{authtoken} = $authtoken;
93 $data{editdn} = $editdn;
94 $data{staddress} = $entry->{postaladdress}->[0];
95 $data{staddress} =~ s/\$/\n/;
96 $data{staddress} = CGI::escapeHTML($data{staddress});
97 $data{countryname} = &Util::LookupCountry($data{c});
99 if ($data{mailgreylisting} eq "TRUE") {
100 $data{mailgreylisting} = " checked";
102 $data{mailgreylisting} = "";
105 if ($data{mailcallout} eq "TRUE") {
106 $data{mailcallout} = " checked";
108 $data{mailcallout} = "";
111 if ($data{maildefaultoptions} eq "TRUE") {
112 $data{maildefaultoptions} = " checked";
114 $data{maildefaultoptions} = "";
117 $data{mailcontentinspectionaction} ||= 'reject';
119 $data{email} = CGI::escapeHTML(join(", ", @{$entry->{emailforward}}));
121 my $genderselect = '<select name="gender">'
122 . '<option value="9"'
123 . ($data{gender} == 9 ? ' selected' : '')
125 . '<option value="1"'
126 . ($data{gender} == 1 ? ' selected' : '')
127 . '>male<option value="2"'
128 . ($data{gender} == 2 ? ' selected' : '')
129 . '>female</select>';
130 my $mailcontentselect = '<select name="mailcontentinspection">'
131 . '<option value="blackhole"'
132 . ($data{mailcontentinspectionaction} eq 'blackhole' ? ' selected' : '')
134 . '<option value="markup"'
135 . ($data{mailcontentinspectionaction} eq 'markup' ? ' selected' : '')
137 . '<option value="reject"'
138 . ($data{mailcontentinspectionaction} eq 'reject' ? ' selected' : '')
141 my $confirmstring = '';
142 my $sudopassword = '';
143 for my $e(@{$entry->{'sudopassword'}}) {
144 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.,*]+) ([^ ]+)$/);
145 unless (defined $uuid) {
146 $sudopassword .= "<tr><td>Unparseable line!</td></tr>\n";
149 if ($status =~ /^confirmed:/) {
150 my $data = join(':', 'password-is-confirmed', 'sudo', $data{'uid'}, $uuid, $hosts, $crypted);
151 my $hmac = hmac_sha1_hex( $data, $hmac_key);
152 if ($status eq "confirmed:$hmac") {
153 $status = 'confirmed';
158 my $e = "<tr><td>".CGI::escapeHTML($hosts)."</td>
159 <td>".CGI::escapeHTML($status)."</td>
160 <td><small>not shown</small></td>
161 <!--<td><small><code>".CGI::escapeHTML($uuid)."</code></small></td>-->
162 <td><input name=\"sudopassword-delete-".CGI::escapeHTML($uuid)."\" type=\"checkbox\" value=\"delete\"> (delete)</td></tr>\n";
164 if ($status eq 'unconfirmed') {
165 my $data = join(':', 'confirm-new-password', 'sudo', $data{'uid'}, $uuid, $hosts, $crypted);
166 my $hmac = hmac_sha1_hex( $data, $hmac_key);
167 $confirmstring .= CGI::escapeHTML("confirm sudopassword $uuid $hosts $hmac\n");
170 if ($confirmstring ne '') {
171 $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>";
174 my $sudopasswordhosts = '<select name="newsudopass-host"> <option value="*">ALL';
175 for my $dn (sort {$host_entries->{$a}->{host}->[0] cmp $host_entries->{$b}->{host}->[0]} keys(%$host_entries)) {
176 my $data = $host_entries->{$dn};
177 my $host = $data->{'host'}->[0];
178 my $hostname = $data->{'hostname'}->[0];
179 $sudopasswordhosts .= "<option value=\"$hostname\">$host\n";
181 $sudopasswordhosts .= '</select>';
183 # finally we can send output...
185 &Util::HTMLSendHeader;
186 open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
188 s/~(.+?)~/$data{$1}/g;
189 s/<\?genderselect>/$genderselect/;
190 s/<\?mailcontentselect>/$mailcontentselect/;
191 s/<\?sudopassword>/$sudopassword/;
192 s/<\?sudopasswordhosts>/$sudopasswordhosts/;
193 s/<\?confirmstring>/$confirmstring/;
198 my @ldapinfo_for_pwcheck;
199 for my $a (qw{cn sn md gecos uid}) {
200 for my $e (@{$entry->{$a}}) {
201 push @ldapinfo_for_pwcheck, $e;
206 # Actually update stuff...
207 my ($newpassword, $newstaddress);
209 # Good god, why would we want to do that here? it breaks password setting
210 # etc, and it doesn't prevent people from setting eveil stuff in ldap
212 # &Util::FixParams($query);
214 if (($query->param('labeleduri')) &&
215 ($query->param('labeleduri') !~ /^https?:\/\//i)) {
216 &Util::HTMLError("Your homepage URL is invalid");
219 if ($query->param('newpass') && $query->param('newpassvrfy')) {
220 if ($query->param('newpass') ne $query->param('newpassvrfy')) {
221 # passwords don't match...
222 &Util::HTMLError("The passwords you specified do not match. Please go back and try again.");
225 my ($r, $msg) = &Util::checkPasswordQuality($query->param('newpass'), undef, [@ldapinfo_for_pwcheck]);
227 &Util::HTMLError("Password check failed: $msg. Please go back and try again.");
230 # create a md5 crypted password
231 $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
233 &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
234 &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
237 $newstaddress = $query->param('staddress');
238 $newstaddress =~ s/\n/\$/m;
240 my $gender = $query->param('gender');
241 if ($gender != 1 && $gender != 2) {
242 $gender = 9; # unspecified
245 my ($bd_ok, $bd_yr, $bd_mo, $bd_day);
247 if ($query->param('birthdate') =~ /^([1-9][0-9]{3})([01][0-9])([0-3][0-9])$/) {
248 $bd_yr = $1; $bd_mo = $2; $bd_day = $3;
249 if ($bd_yr > 1850 and $bd_mo > 0 and $bd_mo <= 12 and $bd_day > 0) {
251 if ($bd_day == 29 and ($bd_yr % 4 == 0 && ($bd_yr % 100 != 0 || $bd_yr % 400 == 0))) {
253 } elsif ($bd_day <= 28) {
256 } elsif ($bd_mo == 4 or $bd_mo == 6 or $bd_mo == 9 or $bd_mo == 11) {
266 } elsif (not defined($query->param('birthdate')) or $query->param('birthdate') =~ /^\s*$/) {
270 ($lat, $long) = &Util::CheckLatLong($query->param('latitude'),
271 $query->param('longitude'));
272 my ($greylisting, $callout, $mailcontentinspection, $defaultoptions);
274 $greylisting = $query->param('mailgreylisting');
275 if (!$greylisting or $greylisting ne "TRUE") {
276 $greylisting = "FALSE";
279 $callout = $query->param('mailcallout');
280 if (!$callout or $callout ne "TRUE") {
284 $mailcontentinspection = $query->param('mailcontentinspection');
285 if (!$mailcontentinspection or ($mailcontentinspection ne "blackhole" and $mailcontentinspection ne "markup")) {
286 $mailcontentinspection = "reject";
289 $defaultoptions = $query->param('maildefaultoptions');
290 if (!$defaultoptions or $defaultoptions ne "TRUE") {
291 $defaultoptions = "FALSE";
296 if ($query->param('newsudopass') && $query->param('newsudopassvrfy')) {
297 my $host = $query->param('newsudopass-host');
298 if ($host =~ /[^a-z0-9.-]/ and $host ne '*') {
299 &Util::HTMLError("The sudo host has weird characters '$host'.");
302 if ($query->param('newsudopass') ne $query->param('newsudopassvrfy')) {
303 &Util::HTMLError("The sudo passwords you specified do not match. Please go back and try again.");
306 my $ldappass = $password;
307 $ldappass = $query->param('newpass') if $query->param('newpass');
308 push @ldapinfo_for_pwcheck, $host, split(/\./, $host);
309 my ($r, $msg) = &Util::checkPasswordQuality($query->param('newsudopass'), $ldappass, [@ldapinfo_for_pwcheck]);
311 &Util::HTMLError("Password check failed for new sudo pass: $msg. Please go back and try again.");
314 # create a md5 crypted password
315 my $newsudopassword = crypt($query->param('newsudopass'), &Util::CreateCryptSalt(1));
316 my $ug = new Data::UUID;
317 my $uuid = $ug->create_str();
319 $newsudo = "$uuid unconfirmed $host $newsudopassword";
320 $newsudo_hosts = $host;
323 my %delete_uuids = map { s/^sudopassword-delete-//; $_ => 1} grep { $query->param($_) eq 'delete' } grep { /^sudopassword-delete-/ } $query->param;
325 for my $entry (@{$entry->{'sudopassword'}}) {
326 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.,*]+) ([^ ]+)$/);
327 next unless defined ($uuid);
328 next if (defined $delete_uuids{$uuid});
329 my %hosts = map { $_ => 1 } split(/,/, $hosts);
330 next if ($hosts{$newsudo_hosts});
331 push @keepsudo, $entry;
333 if ($newsudo ne '') {
334 push @keepsudo, $newsudo;
337 &Util::LDAPUpdate($ldap, $editdn, 'postalAddress', $newstaddress);
338 &Util::LDAPUpdate($ldap, $editdn, 'l', $query->param('l'));
339 &Util::LDAPUpdate($ldap, $editdn, 'latitude', $lat);
340 &Util::LDAPUpdate($ldap, $editdn, 'longitude', $long);
341 &Util::LDAPUpdate($ldap, $editdn, 'c', $query->param('country'));
342 &Util::LDAPUpdate($ldap, $editdn, 'postalCode', $query->param('postalcode'));
343 &Util::LDAPUpdate($ldap, $editdn, 'telephoneNumber', $query->param('telephonenumber'));
344 &Util::LDAPUpdate($ldap, $editdn, 'facsimileTelephoneNumber', $query->param('facsimiletelephonenumber'));
345 &Util::LDAPUpdate($ldap, $editdn, 'bATVToken', $query->param('bATVToken'));
346 &Util::LDAPUpdate($ldap, $editdn, 'VoIP', $query->param('VoIP'));
347 &Util::LDAPUpdate($ldap, $editdn, 'loginShell', $query->param('loginshell'));
348 &Util::LDAPUpdate($ldap, $editdn, 'emailForward', $query->param('email'));
349 &Util::LDAPUpdate($ldap, $editdn, 'privateSub', $query->param('privatesub'));
350 &Util::LDAPUpdate($ldap, $editdn, 'ircNick', $query->param('ircnick'));
351 &Util::LDAPUpdate($ldap, $editdn, 'icqUin', $query->param('icquin'));
352 &Util::LDAPUpdate($ldap, $editdn, 'jabberJID', $query->param('jabberjid'));
353 &Util::LDAPUpdate($ldap, $editdn, 'labeledURI', $query->param('labeleduri'));
354 &Util::LDAPUpdate($ldap, $editdn, 'onVacation', $query->param('onvacation'));
355 &Util::LDAPUpdate($ldap, $editdn, 'gender', $gender);
356 &Util::LDAPUpdate($ldap, $editdn, 'birthDate', $query->param('birthdate')) if $bd_ok;
357 &Util::LDAPUpdate($ldap, $editdn, 'mailDisableMessage', $query->param('maildisablemessage'));
358 &Util::LDAPUpdate($ldap, $editdn, 'mailCallout', $callout);
359 &Util::LDAPUpdate($ldap, $editdn, 'mailContentInspectionAction', $mailcontentinspection);
360 &Util::LDAPUpdate($ldap, $editdn, 'mailGreylisting', $greylisting);
361 &Util::LDAPUpdate($ldap, $editdn, 'mailDefaultOptions', $defaultoptions);
362 &Util::LDAPUpdate($ldap, $editdn, 'sudoPassword', \@keepsudo);
364 # when we are done, reload the page with the updated details.
365 my $url = "https://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id&authtoken=$authtoken&editdn=";
366 $url .= uri_escape($editdn, "\x00-\x40\x7f-\xff");
367 print "Location: $url\n\n";