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});
41 $mesg = $ldap->bind($editdn, password => $password);
43 $auth = ($mesg->code == LDAP_SUCCESS);
47 &Util::HTMLError("You have not been authenticated. Please <a href=\"https://$ENV{SERVER_NAME}/$config{webloginhtml}\">Login</a>");
52 $mesg = $ldap->search(base => $editdn,
54 $mesg->code && &Util::HTMLError($mesg->error);
56 my $entries = $mesg->as_struct;
57 if ($mesg->count != 1) {
61 my @dns = keys(%$entries);
62 my $entry = $entries->{$dns[0]};
64 if (!($query->param('doupdate'))) {
65 # Not yet update, just fill in the form with the current values
69 # First do the easy stuff - this catches most of the cases
70 foreach (keys(%$entry)) {
71 $data{$_} = $entry->{$_}->[0];
74 $data{gender} = 9 if not $data{gender};
76 # Now we have to fill in the rest that needs some processing...
78 $data{authtoken} = $authtoken;
79 $data{editdn} = $editdn;
80 $data{staddress} = $entry->{postaladdress}->[0];
81 $data{staddress} =~ s/\$/\n/;
82 $data{countryname} = &Util::LookupCountry($data{c});
84 if ($data{mailgreylisting} eq "TRUE") {
85 $data{mailgreylisting} = " checked";
87 $data{mailgreylisting} = "";
90 if ($data{mailcallout} eq "TRUE") {
91 $data{mailcallout} = " checked";
93 $data{mailcallout} = "";
96 $data{email} = join(", ", @{$entry->{emailforward}});
98 my $genderselect = '<select name="gender">'
100 . ($data{gender} == 9 ? ' selected' : '')
102 . '<option value="1"'
103 . ($data{gender} == 1 ? ' selected' : '')
104 . '>male<option value="2"'
105 . ($data{gender} == 2 ? ' selected' : '')
106 . '>female</select>';
108 # finally we can send output...
110 &Util::HTMLSendHeader;
111 open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
113 s/~(.+?)~/$data{$1}/g;
114 s/<\?genderselect>/$genderselect/;
120 # Actually update stuff...
121 my ($newpassword, $newstaddress);
123 &Util::FixParams($query);
125 if (($query->param('labeleduri')) &&
126 ($query->param('labeleduri') !~ /^https?:\/\//i)) {
127 &Util::HTMLError("Your homepage URL is invalid");
130 if ($query->param('newpass') && $query->param('newpassvrfy')) {
131 if ($query->param('newpass') ne $query->param('newpassvrfy')) {
132 # passwords don't match...
133 &Util::HTMLError("The passwords you specified do not match. Please go back and try again.");
135 # create a md5 crypted password
136 $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
138 &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
139 &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
142 $newstaddress = $query->param('staddress');
143 $newstaddress =~ s/\n/\$/m;
145 my $gender = $query->param('gender');
146 if ($gender != 1 && $gender != 2) {
147 $gender = 9; # unspecified
150 my ($bd_ok, $bd_yr, $bd_mo, $bd_day);
152 if ($query->param('birthdate') =~ /^([1-9][0-9]{3})([01][0-9])([0-3][0-9])$/) {
153 $bd_yr = $1; $bd_mo = $2; $bd_day = $3;
154 if ($bd_yr > 1850 and $bd_mo > 0 and $bd_mo <= 12 and $bd_day > 0) {
156 if ($bd_day == 29 and ($bd_yr % 4 == 0 && ($bd_yr % 100 != 0 || $bd_yr % 400 == 0))) {
158 } elsif ($bd_day <= 28) {
161 } elsif ($bd_mo == 4 or $bd_mo == 6 or $bd_mo == 9 or $bd_mo == 11) {
171 } elsif (not defined($query->param('birthdate')) or $query->param('birthdate') =~ /^\s*$/) {
175 ($lat, $long) = &Util::CheckLatLong($query->param('latitude'),
176 $query->param('longitude'));
177 my ($greylisting, $callout);
179 $greylisting = $query->param('mailgreylisting');
180 if (!$greylisting or $greylisting ne "TRUE") {
181 $greylisting = "FALSE";
184 $callout = $query->param('mailcallout');
185 if (!$callout or $callout ne "TRUE") {
189 &Util::LDAPUpdate($ldap, $editdn, 'postalAddress', $newstaddress);
190 &Util::LDAPUpdate($ldap, $editdn, 'l', $query->param('l'));
191 &Util::LDAPUpdate($ldap, $editdn, 'latitude', $lat);
192 &Util::LDAPUpdate($ldap, $editdn, 'longitude', $long);
193 &Util::LDAPUpdate($ldap, $editdn, 'c', $query->param('country'));
194 &Util::LDAPUpdate($ldap, $editdn, 'postalCode', $query->param('postalcode'));
195 &Util::LDAPUpdate($ldap, $editdn, 'telephoneNumber', $query->param('telephonenumber'));
196 &Util::LDAPUpdate($ldap, $editdn, 'facsimileTelephoneNumber', $query->param('facsimiletelephonenumber'));
197 &Util::LDAPUpdate($ldap, $editdn, 'VoIP', $query->param('VoIP'));
198 &Util::LDAPUpdate($ldap, $editdn, 'loginShell', $query->param('loginshell'));
199 &Util::LDAPUpdate($ldap, $editdn, 'emailForward', $query->param('email'));
200 &Util::LDAPUpdate($ldap, $editdn, 'privateSub', $query->param('privatesub'));
201 &Util::LDAPUpdate($ldap, $editdn, 'ircNick', $query->param('ircnick'));
202 &Util::LDAPUpdate($ldap, $editdn, 'icqUin', $query->param('icquin'));
203 &Util::LDAPUpdate($ldap, $editdn, 'jabberJID', $query->param('jabberjid'));
204 &Util::LDAPUpdate($ldap, $editdn, 'labeledURI', $query->param('labeleduri'));
205 &Util::LDAPUpdate($ldap, $editdn, 'onVacation', $query->param('onvacation'));
206 &Util::LDAPUpdate($ldap, $editdn, 'gender', $gender);
207 &Util::LDAPUpdate($ldap, $editdn, 'birthDate', $query->param('birthdate')) if $bd_ok;
208 &Util::LDAPUpdate($ldap, $editdn, 'mailDisableMessage', $query->param('maildisablemessage'));
209 &Util::LDAPUpdate($ldap, $editdn, 'mailCallout', $callout);
210 &Util::LDAPUpdate($ldap, $editdn, 'mailGreylisting', $greylisting);
212 # when we are done, reload the page with the updated details.
213 my $url = "https://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id&authtoken=$authtoken&editdn=";
214 $url .= uri_escape($editdn, "\x00-\x40\x7f-\xff");
215 print "Location: $url\n\n";