Annotation of loncom/build/make_domain_coordinator.pl, revision 1.8

1.1       harris41    1: #!/usr/bin/perl
                      2: 
                      3: =pod
                      4: 
                      5: =head1 NAME
                      6: 
                      7: make_domain_coordinator.pl - Make a domain coordinator on a LON-CAPA system
                      8: 
1.2       harris41    9: =cut
                     10: 
                     11: # The LearningOnline Network
                     12: # make_domain_coordinator.pl - Make a domain coordinator on a system
                     13: #
1.8     ! harris41   14: # $Id: make_domain_coordinator.pl,v 1.7 2002/10/12 16:23:21 harris41 Exp $
1.2       harris41   15: #
                     16: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     17: #
                     18: # LON-CAPA is free software; you can redistribute it and/or modify
                     19: # it under the terms of the GNU General Public License as published by
                     20: # the Free Software Foundation; either version 2 of the License, or
                     21: # (at your option) any later version.
                     22: #
                     23: # LON-CAPA is distributed in the hope that it will be useful,
                     24: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     25: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     26: # GNU General Public License for more details.
                     27: #
                     28: # You should have received a copy of the GNU General Public License
                     29: # along with LON-CAPA; if not, write to the Free Software
                     30: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     31: #
                     32: # /home/httpd/html/adm/gpl.txt
                     33: #
                     34: # http://www.lon-capa.org/
                     35: #
                     36: # YEAR=2002
                     37: # 3/1,3/3,3/4 Scott Harrison
1.7       harris41   38: # 10/12 Scott Harrison
1.2       harris41   39: #
                     40: ###
                     41: 
                     42: =pod
                     43: 
1.1       harris41   44: =head1 DESCRIPTION
                     45: 
                     46: Automates the steps for domain coordinator creation.  This
                     47: program also describes a manual procedure (see below).
                     48: 
                     49: These are the steps that are executed on the linux operating system:
                     50: 
                     51: =over 4
                     52: 
                     53: =item * 
                     54: 
                     55: Tests to see if user already exists for linux system or for
1.7       harris41   56: LON-CAPA, if so aborts.  A message is output that recommends following
                     57: a manual procedure enabling this user if so desired.
1.1       harris41   58: 
                     59: =item *
                     60: 
                     61: Creates a linux system user
                     62: 
                     63: =item *
                     64: 
                     65: Sets password
                     66: 
                     67: =item *
                     68: 
                     69: Creates a LON-CAPA lonUsers directory for user
                     70: 
                     71: =item *
                     72: 
                     73: Sets LON-CAPA password mechanism to be "unix"
                     74: 
                     75: =item *
                     76: 
                     77: Set roles.hist and roles.db
                     78: 
                     79: =back
                     80: 
                     81: =cut
                     82: 
                     83: # NOTE: I am interspersing the manual procedure with the automation.
                     84: # To see the manual procedure, do perldoc ./make_domain_coordinator.pl
                     85: 
                     86: # This is a standalone script.  It *could* alternatively use the
                     87: # lcuseradd script, however lcuseradd relies on certain system
1.7       harris41   88: # dependencies.  In order to have a focused performance, I am trying
                     89: # to avoid system dependencies until the LON-CAPA code base becomes
                     90: # more robust and well-boundaried.  make_domain_coordinator.pl should be able
                     91: # to run freely as possible, irrespective of the status of a LON-CAPA
1.1       harris41   92: # installation.
                     93: 
                     94: # ---------------------------------------------------- Configure general values
                     95: 
1.7       harris41   96: my %perlvar; # Holds network-wide and machine-specific configuration values.
                     97: # We only need one configuration value however, lonUsersDir.  Rather than
                     98: # read this out of loncapa.conf, I am just going to hard-code this for now.
1.1       harris41   99: $perlvar{'lonUsersDir'}='/home/httpd/lonUsers';
                    100: 
                    101: =pod
                    102: 
                    103: =head1 OPTIONS
                    104: 
                    105: There are no flags to this script.
                    106: 
                    107: usage: make_domain_coordinator.pl [USERNAME] [DOMAIN] 
                    108: 
1.3       harris41  109: The password is accepted through standard input
                    110: and should only consist of printable ASCII
                    111: characters and be a string of length greater than 5 characters.
1.1       harris41  112: 
                    113: The first argument
                    114: specifies the user name of the domain coordinator and
                    115: should consist of only alphanumeric characters.
1.8     ! harris41  116: It is recommended that the USERNAME should be institution-specific
        !           117: as opposed to something like "Sammy" or "Jo".
        !           118: For example, "dcmsu" or "dcumich" would be good domain coordinator
        !           119: USERNAMEs for places like Mich State Univ, etc.
1.1       harris41  120: 
1.3       harris41  121: The second argument specifies the domain of the computer
                    122: coordinator and should consist of only alphanumeric characters.
1.1       harris41  123: 
                    124: =cut
                    125: 
                    126: # ----------------------------------------------- So, are we invoked correctly?
                    127: # Two arguments or abort
                    128: if (@ARGV!=2) {
1.8     ! harris41  129:     die('usage: make_domain_coordinator.pl [USERNAME] [DOMAIN] '."\n".
        !           130: 	'(and password through standard input)'."\n".
        !           131: 	'It is recommended that the USERNAME should be institution-specific '.
        !           132: 	"\n".'as opposed to something like "Sammy" or "Jo".'."\n".
        !           133: 	'For example, "dcmsu" or "dcumich" would be good domain coordinator'.
        !           134: 	"\n".'USERNAMEs for places like Mich State Univ, etc.'."\n");
1.1       harris41  135: }
                    136: my ($username,$domain)=(@ARGV); shift @ARGV; shift @ARGV;
                    137: unless ($username=~/^\w+$/ and $username!~/\_/) {
1.7       harris41  138:     die('**** ERROR **** '.
                    139: 	'Username '.$username.' must consist only of alphanumeric characters'.
                    140: 	"\n");
1.1       harris41  141: }
                    142: unless ($domain=~/^\w+$/ and $domain!~/\_/) {
1.7       harris41  143:     die('**** ERROR **** '.
                    144: 	'Domain '.$domain.' must consist only of alphanumeric characters'.
                    145: 	"\n");
1.1       harris41  146: }
                    147: 
1.7       harris41  148: # Output a warning message.
                    149: print('**** NOTE **** '.
                    150:       'Generating a domain coordinator is "serious business".'."\n".
                    151:       'Choosing a difficult-to-guess (and keeping it a secret) password '."\n".
                    152:       'is highly recommended.'."\n");
                    153: 
                    154: print("Password: "); $|=1;
1.1       harris41  155: my $passwd=<>; # read in password from standard input
                    156: chomp($passwd);
                    157: 
                    158: if (length($passwd)<6 or length($passwd)>30) {
1.7       harris41  159:     die('**** ERROR **** '.'Password is an unreasonable length.'."\n".
                    160: 	'It should be at least 6 characters in length.'."\n");
1.1       harris41  161: }
                    162: my $pbad=0;
                    163: foreach (split(//,$passwd)) {if ((ord($_)<32)||(ord($_)>126)){$pbad=1;}}
                    164: if ($pbad) {
1.7       harris41  165:     die('**** ERROR **** '.
                    166: 	'Password must consist of standard ASCII characters'."\n");
1.1       harris41  167: }
                    168: 
                    169: # And does user already exist
                    170: 
1.7       harris41  171: my $caveat =
                    172:     'For security reasons, this script will only automatically generate '."\n".
                    173:     'new users, not pre-existing users.'."\n".
                    174:     "If you want to make '$username' a domain coordinator, you "."\n".
                    175:     'should do so manually by customizing the MANUAL PROCEDURE'."\n".
                    176:     'described in the documentation.  To view the documentation '."\n".
                    177:     'for this script, type '.
                    178:     "'perldoc ./make_domain_coordinator.pl'."."\n";
                    179: 
1.1       harris41  180: if (-d "/home/$username") {
1.7       harris41  181:     die ('**** ERROR **** '.$username.' is already a linux operating system '.
                    182: 	 'user.'."\n".$caveat);
1.1       harris41  183: }
                    184: my $udpath=propath($domain,$username);
                    185: if (-d $udpath) {
1.7       harris41  186:     die ('**** ERROR **** '.$username.' is already defined as a LON-CAPA '.
                    187: 	 'user.'."\n".$caveat);
1.1       harris41  188: }
                    189: 
                    190: =pod
                    191: 
                    192: =head1 MANUAL PROCEDURE
                    193: 
1.7       harris41  194: There are 10 steps to manually recreating what this script performs
                    195: automatically.
1.1       harris41  196: 
                    197: You need to decide on three pieces of information
                    198: to create a domain coordinator.
                    199: 
                    200:  * USERNAME (kermit, albert, joe, etc)
1.6       harris41  201:  * DOMAIN (should be the same as lonDefDomain in /etc/httpd/conf/loncapa.conf)
1.1       harris41  202:  * PASSWORD (don't tell me)
                    203: 
                    204: The examples in these instructions will be based
                    205: on three example pieces of information:
                    206: 
                    207:  * USERNAME=dc103
                    208:  * DOMAIN=103
                    209:  * PASSWORD=sesame
                    210: 
                    211: You will also need to know your "root" password
                    212: and your "www" password.
                    213: 
                    214: =over 4
                    215: 
                    216: =item 1.
                    217: 
                    218: login as root on your Linux system
                    219:  [prompt %] su
                    220: 
                    221: =cut
                    222: 
                    223: # ------------------------------------------------------------ So, are we root?
                    224: 
1.7       harris41  225: if ($< != 0) { # Am I root?
1.1       harris41  226:   die 'You must be root in order to generate a domain coordinator.'."\n";
                    227: }
                    228: 
                    229: =pod
                    230: 
                    231: =item 2 (as root). add the user
                    232: 
                    233:  Command: [prompt %] /usr/sbin/useradd USERNAME
                    234:  Example: [prompt %] /usr/sbin/useradd dc103
                    235: 
                    236: =cut
                    237: 
                    238: # ----------------------------------------------------------- /usr/sbin/useradd
                    239: 
                    240: $username=~s/\W//g; # an extra filter, just to be sure
1.7       harris41  241: `/usr/sbin/useradd $username`; # Add the user with the 'useradd' command.
1.1       harris41  242: 
                    243: =pod
                    244: 
                    245: =item 3 (as root). enter in a password
                    246: 
                    247:  Command: [prompt %] passwd USERNAME
                    248:           New UNIX password: PASSWORD
                    249:           Retype new UNIX passwd: PASSWORD
                    250:  Example: [prompt %] passwd dc103
                    251:           New UNIX password: sesame
                    252:           Retype new UNIX passwd: sesame
                    253: 
                    254: =cut
                    255: 
1.7       harris41  256: # Process password (taint-check, then pass to the UNIX passwd command).
                    257: $username =~ s/\W//g; # an extra filter, just to be sure
                    258: $pbad = 0;
1.1       harris41  259: foreach (split(//,$passwd)) {if ((ord($_)<32)||(ord($_)>126)){$pbad=1;}}
                    260: if ($pbad) {
1.7       harris41  261:     die('Password must consist of standard ASCII characters'."\n");
1.1       harris41  262: }
1.7       harris41  263: open(OUT,"|passwd --stdin $username");
                    264: print(OUT $passwd."\n");
                    265: close(OUT);
1.1       harris41  266: 
                    267: =pod
                    268: 
                    269: =cut
                    270: 
                    271: =pod
                    272: 
                    273: =item 4. login as user=www
                    274: 
                    275:  Command: [prompt %] su www
                    276:  Password: WWWPASSWORD
                    277: 
                    278: =item 5. (as www). cd /home/httpd/lonUsers
                    279: 
                    280: =item 6. (as www) Create user directory for your new user.
                    281: 
                    282:  Let U equal first letter of USERNAME
                    283:  Let S equal second letter of USERNAME
                    284:  Let E equal third letter of USERNAME
                    285:  Command: [prompt %] install -d DOMAIN/U/S/E/USERNAME
1.7       harris41  286: 
                    287:  Here are three examples of the commands that would be needed
                    288:  for different domain coordinator names (dc103, morphy, or ng):
                    289: 
                    290:  Example #1 (dc103):  [prompt %] install -d 103/d/c/1/dc103
                    291:  Example #2 (morphy): [prompt %] install -d 103/m/o/r/morphy
                    292:  Example #3 (ng):     [prompt %] install -d 103/n/g/_/ng
1.1       harris41  293: 
                    294: =cut
                    295: 
1.7       harris41  296: # Generate the user directory.
                    297: `install -o www -g www -d $udpath`; # Must be writeable by httpd process.
1.1       harris41  298: 
                    299: =pod
                    300: 
                    301: =item 7. (as www) Enter the newly created user directory.
                    302: 
                    303:  Command: [prompt %] cd DOMAIN/U/S/E/USERNAME
                    304:  Example: [prompt %] cd 103/d/c/1/dc103
                    305: 
                    306: =item 8. (as www). Set your password mechanism to 'unix' 
                    307: 
                    308:  Command: [prompt %] echo "unix:" > passwd
                    309: 
                    310: =cut
                    311: 
1.7       harris41  312: # UNIX (/etc/passwd) style authentication is asserted for domain coordinators.
                    313: open(OUT, ">$udpath/passwd");
                    314: print(OUT 'unix:'."\n");
                    315: close(OUT);
                    316: `chown www:www $udpath/passwd`; # Must be writeable by httpd process.
1.1       harris41  317: 
                    318: =pod
                    319: 
                    320: =item 9. (as www). Run CVS:loncapa/doc/rolesmanip.pl:
                    321: 
                    322:  Command: [prompt %] perl rolesmanip.pl DOMAIN USERNAME
                    323:  Example: [prompt %] perl rolesmanip.pl 103 dc103
                    324: 
                    325: =cut
                    326: 
1.7       harris41  327: use GDBM_File; # A simplistic key-value pairing database.
1.1       harris41  328: my %hash;
                    329: 
1.7       harris41  330: tie(%hash,'GDBM_File',"$udpath/roles.db",
                    331:     &GDBM_WRCREAT,0640); # Interface with GDBM database thru a hash variable.
                    332: 
                    333: $hash{'/'.$domain.'/_dc'}='dc'; # Set the domain coordinator role.
                    334: open(OUT, ">$udpath/roles.hist"); # roles.hist is the synchronous plain text.
1.1       harris41  335: map {
1.7       harris41  336:     print(OUT $_.' : '.$hash{$_}."\n");
1.1       harris41  337: } keys %hash;
1.7       harris41  338: close(OUT);
                    339: untie(%hash); # Finish interfacing with GDBM database.
1.1       harris41  340: 
1.7       harris41  341: `chown www:www $udpath/roles.hist`; # Must be writeable by httpd process.
                    342: `chown www:www $udpath/roles.db`; # Must be writeable by httpd process.
1.1       harris41  343: 
                    344: =pod
                    345: 
                    346: =item 10.
                    347: 
                    348: You may further define the domain coordinator user (i.e. dc103)
                    349: by going to http://MACHINENAME/adm/createuser.
                    350: 
                    351: =cut
                    352: 
1.7       harris41  353: # Output success message, and inform sysadmin about how to further proceed.
                    354: print("$username is now a domain coordinator\n"); # Output success message.
                    355: my $hostname=`hostname`; chomp($hostname); # Read in hostname.
                    356: print("http://$hostname/adm/createuser will allow you to further define".
                    357:       " this user.\n"); # Output a suggested URL.
1.1       harris41  358: 
1.7       harris41  359: # ================================================================= SUBROUTINES
                    360: # Subroutine propath: take in domain and username, and generate filesystem path
1.1       harris41  361: sub propath {
1.7       harris41  362:     my ($udom,$uname)=@_; # The lonDefDomain, and the domain coord. username.
                    363:     $udom =~ s/\W//g; # Taint removal.
                    364:     $uname =~ s/\W//g; # Taint removal.
                    365:     my $subdir = $uname.'__';
                    366:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/; # The path must have three subdirs.
                    367:     my $proname = "$perlvar{'lonUsersDir'}/$udom/$subdir/$uname"; # Total path.
                    368:     return $proname; # Return the total user directory filesystem path.
1.1       harris41  369: }
                    370: 
                    371: =pod
                    372: 
1.2       harris41  373: =head1 AUTHOR
1.1       harris41  374: 
1.7       harris41  375: Written to help the LON-CAPA project.
                    376: 
                    377: Scott Harrison, sharrison@users.sourceforge.net
1.1       harris41  378: 
                    379: =cut

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