Jump to content

Script for adding users and configuring [solved]


Recommended Posts

I need to create a script for adding mailusers. The way my system is set up it needs to do the following tasks, if I was to do them manually:

 

useradd -s /bin/false username
maildirmake /home/username/.maildir
echo 'username@mydomain.com username' >> /etc/postfix/virtual_alias
echo 'username@mydomain.com /home/username/.maildir' >> /etc/postfix/virtual_maps
postmap /etc/postfix/virtual_alias
postmap /etc/postfix/virtual_maps
postfix reload

 

I was thinking of calling the script "newmailuser", and then pass the username, so the script would be ran as follows:

 

newmailuser ian domainname

 

then the two end values would get put in the correct place when the script is ran. Any ideas how the script would need to look? Im not sure how I get the end values placed in the script, the commands are easy, it's just the case of passing the variables if you like.

Link to comment
Share on other sites

I can prolly help :)

I've written mail-admin in php which creates users, add a password, creates a cyrus mailbox, sets saslpasswd, and add all the stuff into a database.

 

My script is only half written, and when complete will be web accessable

Link to comment
Share on other sites

Cool, only I'm not using mysql because of problems under Red Hat, as it doesn't want to work correctly.

 

Although I'm going to try another method now. My main thing is making this easy for normal admins, who can just run the script and pass the two entries at the end and have the rest automated :P

 

Would love to see how a script would work for this.

Link to comment
Share on other sites

#!/usr/bin/php -q
<?
require_once("PEAR.php");
require_once("DB.php");
function lg($txt) {
}

$sbin = "/usr/local/sbin";

$user = "user";
$pass = "pass";
$host = "localhost";
$db_name = "database_name";
$table_name = "mailusers";

$dsn = "mysql://$user:$pass@$host/$db_name";
$db = DB::connect($dsn);
if(DB::isError($db)) {
die ($db->getMessage);
}
lg("DB Connected");

$argc = $_SERVER[argc];
$argv = $_SERVER[argv];

if($argc>1) {
foreach($_SERVER[argv] as $key=>$value) {
	if(strtolower($value)=="help") {
		showMeSomeHelp();
		define('HELP',true);
		exit;
	}
}
} else {
define('HELP',true);
}

function showMeSomeHelp() {
echo $_SERVER['PHP_SELF']." <function> <username> <password> <domain>\n";
echo '
	The "function" argument can be one of:
	add
	del
	update
	help
	list
example: '.$_SERVER['PHP_SELF'].' add NewUser NewPass loudas.com
example: '.$_SERVER['PHP_SELF'].' del UserName
example: '.$_SERVER['PHP_SELF'].' update UserName NewPass

Other Notes of interest:
'.$_SERVER['PHP_SELF'].' help (shows this help)
'.$_SERVER['PHP_SELF'].' list (lists current users and passwords)
  The Passwords are stored in md5 text in a MySQL database
  if that were to get hacked/butchered the password will be incorrect
  Passwords can be verified using:
  '.$_SERVER['PHP_SELF'].' list

  You could test the passwords that are not verified using cyradm
example: cyradmin --user <username> localhost
then enter the password.
but you may spend a long time guessing :)
';
lg("Finished with help");
}


function amIRoot() {
lg("Am I root");
	$userid = exec("whoami");
	if($userid!="root") {
			echo "\n		ERROR: You need to be root to run this\n";
			exit;
	}
} 

function ShowMeTheError($er="Something has gone wrong") {
lg("Error: $er");
	echo "ERROR: $er\n";
	echo "  Usage: ";
showMeSomeHelp();
}

# $params = count($_SERVER[argv]);
# $params = $argc;
# $stdin = fopen('php://stdin', 'r');
amIRoot();
if(!defined('HELP')) { doTheThing(); } else { showMeSomeHelp(); }

function checkParams($argc, $argv, $c) {
lg("Checking params");
if($argc<=$c) {
	$er = "only $argc params, and I was expecting ".$c+1;
	ShowMeTheError($er);
	exit;
}
} 

function doTheThing() {
lg("Starting Main");
global $argc, $argv;
switch($argv[1]) {
	case "add":
		checkParams($argc, $argv, 4);
		add($argc, &$argv);
		break;
	case "del":
		checkParams($argc, $argv, 2);
		del($argc, &$argv);
		break;
	case "update":
		break;
	case "list":
		checkParams($argc, $argv, 1);
		listUsers($argc, &$argv);
		break;
	default:
		$er = "$argv[1] is not a valid function\n";
		ShowMeTheError($er);
}
}

 

There's the beginings ..

I've also completed functions add, del, and list .. but that full of "root only" stuff that I will have to edit out (coz lots o people here have ssh acces on my box :P

 

bleh .. its got all screwed during a post :banghead:

Link to comment
Share on other sites

Unfortunately, I'm not using mysql, I'm using flat text files for the virtual side of things. This is because Red Hat wouldn't work with mysql correctly for the authentication.

 

My virtual lines read hash:/etc/postfix/virtual_alias and/or virtual_maps instead of the normal mysql:/ statements.

 

That's why the script is more suitable for my needs, than web-based, because they can't edit the text files how I want them to. They relate to mysql which I'm not using.

 

But thanks anyway, if I build on another system type I can try mysql and the postfix admin.

Link to comment
Share on other sites

hmmmn.. I wonder why and how come you have a problem with regards to your redhat-mysql-postfix....

.

.

.

edit:

ok, I remember, I made it to work on centos, which is a redhat clone, but I did recompile the postfix for it to support mysql, since the default package does not support it (mysql)

Edited by aioshin
Link to comment
Share on other sites

The problem lies with cyrus-sasl. It doesn't seem to be compiled with mysql support, therefore I can't get courier-authlib to work correctly to allow access.

 

saslauthd can't be configured for it apparently, and I cannot remove cyrus-sasl to be able to configure it with mysql support. At least I think so, I'm just installing one more time from clean now to make doubly sure this is the case.

 

Otherwise I can get partially working with mysql for the virtual domain management, but it relies on users being configured using "useradd" to get them on the system, instead of being stored within mysql.

 

EDIT: I did the same for postfix, but I have a feeling it's related to cyrus-sasl, but I'm checking now in case I did something wrong :P

Link to comment
Share on other sites

After a reinstall today, I managed to get it working. I had what I thought were all the components, using courier-authlib, etc, but I'd missed courier-authlib-mysql from the rpms when I did the rpmbuild.

 

As soon as I'd installed this, the mysql stuff started working, so I can now do it both ways :P

 

Not tested the script yet as I was due to do that shortly, but it will come in handy for part of the user creation stage.

 

EDIT:

 

Just tested the script, the $1 and $2 values work a treat :P

Link to comment
Share on other sites

Oh, here's the working script:

 

[ian@esprit ~]$ cat newmailuser
#/bin/bash
# script to create new mail users
#
echo Creating user	  ..... $1
echo For Email use on   ..... $2

# Create Linux User
useradd -s /bin/false $1

# Create Mail Directory
maildirmake /home/$1/.maildir

# Update virtual_alias and virtual_maps for Postfix
echo $1@$2 $1 >> /etc/postfix/virtual_alias
echo $1@$2 /home/$1/.maildir >> /etc/postfix/virtual_maps
postmap /etc/postfix/virtual_alias
postmap /etc/postfix/virtual_maps

# Restart Postfix
postfix reload

Link to comment
Share on other sites

Nice one! :thumbs:

It might be nice to check whether any parameters were given - so it doesn't try to create a blank user:

if [ -z "$1" ] || [ -z "$2" ]; then
echo "Two parameters required for this script - username and domain (or some such meaningful help message)";
else
# do what you do
fi

Edited by neddie
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...