add support for some new fields and some output cleanups.
[mirror/userdir-ldap-cgi.git] / update.cgi
1 #!/usr/bin/perl
2
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>
6
7 use lib '.';
8 use strict vars;
9 #use Apache::Registry;
10 use CGI;
11 use Util;
12 use URI::Escape;
13 use Net::LDAP qw(:all);
14
15 my %config = &Util::ReadConfigFile;
16
17 my $query = new CGI;
18 my $proto = ($ENV{HTTPS} ? "https" : "http");
19
20 my $id = $query->param('id');
21 my $authtoken = $query->param('authtoken');
22 my $password = &Util::CheckAuthToken($authtoken);
23 my $editdn = $query->param('editdn');
24
25 if ($proto eq "http" || !($id && $password)) {
26   print "Location: https://$ENV{SERVER_NAME}/$config{webloginhtml}\n\n";
27   exit;
28
29
30 my $ldap;
31
32 sub DieHandler {
33   $ldap->unbind if (defined($ldap));
34 }
35
36 $SIG{__DIE__} = \&DieHandler;
37
38 $ldap = Net::LDAP->new($config{ldaphost});
39 my $auth = 0;
40 my $mesg;
41 $mesg = $ldap->bind($editdn, password => $password);
42 $mesg->sync;
43 $auth = ($mesg->code == LDAP_SUCCESS);
44
45 if (!$auth) {
46   $ldap->unbind;
47   &Util::HTMLError("You have not been authenticated. Please <a href=\"https://$ENV{SERVER_NAME}/$config{webloginhtml}\">Login</a>");
48 }
49
50 # Authenticated....
51 # Get our entry...
52 $mesg = $ldap->search(base   => $editdn,
53                       filter => "uid=*");
54 $mesg->code && &Util::HTMLError($mesg->error);
55   
56 my $entries = $mesg->as_struct;
57 if ($mesg->count != 1) {
58   # complain and quit
59 }
60   
61 my @dns = keys(%$entries);
62 my $entry = $entries->{$dns[0]};
63
64 if (!($query->param('doupdate'))) {
65   # Not yet update, just fill in the form with the current values
66   my %data;
67   
68   # Fill in %data
69   # First do the easy stuff - this catches most of the cases
70   foreach (keys(%$entry)) {
71     $data{$_} = $entry->{$_}->[0];
72   }
73   
74   $data{gender} = 9 if not $data{gender};
75
76   # Now we have to fill in the rest that needs some processing...
77   $data{id} = $id;
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});
83   
84   $data{email} = join(", ", @{$entry->{emailforward}});  
85
86   my $genderselect = '<select name="gender">'
87                    . '<option value="9"'
88                    . ($data{gender} == 9 ? ' selected' : '')
89                    . '>unspecified'
90                    . '<option value="1"'
91                    . ($data{gender} == 1 ? ' selected' : '')
92                    . '>male<option value="2"'
93                    . ($data{gender} == 2 ? ' selected' : '')
94                    . '>female</select>';
95
96   # finally we can send output...
97   my ($sub, $substr);
98   &Util::HTMLSendHeader;
99   open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
100   while (<F>) {
101     s/~(.+?)~/$data{$1}/g;
102     s/<\?genderselect>/$genderselect/;
103     print;
104   }
105   close F;
106   
107 } else {
108   # Actually update stuff...
109   my ($newpassword, $newstaddress);
110   
111   &Util::FixParams($query);
112
113   if (($query->param('labeleduri')) && 
114       ($query->param('labeleduri') !~ /^https?:\/\//i)) {
115     &Util::HTMLError("Your homepage URL is invalid");
116   }
117   
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.");
122     }    
123     # create a md5 crypted password
124     $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
125     
126     &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
127     &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
128   }  
129
130   $newstaddress = $query->param('staddress');
131   $newstaddress =~ s/\n/\$/m;
132
133   my $gender = $query->param('gender');
134   if ($gender != 1 && $gender != 2) {
135     $gender = 9; # unspecified
136   }
137   
138   my ($bd_ok, $bd_yr, $bd_mo, $bd_day);
139
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) {
143       if ($bd_mo == 2) {
144          if ($bd_day == 29 and ($bd_yr % 4 == 0 && ($bd_yr % 100 != 0 || $bd_yr % 400 == 0))) {
145            $bd_ok = 1;
146          } elsif ($bd_day <= 28) {
147            $bd_ok = 1;
148          }
149       } elsif ($bd_mo == 4 or $bd_mo == 6 or $bd_mo == 9 or $bd_mo == 11) {
150         if ($bd_day <= 30) {
151           $bd_ok = 1;
152         }
153       } else {
154         if ($bd_day <= 31) {
155           $bd_ok = 1;
156         }
157       }
158     }
159   } elsif (not defined($query->param('birthdate')) or $query->param('birthdate') =~ /^\s*$/) {
160     $bd_ok = 1;
161   }
162   my ($lat, $long);
163   ($lat, $long) = &Util::CheckLatLong($query->param('latitude'), 
164                                       $query->param('longitude'));
165   
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'));
185
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";  
190 }
191
192 $ldap->unbind;