Annotation of loncom/build/add_domain_coordinator_privilege.pl, revision 1.9

1.1       albertel    1: #!/usr/bin/perl
                      2: 
                      3: =pod
                      4: 
                      5: =head1 NAME
                      6: 
                      7: add_domain_coordinator_privilege.pl - Add domain coordinator to an
                      8: exisiting user on a LON-CAPA system.
                      9: 
                     10: =cut
                     11: 
                     12: # The LearningOnline Network
                     13: #
                     14: # add_domain_coordinator_privilege.pl - Add domain coordinator to an
                     15: #    exisiting user on a LON-CAPA system.
                     16: #
1.9     ! raeburn    17: # $Id: add_domain_coordinator_privilege.pl,v 1.8 2012/08/17 22:43:03 raeburn Exp $
1.1       albertel   18: #
                     19: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     20: #
                     21: # LON-CAPA is free software; you can redistribute it and/or modify
                     22: # it under the terms of the GNU General Public License as published by
                     23: # the Free Software Foundation; either version 2 of the License, or
                     24: # (at your option) any later version.
                     25: #
                     26: # LON-CAPA is distributed in the hope that it will be useful,
                     27: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     28: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     29: # GNU General Public License for more details.
                     30: #
                     31: # You should have received a copy of the GNU General Public License
                     32: # along with LON-CAPA; if not, write to the Free Software
                     33: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     34: #
                     35: # /home/httpd/html/adm/gpl.txt
                     36: #
                     37: # http://www.lon-capa.org/
                     38: #
                     39: ###
                     40: 
                     41: =pod
                     42: 
                     43: =head1 DESCRIPTION
                     44: 
                     45: Automates the steps for domain coordinator creation.  This
                     46: program also describes a manual procedure (see below).
                     47: 
                     48: These are the steps that are executed on the linux operating system:
                     49: 
                     50: =over 4
                     51: 
                     52: =item * 
                     53: 
                     54: Tests to see if user already exists for LON-CAPA, if not it aborts. 
                     55: 
                     56: =item *
                     57: 
                     58: Set roles.hist and roles.db
                     59: 
                     60: =back
                     61: 
                     62: =cut
                     63: 
                     64: # NOTE: I am interspersing the manual procedure with the automation.
                     65: # To see the manual procedure, do perldoc ./make_domain_coordinator.pl
                     66: 
                     67: # This is a standalone script.  It *could* alternatively use the
                     68: # lcuseradd script, however lcuseradd relies on certain system
                     69: # dependencies.  In order to have a focused performance, I am trying
                     70: # to avoid system dependencies until the LON-CAPA code base becomes
                     71: # more robust and well-boundaried.  make_domain_coordinator.pl should be able
                     72: # to run freely as possible, irrespective of the status of a LON-CAPA
                     73: # installation.
                     74: 
                     75: # ---------------------------------------------------- Configure general values
                     76: 
                     77: use lib '/home/httpd/lib/perl/';
                     78: use LONCAPA;
                     79: use Apache::lonnet;
                     80: use Apache::loncommon;
                     81: use Apache::lonlocal;
1.8       raeburn    82: use Storable qw(nfreeze);
1.1       albertel   83: 
                     84: =pod
                     85: 
                     86: =head1 OPTIONS
                     87: 
                     88: There are no flags to this script.
                     89: 
1.3       raeburn    90: usage: add_domain_coordinator_privilege.pl [USERNAME:DOMAIN] [NEWDOMAIN]
1.1       albertel   91: 
                     92: The first argument specifies the user name domain of an existing user.
                     93: 
                     94: The second argument specifies the domain to add to coordinate.
                     95: 
                     96: =cut
                     97: 
1.6       raeburn    98: my ($user,$add_domain)=(@ARGV);
                     99: my $lang = &Apache::lonlocal::choose_language();
                    100: &Apache::lonlocal::get_language_handle(undef,$lang);
                    101: 
                    102: if ($< != 0) { # Am I root?
                    103:    print(&mt('You must be root in order to assign domain coordinator roles.').
                    104:          "\n");
                    105: }
                    106: 
1.1       albertel  107: # ----------------------------------------------- So, are we invoked correctly?
                    108: # Two arguments or abort
                    109: if (@ARGV!=2) {
1.6       raeburn   110:     print(&mt('usage: [_1]','add_domain_coordinator_privilege.pl [USERNAME:DOMAIN] [NEWDOMAIN]').
1.1       albertel  111: 	"\n");
1.6       raeburn   112:     exit;
1.1       albertel  113: }
                    114: my ($username,$domain)=split(':',$user);
                    115: if (!grep(/^\Q$add_domain\E$/,&Apache::lonnet::current_machine_domains())) {
1.6       raeburn   116:     print(&mt('**** ERROR **** Domain [_1] is unknown.',$add_domain)."\n");
                    117:     exit;
1.1       albertel  118: }
                    119: 
                    120: my $udpath=&propath($domain,$username);
                    121: if (!-d $udpath) {
1.6       raeburn   122:     print(&mt('**** ERROR **** [_1] is NOT already defined as a LON-CAPA '.
                    123: 	 'user.',$user)."\n");
                    124:     exit;
1.1       albertel  125: }
                    126: 
                    127: =pod
                    128: 
                    129: =head1 MANUAL PROCEDURE
                    130: 
                    131: There are 2 steps to manually recreating what this script performs
                    132: automatically.
                    133: 
                    134: You need to decide on two pieces of information
                    135: to create a domain coordinator.
                    136: 
                    137:  * USERNAME (kermit, albert, joe, etc)
                    138:  * DOMAIN (should be a domain for thsi machine from domain.tab)
                    139: 
                    140: The examples in these instructions will be based
                    141: on two example pieces of information:
                    142: 
                    143:  * USERNAME=dc103
                    144:  * DOMAIN=103
                    145: 
                    146: You will also need to know your "root" password
                    147: or your "www" password.
                    148: 
                    149: =over 4
                    150: 
                    151: =pod
                    152: 
                    153: =item 1. (as www). Run CVS:loncapa/doc/rolesmanip.pl:
                    154: 
                    155:  Command: [prompt %] perl rolesmanip.pl NEWDOMAIN USERNAME
                    156:  Example: [prompt %] perl rolesmanip.pl 103 dc103
                    157: 
                    158: =cut
                    159: 
                    160: use GDBM_File; # A simple key-value pairing database.
                    161: 
                    162: my $rolesref=&LONCAPA::locking_hash_tie("$udpath/roles.db",&GDBM_WRCREAT());
                    163: if (!$rolesref) {
1.6       raeburn   164:     print(&mt('unable to tie [_1]',"roles db: $udpath/roles.db")."\n");
                    165:     exit;
1.1       albertel  166: }
1.5       raeburn   167: my $status;
                    168: my $now = time;
1.1       albertel  169: if (exists($rolesref->{'/'.$add_domain.'/_dc'})) {
                    170:     my ($role,$end,$start) = split('_',$rolesref->{'/'.$add_domain.'/_dc'});
1.3       raeburn   171:     print(&mt("[_1] already has a dc privilege for [_2].",
1.1       albertel  172: 	      $user,$add_domain)."\n");
                    173:     if ($start) {
                    174: 	print(&mt("Start date: [_1]",&Apache::lonlocal::locallocaltime($start)).
                    175: 	      "\n");
                    176: 	if (!$end) {
                    177: 	    print(&mt("No planned end date.")."\n");
1.5       raeburn   178: 	} else {
                    179:             print(&mt("End date: [_1]",&Apache::lonlocal::locallocaltime($end)).
                    180:                   "\n");
                    181:         }
                    182: 	if (($start <= $now) && (!$end || $end > $now)) {
                    183: 	    print(&mt("It is currently active.")."\n");
                    184: 	    $status = 'active';
1.1       albertel  185: 	}
                    186:     } elsif ($end) {
                    187: 	print(&mt("End date: [_1]",&Apache::lonlocal::locallocaltime($end)).
                    188: 	      "\n");
1.5       raeburn   189: 	if ($end > $now) {
1.1       albertel  190: 	    print(&mt("It is currently active.")."\n");
1.5       raeburn   191: 	    $status = 'active';
1.1       albertel  192: 	}
                    193:     }
1.5       raeburn   194:     if ((!$start) && (!$end)) {
1.1       albertel  195: 	print(&mt("It is currently active.")."\n");
1.5       raeburn   196: 	$status = 'active';
1.1       albertel  197:     }
1.5       raeburn   198:     unless ($status eq 'active') {
                    199:         print(&mt("It is currently not active. Proceeding to make role active now.")."\n");
                    200:     }
                    201: }
                    202: 
                    203: if ($status eq 'active') {
                    204:     &LONCAPA::locking_hash_untie($rolesref);
                    205:     exit(0);
1.1       albertel  206: }
                    207: 
1.3       raeburn   208: my $now = time;
                    209: $rolesref->{'/'.$add_domain.'/_dc'}='dc_0_'.$now; # Set the domain coordinator role.
1.1       albertel  210: open(OUT, ">$udpath/roles.hist"); # roles.hist is the synchronous plain text.
                    211: foreach my $key (keys(%{$rolesref})) {
                    212:     print(OUT $key.' : '.$rolesref->{$key}."\n");
                    213: }
                    214: close(OUT);
                    215: &LONCAPA::locking_hash_untie($rolesref);
                    216: 
                    217: 
                    218: `chown www:www $udpath/roles.hist`; # Must be writeable by httpd process.
                    219: `chown www:www $udpath/roles.db`; # Must be writeable by httpd process.
                    220: 
1.3       raeburn   221: my %perlvar = %{&LONCAPA::Configuration::read_conf('loncapa.conf')};
                    222: my $dompath = $perlvar{'lonUsersDir'}.'/'.$domain;
                    223: my $domrolesref = &LONCAPA::locking_hash_tie("$dompath/nohist_domainroles.db",&GDBM_WRCREAT());
                    224: 
                    225: if (!$domrolesref) {
1.6       raeburn   226:     print(&mt('unable to tie [_1]',"nohist_domainroles db: $dompath/nohist_domainroles.db")."\n");
                    227:     exit;
1.3       raeburn   228: }
                    229: 
                    230: # Store in nohist_domainroles.db
                    231: my $domkey=&LONCAPA::escape('dc:'.$username.':'.$domain.'::'.$domain.':');
                    232: $domrolesref->{$domkey}= &LONCAPA::escape('0:'.$now);
                    233: &LONCAPA::locking_hash_untie($domrolesref);
                    234: 
1.6       raeburn   235: system('/bin/chown',"www:www","$dompath/nohist_domainroles.db"); # Must be writeable by httpd process.
                    236: system('/bin/chown',"www:www","$dompath/nohist_domainroles.db.lock");
1.4       raeburn   237: 
1.8       raeburn   238: # Log with domainconfiguser in nohist_rolelog.db
                    239: my $domconfiguser = $domain.'-domainconfig';
                    240: my $subdir = $domconfiguser;
1.9     ! raeburn   241: $subdir =~ s/^(.)(.)(.).*$/$1\/$2\/$3/;
1.8       raeburn   242: 
                    243: my $rolelogref = &LONCAPA::locking_hash_tie("$dompath/$subdir/$domconfiguser/nohist_rolelog.db",&GDBM_WRCREAT());
                    244: my $domlogkey = &LONCAPA::escape($now.'00000'.$$.'000000');
                    245: my $storehash = {
                    246:                    role    => 'dc',
                    247:                    start   => $now,
                    248:                    end     => 0,
                    249:                    context => 'server',
                    250:                 };
                    251: my $domlogvalue = {
                    252:                     'exe_uname' => '',
                    253:                     'exe_udom'  => $domain,
                    254:                     'exe_time'  => $now,
                    255:                     'exe_ip'    => '127.0.0.1',
                    256:                     'delflag'   => '',
                    257:                     'logentry'  => $storehash,
                    258:                     'uname'     => $username,
                    259:                     'udom'      => $domain,
                    260:                  };
                    261: $rolelogref->{$domlogkey}=&freeze_escape($domlogvalue);
                    262: &LONCAPA::locking_hash_untie($rolelogref);
                    263: 
                    264:  system('/bin/chown',"www:www","$dompath/$subdir/nohist_rolelog.db"); # Must be writeable by httpd process.
                    265:  system('/bin/chown',"www:www","$dompath/$subdir/nohist_rolelog.db.lock");
                    266: 
1.1       albertel  267: =pod
                    268: 
                    269: =item 2.
                    270: 
                    271: You may further define the domain coordinator user (i.e. dc103)
                    272: by going to http://MACHINENAME/adm/createuser.
                    273: 
                    274: =cut
                    275: 
                    276: # Output success message, and inform sysadmin about how to further proceed.
1.6       raeburn   277: print(&mt('[_1] is now a domain coordinator for [_2].',$username,$add_domain).
                    278:       "\n");
1.7       raeburn   279: exit;
1.1       albertel  280: 
1.8       raeburn   281: sub freeze_escape {
                    282:     my ($value)=@_;
                    283:     if (ref($value)) {
                    284:         $value=&nfreeze($value);
                    285:         return '__FROZEN__'.&LONCAPA::escape($value);
                    286:     }
                    287:     return &LONCAPA::escape($value);
                    288: }
                    289: 

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