File:
[LON-CAPA] /
loncom /
interface /
lonpreferences.pm
Revision
1.10:
download - view:
text,
annotated -
select for diffs
Wed Aug 21 17:18:08 2002 UTC (21 years, 11 months ago) by
www
Branches:
MAIN
CVS tags:
HEAD
Starting to implement common header and color scheme for LON-CAPA handlers
(non-content pages).
Instead of <body bgcolor="#...."><h1>... call
&Apache::loncommon::bodytag(title,[role],[add_body_parms]);
title: what it says in the header
role (OPTIONAL): override role choice
('admin','coordinator','student','author')
add_body_parms: additional parameters to be put into the body tag, for
example 'onLoad="init();" or stuff
Colors and layout will likely change in the future, including domain
customization, help function calls, (css?)
1: # The LearningOnline Network
2: # Preferences
3: #
4: # $Id: lonpreferences.pm,v 1.10 2002/08/21 17:18:08 www Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # (Internal Server Error Handler
29: #
30: # (Login Screen
31: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
32: # 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
33: #
34: # 3/1/1 Gerd Kortemeyer)
35: #
36: # 3/1 Gerd Kortemeyer
37: #
38: # 2/13/02 2/14 2/15 Matthew Hall
39: #
40: # This package uses the "londes.js" javascript code.
41: #
42: # TODOs that have to be completed:
43: # interface with lonnet to change the password
44:
45: package Apache::lonpreferences;
46:
47: use strict;
48: use Apache::Constants qw(:common);
49: use Apache::File;
50: use Crypt::DES;
51: use DynaLoader; # for Crypt::DES version
52: use Apache::loncommon();
53:
54: #
55: # Write lonnet::passwd to do the call below.
56: # Use:
57: # my $answer=reply("encrypt:passwd:$udom:$uname:$upass",$tryserver);
58: #
59: ##################################################
60: # password associated functions #
61: ##################################################
62: sub des_keys {
63: # Make a new key for DES encryption.
64: # Each key has two parts which are returned seperately.
65: # Please note: Each key must be passed through the &hex function
66: # before it is output to the web browser. The hex versions cannot
67: # be used to decrypt.
68: my @hexstr=('0','1','2','3','4','5','6','7',
69: '8','9','a','b','c','d','e','f');
70: my $lkey='';
71: for (0..7) {
72: $lkey.=$hexstr[rand(15)];
73: }
74: my $ukey='';
75: for (0..7) {
76: $ukey.=$hexstr[rand(15)];
77: }
78: return ($lkey,$ukey);
79: }
80:
81: sub des_decrypt {
82: my ($key,$cyphertext) = @_;
83: my $keybin=pack("H16",$key);
84: my $cypher;
85: if ($Crypt::DES::VERSION>=2.03) {
86: $cypher=new Crypt::DES $keybin;
87: } else {
88: $cypher=new DES $keybin;
89: }
90: my $plaintext=
91: $cypher->decrypt(unpack("a8",pack("H16",substr($cyphertext,0,16))));
92: $plaintext.=
93: $cypher->decrypt(unpack("a8",pack("H16",substr($cyphertext,16,16))));
94: $plaintext=substr($plaintext,1,ord(substr($plaintext,0,1)) );
95: return $plaintext;
96: }
97:
98: ################################################################
99: # Handler subroutines #
100: ################################################################
101:
102: ################################################################
103: # Anonymous Discussion Name Change Subroutines #
104: ################################################################
105: sub screennamechanger {
106: my $r = shift;
107: my $user = $ENV{'user.name'};
108: my $domain = $ENV{'user.domain'};
109: my %userenv = &Apache::lonnet::get('environment',['screenname']);
110: my $screenname=$userenv{'screenname'};
111: my $bodytag=&Apache::loncommon::bodytag(
112: 'Change Your Anonymous Screen Name');
113: $r->print(<<ENDSCREEN);
114: <html>
115: $bodytag
116:
117: <form name="server" action="/adm/preferences" method="post">
118: <input type="hidden" name="action" value="verify_and_change_screenname" />
119: New screenname:
120: <input type="text" size="20" value="$screenname" name="screenname" />
121: <input type="submit" value="Change" />
122: </form>
123: </body>
124: </html>
125: ENDSCREEN
126: }
127:
128: sub verify_and_change_screenname {
129: my $r = shift;
130: my $user = $ENV{'user.name'};
131: my $domain = $ENV{'user.domain'};
132: my $newscreen = $ENV{'form.screenname'};
133: $newscreen=~s/\W//g;
134: my $message='';
135: if ($newscreen) {
136: &Apache::lonnet::put('environment',{'screenname' => $newscreen});
137: &Apache::lonnet::appenv('environment.screenname' => $newscreen);
138: $message='Set new screenname to '.$newscreen;
139: } else {
140: &Apache::lonnet::del('environment',['screenname']);
141: &Apache::lonnet::delenv('environment\.screenname');
142: $message='Reset screenname';
143: }
144: my $bodytag=&Apache::loncommon::bodytag(
145: 'Change Your Anonymous Screen Name');
146: $r->print(<<ENDVCSCREEN);
147: <html>
148: $bodytag
149: </p>
150: $message
151: </body></html>
152: ENDVCSCREEN
153: }
154:
155: ######################################################
156: # password handler subroutines #
157: ######################################################
158: sub passwordchanger {
159: # This function is a bit of a mess....
160: # Passwords are encrypted using londes.js (DES encryption)
161: my $r = shift;
162: my $errormessage = shift;
163: $errormessage = ($errormessage || '');
164: my $user = $ENV{'user.name'};
165: my $domain = $ENV{'user.domain'};
166: my $homeserver = $ENV{'user.home'};
167: my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
168: # Check for authentication types that allow changing of the password.
169: return if ($currentauth !~ /^(unix|internal):/);
170: #
171: # Generate keys
172: my ($lkey_cpass ,$ukey_cpass ) = &des_keys();
173: my ($lkey_npass1,$ukey_npass1) = &des_keys();
174: my ($lkey_npass2,$ukey_npass2) = &des_keys();
175: # Store the keys in the log files
176: my $lonhost = $r->dir_config('lonHostID');
177: my $logtoken=Apache::lonnet::reply('tmpput:'
178: .$ukey_cpass . $lkey_cpass .'&'
179: .$ukey_npass1 . $lkey_npass1.'&'
180: .$ukey_npass2 . $lkey_npass2,
181: $lonhost);
182: # Hexify the keys for output as javascript variables
183: $ukey_cpass = hex($ukey_cpass);
184: $lkey_cpass = hex($lkey_cpass);
185: $ukey_npass1= hex($ukey_npass1);
186: $lkey_npass1= hex($lkey_npass1);
187: $ukey_npass2= hex($ukey_npass2);
188: $lkey_npass2= hex($lkey_npass2);
189: # Output javascript to deal with passwords
190: # Output DES javascript
191: $r->print("<html><head>");
192: {
193: my $include = $r->dir_config('lonIncludes');
194: my $jsh=Apache::File->new($include."/londes.js");
195: $r->print(<$jsh>);
196: }
197: my $bodytag=&Apache::loncommon::bodytag('Change Password','',
198: 'onLoad="init();"');
199: $r->print(<<ENDFORM);
200: </head>
201: $bodytag
202:
203: <script language="JavaScript">
204:
205: function send() {
206: uextkey=this.document.client.elements.ukey_cpass.value;
207: lextkey=this.document.client.elements.lkey_cpass.value;
208: initkeys();
209:
210: this.document.server.elements.currentpass.value
211: =crypted(this.document.client.elements.currentpass.value);
212:
213: uextkey=this.document.client.elements.ukey_npass1.value;
214: lextkey=this.document.client.elements.lkey_npass1.value;
215: initkeys();
216: this.document.server.elements.newpass_1.value
217: =crypted(this.document.client.elements.newpass_1.value);
218:
219: uextkey=this.document.client.elements.ukey_npass2.value;
220: lextkey=this.document.client.elements.lkey_npass2.value;
221: initkeys();
222: this.document.server.elements.newpass_2.value
223: =crypted(this.document.client.elements.newpass_2.value);
224:
225: this.document.server.submit();
226: }
227:
228: </script>
229: $errormessage
230:
231: <p>
232: <!-- We seperate the forms into 'server' and 'client' in order to
233: ensure that unencrypted passwords will not be sent out by a
234: crappy browser -->
235:
236: <form name="server" action="/adm/preferences" method="post">
237: <input type="hidden" name="logtoken" value="$logtoken" />
238: <input type="hidden" name="action" value="verify_and_change_pass" />
239: <input type="hidden" name="currentpass" value="" />
240: <input type="hidden" name="newpass_1" value="" />
241: <input type="hidden" name="newpass_2" value="" />
242: </form>
243:
244: <form name="client" >
245: <table>
246: <tr><td align="right"> Current password: </td>
247: <td><input type="password" name="currentpass" size="10"/> </td></tr>
248: <tr><td align="right"> New password: </td>
249: <td><input type="password" name="newpass_1" size="10" /> </td></tr>
250: <tr><td align="right"> Confirm password: </td>
251: <td><input type="password" name="newpass_2" size="10" /> </td></tr>
252: <tr><td colspan="2" align="center">
253: <input type="button" value="Change Password" onClick="send();">
254: </table>
255: <input type="hidden" name="ukey_cpass" value="$ukey_cpass" />
256: <input type="hidden" name="lkey_cpass" value="$lkey_cpass" />
257: <input type="hidden" name="ukey_npass1" value="$ukey_npass1" />
258: <input type="hidden" name="lkey_npass1" value="$lkey_npass1" />
259: <input type="hidden" name="ukey_npass2" value="$ukey_npass2" />
260: <input type="hidden" name="lkey_npass2" value="$lkey_npass2" />
261: </form>
262: </p>
263: ENDFORM
264: #
265: return;
266: }
267:
268: sub verify_and_change_password {
269: my $r = shift;
270: my $user = $ENV{'user.name'};
271: my $domain = $ENV{'user.domain'};
272: my $homeserver = $ENV{'user.home'};
273: my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
274: # Check for authentication types that allow changing of the password.
275: return if ($currentauth !~ /^(unix|internal):/);
276: #
277: $r->print(<<ENDHEADER);
278: <html>
279: <head>
280: <title>LON-CAPA Preferences: Change password for $user</title>
281: </head>
282: ENDHEADER
283: #
284: my $currentpass = $ENV{'form.currentpass'};
285: my $newpass1 = $ENV{'form.newpass_1'};
286: my $newpass2 = $ENV{'form.newpass_2'};
287: my $logtoken = $ENV{'form.logtoken'};
288: # Check for empty data
289: unless (defined($currentpass) &&
290: defined($newpass1) &&
291: defined($newpass2) ){
292: &passwordchanger($r,"<p>\n<font color='#ff0000'>ERROR</font>".
293: "Password data was blank.\n</p>");
294: return;
295: }
296: # Get the keys
297: my $lonhost = $r->dir_config('lonHostID');
298: my $tmpinfo = Apache::lonnet::reply('tmpget:'.$logtoken,$lonhost);
299: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
300: # I do not a have a better idea about how to handle this
301: $r->print(<<ENDERROR);
302: <p>
303: <font color="#ff0000">ERROR:</font> Unable to retrieve stored token for
304: password decryption. Please log out and try again.
305: </p>
306: ENDERROR
307: # Probably should log an error here
308: return;
309: }
310: my ($ckey,$n1key,$n2key)=split(/&/,$tmpinfo);
311: #
312: my $currentpass = &des_decrypt($ckey ,$currentpass);
313: my $newpass1 = &des_decrypt($n1key,$newpass1);
314: my $newpass2 = &des_decrypt($n2key,$newpass2);
315: #
316: if ($newpass1 ne $newpass2) {
317: &passwordchanger($r,
318: '<font color="#ff0000">ERROR:</font>'.
319: 'The new passwords you entered do not match. '.
320: 'Please try again.');
321: return;
322: }
323: if (length($newpass1) < 7) {
324: &passwordchanger($r,
325: '<font color="#ff0000">ERROR:</font>'.
326: 'Passwords must be a minimum of 7 characters long. '.
327: 'Please try again.');
328: return;
329: }
330: #
331: # Check for bad characters
332: my $badpassword = 0;
333: foreach (split(//,$newpass1)) {
334: $badpassword = 1 if ((ord($_)<32)||(ord($_)>126));
335: }
336: if ($badpassword) {
337: # I can't figure out how to enter bad characters on my browser.
338: &passwordchanger($r,<<ENDERROR);
339: <font color="#ff0000">ERROR:</font>
340: The password you entered contained illegal characters.<br />
341: Valid characters are: space and <br />
342: <pre>
343: !"\#$%&\'()*+,-./0123456789:;<=>?\@
344: ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_\`abcdefghijklmnopqrstuvwxyz{|}~
345: </pre>
346: ENDERROR
347: }
348: #
349: # Change the password (finally)
350: my $result = &Apache::lonnet::changepass
351: ($user,$domain,$currentpass,$newpass1,$homeserver);
352: # Inform the user the password has (not?) been changed
353: if ($result =~ /^ok$/) {
354: $r->print(<<"ENDTEXT");
355: <h2>The password for $user was successfully changed</h2>
356: ENDTEXT
357: } else {
358: # error error: run in circles, scream and shout
359: $r->print(<<ENDERROR);
360: <h2><font color="#ff0000">The password for $user was not changed</font></h2>
361: Please make sure your old password was entered correctly.
362: ENDERROR
363: }
364: return;
365: }
366:
367: ######################################################
368: # other handler subroutines #
369: ######################################################
370:
371: ################################################################
372: # Main handler #
373: ################################################################
374: sub handler {
375: my $r = shift;
376: my $user = $ENV{'user.name'};
377: my $domain = $ENV{'user.domain'};
378: $r->content_type('text/html');
379: # Some pages contain DES keys and should not be cached.
380: &Apache::loncommon::no_cache($r);
381: $r->send_http_header;
382: return OK if $r->header_only;
383: #
384: if ($ENV{'form.action'} eq 'changepass') {
385: &passwordchanger($r);
386: } elsif ($ENV{'form.action'} eq 'verify_and_change_pass') {
387: &verify_and_change_password($r);
388: } elsif ($ENV{'form.action'} eq 'changescreenname') {
389: &screennamechanger($r);
390: } elsif ($ENV{'form.action'} eq 'verify_and_change_screenname') {
391: &verify_and_change_screenname($r);
392: } else {
393: $r->print(<<ENDHEADER);
394: <html>
395: <head>
396: <title>LON-CAPA Preferences</title>
397: </head>
398: ENDHEADER
399: $r->print(&Apache::loncommon::bodytag('Change Your Preferences'));
400: # Determine current authentication method
401: my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
402: if ($currentauth =~ /^(unix|internal):/) {
403: $r->print(<<ENDPASSWORDFORM);
404: <form name="client" action="/adm/preferences" method="post">
405: <input type="hidden" name="action" value="changepass">
406: <input type="submit" value="Change password">
407: </form>
408: ENDPASSWORDFORM
409: # Change screen name
410: $r->print(<<ENDSCREENNAMEFORM);
411: <form name="client" action="/adm/preferences" method="post">
412: <input type="hidden" name="action" value="changescreenname">
413: <input type="submit" value="Change anonymous discussion screen name">
414: </form>
415: ENDSCREENNAMEFORM
416: # Other preference setting code should be added here
417: }
418: }
419: $r->print(<<ENDFOOTER);
420: </body>
421: </html>
422: ENDFOOTER
423: return OK;
424: }
425:
426: 1;
427: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>