20 my ($value) = ($option =~ /^[^=]+=\s*(\S.*\S)/ );
30 --infile=<filename> # smbpasswd style file to read entries from
31 --outfile=<filename> # file to dump results to, format depending
33 # With --infile: Dump mysql script queries
34 # Without --infile: Dump smbpasswd format
35 # from reading mysql database
36 --host=<hostname> # Mysql Server name (default: localhost)
37 --db=<database> # Mysql Database name
38 --user=<user> # Mysql User
39 --password[=<password>] # Mysql password for --user
40 --table=<table> # Mysql table
41 --create # Generate 'create table' query
42 --file=[trash|append] # Action to take if --outfile file exists
43 --check # Do not alter or skip bad uids
62 my ($name,$uid,$lm,$nt,$f,$lct) = ();
66 ($name,$uid,$lm,$nt,$f,$lct) = $mysqlquery->fetchrow();
71 return () if ( not $line );
75 next if ( $line !~ /^[^: ]+:\d+:/ );
77 ($name,$uid,$lm,$nt,$f,$lct) = split(/:/,$line);
79 if ( $lct =~ /^LCT-/ ) {
80 # New Format smbpasswd file
83 $flags |= $ACB_PWNOTREQ if ( $f =~ /N/ );
84 $flags |= $ACB_DISABLED if ( $f =~ /D/ );
85 $flags |= $ACB_HOMDIRREQ if ( $f =~ /H/ );
86 $flags |= $ACB_TEMPDUP if ( $f =~ /T/ );
87 $flags |= $ACB_NORMAL if ( $f =~ /U/ );
88 $flags |= $ACB_MNS if ( $f =~ /M/ );
89 $flags |= $ACB_WSTRUST if ( $f =~ /W/ );
90 $flags |= $ACB_SVRTRUST if ( $f =~ /S/ );
91 $flags |= $ACB_AUTOLOCK if ( $f =~ /L/ );
92 $flags |= $ACB_PWNOEXP if ( $f =~ /X/ );
93 $flags |= $ACB_DOMTRUST if ( $f =~ /I/ );
97 $f = $ACB_NORMAL if ( not $f );
100 $lct = (unpack("L",pack("H8",$lct)))[0];
103 # Old Format smbpasswd file
106 if ( $lm =~ /^NO PASS/ ) {
111 elsif ( $lm =~ /^XX/ ) {
118 if ( $name =~ /\$$/ ) {
122 $f = $ACB_NORMAL if ( not $f );
125 return () if ( not $name );
126 ($name,$uid,$lm,$nt,$f,$lct);
134 print OUTFILE "$query;\n";
137 if ( not $mysqldb->query($query) ) {
138 print "$query: $Mysql::db_errstr\n";
144 my ($file,$name,$uid,$lm,$nt,$f,$lct)=@_;
148 $strings .= "N" if ( $f & $ACB_PWNOTREQ );
149 $strings .= "D" if ( $f & $ACB_DISABLED );
150 $strings .= "H" if ( $f & $ACB_HOMDIRREQ );
151 $strings .= "T" if ( $f & $ACB_TEMPDUP );
152 $strings .= "U" if ( $f & $ACB_NORMAL );
153 $strings .= "M" if ( $f & $ACB_MNS );
154 $strings .= "W" if ( $f & $ACB_WSTRUST );
155 $strings .= "S" if ( $f & $ACB_SVRTRUST );
156 $strings .= "L" if ( $f & $ACB_AUTOLOCK );
157 $strings .= "X" if ( $f & $ACB_PWNOEXP );
158 $strings .= "I" if ( $f & $ACB_DOMTRUST );
160 $f = sprintf( "[%-11s]", $strings );
162 $lct=uc("LCT-".(unpack("H8",pack("L","$lct")))[0]);
164 $lm = "X"x32 if ( not $lm );
165 $nt = "X"x32 if ( not $nt );
167 print $file "$name:$uid:$lm:$nt:$f:$lct\n";
170 $dbhost = "localhost";
172 for $option ( @ARGV ) {
173 if ( $option =~ /--outfile=/ ) {
174 $outfile = getoptionval($option);
176 elsif ( $option =~ /--infile=/ ) {
177 $infile = getoptionval($option);
179 elsif ( $option =~ /--db=/ ) {
180 $dbname = getoptionval($option);
182 elsif ( $option =~ /--user=/ ) {
183 $dbuser = getoptionval($option);
185 elsif ( $option =~ /--host=/ ) {
186 $dbhost = getoptionval($option);
188 elsif ( $option =~ /--password/ ) {
189 $dbpasswd = getoptionval($option);
190 $need_password = "yes"
192 elsif ( $option =~ /--table=/ ) {
193 $dbtable = getoptionval($option);
195 elsif ( $option =~ /--create/ ) {
196 $create_table = "yes";
198 elsif ( $option =~ /--file=/ ) {
199 $file_action = getoptionval($option);
201 elsif ( $option =~ /--check/ ) {
205 print "Unknown option: $option\n";
210 &usage if ( $unknown eq "yes" );
212 if ( ( not $infile ) && ( not $outfile ) && ( $create_table ne "yes" ) ) {
213 print "Need file to read from or write to\n";
216 elsif ( $infile && $outfile ) {
217 if ( not $dbtable ) {
218 print "Need --table to create queries\n";
222 # Reading a smbpasswd file, dumping queries into an file which
223 # can be used for a mysql script
224 # --db* options are ignored.
227 $ignored .= " --db" if ( $dbname );
228 $ignored .= " --user" if ( $dbuser );
229 $ignored .= " --password" if ( $dbuser );
232 print "Ignoring options: $ignored\n";
235 elsif ( (not $dbname) || (not $dbtable) || (not $dbuser) ) {
236 print "Missing database particulars:\n";
237 print " --db=??\n" if ( not $dbname );
238 print " --user=??\n" if ( not $dbuser );
239 print " --table=??\n" if ( not $dbtable );
243 if ( ($need_password eq "yes") && ( not $dbpasswd )) {
244 $dbpasswd = getpass("Enter MySQL password for $dbuser: ");
246 $mysqldb = Connect Mysql($dbhost,$dbname,$dbuser,$dbpasswd);
248 if ( not $mysqldb ) {
249 print "Cannot connect to database: $Mysql::db_errstr\n";
254 $mysqlquery = $mysqldb->query("select unix_name,unix_uid,smb_passwd,smb_nt_passwd,acct_ctrl,pass_last_set_time from $dbtable");
256 if ( not $mysqlquery ) {
257 print "MySQL Query failed: $Mysql::db_errstr\n";
263 if ( $create_table eq "yes" ) {
264 $create_table_query=<<EOSQL;
265 create table $dbtable (
266 unix_name char(20) not null,
267 unix_uid int(10) unsigned not null,
268 nt_name char(20) not null,
269 user_rid int(10) unsigned not null,
271 smb_nt_passwd char(32),
272 acct_ctrl int(10) unsigned not null,
273 pass_last_set_time int(10) unsigned not null,
278 print "$create_table_query\n";
281 if ( not open(INFILE,$infile) ) {
282 print "$infile: $!\n";
288 if ( ! -f $outfile ) {
289 $open_string=">$outfile";
291 elsif ( not $file_action ) {
292 print "File $outfile exists:\n";
293 print "Please use --file=[trash|append] option to determine destiny of file\n";
296 elsif ( $file_action eq "append" ) {
297 $open_string = ">>$outfile";
300 $open_string = ">$outfile";
303 if ( not open(OUTFILE,$open_string) ) {
304 print "$outfile: $!\n";
309 do_query($create_table_query) if ( $create_table_query );
312 while (($name,$uid,$lm,$nt,$f,$lct)=next_entry()) {
320 $nuid = (getpwnam(lc($name)))[2];
324 # print "Removing $name: Does not exist\n";
325 push(@removed,[$name,$uid,$lm,$nt,$f,$lct]);
329 # print "Changing uid of $name\n";
348 do_query("insert into $dbtable (unix_name,unix_uid,smb_passwd,smb_nt_passwd,acct_ctrl,pass_last_set_time,nt_name,user_rid) values ('$name',$uid,$lm,$nt,$f,$lct,'$name',$rid)");
351 do_file(OUTFILE,$name,$uid,$lm,$nt,$f,$lct);
356 print "\n\nIgnored entries because usernames do not exist\n";
357 foreach $line ( @removed ) {
358 do_file(STDOUT,@{ $line });
362 close (OUTFILE) if ( $outfile );
363 close (INFILE) if ( $infile );