File:  [LON-CAPA] / loncom / auth / lonwebdavauth.pm
Revision 1.2: download - view: text, annotated - select for diffs
Fri Jun 1 11:39:29 2012 UTC (12 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, HEAD
- webDAV access can be controlled by user status and/or overridden for
  specific users.

    1: # The LearningOnline Network
    2: # Authentication Handler for webDAV access to Authoring Space.
    3: #
    4: # $Id: lonwebdavauth.pm,v 1.2 2012/06/01 11:39:29 raeburn 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: 
   29: =head1 NAME
   30: 
   31: Apache::lonwebdavauth - webDAV Authentication Handler
   32: 
   33: =head1 SYNOPSIS
   34: 
   35: Invoked for /+webdav/[\w\-]+/[\w\-]+/ by 
   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);
  172:                     if (&Apache::lonnet::usertools_access($uname,$udom,'webdav')) {
  173:                         return OK;
  174:                     } else {
  175:                         return FORBIDDEN;
  176:                     }
  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 '')) {
  211:                 $handle = &init_webdav_env($sessiondir,$uname,$udom,
  212:                                            $uhome,$now,$timetolive);
  213:                 if ($handle ne '') {
  214:                     if (&Apache::lonnet::usertools_access($uname,$udom,'webdav')) {
  215:                         my $cookie = "lonDAV=$handle; path=/webdav/; secure; HttpOnly;";
  216:                         $r->header_out('Set-cookie' => $cookie);
  217:                         $r->send_http_header;
  218:                         return OK;
  219:                     } else {
  220:                         return FORBIDDEN;
  221:                     }
  222:                 }
  223:             }
  224:         }
  225:     }
  226:     $r->note_basic_auth_failure;
  227:     return AUTH_REQUIRED;
  228: }
  229: 
  230: sub init_webdav_env {
  231:     my ($sessiondir,$uname,$udom,$uhome,$now,$timetolive) = @_;
  232:     my $handle;
  233:     my $currnewest = 0;
  234:     if ($sessiondir ne '') {
  235:         if (opendir(DIR,$sessiondir)) {
  236:             while (my $filename=readdir(DIR)) {
  237:                 if ($filename=~/^($uname\_$udom\_(\d+)\_\d+\_$uhome)\.id$/) {
  238:                     my $oldhandle = $1;
  239:                     my $sesstime = $2;
  240:                     if ($now-$sesstime < $timetolive) {
  241:                         if ($sesstime > $currnewest) {
  242:                             $currnewest = $sesstime;
  243:                             if ($handle ne '') {
  244:                                 unlink("$sessiondir/$handle.id");
  245:                             }
  246:                             $handle = $oldhandle;
  247:                             next;
  248:                         }
  249:                     }
  250:                     unlink($sessiondir.'/'.$filename);
  251:                 }
  252:             }
  253:             closedir(DIR);
  254:         }
  255:     }
  256:     if ($handle ne '') {
  257:         &Apache::lonnet::transfer_profile_to_env($sessiondir,$handle);
  258:         return $handle;
  259:     } else {
  260:         my $id = $$.int(rand(10000000));
  261:         $handle = "$uname\_$udom\_$now\_$id\_$uhome";
  262:         my $sessionfile = "$sessiondir/$handle.id";
  263:         if (tie(my %disk_env,'GDBM_File',"$sessionfile",
  264:                 &GDBM_WRCREAT(),0640)) {
  265:             $disk_env{'user.name'} = $uname;
  266:             $disk_env{'user.domain'} = $udom;
  267:             $disk_env{'user.home'} = $uhome;
  268:             my %userenv = &Apache::lonnet::get('environment',['inststatus','tools.webdav'],
  269:                                                $udom,$uname);
  270:             my ($tmp) = keys(%userenv);
  271:             if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
  272:                 $disk_env{'environment.inststatus'} = $userenv{'inststatus'};
  273:                 $disk_env{'environment.tools.webdav'} = $userenv{'tools.webdav'};
  274:             }
  275:             $disk_env{'user.environment'} = $sessionfile;
  276:             my $possroles = ['au','ca','aa'];
  277:             my @possdoms = &Apache::lonnet::current_machine_domains();
  278:             my %cstr_roles =
  279:                 &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
  280:                                               undef,$possroles,\@possdoms);
  281:             if (keys(%cstr_roles) > 0) {
  282:                 $disk_env{'user.adv'} = 1;
  283:                 $disk_env{'user.author'} = 1;
  284:                 foreach my $item (keys(%cstr_roles)) {
  285:                     my ($aname,$adom,$role) = split(/:/,$item);
  286:                     if ($role eq 'au') {
  287:                         $disk_env{"user.role.$role./$adom/"} = $cstr_roles{$item};
  288:                     } else {
  289:                         $disk_env{"user.role.$role./$adom/$aname"} = $cstr_roles{$item};
  290:                     }
  291:                 }
  292:             }
  293:             my %is_adv = ( is_adv => $disk_env{'user.adv'} );
  294:             my %domdef = &Apache::lonnet::get_domain_defaults($udom);
  295:             $disk_env{'environment.availabletools.webdav'} =
  296:                 &Apache::lonnet::usertools_access($uname,$udom,'webdav','reload',undef,
  297:                                                   \%userenv,\%domdef,\%is_adv);
  298:             @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  299:             untie(%disk_env);
  300:         }
  301:         return $handle;
  302:     }
  303:     return;
  304: }
  305: 
  306: 1;

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