rlm@2: #!/usr/bin/env perl rlm@2: rlm@3: rlm@3: #this is the root of the package users' homes rlm@3: $pkg_home_root = "/pkg"; rlm@3: rlm@3: rlm@2: if ($ARGV[0] eq "--help"){ rlm@2: print <<"HERE"; rlm@2: rlm@2: pkg -- create a package user. rlm@2: rlm@2: syntax: rlm@2: pkg package-name description rlm@2: pkg --help rlm@3: rlm@2: Create a package user for the purpose of compiling rlm@2: a particular package. rlm@2: rlm@2: The user will be created with user and group both rlm@2: equal to the name you specify. rlm@2: rlm@2: You will then be put into the user's home rlm@2: directory where you may proceed with rlm@3: compiling/installing the package. rlm@2: rlm@2: If the user already exists, this is equivalent to rlm@3: su --login package-name rlm@2: rlm@2: Written by Robert McIntyre. This software is free rlm@2: software and is released to the public domain. rlm@2: rlm@2: HERE rlm@2: exit 0; rlm@2: } rlm@3: $" = " "; rlm@2: rlm@2: $pkg_name = $ARGV[0]; rlm@2: rlm@2: $pkg_desc = $ARGV[1]; rlm@2: rlm@2: # Validate pkg_user as a user name. rlm@3: if (!($pkg_name =~ m/^[a-z_][a-z0-9_-]*\$?$/)){ rlm@2: print "$pkg_name is not a valid user name!\n"; rlm@3: exit 1; rlm@2: } rlm@2: rlm@2: # Validate description. rlm@2: if ($pkg_desc =~ m/:/){ rlm@2: print "Package Description cannot contain \":\"\n"; rlm@3: exit 1; rlm@2: } rlm@2: rlm@2: rlm@3: $pkg_home = "$pkg_home_root/$pkg_name"; rlm@2: rlm@3: #set default for $pkg_desc. rlm@3: if (!($pkg_desc)){$pkg_desc = $pkg_name;} rlm@2: rlm@3: @pkg_switch_cmd = ("su", "--login", $pkg_name); rlm@3: rlm@3: @pkg_create_cmd = rlm@3: ("useradd", "-c", $pkg_desc, "-d", $pkg_home, rlm@3: "-g", $pkg_name, "-s", "/bin/bash", rlm@3: $pkg_name); rlm@3: rlm@3: @pkg_create_group_cmd = rlm@3: ("groupadd", "--force", $pkg_name); rlm@3: rlm@3: @pkg_create_home_cmd = rlm@3: ("install", "-d", "-g", $pkg_name, "-o", $pkg_name, "-m", "700", $pkg_home); rlm@3: rlm@4: sub execute{print "\t@_\n"; system(@_) and die $!;} rlm@3: rlm@3: #determine if the user already exists: rlm@3: `id $pkg_name 2>/dev/null`; rlm@4: if (!$?){ rlm@4: print "Change user.\n"; rlm@4: execute(@pkg_switch_cmd); exit 0;} rlm@3: rlm@3: print "Creating package group.\n"; rlm@3: execute(@pkg_create_group_cmd); rlm@3: rlm@3: print "Creating package user.\n"; rlm@3: execute(@pkg_create_cmd); rlm@3: rlm@3: print "Create home directory for $pkg_name.\n"; rlm@3: execute(@pkg_create_home_cmd); rlm@3: rlm@3: print "Change user.\n"; rlm@3: execute(@pkg_switch_cmd); rlm@3: rlm@3: rlm@3: rlm@3: rlm@3: rlm@3: rlm@3: