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>
13 use Net::LDAP qw(:all);
15 my %config = &Util::ReadConfigFile;
18 my $proto = ($ENV{HTTPS} ? "https" : "http");
20 my $id = $query->param('id');
21 my $authtoken = $query->param('authtoken');
22 my $password = &Util::CheckAuthToken($authtoken);
23 my $editdn = $query->param('editdn');
25 if ($proto eq "http" || !($id && $password)) {
26 print "Location: https://$ENV{SERVER_NAME}/$config{webloginhtml}\n\n";
33 $ldap->unbind if (defined($ldap));
36 $SIG{__DIE__} = \&DieHandler;
38 $ldap = Net::LDAP->new($config{ldaphost});
39 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
42 $mesg = $ldap->bind($editdn, password => $password);
44 $auth = ($mesg->code == LDAP_SUCCESS);
48 &Util::HTMLError("You have not been authenticated. Please <a href=\"https://$ENV{SERVER_NAME}/$config{webloginhtml}\">Login</a>");
53 $mesg = $ldap->search(base => $editdn,
55 $mesg->code && &Util::HTMLError($mesg->error);
57 my $entries = $mesg->as_struct;
58 if ($mesg->count != 1) {
62 my @dns = keys(%$entries);
63 my $entry = $entries->{$dns[0]};
65 if (!($query->param('doupdate'))) {
66 # Not yet update, just fill in the form with the current values
70 # First do the easy stuff - this catches most of the cases
71 foreach (keys(%$entry)) {
72 $data{$_} = $entry->{$_}->[0];
75 $data{gender} = 9 if not $data{gender};
77 # Now we have to fill in the rest that needs some processing...
79 $data{authtoken} = $authtoken;
80 $data{editdn} = $editdn;
81 $data{staddress} = $entry->{postaladdress}->[0];
82 $data{staddress} =~ s/\$/\n/;
83 $data{countryname} = &Util::LookupCountry($data{c});
85 if ($data{mailgreylisting} eq "TRUE") {
86 $data{mailgreylisting} = " checked";
88 $data{mailgreylisting} = "";
91 if ($data{mailcallout} eq "TRUE") {
92 $data{mailcallout} = " checked";
94 $data{mailcallout} = "";
97 $data{email} = join(", ", @{$entry->{emailforward}});
99 my $genderselect = '<select name="gender">'
100 . '<option value="9"'
101 . ($data{gender} == 9 ? ' selected' : '')
103 . '<option value="1"'
104 . ($data{gender} == 1 ? ' selected' : '')
105 . '>male<option value="2"'
106 . ($data{gender} == 2 ? ' selected' : '')
107 . '>female</select>';
109 # finally we can send output...
111 &Util::HTMLSendHeader;
112 open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
114 s/~(.+?)~/$data{$1}/g;
115 s/<\?genderselect>/$genderselect/;
121 # Actually update stuff...
122 my ($newpassword, $newstaddress);
124 &Util::FixParams($query);
126 if (($query->param('labeleduri')) &&
127 ($query->param('labeleduri') !~ /^https?:\/\//i)) {
128 &Util::HTMLError("Your homepage URL is invalid");
131 if ($query->param('newpass') && $query->param('newpassvrfy')) {
132 if ($query->param('newpass') ne $query->param('newpassvrfy')) {
133 # passwords don't match...
134 &Util::HTMLError("The passwords you specified do not match. Please go back and try again.");
136 # create a md5 crypted password
137 $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
139 &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
140 &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
143 $newstaddress = $query->param('staddress');
144 $newstaddress =~ s/\n/\$/m;
146 my $gender = $query->param('gender');
147 if ($gender != 1 && $gender != 2) {
148 $gender = 9; # unspecified
151 my ($bd_ok, $bd_yr, $bd_mo, $bd_day);
153 if ($query->param('birthdate') =~ /^([1-9][0-9]{3})([01][0-9])([0-3][0-9])$/) {
154 $bd_yr = $1; $bd_mo = $2; $bd_day = $3;
155 if ($bd_yr > 1850 and $bd_mo > 0 and $bd_mo <= 12 and $bd_day > 0) {
157 if ($bd_day == 29 and ($bd_yr % 4 == 0 && ($bd_yr % 100 != 0 || $bd_yr % 400 == 0))) {
159 } elsif ($bd_day <= 28) {
162 } elsif ($bd_mo == 4 or $bd_mo == 6 or $bd_mo == 9 or $bd_mo == 11) {
172 } elsif (not defined($query->param('birthdate')) or $query->param('birthdate') =~ /^\s*$/) {
176 ($lat, $long) = &Util::CheckLatLong($query->param('latitude'),
177 $query->param('longitude'));
178 my ($greylisting, $callout);
180 $greylisting = $query->param('mailgreylisting');
181 if (!$greylisting or $greylisting ne "TRUE") {
182 $greylisting = "FALSE";
185 $callout = $query->param('mailcallout');
186 if (!$callout or $callout ne "TRUE") {
190 &Util::LDAPUpdate($ldap, $editdn, 'postalAddress', $newstaddress);
191 &Util::LDAPUpdate($ldap, $editdn, 'l', $query->param('l'));
192 &Util::LDAPUpdate($ldap, $editdn, 'latitude', $lat);
193 &Util::LDAPUpdate($ldap, $editdn, 'longitude', $long);
194 &Util::LDAPUpdate($ldap, $editdn, 'c', $query->param('country'));
195 &Util::LDAPUpdate($ldap, $editdn, 'postalCode', $query->param('postalcode'));
196 &Util::LDAPUpdate($ldap, $editdn, 'telephoneNumber', $query->param('telephonenumber'));
197 &Util::LDAPUpdate($ldap, $editdn, 'facsimileTelephoneNumber', $query->param('facsimiletelephonenumber'));
198 &Util::LDAPUpdate($ldap, $editdn, 'VoIP', $query->param('VoIP'));
199 &Util::LDAPUpdate($ldap, $editdn, 'loginShell', $query->param('loginshell'));
200 &Util::LDAPUpdate($ldap, $editdn, 'emailForward', $query->param('email'));
201 &Util::LDAPUpdate($ldap, $editdn, 'privateSub', $query->param('privatesub'));
202 &Util::LDAPUpdate($ldap, $editdn, 'ircNick', $query->param('ircnick'));
203 &Util::LDAPUpdate($ldap, $editdn, 'icqUin', $query->param('icquin'));
204 &Util::LDAPUpdate($ldap, $editdn, 'jabberJID', $query->param('jabberjid'));
205 &Util::LDAPUpdate($ldap, $editdn, 'labeledURI', $query->param('labeleduri'));
206 &Util::LDAPUpdate($ldap, $editdn, 'onVacation', $query->param('onvacation'));
207 &Util::LDAPUpdate($ldap, $editdn, 'gender', $gender);
208 &Util::LDAPUpdate($ldap, $editdn, 'birthDate', $query->param('birthdate')) if $bd_ok;
209 &Util::LDAPUpdate($ldap, $editdn, 'mailDisableMessage', $query->param('maildisablemessage'));
210 &Util::LDAPUpdate($ldap, $editdn, 'mailCallout', $callout);
211 &Util::LDAPUpdate($ldap, $editdn, 'mailGreylisting', $greylisting);
213 # when we are done, reload the page with the updated details.
214 my $url = "https://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id&authtoken=$authtoken&editdn=";
215 $url .= uri_escape($editdn, "\x00-\x40\x7f-\xff");
216 print "Location: $url\n\n";