fix copyright info
[mirror/userdir-ldap-cgi.git] / update.cgi
1 #!/usr/bin/perl
2
3 # $Id: update.cgi,v 1.2 1999/09/26 01:20:39 tausq 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=\"$proto://$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   if ($query->param('newpass') && $query->param('newpassvrfy')) {
98     if ($query->param('newpass') ne $query->param('newpassvrfy')) {
99       # passwords don't match...
100       &Util::HTMLError("The passwords you specified do not match. Please go back and try again.");
101     }    
102     # create a md5 crypted password
103     $newpassword = '{crypt}'.crypt($query->param('newpass'), &Util::CreateCryptSalt(1));
104     
105     LDAPUpdate($ldap, $editdn, 'userPassword', $newpassword);
106     &Util::UpdateAuthToken($authtoken, $query->param('newpass'));
107   }  
108
109   $newstaddress = $query->param('staddress');
110   $newstaddress =~ s/\n/\$/m;
111   
112   my ($lat, $long);
113   ($lat, $long) = &Util::CheckLatLong($query->param('latitude'), 
114                                       $query->param('longitude'));
115   
116   LDAPUpdate($ldap, $editdn, 'postalAddress', $newstaddress);
117   LDAPUpdate($ldap, $editdn, 'l', $query->param('l'));
118   LDAPUpdate($ldap, $editdn, 'latitude', $lat);
119   LDAPUpdate($ldap, $editdn, 'longitude', $long);
120   LDAPUpdate($ldap, $editdn, 'c', $query->param('country'));
121   LDAPUpdate($ldap, $editdn, 'postalcode', $query->param('postalcode'));
122   LDAPUpdate($ldap, $editdn, 'telephoneNumber', $query->param('telephonenumber'));
123   LDAPUpdate($ldap, $editdn, 'facsimileTelephoneNumber', $query->param('facsimiletelephonenumber'));
124   LDAPUpdate($ldap, $editdn, 'loginShell', $query->param('loginshell'));
125   LDAPUpdate($ldap, $editdn, 'emailForward', $query->param('email'));
126   LDAPUpdate($ldap, $editdn, 'privatesub', $query->param('privatesub'));
127   LDAPUpdate($ldap, $editdn, 'ircNick', $query->param('ircnick'));
128   LDAPUpdate($ldap, $editdn, 'labeledUrl', $query->param('labeledurl'));
129   LDAPUpdate($ldap, $editdn, 'onvacation', $query->param('onvacation'));
130
131   # when we are done, reload the page with the updated details.
132   my $url = "$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id&authtoken=$authtoken&editdn=";
133   $url .= uri_escape($editdn, "\x00-\x40\x7f-\xff");
134   print "Location: $url\n\n";  
135 }
136
137 $ldap->unbind;
138
139 sub LDAPUpdate {
140   my $ldap = shift;
141   my $dn = shift;
142   my $attr = shift;
143   my $val = shift;
144   my $mesg;
145   
146   if (!$val) {
147     $mesg = $ldap->modify($dn, delete => { $attr => [] });
148   } else {
149     $val = [ $val ] if (!ref($val));
150     $mesg = $ldap->modify($dn, replace => { $attr => $val });
151     $mesg->code && &Util::HTMLError("error updating $attr: ".$mesg->error);
152   }
153 }