fix capitalisation for voip field
[mirror/userdir-ldap-cgi.git] / update.cgi
1 #!/usr/bin/perl
2
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>
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 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
40 my $auth = 0;
41 my $mesg;
42 $mesg = $ldap->bind($editdn, password => $password);
43 $mesg->sync;
44 $auth = ($mesg->code == LDAP_SUCCESS);
45
46 if (!$auth) {
47   $ldap->unbind;
48   &Util::HTMLError("You have not been authenticated. Please <a href=\"https://$ENV{SERVER_NAME}/$config{webloginhtml}\">Login</a>");
49 }
50
51 # Authenticated....
52 # Get our entry...
53 $mesg = $ldap->search(base   => $editdn,
54                       filter => "uid=*");
55 $mesg->code && &Util::HTMLError($mesg->error);
56   
57 my $entries = $mesg->as_struct;
58 if ($mesg->count != 1) {
59   # complain and quit
60 }
61   
62 my @dns = keys(%$entries);
63 my $entry = $entries->{$dns[0]};
64
65 if (!($query->param('doupdate'))) {
66   # Not yet update, just fill in the form with the current values
67   my %data;
68   
69   # Fill in %data
70   # First do the easy stuff - this catches most of the cases
71   foreach (keys(%$entry)) {
72     $data{$_} = $entry->{$_}->[0];
73   }
74   
75   $data{gender} = 9 if not $data{gender};
76
77   # Now we have to fill in the rest that needs some processing...
78   $data{id} = $id;
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});
84
85   if ($data{mailgreylisting} eq "TRUE") {
86     $data{mailgreylisting} = " checked";
87   } else {
88     $data{mailgreylisting} = "";
89   }
90
91   if ($data{mailcallout} eq "TRUE") {
92     $data{mailcallout} = " checked";
93   } else {
94     $data{mailcallout} = "";
95   }
96   
97   $data{email} = join(", ", @{$entry->{emailforward}});  
98
99   my $genderselect = '<select name="gender">'
100                    . '<option value="9"'
101                    . ($data{gender} == 9 ? ' selected' : '')
102                    . '>unspecified'
103                    . '<option value="1"'
104                    . ($data{gender} == 1 ? ' selected' : '')
105                    . '>male<option value="2"'
106                    . ($data{gender} == 2 ? ' selected' : '')
107                    . '>female</select>';
108
109   # finally we can send output...
110   my ($sub, $substr);
111   &Util::HTMLSendHeader;
112   open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
113   while (<F>) {
114     s/~(.+?)~/$data{$1}/g;
115     s/<\?genderselect>/$genderselect/;
116     print;
117   }
118   close F;
119   
120 } else {
121   # Actually update stuff...
122   my ($newpassword, $newstaddress);
123   
124   &Util::FixParams($query);
125
126   if (($query->param('labeleduri')) && 
127       ($query->param('labeleduri') !~ /^https?:\/\//i)) {
128     &Util::HTMLError("Your homepage URL is invalid");
129   }
130   
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.");
135     }    
136     # create a md5 crypted password
137     $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
138     
139     &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
140     &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
141   }  
142
143   $newstaddress = $query->param('staddress');
144   $newstaddress =~ s/\n/\$/m;
145
146   my $gender = $query->param('gender');
147   if ($gender != 1 && $gender != 2) {
148     $gender = 9; # unspecified
149   }
150   
151   my ($bd_ok, $bd_yr, $bd_mo, $bd_day);
152
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) {
156       if ($bd_mo == 2) {
157          if ($bd_day == 29 and ($bd_yr % 4 == 0 && ($bd_yr % 100 != 0 || $bd_yr % 400 == 0))) {
158            $bd_ok = 1;
159          } elsif ($bd_day <= 28) {
160            $bd_ok = 1;
161          }
162       } elsif ($bd_mo == 4 or $bd_mo == 6 or $bd_mo == 9 or $bd_mo == 11) {
163         if ($bd_day <= 30) {
164           $bd_ok = 1;
165         }
166       } else {
167         if ($bd_day <= 31) {
168           $bd_ok = 1;
169         }
170       }
171     }
172   } elsif (not defined($query->param('birthdate')) or $query->param('birthdate') =~ /^\s*$/) {
173     $bd_ok = 1;
174   }
175   my ($lat, $long);
176   ($lat, $long) = &Util::CheckLatLong($query->param('latitude'), 
177                                       $query->param('longitude'));
178   my ($greylisting, $callout);
179
180   $greylisting = $query->param('mailgreylisting');
181   if (!$greylisting or $greylisting ne "TRUE") {
182      $greylisting = "FALSE";
183   }
184
185   $callout = $query->param('mailcallout');
186   if (!$callout or $callout ne "TRUE") {
187      $callout = "FALSE";
188   }
189
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);
212
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";  
217 }
218
219 $ldap->unbind;