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 $data{email} = CGI::escapeHTML(join(", ", @{$entry->{emailforward}}));
113 my $genderselect = '<select name="gender">'
114 . '<option value="9"'
115 . ($data{gender} == 9 ? ' selected' : '')
117 . '<option value="1"'
118 . ($data{gender} == 1 ? ' selected' : '')
119 . '>male<option value="2"'
120 . ($data{gender} == 2 ? ' selected' : '')
121 . '>female</select>';
122 my $confirmstring = '';
123 my $sudopassword = '';
124 for my $e(@{$entry->{'sudopassword'}}) {
125 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.,*]+) ([^ ]+)$/);
126 unless (defined $uuid) {
127 $sudopassword .= "<tr><td>Unparseable line!</td></tr>\n";
130 $status =~ s/:.*//; # remove verification hmac, it's just noise here.
131 my $e = "<tr><td>".CGI::escapeHTML($hosts)."</td>
132 <td>".CGI::escapeHTML($status)."</td>
133 <td><small>not shown</small></td>
134 <!--<td><small><code>".CGI::escapeHTML($uuid)."</code></small></td>-->
135 <td><input name=\"sudopassword-delete-".CGI::escapeHTML($uuid)."\" type=\"checkbox\" value=\"delete\"> (delete)</td></tr>\n";
137 if ($status eq 'unconfirmed') {
138 my $data = join(':', 'confirm-new-password', $uuid, $hosts, $crypted);
139 my $hmac = hmac_sha1_hex( $data, $hmac_key);
140 $confirmstring .= CGI::escapeHTML("confirm sudopassword $uuid $hosts $hmac\n");
143 if ($confirmstring ne '') {
144 $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>";
147 my $sudopasswordhosts = '<select name="newsudopass-host"> <option value="*">ALL';
148 for my $dn (sort {$host_entries->{$a}->{host}->[0] cmp $host_entries->{$b}->{host}->[0]} keys(%$host_entries)) {
149 my $data = $host_entries->{$dn};
150 my $host = $data->{'host'}->[0];
151 my $hostname = $data->{'hostname'}->[0];
152 $sudopasswordhosts .= "<option value=\"$hostname\">$host\n";
154 $sudopasswordhosts .= '</select>';
156 # finally we can send output...
158 &Util::HTMLSendHeader;
159 open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
161 s/~(.+?)~/$data{$1}/g;
162 s/<\?genderselect>/$genderselect/;
163 s/<\?sudopassword>/$sudopassword/;
164 s/<\?sudopasswordhosts>/$sudopasswordhosts/;
165 s/<\?confirmstring>/$confirmstring/;
170 my @ldapinfo_for_pwcheck;
171 for my $a (qw{cn sn md gecos uid}) {
172 for my $e (@{$entry->{$a}}) {
173 push @ldapinfo_for_pwcheck, $e;
178 # Actually update stuff...
179 my ($newpassword, $newstaddress);
181 # Good god, why would we want to do that here? it breaks password setting
182 # etc, and it doesn't prevent people from setting eveil stuff in ldap
184 # &Util::FixParams($query);
186 if (($query->param('labeleduri')) &&
187 ($query->param('labeleduri') !~ /^https?:\/\//i)) {
188 &Util::HTMLError("Your homepage URL is invalid");
191 if ($query->param('newpass') && $query->param('newpassvrfy')) {
192 if ($query->param('newpass') ne $query->param('newpassvrfy')) {
193 # passwords don't match...
194 &Util::HTMLError("The passwords you specified do not match. Please go back and try again.");
197 my ($r, $msg) = &Util::checkPasswordQuality($query->param('newpass'), undef, [@ldapinfo_for_pwcheck]);
199 &Util::HTMLError("Password check failed: $msg. Please go back and try again.");
202 # create a md5 crypted password
203 $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
205 &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
206 &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
209 $newstaddress = $query->param('staddress');
210 $newstaddress =~ s/\n/\$/m;
212 my $gender = $query->param('gender');
213 if ($gender != 1 && $gender != 2) {
214 $gender = 9; # unspecified
217 my ($bd_ok, $bd_yr, $bd_mo, $bd_day);
219 if ($query->param('birthdate') =~ /^([1-9][0-9]{3})([01][0-9])([0-3][0-9])$/) {
220 $bd_yr = $1; $bd_mo = $2; $bd_day = $3;
221 if ($bd_yr > 1850 and $bd_mo > 0 and $bd_mo <= 12 and $bd_day > 0) {
223 if ($bd_day == 29 and ($bd_yr % 4 == 0 && ($bd_yr % 100 != 0 || $bd_yr % 400 == 0))) {
225 } elsif ($bd_day <= 28) {
228 } elsif ($bd_mo == 4 or $bd_mo == 6 or $bd_mo == 9 or $bd_mo == 11) {
238 } elsif (not defined($query->param('birthdate')) or $query->param('birthdate') =~ /^\s*$/) {
242 ($lat, $long) = &Util::CheckLatLong($query->param('latitude'),
243 $query->param('longitude'));
244 my ($greylisting, $callout);
246 $greylisting = $query->param('mailgreylisting');
247 if (!$greylisting or $greylisting ne "TRUE") {
248 $greylisting = "FALSE";
251 $callout = $query->param('mailcallout');
252 if (!$callout or $callout ne "TRUE") {
258 if ($query->param('newsudopass') && $query->param('newsudopassvrfy')) {
259 my $host = $query->param('newsudopass-host');
260 if ($host =~ /[^a-z0-9.-]/ and $host ne '*') {
261 &Util::HTMLError("The sudo host has weird characters '$host'.");
264 if ($query->param('newsudopass') ne $query->param('newsudopassvrfy')) {
265 &Util::HTMLError("The sudo passwords you specified do not match. Please go back and try again.");
268 my $ldappass = $password;
269 $ldappass = $query->param('newpass') if $query->param('newpass');
270 push @ldapinfo_for_pwcheck, $host, split(/\./, $host);
271 my ($r, $msg) = &Util::checkPasswordQuality($query->param('newsudopass'), $ldappass, [@ldapinfo_for_pwcheck]);
273 &Util::HTMLError("Password check failed for new sudo pass: $msg. Please go back and try again.");
276 # create a md5 crypted password
277 my $newsudopassword = crypt($query->param('newsudopass'), &Util::CreateCryptSalt(1));
278 my $ug = new Data::UUID;
279 my $uuid = $ug->create_str();
281 $newsudo = "$uuid unconfirmed $host $newsudopassword";
282 $newsudo_hosts = $host;
285 my %delete_uuids = map { s/^sudopassword-delete-//; $_ => 1} grep { $query->param($_) eq 'delete' } grep { /^sudopassword-delete-/ } $query->param;
287 for my $entry (@{$entry->{'sudopassword'}}) {
288 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.,*]+) ([^ ]+)$/);
289 next unless defined ($uuid);
290 next if (defined $delete_uuids{$uuid});
291 my %hosts = map { $_ => 1 } split(/,/, $hosts);
292 next if ($hosts{$newsudo_hosts});
293 push @keepsudo, $entry;
295 if ($newsudo ne '') {
296 push @keepsudo, $newsudo;
299 &Util::LDAPUpdate($ldap, $editdn, 'postalAddress', $newstaddress);
300 &Util::LDAPUpdate($ldap, $editdn, 'l', $query->param('l'));
301 &Util::LDAPUpdate($ldap, $editdn, 'latitude', $lat);
302 &Util::LDAPUpdate($ldap, $editdn, 'longitude', $long);
303 &Util::LDAPUpdate($ldap, $editdn, 'c', $query->param('country'));
304 &Util::LDAPUpdate($ldap, $editdn, 'postalCode', $query->param('postalcode'));
305 &Util::LDAPUpdate($ldap, $editdn, 'telephoneNumber', $query->param('telephonenumber'));
306 &Util::LDAPUpdate($ldap, $editdn, 'facsimileTelephoneNumber', $query->param('facsimiletelephonenumber'));
307 &Util::LDAPUpdate($ldap, $editdn, 'VoIP', $query->param('VoIP'));
308 &Util::LDAPUpdate($ldap, $editdn, 'loginShell', $query->param('loginshell'));
309 &Util::LDAPUpdate($ldap, $editdn, 'emailForward', $query->param('email'));
310 &Util::LDAPUpdate($ldap, $editdn, 'privateSub', $query->param('privatesub'));
311 &Util::LDAPUpdate($ldap, $editdn, 'ircNick', $query->param('ircnick'));
312 &Util::LDAPUpdate($ldap, $editdn, 'icqUin', $query->param('icquin'));
313 &Util::LDAPUpdate($ldap, $editdn, 'jabberJID', $query->param('jabberjid'));
314 &Util::LDAPUpdate($ldap, $editdn, 'labeledURI', $query->param('labeleduri'));
315 &Util::LDAPUpdate($ldap, $editdn, 'onVacation', $query->param('onvacation'));
316 &Util::LDAPUpdate($ldap, $editdn, 'gender', $gender);
317 &Util::LDAPUpdate($ldap, $editdn, 'birthDate', $query->param('birthdate')) if $bd_ok;
318 &Util::LDAPUpdate($ldap, $editdn, 'mailDisableMessage', $query->param('maildisablemessage'));
319 &Util::LDAPUpdate($ldap, $editdn, 'mailCallout', $callout);
320 &Util::LDAPUpdate($ldap, $editdn, 'mailGreylisting', $greylisting);
321 &Util::LDAPUpdate($ldap, $editdn, 'sudoPassword', \@keepsudo);
323 # when we are done, reload the page with the updated details.
324 my $url = "https://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id&authtoken=$authtoken&editdn=";
325 $url .= uri_escape($editdn, "\x00-\x40\x7f-\xff");
326 print "Location: $url\n\n";