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