remove non-https logins
[mirror/userdir-ldap-cgi.git] / update.cgi
1 #!/usr/bin/perl
2
3 # $Id: update.cgi,v 1.9 2003/03/13 04:25:56 rmurray Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5
6 use lib '.';
7 use strict vars;
8 #use Apache::Registry;
9 use CGI;
10 use Util;
11 use URI::Escape;
12 use Net::LDAP qw(:all);
13
14 my %config = &Util::ReadConfigFile;
15
16 my $query = new CGI;
17 my $proto = ($ENV{HTTPS} ? "https" : "http");
18
19 my $id = $query->param('id');
20 my $authtoken = $query->param('authtoken');
21 my $password = &Util::CheckAuthToken($authtoken);
22 my $editdn = $query->param('editdn');
23
24 if (!($id && $password)) {
25   print "Location: $proto://$ENV{SERVER_NAME}/$config{webloginurl}\n\n";
26   exit;
27
28
29 my $ldap;
30
31 sub DieHandler {
32   $ldap->unbind if (defined($ldap));
33 }
34
35 $SIG{__DIE__} = \&DieHandler;
36
37 $ldap = Net::LDAP->new($config{ldaphost});
38 my $auth = 0;
39 my $mesg;
40 $mesg = $ldap->bind($editdn, password => $password);
41 $mesg->sync;
42 $auth = ($mesg->code == LDAP_SUCCESS);
43
44 if (!$auth) {
45   $ldap->unbind;
46   &Util::HTMLError("You have not been authenticated. Please <a href=\"https://$ENV{SERVER_NAME}/$config{webloginurl}\">Login</a>");
47 }
48
49 # Authenticated....
50 # Get our entry...
51 $mesg = $ldap->search(base   => $editdn,
52                       filter => "uid=*");
53 $mesg->code && &Util::HTMLError($mesg->error);
54   
55 my $entries = $mesg->as_struct;
56 if ($mesg->count != 1) {
57   # complain and quit
58 }
59   
60 my @dns = keys(%$entries);
61 my $entry = $entries->{$dns[0]};
62
63 if (!($query->param('doupdate'))) {
64   # Not yet update, just fill in the form with the current values
65   my %data;
66   
67   # Fill in %data
68   # First do the easy stuff - this catches most of the cases
69   foreach (keys(%$entry)) {
70     $data{$_} = $entry->{$_}->[0];
71   }
72   
73   # Now we have to fill in the rest that needs some processing...
74   $data{id} = $id;
75   $data{authtoken} = $authtoken;
76   $data{editdn} = $editdn;
77   $data{staddress} = $entry->{postaladdress}->[0];
78   $data{staddress} =~ s/\$/\n/;
79   $data{countryname} = &Util::LookupCountry($data{c});
80   
81   $data{email} = join(", ", @{$entry->{emailforward}});  
82
83   # finally we can send output...
84   my ($sub, $substr);
85   &Util::HTMLSendHeader;
86   open (F, "<$config{webupdatehtml}") || &Util::HTMLError($!);
87   while (<F>) {
88     s/~(.+?)~/$data{$1}/g;
89     print;
90   }
91   close F;
92   
93 } else {
94   # Actually update stuff...
95   my ($newpassword, $newstaddress);
96   
97   &Util::FixParams($query);
98
99   if (($query->param('labeledurl')) && 
100       ($query->param('labeledurl') !~ /^https?:\/\//i)) {
101     &Util::HTMLError("Your homepage URL is invalid");
102   }
103   
104   if ($query->param('newpass') && $query->param('newpassvrfy')) {
105     if ($query->param('newpass') ne $query->param('newpassvrfy')) {
106       # passwords don't match...
107       &Util::HTMLError("The passwords you specified do not match. Please go back and try again.");
108     }    
109     # create a md5 crypted password
110     $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
111     
112     &Util::LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
113     &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
114   }  
115
116   $newstaddress = $query->param('staddress');
117   $newstaddress =~ s/\n/\$/m;
118   
119   my ($lat, $long);
120   ($lat, $long) = &Util::CheckLatLong($query->param('latitude'), 
121                                       $query->param('longitude'));
122   
123   &Util::LDAPUpdate($ldap, $editdn, 'postalAddress', $newstaddress);
124   &Util::LDAPUpdate($ldap, $editdn, 'l', $query->param('l'));
125   &Util::LDAPUpdate($ldap, $editdn, 'latitude', $lat);
126   &Util::LDAPUpdate($ldap, $editdn, 'longitude', $long);
127   &Util::LDAPUpdate($ldap, $editdn, 'c', $query->param('country'));
128   &Util::LDAPUpdate($ldap, $editdn, 'postalcode', $query->param('postalcode'));
129   &Util::LDAPUpdate($ldap, $editdn, 'telephoneNumber', $query->param('telephonenumber'));
130   &Util::LDAPUpdate($ldap, $editdn, 'facsimileTelephoneNumber', $query->param('facsimiletelephonenumber'));
131   &Util::LDAPUpdate($ldap, $editdn, 'loginShell', $query->param('loginshell'));
132   &Util::LDAPUpdate($ldap, $editdn, 'emailForward', $query->param('email'));
133   &Util::LDAPUpdate($ldap, $editdn, 'privatesub', $query->param('privatesub'));
134   &Util::LDAPUpdate($ldap, $editdn, 'ircNick', $query->param('ircnick'));
135   &Util::LDAPUpdate($ldap, $editdn, 'icquin', $query->param('icquin'));
136   &Util::LDAPUpdate($ldap, $editdn, 'labeledUrl', $query->param('labeledurl'));
137   &Util::LDAPUpdate($ldap, $editdn, 'onvacation', $query->param('onvacation'));
138
139   # when we are done, reload the page with the updated details.
140   my $url = "$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id&authtoken=$authtoken&editdn=";
141   $url .= uri_escape($editdn, "\x00-\x40\x7f-\xff");
142   print "Location: $url\n\n";  
143 }
144
145 $ldap->unbind;
146