Subject: [LON-CAPA-dev] lcscripts patch

From: Martin Siegert <[email-masked]>
Date: Thu, 7 Apr 2005 15:00:21 -0700

Hi,

here is a tiny contribution towards getting LON-CAPA to work on a SuSE
system: I modified the lc* perl scripts in loncapa-1.3.2/loncom
so that they work on SuSE as well. This is necessary because "useradd"
under SuSE does not create a primary group for each user. The modifications
should not have no effect when the scripts are run on a RH like system.

Cheers,
Martin
--- loncapa-1.3.2/loncom/lchtmldir.orig	2005-03-26 12:43:04.216983659 -0800
+++ loncapa-1.3.2/loncom/lchtmldir	2005-03-29 09:39:24.038656229 -0800
@@ -144,7 +144,7 @@
 if($DEBUG) {
     print("Save username = $safeuser \n");
 }
-if(($username ne $safeuser) or ($safeuser!~/^[A-za-z]/)) {
+if(($username ne $safeuser) or ($safeuser!~/^[A-z]/)) {
     if($DEBUG) {
 	print("User name $username had illegal characters\n");
     }
@@ -185,6 +185,17 @@
         exit 6;
     }
 }
+if ($authentication eq "unix:") {
+    # check whether group $safeuser exists.
+    my $usergroups = `id -nG $safeuser`;
+    if (! grep /^$safeuser$/, split(/\s+/,$usergroups)) { 
+        if($DEBUG) {
+            print("Group \"$safeuser\" does not exist or $safeuser is not a member of that group.\n");
+        }
+        exit 7;
+    }
+}
+
 &EnableRoot;
 
 &System("/bin/mkdir -p $fulldir")   unless (-e $fulldir);
--- loncapa-1.3.2/loncom/lcpasswd.orig	2005-03-26 12:08:14.278942939 -0800
+++ loncapa-1.3.2/loncom/lcpasswd	2005-03-26 12:19:49.730559660 -0800
@@ -159,9 +159,9 @@
 print OUT $password1;
 print OUT "\n";
 close OUT;
-($>,$<)=(0,500);
+($>,$<)=(0,$wwwid);
 
-print "pwchange done, back to uid 500" unless $noprint;
+print "pwchange done, back to uid $wwwid" unless $noprint;
 
 if ($?) {
     exit 8;
@@ -173,7 +173,7 @@
     ($>,$<)=(0,0); # fool smbpasswd here to think this is not a setuid
                    # environment
 
-#   If the -a swithc is put on the smbpasswd
+#   If the -a switch is put on the smbpasswd
 # command line, either a new entry will be created or the old one
 # will be used. 
 # Therefore the old strategy of looking for and adding a dummy entry is 
--- loncapa-1.3.2/loncom/lcuseradd.orig	2005-03-23 18:40:58.761266509 -0800
+++ loncapa-1.3.2/loncom/lcuseradd	2005-03-23 19:20:29.829750960 -0800
@@ -251,6 +251,33 @@
     unlink('/tmp/lock_lcpasswd');
     &Exit(12);
 }
+my ($dmy1, $dmy2, $uid, $gid) = getpwnam $safeusername;
+my $group = `awk -F: '{if (\$3 == '$gid') print \$1}' /etc/group`;
+chomp($group);
+if (! $group) {
+    print "Error. The primary group of user \"$safeusername\" could not be".
+          "determined.\n" unless $noprint;
+    system("/usr/sbin/userdel -r $safeusername");
+    unlink('/tmp/lock_lcpasswd');
+    &Exit(12);
+}
+if ($group != $safeusername) {
+   $status = system("/usr/sbin/groupadd -g $uid $safeusername");
+   if (! $status) {
+      $status = system("/usr/sbin/usermod -g $uid $safeusername");
+      if ($status) {
+         system("/usr/sbin/groupdel $safeusername");
+      }
+   }
+   if ($status) {
+      print "Error.  Something went wrong with the addition of user ".
+            "\"$safeusername\".\n" unless $noprint;
+      system("/usr/sbin/userdel -r $safeusername");
+      unlink('/tmp/lock_lcpasswd');
+      &Exit(12);
+   }
+}
+
 print "Done adding user\n" unless $noprint;
 # Make www a member of that user group.
 my $groups=`/usr/bin/groups www` or &Exit(6);

---

From: Guy Albertelli II <[email-masked]>
Date: Thu, 7 Apr 2005 18:28:14 -0400 (EDT)

Hi Martin,

Instead of 
 my $group = `awk -F: '{if (\$3 == '$gid') print \$1}' /etc/group`;

I went with this instead:
 my ($group) = getgrgid($gid);

Thanks for the work!
-- 
[email-masked]  LON-CAPA Developer  0-7-2-9-

---

From: Martin Siegert <[email-masked]>
Date: Thu, 7 Apr 2005 16:09:47 -0700

Hi Guy,

>  my ($group) = getgrgid($gid);

Amazing. I do not know how many perl scripts I wrote that use the ugly
awk workaround above. Simply because I couldn't find a perl function
that woudl do the thing directly. Now this Guy comes up with "getgrgid".
And it is even in the perl book...
I must be getting blind. Sigh. And thanks. I promise to use it in the
future.

Cheers,
Martin