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];
87 $data{gender} = 9 if not $data{gender};
89 # Now we have to fill in the rest that needs some processing...
91 $data{authtoken} = $authtoken;
92 $data{editdn} = $editdn;
93 $data{staddress} = $entry->{postaladdress}->[0];
94 $data{staddress} =~ s/\$/\n/;
95 $data{countryname} = &Util::LookupCountry($data{c});
97 if ($data{mailgreylisting} eq "TRUE") {
98 $data{mailgreylisting} = " checked";
100 $data{mailgreylisting} = "";
103 if ($data{mailcallout} eq "TRUE") {
104 $data{mailcallout} = " checked";
106 $data{mailcallout} = "";
109 $data{email} = join(", ", @{$entry->{emailforward}});
111 my $genderselect = '<select name="gender">'
112 . '<option value="9"'
113 . ($data{gender} == 9 ? ' selected' : '')
115 . '<option value="1"'
116 . ($data{gender} == 1 ? ' selected' : '')
117 . '>male<option value="2"'
118 . ($data{gender} == 2 ? ' selected' : '')
119 . '>female</select>';
120 my $confirmstring = '';
121 my $sudopassword = '';
122 for my $entry (@{$entry->{'sudopassword'}}) {
123 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.,*]+) ([^ ]+)$/);
124 unless (defined $uuid) {
125 $sudopassword .= "<tr><td>Unparseable line!</td></tr>\n";
128 $status =~ s/:.*//; # remove verification hmac, it's just noise here.
129 my $e = "<tr><td>$hosts</td>
131 <td><small>not shown</small></td>
132 <td><small><code>$uuid</code></small></td>
133 <td><input name=\"sudopassword-delete-$uuid\" type=\"checkbox\" value=\"delete\"> (delete)</td></tr>\n";
135 if ($status eq 'unconfirmed') {
136 my $hmac = hmac_sha1_hex( join(':', 'confirm-new-password', $uuid, $hosts, $crypted), $hmac_key);
137 $confirmstring .= "confirm sudopassword $uuid $hosts $hmac\n";
140 if ($confirmstring ne '') {
141 $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>";
144 my $sudopasswordhosts = '<select name="newsudopass-host"> <option value="*">ALL';
145 for my $dn (sort {$host_entries->{$a}->{host}->[0] cmp $host_entries->{$b}->{host}->[0]} keys(%$host_entries)) {
146 my $data = $host_entries->{$dn};
147 my $host = $data->{'host'}->[0];
148 my $hostname = $data->{'hostname'}->[0];
149 $sudopasswordhosts .= "<option value=\"$host\">$hostname\n";
151 $sudopasswordhosts .= '</select>';
153 # finally we can send output...
155 &Util::HTMLSendHeader;
156 open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
158 s/~(.+?)~/$data{$1}/g;
159 s/<\?genderselect>/$genderselect/;
160 s/<\?sudopassword>/$sudopassword/;
161 s/<\?sudopasswordhosts>/$sudopasswordhosts/;
162 s/<\?confirmstring>/$confirmstring/;
167 # Actually update stuff...
168 my ($newpassword, $newstaddress);
170 &Util::FixParams($query);
172 if (($query->param('labeleduri')) &&
173 ($query->param('labeleduri') !~ /^https?:\/\//i)) {
174 &Util::HTMLError("Your homepage URL is invalid");
177 if ($query->param('newpass') && $query->param('newpassvrfy')) {
178 if ($query->param('newpass') ne $query->param('newpassvrfy')) {
179 # passwords don't match...
180 &Util::HTMLError("The passwords you specified do not match. Please go back and try again.");
182 # create a md5 crypted password
183 $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
185 &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
186 &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
189 $newstaddress = $query->param('staddress');
190 $newstaddress =~ s/\n/\$/m;
192 my $gender = $query->param('gender');
193 if ($gender != 1 && $gender != 2) {
194 $gender = 9; # unspecified
197 my ($bd_ok, $bd_yr, $bd_mo, $bd_day);
199 if ($query->param('birthdate') =~ /^([1-9][0-9]{3})([01][0-9])([0-3][0-9])$/) {
200 $bd_yr = $1; $bd_mo = $2; $bd_day = $3;
201 if ($bd_yr > 1850 and $bd_mo > 0 and $bd_mo <= 12 and $bd_day > 0) {
203 if ($bd_day == 29 and ($bd_yr % 4 == 0 && ($bd_yr % 100 != 0 || $bd_yr % 400 == 0))) {
205 } elsif ($bd_day <= 28) {
208 } elsif ($bd_mo == 4 or $bd_mo == 6 or $bd_mo == 9 or $bd_mo == 11) {
218 } elsif (not defined($query->param('birthdate')) or $query->param('birthdate') =~ /^\s*$/) {
222 ($lat, $long) = &Util::CheckLatLong($query->param('latitude'),
223 $query->param('longitude'));
224 my ($greylisting, $callout);
226 $greylisting = $query->param('mailgreylisting');
227 if (!$greylisting or $greylisting ne "TRUE") {
228 $greylisting = "FALSE";
231 $callout = $query->param('mailcallout');
232 if (!$callout or $callout ne "TRUE") {
238 if ($query->param('newsudopass') && $query->param('newsudopassvrfy')) {
239 if ($query->param('newsudopass') ne $query->param('newsudopassvrfy')) {
240 &Util::HTMLError("The sudo passwords you specified do not match. Please go back and try again.");
242 my $host = $query->param('newsudopass-host');
243 if ($host =~ /[^a-z0-9.-]/ and $host ne '*') {
244 &Util::HTMLError("The sudo host has weird characters '$host'.");
246 # create a md5 crypted password
247 my $newsudopassword = crypt($query->param('newsudopass'), &Util::CreateCryptSalt(1));
248 my $ug = new Data::UUID;
249 my $uuid = $ug->create_str();
251 $newsudo = "$uuid unconfirmed $host $newsudopassword";
252 $newsudo_hosts = $host;
255 my %delete_uuids = map { s/^sudopassword-delete-//; $_ => 1} grep { $query->param($_) eq 'delete' } grep { /^sudopassword-delete-/ } $query->param;
257 for my $entry (@{$entry->{'sudopassword'}}) {
258 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.,*]+) ([^ ]+)$/);
259 next unless defined ($uuid);
260 next if (defined $delete_uuids{$uuid});
261 my %hosts = map { $_ => 1 } split(/,/, $hosts);
262 next if ($hosts{$newsudo_hosts});
263 push @keepsudo, $entry;
265 if ($newsudo ne '') {
266 push @keepsudo, $newsudo;
269 &Util::LDAPUpdate($ldap, $editdn, 'postalAddress', $newstaddress);
270 &Util::LDAPUpdate($ldap, $editdn, 'l', $query->param('l'));
271 &Util::LDAPUpdate($ldap, $editdn, 'latitude', $lat);
272 &Util::LDAPUpdate($ldap, $editdn, 'longitude', $long);
273 &Util::LDAPUpdate($ldap, $editdn, 'c', $query->param('country'));
274 &Util::LDAPUpdate($ldap, $editdn, 'postalCode', $query->param('postalcode'));
275 &Util::LDAPUpdate($ldap, $editdn, 'telephoneNumber', $query->param('telephonenumber'));
276 &Util::LDAPUpdate($ldap, $editdn, 'facsimileTelephoneNumber', $query->param('facsimiletelephonenumber'));
277 &Util::LDAPUpdate($ldap, $editdn, 'VoIP', $query->param('VoIP'));
278 &Util::LDAPUpdate($ldap, $editdn, 'loginShell', $query->param('loginshell'));
279 &Util::LDAPUpdate($ldap, $editdn, 'emailForward', $query->param('email'));
280 &Util::LDAPUpdate($ldap, $editdn, 'privateSub', $query->param('privatesub'));
281 &Util::LDAPUpdate($ldap, $editdn, 'ircNick', $query->param('ircnick'));
282 &Util::LDAPUpdate($ldap, $editdn, 'icqUin', $query->param('icquin'));
283 &Util::LDAPUpdate($ldap, $editdn, 'jabberJID', $query->param('jabberjid'));
284 &Util::LDAPUpdate($ldap, $editdn, 'labeledURI', $query->param('labeleduri'));
285 &Util::LDAPUpdate($ldap, $editdn, 'onVacation', $query->param('onvacation'));
286 &Util::LDAPUpdate($ldap, $editdn, 'gender', $gender);
287 &Util::LDAPUpdate($ldap, $editdn, 'birthDate', $query->param('birthdate')) if $bd_ok;
288 &Util::LDAPUpdate($ldap, $editdn, 'mailDisableMessage', $query->param('maildisablemessage'));
289 &Util::LDAPUpdate($ldap, $editdn, 'mailCallout', $callout);
290 &Util::LDAPUpdate($ldap, $editdn, 'mailGreylisting', $greylisting);
291 &Util::LDAPUpdate($ldap, $editdn, 'sudoPassword', \@keepsudo);
293 # when we are done, reload the page with the updated details.
294 my $url = "https://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id&authtoken=$authtoken&editdn=";
295 $url .= uri_escape($editdn, "\x00-\x40\x7f-\xff");
296 print "Location: $url\n\n";