Psema4Topic General Programming

From GTALUG

Sitemap > Culture > Psema4 > Psema4Topic General Programming

Contents

ua-wrapper

Synopsis

As root:

ua-wrapper <login> "<realname>" <pass> <prefs>

Use 'none' for prefs - it's just here as an example.

Description

ua-wrapper is a simple cover script for useradd on Red Hat / Fedora Core systems.

Also demonstrates shell executing commands from perl and a method for setting up databases and such.

Security Notice

There are some security concerns with this example. Namely:

  • Anyone with access to the user information in the database can get the users system password.
  • In this example, ALL USERS get access to ALL DATABASES on localhost - including the above table.
    • This /needs/ to be fixed.
  • Installing the script without setting the proper file mode (chmod) would NOT be a good idea.

~Fixme: Need disclaimer or pull the script from the site.

Installation

  • Copy the script into an editor, and save as /tmp/ua-wrapper
  • As root:
    1. mv /tmp/ua-wrapper /usr/local/sbin/ua-wrapper
    2. chown root.root /usr/local/sbin/ua-wrapper
    3. chmod 740 /usr/local/sbin/ua-wrapper

Script

#!/usr/bin/perl

#
# ua-wrapper  -  Wrapper script for useradd
#

# NOTE:  You MUST set the MySQL administrator info below before running!

# There are better ways to handle parameters... and lots of other parts
#  of this script.
if (($#ARGV + 1) != 4) { usage_error() };

# Get parameters (username, realname, pass, prefs)
$user = shift;
$realname = shift;
$pass = shift;
$prefs = shift;


# Get envivronment info
$cmd_useradd = `which useradd`;	chomp($cmd_useradd);
$cmd_passwd = `which passwd`;	chomp($cmd_passwd);
$cmd_mysql = `which mysql`;	chomp($cmd_mysql);
$cmd_echo = `which echo`;	chomp($cmd_echo);


# Set the MySQL administrator info
$admin_user = 'REPLACE-ME';  #replace with your admin user
$admin_pass = 'REPLACE-ME';  #replace with your admin pass

# Create a database with the name of the user
$sqldb = qq[CREATE DATABASE $user];

# Create your default db structure here.
$sqldb1 = qq[USE $user; CREATE TABLE users (name VARCHAR(8), realname VARCHAR(64), preferences VARCHAR(255))];
$sqldb2 = qq[USE $user; INSERT INTO users VALUES('$user', '$realname', '$prefs')];

# Grant *all privileges* on *all databases* to that user
# * Read the docs thoroughly for security and adminstration info
#   http://dev.mysql.com/doc/mysql/en/adding-users.html
#
$sqluser = qq[GRANT ALL PRIViLEGES ON *.* TO '$user'\@'localhost' IDENTIFIED BY '$pass'];


# set some constants
$no_trap = 0;
$trap = 1;

# execute the steps 
shell_execute($trap,	qq[$cmd_useradd -c "$realname" -m $user]);
shell_execute($no_trap,	qq[$cmd_echo "$pass" | $cmd_passwd --stdin $user]);
shell_execute($trap,	qq[$cmd_mysql -u $admin_user --password=$admin_pass --exec="$sqldb"]);
shell_execute($trap,	qq[$cmd_mysql -u $admin_user --password=$admin_pass --exec="$sqldb1"]);
shell_execute($trap,	qq[$cmd_mysql -u $admin_user --password=$admin_pass --exec="$sqldb2"]);
shell_execute($trap,	qq[$cmd_mysql -u $admin_user --password=$admin_pass --exec="$sqluser"]);

# we're done.
print "Finished.\n\n";
exit;

# Subs 'n funcs
sub usage_error {
	die qq[ usage: ua-wrapper <login> "<realname>" <pass> <prefs>] . "\n";
}

sub shell_execute {
	$trap = shift;
	$cmd = shift; 

	print "-> $cmd\n";
	if ($trap) {
		die if( `$cmd` );
	} else {
		`$cmd`;
	}
}

See Also

MySQL Reference Manual, man useradd, man passwd, man mysql

Todo

  • Bash version?
  • Other languages?
Customize