Annotation of loncom/auth/lonwebdavauth.pm, revision 1.3

1.1       raeburn     1: # The LearningOnline Network
                      2: # Authentication Handler for webDAV access to Authoring Space.
                      3: #
1.3     ! raeburn     4: # $Id: lonwebdavauth.pm,v 1.2 2012/06/01 11:39:29 raeburn Exp $
1.1       raeburn     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: 
                     29: =head1 NAME
                     30: 
                     31: Apache::lonwebdavauth - webDAV Authentication Handler
                     32: 
                     33: =head1 SYNOPSIS
                     34: 
1.3     ! raeburn    35: Invoked for ^/+webdav/[\w\-.]+/\w[\w.\-\@]+/ by 
1.1       raeburn    36: /etc/httpd/conf/loncapa_apache.conf:
                     37: 
                     38: PerlAuthenHandler	Apache::lonwebdavauth
                     39: 
                     40: =head1 INTRODUCTION
                     41: 
                     42: This module employs Apache Basic Auth authentication and is used
                     43: to provide authentication for webDAV client access to author
                     44: space(s). Note: because Apache Basic Auth is used, webDAV access
                     45: is only available for servers running Apache with SSL.
                     46: 
                     47: If the webDAV client supports cookies then whenever the client  
                     48: sends the cookie back, a check is made to see if a corresponding
                     49: valid session file exists in the server's webDAV session directory.
                     50: Support for cookies by webDAV clients is optional.
                     51: 
                     52: If a valid session file exists, a cookie is returned containing
                     53: the session ID,  otherwise a new session file is created and
                     54: returned in the cookie.
                     55: 
                     56: The perlvar "lonDAVsessDir" in /etc/httpd/conf/loncapa_apache.conf
                     57: provides the directory location: /home/httpd/webdav/sessionIDs.
                     58: 
                     59: If the session is stale, or the cookie is missing or invalid, 
                     60: the user is re-challenged for login information. If the perlvar
                     61: lonOtherAuthen has been set, Single Sign On will be used, otherwise
                     62: an Apache Basic Auth request will be sent to the client.
                     63: 
                     64: If Apache Basic Auth is used, successful authentication will
                     65: result in creation of a webDAV session file containing a 
                     66: minimal set of information about the user which will also be 
                     67: loaded into the user's environment.  The environment persists
                     68: until the end of the Apache request, when it is cleaned up
                     69: by lonacc::cleanup, as is the case for requests for non-webdav URLs.
                     70: 
                     71: If a valid session file exists, a cookie is returned containing
                     72: the session ID, otherwise a new session file is created and
                     73: returned in the cookie.
                     74: 
                     75: This is part of the LearningOnline Network with CAPA project
                     76: described at http://www.lon-capa.org.
                     77: 
                     78: =head1 HANDLER SUBROUTINE
                     79: 
                     80: This routine is called by Apache and mod_perl.
                     81: 
                     82: =over 4
                     83: 
                     84: =item *
                     85: 
                     86: Check for valid webDAV session
                     87: 
                     88: =item *
                     89: 
                     90: No session? - if SSO enabled: return DECLINED
                     91: 
                     92: =item *
                     93: 
                     94: No session? - if SSO not enabled: return AUTH_REQUIRED
                     95: which will prompt webDAV client to authenticate user 
                     96: (via Apache Basic Auth).
                     97: 
                     98: =item *
                     99: 
                    100: If authenticated, call &init_webdav_env() to create sessionID
                    101: and populate environment, and send header containing cookie.
                    102: 
                    103: =back
                    104: 
                    105: =head1 NOTABLE SUBROUTINES
                    106: 
                    107: =over
                    108: 
                    109: =item * init_webdav_env()
                    110: 
                    111: =over
                    112: 
                    113: =item *
                    114: 
                    115: Checks for valid session files in webDAV session directory.
                    116: 
                    117: =item * 
                    118: 
                    119: Calls Apache::lonnet::transfer_profile_to_env() to create
                    120: %env for user if session is valid. 
                    121: 
                    122: =item * 
                    123: 
                    124: Otherwise creates new session file and populates %env. 
                    125: 
                    126: =item *
                    127: 
                    128: Session ID file is a GDBM file containing:
                    129: 
                    130: =over
                    131: 
                    132: =item * user.name, user.domain, user.home, user.environment
                    133: 
                    134: =item * user.role entries for:
                    135: 
                    136: active author, co-author, and assistant co-author roles
                    137: in domains hosted on this machine.
                    138: 
                    139: =back
                    140: 
                    141: =back
                    142: 
                    143: =back
                    144: 
                    145: =cut 
                    146: 
                    147: package Apache::lonwebdavauth;
                    148: 
                    149: use strict;
                    150: use GDBM_File;
                    151: use Apache::Constants qw(:common :http :methods);
                    152: use Apache::lonnet;
                    153: use LONCAPA qw(:DEFAULT :match);
                    154: 
                    155: sub handler {
                    156:     my $r = shift;
                    157:     my $now = time;
                    158:     my $timetolive = 600;
                    159:     my $sessiondir=$r->dir_config('lonDAVsessDir');
                    160:     my ($uname,$udom,$uhome);
                    161: 
                    162: # Check for cookie
                    163:     my $handle = &Apache::lonnet::check_for_valid_session($r,'lonDAV');
                    164:     if ($handle ne '') {
                    165:         my $sesstime;
                    166:         ($uname,$udom,$sesstime,$uhome) =  
                    167:             ($handle =~ /^($match_username)_($match_domain)_(\d+)_\d+_(.+)$/);
                    168:         if ($sesstime) {
                    169:             if ($now-$sesstime < $timetolive) {
                    170:                 if (&Apache::lonnet::homeserver($uname,$udom) eq $uhome) {
                    171:                     &Apache::lonnet::transfer_profile_to_env($sessiondir,$handle);
1.2       raeburn   172:                     if (&Apache::lonnet::usertools_access($uname,$udom,'webdav')) {
                    173:                         return OK;
                    174:                     } else {
                    175:                         return FORBIDDEN;
                    176:                     }
1.1       raeburn   177:                 }
                    178:             }
                    179:         }
                    180:     }
                    181: 
                    182:     if ($r->dir_config('lonOtherAuthen') eq 'yes') {
                    183:         if (defined($r->dir_config('lonOtherAuthenType'))) {
                    184:             $r->auth_type($r->dir_config('lonOtherAuthenType'));
                    185:         }
                    186:         return DECLINED;
                    187:     }
                    188: 
                    189:     my ($status,$upass) = $r->get_basic_auth_pw;
                    190:     return $status unless ($status == 0 || $status == OK);
                    191: 
                    192:     if ($r->user =~ /,/) {
                    193:         ($uname,$udom) = split(/,/,$r->user);
                    194:         unless (($uname =~ /^$match_username$/) && ($udom =~ /^$match_domain$/)) {
                    195:             $r->note_basic_auth_failure;
                    196:             return AUTH_REQUIRED;
                    197:         }
                    198:     } else {
                    199:         $uname = $r->user;
                    200:         ($udom) = ($r->uri =~ m{^/webdav/($match_domain)/});
                    201:         unless (($udom ne '' ) && ($uname =~ /^$match_username$/)) {
                    202:             $r->note_basic_auth_failure;
                    203:             return AUTH_REQUIRED;
                    204:         }
                    205:     }
                    206:     if (&Apache::lonnet::domain($udom) ne '') {
                    207:         if (&Apache::lonnet::homeserver($uname,$udom) ne 'no_host') {
                    208:             my $uhome = &Apache::lonnet::authenticate($uname,$upass,$udom);
                    209:             if (($uhome ne 'no_host') && 
                    210:                 (&Apache::lonnet::hostname($uhome) ne '')) {
1.3     ! raeburn   211:                 my ($author) = ($r->uri =~ m{^/webdav/($match_domain/$match_username)/});
        !           212:                 $handle = &init_webdav_env($r,$sessiondir,$uname,$udom,
        !           213:                                            $uhome,$now,$timetolive,$author);
1.1       raeburn   214:                 if ($handle ne '') {
1.2       raeburn   215:                     if (&Apache::lonnet::usertools_access($uname,$udom,'webdav')) {
                    216:                         my $cookie = "lonDAV=$handle; path=/webdav/; secure; HttpOnly;";
                    217:                         $r->header_out('Set-cookie' => $cookie);
                    218:                         $r->send_http_header;
                    219:                         return OK;
                    220:                     } else {
                    221:                         return FORBIDDEN;
                    222:                     }
1.1       raeburn   223:                 }
                    224:             }
                    225:         }
                    226:     }
                    227:     $r->note_basic_auth_failure;
                    228:     return AUTH_REQUIRED;
                    229: }
                    230: 
                    231: sub init_webdav_env {
1.3     ! raeburn   232:     my ($r,$sessiondir,$uname,$udom,$uhome,$now,$timetolive,$author) = @_;
1.1       raeburn   233:     my $handle;
                    234:     my $currnewest = 0;
                    235:     if ($sessiondir ne '') {
                    236:         if (opendir(DIR,$sessiondir)) {
                    237:             while (my $filename=readdir(DIR)) {
                    238:                 if ($filename=~/^($uname\_$udom\_(\d+)\_\d+\_$uhome)\.id$/) {
                    239:                     my $oldhandle = $1;
                    240:                     my $sesstime = $2;
                    241:                     if ($now-$sesstime < $timetolive) {
                    242:                         if ($sesstime > $currnewest) {
                    243:                             $currnewest = $sesstime;
                    244:                             if ($handle ne '') {
                    245:                                 unlink("$sessiondir/$handle.id");
                    246:                             }
                    247:                             $handle = $oldhandle;
                    248:                             next;
                    249:                         }
                    250:                     }
                    251:                     unlink($sessiondir.'/'.$filename);
                    252:                 }
                    253:             }
                    254:             closedir(DIR);
                    255:         }
                    256:     }
                    257:     if ($handle ne '') {
                    258:         &Apache::lonnet::transfer_profile_to_env($sessiondir,$handle);
                    259:         return $handle;
                    260:     } else {
                    261:         my $id = $$.int(rand(10000000));
                    262:         $handle = "$uname\_$udom\_$now\_$id\_$uhome";
                    263:         my $sessionfile = "$sessiondir/$handle.id";
                    264:         if (tie(my %disk_env,'GDBM_File',"$sessionfile",
                    265:                 &GDBM_WRCREAT(),0640)) {
                    266:             $disk_env{'user.name'} = $uname;
                    267:             $disk_env{'user.domain'} = $udom;
                    268:             $disk_env{'user.home'} = $uhome;
1.2       raeburn   269:             my %userenv = &Apache::lonnet::get('environment',['inststatus','tools.webdav'],
                    270:                                                $udom,$uname);
                    271:             my ($tmp) = keys(%userenv);
                    272:             if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                    273:                 $disk_env{'environment.inststatus'} = $userenv{'inststatus'};
                    274:                 $disk_env{'environment.tools.webdav'} = $userenv{'tools.webdav'};
                    275:             }
1.1       raeburn   276:             $disk_env{'user.environment'} = $sessionfile;
                    277:             my $possroles = ['au','ca','aa'];
                    278:             my @possdoms = &Apache::lonnet::current_machine_domains();
                    279:             my %cstr_roles =
                    280:                 &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                    281:                                               undef,$possroles,\@possdoms);
1.2       raeburn   282:             if (keys(%cstr_roles) > 0) {
                    283:                 $disk_env{'user.adv'} = 1;
                    284:                 $disk_env{'user.author'} = 1;
                    285:                 foreach my $item (keys(%cstr_roles)) {
                    286:                     my ($aname,$adom,$role) = split(/:/,$item);
                    287:                     if ($role eq 'au') {
                    288:                         $disk_env{"user.role.$role./$adom/"} = $cstr_roles{$item};
                    289:                     } else {
                    290:                         $disk_env{"user.role.$role./$adom/$aname"} = $cstr_roles{$item};
                    291:                     }
1.1       raeburn   292:                 }
                    293:             }
1.2       raeburn   294:             my %is_adv = ( is_adv => $disk_env{'user.adv'} );
                    295:             my %domdef = &Apache::lonnet::get_domain_defaults($udom);
                    296:             $disk_env{'environment.availabletools.webdav'} =
                    297:                 &Apache::lonnet::usertools_access($uname,$udom,'webdav','reload',undef,
                    298:                                                   \%userenv,\%domdef,\%is_adv);
1.1       raeburn   299:             @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    300:             untie(%disk_env);
1.3     ! raeburn   301:             my $ip;
        !           302:             my $c = $r->connection;
        !           303:             if (ref($c)) {
        !           304:                 $ip = $c->remote_ip;
        !           305:             }
        !           306:             &Apache::lonnet::log($udom,$uname,$uhome,
        !           307:                                  "Login webdav/$author $ip");
1.1       raeburn   308:         }
                    309:         return $handle;
                    310:     }
                    311:     return;
                    312: }
                    313: 
                    314: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>