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

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

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