add VoIP to cgi
[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 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   if ($data{mailgreylisting} eq "TRUE") {
85     $data{mailgreylisting} = " checked";
86   } else {
87     $data{mailgreylisting} = "";
88   }
89
90   if ($data{mailcallout} eq "TRUE") {
91     $data{mailcallout} = " checked";
92   } else {
93     $data{mailcallout} = "";
94   }
95   
96   $data{email} = join(", ", @{$entry->{emailforward}});  
97
98   my $genderselect = '<select name="gender">'
99                    . '<option value="9"'
100                    . ($data{gender} == 9 ? ' selected' : '')
101                    . '>unspecified'
102                    . '<option value="1"'
103                    . ($data{gender} == 1 ? ' selected' : '')
104                    . '>male<option value="2"'
105                    . ($data{gender} == 2 ? ' selected' : '')
106                    . '>female</select>';
107
108   # finally we can send output...
109   my ($sub, $substr);
110   &Util::HTMLSendHeader;
111   open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
112   while (<F>) {
113     s/~(.+?)~/$data{$1}/g;
114     s/<\?genderselect>/$genderselect/;
115     print;
116   }
117   close F;
118   
119 } else {
120   # Actually update stuff...
121   my ($newpassword, $newstaddress);
122   
123   &Util::FixParams($query);
124
125   if (($query->param('labeleduri')) && 
126       ($query->param('labeleduri') !~ /^https?:\/\//i)) {
127     &Util::HTMLError("Your homepage URL is invalid");
128   }
129   
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.");
134     }    
135     # create a md5 crypted password
136     $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
137     
138     &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
139     &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
140   }  
141
142   $newstaddress = $query->param('staddress');
143   $newstaddress =~ s/\n/\$/m;
144
145   my $gender = $query->param('gender');
146   if ($gender != 1 && $gender != 2) {
147     $gender = 9; # unspecified
148   }
149   
150   my ($bd_ok, $bd_yr, $bd_mo, $bd_day);
151
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) {
155       if ($bd_mo == 2) {
156          if ($bd_day == 29 and ($bd_yr % 4 == 0 && ($bd_yr % 100 != 0 || $bd_yr % 400 == 0))) {
157            $bd_ok = 1;
158          } elsif ($bd_day <= 28) {
159            $bd_ok = 1;
160          }
161       } elsif ($bd_mo == 4 or $bd_mo == 6 or $bd_mo == 9 or $bd_mo == 11) {
162         if ($bd_day <= 30) {
163           $bd_ok = 1;
164         }
165       } else {
166         if ($bd_day <= 31) {
167           $bd_ok = 1;
168         }
169       }
170     }
171   } elsif (not defined($query->param('birthdate')) or $query->param('birthdate') =~ /^\s*$/) {
172     $bd_ok = 1;
173   }
174   my ($lat, $long);
175   ($lat, $long) = &Util::CheckLatLong($query->param('latitude'), 
176                                       $query->param('longitude'));
177   my ($greylisting, $callout);
178
179   $greylisting = $query->param('mailgreylisting');
180   if (!$greylisting or $greylisting ne "TRUE") {
181      $greylisting = "FALSE";
182   }
183
184   $callout = $query->param('mailcallout');
185   if (!$callout or $callout ne "TRUE") {
186      $callout = "FALSE";
187   }
188
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);
211
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";  
216 }
217
218 $ldap->unbind;