# HG changeset patch # User Robert McIntyre # Date 1357656129 0 # Node ID 9ff0fcf3492079aa48a2d6e81d560a7d6c322c48 # Parent a75581c89ddea1b4962eb592dcbe03ebff195ecf first draft of pkg.pl complete. diff -r a75581c89dde -r 9ff0fcf34920 src/pkg.pl --- a/src/pkg.pl Tue Jan 08 12:48:59 2013 +0000 +++ b/src/pkg.pl Tue Jan 08 14:42:09 2013 +0000 @@ -1,5 +1,10 @@ #!/usr/bin/env perl + +#this is the root of the package users' homes +$pkg_home_root = "/pkg"; + + if ($ARGV[0] eq "--help"){ print <<"HERE"; @@ -8,7 +13,7 @@ syntax: pkg package-name description pkg --help - + Create a package user for the purpose of compiling a particular package. @@ -17,10 +22,10 @@ You will then be put into the user's home directory where you may proceed with -compiling/installation of the package. +compiling/installing the package. If the user already exists, this is equivalent to - su package-name; cd ~; + su --login package-name Written by Robert McIntyre. This software is free software and is released to the public domain. @@ -28,25 +33,64 @@ HERE exit 0; } - +$" = " "; $pkg_name = $ARGV[0]; $pkg_desc = $ARGV[1]; # Validate pkg_user as a user name. -if (!($pkg_name =~ m/^[A-Za-z][A-Za-z0-9_-]*$/)){ +if (!($pkg_name =~ m/^[a-z_][a-z0-9_-]*\$?$/)){ print "$pkg_name is not a valid user name!\n"; - exit 0; + exit 1; } # Validate description. if ($pkg_desc =~ m/:/){ print "Package Description cannot contain \":\"\n"; - exit 0 + exit 1; } -print "name: $pkg_name\ndesc: $pkg_desc\n"; +$pkg_home = "$pkg_home_root/$pkg_name"; +#set default for $pkg_desc. +if (!($pkg_desc)){$pkg_desc = $pkg_name;} +@pkg_switch_cmd = ("su", "--login", $pkg_name); + +@pkg_create_cmd = + ("useradd", "-c", $pkg_desc, "-d", $pkg_home, + "-g", $pkg_name, "-s", "/bin/bash", + $pkg_name); + +@pkg_create_group_cmd = + ("groupadd", "--force", $pkg_name); + +@pkg_create_home_cmd = + ("install", "-d", "-g", $pkg_name, "-o", $pkg_name, "-m", "700", $pkg_home); + +sub execute{print "@_\n"; system(@_) and die $!;} + +#determine if the user already exists: +`id $pkg_name 2>/dev/null`; +if (!$?){execute(@pkg_switch_cmd); exit 0;} + +print "Creating package group.\n"; +execute(@pkg_create_group_cmd); + +print "Creating package user.\n"; +execute(@pkg_create_cmd); + +print "Create home directory for $pkg_name.\n"; +execute(@pkg_create_home_cmd); + +print "Change user.\n"; +execute(@pkg_switch_cmd); + + + + + + +