ianw1974 Posted March 3, 2006 Report Share Posted March 3, 2006 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. Quote Link to comment Share on other sites More sharing options...
paul Posted March 3, 2006 Report Share Posted March 3, 2006 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 Quote Link to comment Share on other sites More sharing options...
ianw1974 Posted March 3, 2006 Author Report Share Posted March 3, 2006 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. Quote Link to comment Share on other sites More sharing options...
paul Posted March 3, 2006 Report Share Posted March 3, 2006 #!/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: Quote Link to comment Share on other sites More sharing options...
ianw1974 Posted March 3, 2006 Author Report Share Posted March 3, 2006 Does that run at the command line? I was wondering if a bash script could do it? Quote Link to comment Share on other sites More sharing options...
neddie Posted March 3, 2006 Report Share Posted March 3, 2006 have you tried $1 and $2 ? I can't check it out at the mo but I thought $1 would be "ian" and $2 would be "domainname" if you called it as above. Quote Link to comment Share on other sites More sharing options...
ianw1974 Posted March 3, 2006 Author Report Share Posted March 3, 2006 Hi Neddie, I'll give that a go, I'm just building a new clean server now, since I've been doing too much testing :P Quote Link to comment Share on other sites More sharing options...
aioshin Posted March 3, 2006 Report Share Posted March 3, 2006 have you tried postfixadmin I think it'll do what you really want, though there's no challenge on your side since you'll not going to do it from scratch , I mean the code.. and its web based, not a command line or bash... just give it a try.. its cool Quote Link to comment Share on other sites More sharing options...
ianw1974 Posted March 3, 2006 Author Report Share Posted March 3, 2006 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. Quote Link to comment Share on other sites More sharing options...
aioshin Posted March 3, 2006 Report Share Posted March 3, 2006 (edited) 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 March 3, 2006 by aioshin Quote Link to comment Share on other sites More sharing options...
ianw1974 Posted March 3, 2006 Author Report Share Posted March 3, 2006 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 Quote Link to comment Share on other sites More sharing options...
aioshin Posted March 3, 2006 Report Share Posted March 3, 2006 have you tried courier-imap? with postfix of course and mysql and postfixadmin... Quote Link to comment Share on other sites More sharing options...
ianw1974 Posted March 3, 2006 Author Report Share Posted March 3, 2006 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 Quote Link to comment Share on other sites More sharing options...
ianw1974 Posted March 3, 2006 Author Report Share Posted March 3, 2006 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 Quote Link to comment Share on other sites More sharing options...
neddie Posted March 4, 2006 Report Share Posted March 4, 2006 (edited) Nice one! 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 March 4, 2006 by neddie Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.