Annotation of loncom/lcinstallfile, revision 1.4

1.1       foxr        1: #!/usr/bin/perl
                      2: #
1.4     ! raeburn     3: # Copyright Michigan State University Board of Trustees
        !             4: #
        !             5: # $Id: lcinstallfile,v 1.4 2009/05/13 16:00:00 raeburn Exp $
1.1       foxr        6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or 
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: #
                     26: #
1.4     ! raeburn    27: # 2/17/2009 - Ron Fox
1.2       foxr       28: #
                     29: # http://www.lon-capa.org/
1.1       foxr       30: #
                     31: #  This file is a setuid script that allows lond or other www programs to install
                     32: #  a file in the lon capa table directory.
                     33: #
                     34: #  Invocation is as follows:
                     35: #    lcinstallfile  source_file_name dest_name
                     36: #
                     37: #  source_file_name - The full path for the source file.
                     38: #  dest_name        - The destination filename.  This will always be in the
                     39: #                     table file directory for this server.
                     40: #
                     41: 
                     42: 
                     43: use strict;
                     44: 
1.4     ! raeburn    45: use lib "/home/httpd/lib/perl";     # Adjust if loncapa lib isn't installed here.
1.1       foxr       46: use LONCAPA;
                     47: use LONCAPA::Configuration;
                     48: use IO::File;
1.2       foxr       49: use File::Copy;
                     50: 
                     51: 
1.1       foxr       52: 
                     53: #
                     54: # Exit codes:
                     55: #
                     56: # 0    - ok
                     57: # 1    - Initial user ID was not www
                     58: # 3    - Usage error not enough command line arguments.
                     59: # 4    - source_file_name does not exist.
                     60: # 5    - destination file does not exist (not allowed to create new files).
                     61: # 6    - Some file operation failed.
1.2       foxr       62: # 7    - Invalid table filename.
1.1       foxr       63: #
1.3       foxr       64: my $noprint = 1;
1.1       foxr       65: #
                     66: #   Ensure we are www:
                     67: #
                     68: # 
1.3       foxr       69: print ("In lcinstallfile\n") unless $noprint;
1.1       foxr       70: 
                     71: my $wwwid=getpwnam('www');
                     72: &disable_root_capability;
                     73: if ($wwwid!=$>) {
                     74:     print("User ID mismatch.  This program must be run as user 'www'\n")
                     75: 	unless $noprint;
                     76:     exit 1;
                     77: }
                     78: #
                     79: #  Ensure we have the right number of command args:
                     80: #
                     81: my $argc = scalar(@ARGV);
                     82: if ($argc != 2) {
1.3       foxr       83:     print("Usage: lcinstallfile sourcepath destfile had $argc parameters\n") unless $noprint;
1.1       foxr       84:     exit 2;
                     85: }
1.2       foxr       86: my $sourcepath = $ARGV[0];
1.1       foxr       87: my $destfile  = $ARGV[1];
                     88: 
1.3       foxr       89: print("From: $sourcepath to: $destfile\n") unless $noprint;
1.2       foxr       90: 
                     91: 
1.1       foxr       92: # Ensure the source file exists, and root can write it.:
                     93: 
1.2       foxr       94: # since this is a setuid program, the sourcepath and destfile
                     95: # must be pattern extracted else they are considered insecure and
                     96: # therefore not validated.  
                     97: # loncapa table files are all of the form.
                     98: #  something.tab where something is all letters and _'s.
                     99: #
1.3       foxr      100: if ($sourcepath =~ /^([\w\/]+\.\w+)$/) {
1.2       foxr      101:     $sourcepath = $1;
                    102: } else {
1.3       foxr      103:     print ("Invalid characters in filename '$sourcepath' \n") unless $noprint;
1.2       foxr      104:     exit 7;
                    105: }
                    106: 
                    107: 
1.1       foxr      108: if (! -r $sourcepath) {
                    109:     print("File $sourcepath either does not exist or cannot be read") unless $noprint;
                    110:     exit 4;
                    111:     
                    112: }
1.2       foxr      113: &enable_root_capability;
                    114: 
1.1       foxr      115: #
                    116: #  Figure out where the lontab directory is and create the destinationfile name:
                    117: #
                    118: #  We're not allowed to create new files, only replace existing files
                    119: #  so ensure that the final destination file actually exists.
                    120: #
                    121: 
1.2       foxr      122: 
                    123: #
                    124: # Now sanitize the final file:
                    125: 
1.3       foxr      126: my $final_file;
                    127: if ($destfile =~ /^([\w\/]+\.\w+)$/) {
1.2       foxr      128:     $final_file = $1;
                    129: } else {
1.3       foxr      130:     print ("'$final_file' failed regexp match\n") unless $noprint;
1.2       foxr      131:     exit 7;
                    132: }
1.1       foxr      133: 
                    134: if (! -w $final_file) {
                    135:     &disable_root_capability;
                    136:     print("The $final_file is either not writable, or does not exist.\n") unless $noprint;
                    137:     exit 5;
                    138: }
                    139: #
                    140: # Copy the destination file to a backup:
                    141: #
1.2       foxr      142: if (!copy($final_file, $final_file.'.backup')) {
1.1       foxr      143:     &disable_root_capability;
                    144:     print ("Failed to create backup copy of $final_file\n") unless $noprint;
                    145:     exit 6;
                    146: }
1.2       foxr      147: &enable_root_capability;
1.1       foxr      148: 
                    149: #  Install the new file to a temp file in the same dir so it can be mv'd in place
                    150: #  this prevents the possibility we wind up with a partial file.:
                    151: 
1.2       foxr      152: if (!copy($sourcepath, $final_file.'.new')) {
1.1       foxr      153:     &disable_root_capability;
                    154:     print("Failed to copy $sourcepath to a tempfile\n") unless $noprint;
                    155:     exit 6;
                    156: }
                    157: #
                    158: #  Move the temp file to the final file
                    159: #
1.2       foxr      160: if (!rename($final_file.'.new', $final_file)) {
1.1       foxr      161:     &disable_root_capability;
1.2       foxr      162:     print ("Failed to move installed file $final_file.new to final resting place\n")
1.1       foxr      163: 	unless $noprint;
                    164:     exit 6;
                    165: }
                    166: 
                    167: #  Ready to exit with success
                    168: 
1.2       foxr      169: &disable_root_capability;
                    170: print ("$sourcepath installed to $final_file\n") unless $noprint;
1.1       foxr      171: exit 0;
                    172: 
1.2       foxr      173: 
1.1       foxr      174: #-------------------------------------------------------------------------
                    175: #
                    176: #  subs that control the setuid-edness of the program.
                    177: 
1.2       foxr      178: #  have setuid script run as root
1.1       foxr      179: sub enable_root_capability {
                    180:     if ($wwwid==$>) {
                    181: 	($<,$>)=($>,0);
                    182: 	($(,$))=($),0);
1.4     ! raeburn   183:     } else {
1.1       foxr      184: 	# root capability is already enabled
                    185:     }
1.3       foxr      186:     print ("Effective uid = $>\n");
1.1       foxr      187:     return $>;
                    188: }
                    189: 
1.2       foxr      190: #  have setuid script run as www
1.1       foxr      191: sub disable_root_capability {
                    192:     if ($wwwid==$<) {
                    193: 	($<,$>)=($>,$<);
                    194: 	($(,$))=($),$();
1.4     ! raeburn   195:     } else {
1.1       foxr      196: 	# root capability is already disabled
                    197:     }
                    198: }

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