Date: Thu, 1 Nov 2001 16:03:02 +0300 From: Odhiambo Washington <wash@wananchi.com> To: FBSD-Q <freebsd-questions@freebsd.org> Subject: OT:RemoteUnixUserMgmt.pl - TWO for Perl Gurus Message-ID: <20011101160302.K21700@ns2.wananchi.com>
next in thread | raw e-mail | index | archive | help
--6Nae48J/T25AfBN4 Content-Type: multipart/mixed; boundary="7qSK/uQB79J36Y4o" Content-Disposition: inline --7qSK/uQB79J36Y4o Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello List-ers, I have two Perl Scripts - not written by me. I have to use these two scripts with a proprietary Billing System whose producers give no support for and as I am writing, the linings in our pockets isn't that good to purchase support. These scripts were meant for Linux (sorry but I hate to venture into it) but now I have to find a way of letting them work on FreeBSD. One script, Local= UnixUser.pl will be started by the proprietary billing app when we add a user. It will = then kinda invoke the RemoteUnixUserMgmt.pl, which I am supposed to run on FreeBSD via= inetd. Looking through the TWO scripts, quite a number of things look unfamiliar a= nd as such I am unable to modify them myself for deployment. I'll be very thankful for any input that would help towards this without mu= ch alteration to the basic purpose. I have attached the two, since they are supposed to work together. TIA -Wash S y s t e m s A d m i n i s t r a t o r -- ~\\_ =20 Odhiambo Washington \\\\ =20 Wananchi Online Ltd., `\\\\\ =20 1st Flr Loita Hse, Loita Street |\\\\\ =20 PO Box 10286,00100-NAIROBI,KE. \\\\\|__.--~~\ =20 Fax: 254 2 313985-9 _--~ / =20 Fax: 254 2 313922 /~ ////// _-~~~~' =20 E-mail: wash@wananchi.com ('-//////-// =20 URL : http://www.wananchi.com //////(((-) =20 GSM: 254 72 743 223 / 254 733 744 121 /////" =20 _///" =20 +++ If only one could get that wonderful feeling of accomplishment without having to accomplish anything. --7qSK/uQB79J36Y4o Content-Type: application/x-perl Content-Disposition: attachment; filename="RemoteUnixUserMgmt.pl" Content-Transfer-Encoding: quoted-printable #!/usr/bin/perl -w=0A#*****************************************************= **************=0A#=0A# Name: RemoteUnixUserMgmt.pl=0A#=0A# Description:=0A#= A server side application that is used to add a user to a unix=0A# system.= =0A#=0A# Notes:=0A# This utility should get a line in the following format:= =0A# [Operation] [Root Password] [User Name to add] [User Password] [Group]= =0A# In order to use this utility, the Unix host should have the=0A# follow= ing applications installed:=0A# Perl 5+ - Unix implementation of Perl=0A#= useradd - A Unix commamd line to add a user.=0A# chpasswd - A Un= ix command line to set a user password.=0A#=0A# Bugs:=0A#=0A# See also:=0A#= =0A# Type: Perl Source.=0A#=0A#********************************************= ***********************=0A=0Amy($Operation,$UserId,$UserPw,$UserGr,$InpLine= );=0A=0Achop($InpLine =3D readline(*STDIN));=0A=0Amy(@ARGS) =3D split(/:/,$= InpLine);=0A=0A$Operation =3D $ARGS[0];=0A=0A&checkAdminPass($ARGS[1]);=0A= =0Aif (lc($Operation) eq "add") {=0A if ($#ARGS < 4) {=0A &exitRC(1,"Not= enough parameters for add, expected 5.");=0A }=0A else {=0A $UserId =3D $= ARGS[2];=0A $UserPw =3D $ARGS[3];=0A $UserGr =3D $ARGS[4];=0A my($UserEx= ) =3D getpwnam($UserId) && &exitRC(1,"User already exists.");=0A system("p= w useradd $UserId -s '/nonexistent'") && &exitRC(1,"Failed adding user.") |= | system("echo '$UserPw' | pw usermod $UserId -h 0") && &exitRC(2,"Failed s= etting user password.") || &exitRC(0);=0A }=0A}=0Aelsif (lc($Operation) eq = "del") {=0A if ($#ARGS < 2) {=0A &exitRC(1,"Not enough parameters for del,= expected 3.");=0A }=0A else {=0A $UserId =3D $ARGS[2];=0A my($UserEx) = =3D getpwnam($UserId) || &exitRC(1,"Cannot delete user, user does not exist= .");=0A system("userdel $UserId") && &exitRC(1,"Failed removing user.") ||= exitRC(0);=0A }=0A}=0Aelsif (lc($Operation) eq "pwd") {=0A if ($#ARGS < 3)= {=0A &exitRC(1,"Not enough parameters for pwd, expected 4.");=0A }=0A el= se {=0A $UserId =3D $ARGS[2];=0A $UserPw =3D $ARGS[3];=0A my($UserEx) = =3D getpwnam($UserId) || &exitRC(1,"Cannot change password, user does not e= xist.");=0A system("echo $UserId:$UserPw | chpasswd") && &exitRC(2,"Failed= changing password.") || &exitRC(0);=0A }=0A}=0Aelse {=0A &exitRC(1,"Unknow= n operation!");=0A}=0A=0Asub exitRC() {=0A local($RC,$Mesg) =3D @_;=0A if (= $RC > 0) {=0A print "Status=3D$RC\nErrorMessage=3DLocalUnixUser Error:$Mes= g\n";=0A if ($RC =3D=3D 2) {=0A system("userdel $UserId") && print "Fail= ed to remove user!\n";=0A }=0A exit($RC);=0A }=0A else {=0A print "Opera= tion completed successfully\n";=0A exit(0);=0A }=0A}=0A=0Asub getShadowPwd= {=0A local($UID) =3D @_;=0A open(SHADOW, "</etc/shadow");=0A local($Sha= dPassword) =3D "*";=0A while (defined(SHADOW) and $Line =3D <SHADOW> and $= ShadPassword eq "*") {=0A if ($Line =3D~ /(.*):(.*):.*:.*:.*:.*:.*:.= *:.*/ ) {=0A if ($1 eq $UID) {=0A $ShadPassword = =3D $2;=0A }=0A }=0A }=0A close (SHADOW);=0A return ($S= hadPassword);=0A}=0A=0Asub checkAdminPass {=0A local($Slt,$Crp,$Real,$User)= ;=0A local($Pwd) =3D @_;=0A=0A $User =3D (getpwuid($<))[0];=0A $Real =3D (= getpwuid($<))[1];=0A=0A if (length($Real) < 3) {=0A $Real =3D &getShadowPw= d($User);=0A }=0A=0A $Slt =3D $Real;=0A $Crp =3D crypt($Pwd, $Slt);=0A=0A i= f ($Crp ne $Real) {=0A &exitRC(1,"Insufficient rights.");=0A }=0A}=0A=0A --7qSK/uQB79J36Y4o Content-Type: application/x-perl Content-Disposition: attachment; filename="LocalUnixUser.pl" Content-Transfer-Encoding: quoted-printable #!/usr/bin/perl -w=0A#*****************************************************= **************=0A#=0A# Name: LocalUnixUser.pl=0A#=0A#=0A# Description:=0A# = A local application that is used to add a user to a unix=0A# system.=0A#=0A= # Notes:=0A# This utility should get a line in the following format:=0A# [O= peration] [User Name to add] [User Password] [Group]=0A# In order to use th= is utility, the Unix host should have the=0A# following applications instal= led:=0A# Perl 5+ - Unix implementation of Perl=0A# useradd - A Unix com= mamd line to add a user.=0A# chpasswd - A Unix command line to set a = user password.=0A#=0A# Bugs:=0A#=0A# See also:=0A#=0A# Type: Perl Source.= =0A#=0A#*******************************************************************= =0A=0Amy($Operation,$UserId,$UserPw,$UserGr);=0A=0A$Operation =3D $ARGV[0];= =0A=0Aif (lc($Operation) eq "add") {=0A if ($#ARGV < 3) {=0A &exitRC(1,"= Not enough parameters for add, expected 3.");=0A }=0A else {=0A $UserId = =3D $ARGV[1];=0A $UserPw =3D $ARGV[2];=0A $UserGr =3D $ARGV[3];=0A my($U= serEx) =3D getpwnam($UserId) && &exitRC(1,"User already exists.");=0A syst= em("useradd -g $UserGr $UserId") && &exitRC(1,"Failed adding user.") || sys= tem("echo $UserId:$UserPw | chpasswd") && &exitRC(2,"Failed setting user pa= ssword.") || &exitRC(0);=0A }=0A}=0Aelsif (lc($Operation) eq "del") {=0A if= ($#ARGV < 1) {=0A &exitRC(1,"Not enough parameters for del, expected 2.")= ;=0A }=0A else {=0A $UserId =3D $ARGV[1];=0A my($UserEx) =3D getpwnam($U= serId) || &exitRC(1,"Cannot delete user, user does not exist.");=0A system= ("userdel $UserId") && &exitRC(1,"Failed removing user.") || exitRC(0);=0A = }=0A}=0Aelsif (lc($Operation) eq "pwd") {=0A if ($#ARGV < 2) {=0A &exitRC(= 1,"Not enough parameters for pwd, expected 3.");=0A }=0A else {=0A $UserI= d =3D $ARGV[1];=0A $UserPw =3D $ARGV[2];=0A my($UserEx) =3D getpwnam($Use= rId) || &exitRC(1,"Cannot change password, user does not exist.");=0A syst= em("echo $UserId:$UserPw | chpasswd") && &exitRC(2,"Failed changing passwor= d.") || &exitRC(0);=0A }=0A}=0Aelse {=0A &exitRC(1,"Unknown operation!");= =0A}=0A=0Asub exitRC() {=0A local($RC,$Mesg) =3D @_;=0A if ($RC > 0) {=0A = print "Status=3D$RC\nErrorMessage=3DLocalUnixUser Error:$Mesg\n";=0A if ($= RC =3D=3D 2) {=0A system("userdel $UserId") && print "Failed to remove us= er!\n";=0A }=0A exit($RC);=0A }=0A else {=0A print "Operation completed = successfully\n";=0A exit(0);=0A }=0A}=0A=0A --7qSK/uQB79J36Y4o-- --6Nae48J/T25AfBN4 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (FreeBSD) Comment: For info see http://www.gnupg.org iD8DBQE74UgGn7LIsuxjem8RAlsrAKCcEIDZ/QhX/beV7QfPK1mTzg+e2QCgkcOq u02OxQ/FYwVXwbBNT7bonc4= =Ox9a -----END PGP SIGNATURE----- --6Nae48J/T25AfBN4-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20011101160302.K21700>