3 # $Id: update.cgi,v 1.12 2006/12/22 08:58:50 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 $data{email} = join(", ", @{$entry->{emailforward}});
86 my $genderselect = '<select name="gender">'
88 . ($data{gender} == 9 ? ' selected' : '')
91 . ($data{gender} == 1 ? ' selected' : '')
92 . '>male<option value="2"'
93 . ($data{gender} == 2 ? ' selected' : '')
96 # finally we can send output...
98 &Util::HTMLSendHeader;
99 open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
101 s/~(.+?)~/$data{$1}/g;
102 s/<\?genderselect>/$genderselect/;
108 # Actually update stuff...
109 my ($newpassword, $newstaddress);
111 &Util::FixParams($query);
113 if (($query->param('labeleduri')) &&
114 ($query->param('labeleduri') !~ /^https?:\/\//i)) {
115 &Util::HTMLError("Your homepage URL is invalid");
118 if ($query->param('newpass') && $query->param('newpassvrfy')) {
119 if ($query->param('newpass') ne $query->param('newpassvrfy')) {
120 # passwords don't match...
121 &Util::HTMLError("The passwords you specified do not match. Please go back and try again.");
123 # create a md5 crypted password
124 $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
126 &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
127 &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
130 $newstaddress = $query->param('staddress');
131 $newstaddress =~ s/\n/\$/m;
133 my $gender = $query->param('gender');
134 if ($gender != 1 && $gender != 2) {
135 $gender = 9; # unspecified
138 my ($bd_ok, $bd_yr, $bd_mo, $bd_day);
140 if ($query->param('birthdate') =~ /^([1-9][0-9]{3})([01][0-9])([0-3][0-9])$/) {
141 $bd_yr = $1; $bd_mo = $2; $bd_day = $3;
142 if ($bd_yr > 1850 and $bd_mo > 0 and $bd_mo <= 12 and $bd_day > 0) {
144 if ($bd_day == 29 and ($bd_yr % 4 == 0 && ($bd_yr % 100 != 0 || $bd_yr % 400 == 0))) {
146 } elsif ($bd_day <= 28) {
149 } elsif ($bd_mo == 4 or $bd_mo == 6 or $bd_mo == 9 or $bd_mo == 11) {
159 } elsif (not defined($query->param('birthdate')) or $query->param('birthdate') =~ /^\s*$/) {
163 ($lat, $long) = &Util::CheckLatLong($query->param('latitude'),
164 $query->param('longitude'));
166 &Util::LDAPUpdate($ldap, $editdn, 'postalAddress', $newstaddress);
167 &Util::LDAPUpdate($ldap, $editdn, 'l', $query->param('l'));
168 &Util::LDAPUpdate($ldap, $editdn, 'latitude', $lat);
169 &Util::LDAPUpdate($ldap, $editdn, 'longitude', $long);
170 &Util::LDAPUpdate($ldap, $editdn, 'c', $query->param('country'));
171 &Util::LDAPUpdate($ldap, $editdn, 'postalCode', $query->param('postalcode'));
172 &Util::LDAPUpdate($ldap, $editdn, 'telephoneNumber', $query->param('telephonenumber'));
173 &Util::LDAPUpdate($ldap, $editdn, 'facsimileTelephoneNumber', $query->param('facsimiletelephonenumber'));
174 &Util::LDAPUpdate($ldap, $editdn, 'loginShell', $query->param('loginshell'));
175 &Util::LDAPUpdate($ldap, $editdn, 'emailForward', $query->param('email'));
176 &Util::LDAPUpdate($ldap, $editdn, 'privateSub', $query->param('privatesub'));
177 &Util::LDAPUpdate($ldap, $editdn, 'ircNick', $query->param('ircnick'));
178 &Util::LDAPUpdate($ldap, $editdn, 'icqUin', $query->param('icquin'));
179 &Util::LDAPUpdate($ldap, $editdn, 'jabberJID', $query->param('jabberjid'));
180 &Util::LDAPUpdate($ldap, $editdn, 'labeledURI', $query->param('labeleduri'));
181 &Util::LDAPUpdate($ldap, $editdn, 'onVacation', $query->param('onvacation'));
182 &Util::LDAPUpdate($ldap, $editdn, 'gender', $gender);
183 &Util::LDAPUpdate($ldap, $editdn, 'birthDate', $query->param('birthdate')) if $bd_ok;
184 &Util::LDAPUpdate($ldap, $editdn, 'mailDisableMessage', $query->param('maildisablemessage'));
186 # when we are done, reload the page with the updated details.
187 my $url = "https://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id&authtoken=$authtoken&editdn=";
188 $url .= uri_escape($editdn, "\x00-\x40\x7f-\xff");
189 print "Location: $url\n\n";