How to add samba users to a Samba server using a script.
Specifically, to the Samba server in Excercise 2.2.3 of Samba 3 by Example .
(An on-line version of Samba 3 by Example is at:
http://us2.samba.org/samba/docs/man/Samba-Guide/)
The excercise gives a table of 10 users in Table 2.1.
Table 2.1. Accounting Office Network Information
| User | Login-ID | Password | Share Name | Directory | Wkst |
|---|
| Alan Meany | alan | alm1961 | alan | /data | PC1 |
| James Meany | james | jimm1962 | james | /data/james | PC2 |
| Jeannie Meany | jeannie | jema1965 | jeannie | /data/jeannie | PC3 |
| Suzy Millicent | suzy | suzy1967 | suzy | /data/suzy | PC4 |
| Ursula Jenning | ujen | ujen1974 | ursula | /data/ursula | PC5 |
| Peter Pan | peter | pete1984 | peter | /data/peter | PC6 |
| Dale Roland | dale | dale1986 | dale | /data/dale | PC7 |
| Bertrand E Paoletti | eric | eric1993 | eric | /data/eric | PC8 |
| Russell Lewis | russ | russ2001 | russell | /data/russell | PC9 |
We are given 14 steps to complete the
"Migration from Windows NT4 Workstation System to Samba-3".
We are instructed to do the following for each user:
6. For each user who uses this system (see Table 2.1), execute the following:
root# useradd -m -G accts -c "Name of User" "LoginID"
root# passwd "LoginID"
Changing password for user "LoginID"
New Password: XXXXXXXXX <-- the password from the table
Retype new password: XXXXXXXXX
root# smbpasswd -a "LoginID"
New SMB password: XXXXXXXXX <-- the password from the table
Retype new SMB password: XXXXXXXXX
Added user "LoginID"
7. Create the directory structure for the file shares by executing the following:
root# mkdir -p /data
root# chown alan /data
root# for i in james suzy ursula peter dale eric jeannie russell
> do
> mkdir -p /data/$i
> chown $i /data/$i
> done
root# chgrp -R accts /data
root# chmod -R ug+rwxs,o-r+x /data
13. Verify that the files are being copied correctly from the Windows NT4 machine to the Samba-3 server. This is best done on the Samba-3 server. Check the contents of the directory tree under /data. This can be done by executing the following command:
root# ls -aR /data
Make certain to check the ownership and permissions on all files. If in doubt, execute
the following:
root# chown alan /data
root# for i in james suzy ursula peter dale eric jeannie russell
> do
> chown $i /data/$i
> done
root# chgrp -R accts /data
root# chmod -R ug+rwxs,o-r+x /data
The set exercise calls for an RH 9 server.
Your author runs Debian Woody on his server.
So the current version of this paper is confined to these two distributions.
Fortunately for diversity, unfortunately for consistency,
tools bearing the same name vary from distro to distro in their capabilities.
You will have to determine how your distribution behaves.
Steps 6 & 7 can be placed in a script,
provided we know how to interact with useradd, passwd, & smbpasswd.
Let's look at the supplied versions & their capabilities in our selected distros.
| Distro | Command | Version | Documented | Undocumented | Unknown |
| RH 9 | useradd | ??? | -c | | PT method |
| RH 9 | passwd | ??? | --stdin | | encrypted |
| RH 9 | smbpasswd | 2.2.7a | PTA | | encrypted |
| Woody | useradd | ??? | -c | | PT method |
| Woody | passwd | ??? | --stdin does not work | any method |
| Woody | smbpasswd | ??? | | PTA | encrypted |
Notes
- "???" in Version indicates no response to
-v, -V, or --version.
This indicates the need for a mandate to provide a version option in all commands,
preferably a uniform one like -V.
- PTA = Plain Text command line Argument
A secure method to add samba users & their passwords would be to provide
a table of user information which includes the encrypted password.
There are two flaws in this approach:
- Even though
useradd can accept an encrypted password, there is no documented way for smbpasswd to do this.
- All the documentation I can find on
crypt (RH et al.) & mcrypt (Debian) is abominable. There is the implication that you can pipe a plaintext password through these commands to get an encrypted password for useradd, but no straightforward method for doing so. RTFM...RTFM?...WAFRFM,AH!!
For RH, at least, we will be able to it all in plaintext.
I would capture the user table as a text file (users.txt)
& process it using awk.
I would embed the necessary bash commands in the awk script
using its (awk's) system command:
#! /bin/bash
.
.
.
awk -F: ' /^[^#]/ {
system ( "useradd -m -G accts -c " "\"" $1 "\" " $2) # Step 6 (1)
system ( "echo " $3 "| passwd --stdin " $2) # Step 6 (2)
system ( "smbpasswd " $2 $3 ) # Step 6 (3)
system ( "mkdir -p " $5) # Step 7 (1)
system ( "chown " $2 " " $5) # Step 7 (2)
}' users.txt
chgrp -R accts /data # Step 7 (3)
chmod -R ug+rwxs,o-r+x /data # Step 7 (4)
Here is code to test the above awk script:
# test code
awk -F: ' /^[^#]/ {
print "useradd -m -G accts -c " "\"" $1 "\" " $2
print "echo " $3 " | passwd --stdin " $2
print "smbpasswd " $2 $3
print "mkdir -p " $5
print "chown " $2 " " $5
print ""
}' users.txt | less
Step 13 can be acomplished by re-running the Step 7 code:
awk -F\\t '{
system ( "mkdir -p " $5) # Step 7 & 13
system ( "chown " $2 " " $5) # Step 7 & 13
}' users.txt
chgrp -R accts /data # Step 7 & 13
chmod -R ug+rwxs,o-r+x /data # Step 7 & 13
.
.
.
.
.
.
-
system ( " ")
-- RickArchibald - 21 Mar 2005
|
Revision r1.6 - 22 Mar 2005 - 00:23 GMT - RickArchibald
|
Copyright © 2003-2007
by F. A. Archibald III & the contributing authors
|
| |