[vhffs-dev] [706] Updated nss mirror script to be compatible with libnss-sqlite 0.2

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 706
Author:   beuss
Date:     2007-07-10 19:45:16 +0000 (Tue, 10 Jul 2007)

Log Message:
-----------
Updated nss mirror script to be compatible with libnss-sqlite 0.2

Modified Paths:
--------------
    trunk/vhffs-backend/src/mirror/nss-mirror.pl


Modified: trunk/vhffs-backend/src/mirror/nss-mirror.pl
===================================================================
--- trunk/vhffs-backend/src/mirror/nss-mirror.pl	2007-07-10 14:02:36 UTC (rev 705)
+++ trunk/vhffs-backend/src/mirror/nss-mirror.pl	2007-07-10 19:45:16 UTC (rev 706)
@@ -33,6 +33,7 @@
 # **** WARNING ***** This file should be owned by root and chmoded 500
 use DBI;
 use Getopt::Long;
+use strict;
 
 # TODO Set us!
 my $PG_DB_HOST = 'localhost';           # Host running pg
@@ -40,12 +41,18 @@
 my $PG_DB_NAME = 'vhffs';               # VHFFS' database name
 my $PG_DB_USER = 'vhffs';               # VHFFS' database user
 my $PG_DB_PASS = 'vhffs';               # VHFFS' database password
-my $ST_DB_FILE = '/var/db/auth.sqlite'; # SQLite shadow database
 
+my $ST_PW_DB = '/var/db/passwd.sqlite'; # SQLite users database
+my $ST_SP_DB = '/var/db/shadow.sqlite'; # SQLite shadow database
+
+# Enforced shell value if any
 my $shell;
+# Enforced homedir if any
 my $homedir;
+# Flag
 my $showhelp;
-my $clearpass;
+# Flag, set if shadow informations doesn't have to be replicated
+my $skipshadow;
 
 sub usage() {
     print <<EOF;
@@ -54,7 +61,7 @@
     --help                     this help
     --force-shell shell_path   force 'shell' field value to shell_path
     --force-homedir home_path  force 'homedir' field value to home_path
-    --clear-pass               all passwords will be set to 'x'
+    --no-shadow                shadowed passwords won't be replicated
 
 EOF
     exit(0);
@@ -63,7 +70,7 @@
 if(!GetOptions( 'force-homedir=s'   => \$homedir,
             'force-shell=s'         => \$shell,
             'help'                  => \$showhelp,
-            'clear-pass'            => \$clearpass
+            'no-shadow'            => \$skipshadow
           )) {
     exit(-1);
 }
@@ -77,42 +84,45 @@
     or die("Unable to open pg connection\n");
 
 # SQLite connection now
-my $st_dbh = DBI->connect("DBI:SQLite:dbname=$ST_DB_FILE", '', '')
-    or die("Unable to open SQLite connection\n");
+my $pw_dbh = DBI->connect("DBI:SQLite:dbname=$ST_PW_DB", '', '')
+    or die("Unable to open SQLite passwd connection\n");
 
+# SQLite shadow connection, will be opened later if needed
+my $sp_dbh;
+
 # Ok, we have to fetch everything from pg and put it into SQLite. Use a
 # transaction to speedup things.
 
-$st_dbh->{AutoCommit} = 0;
+$pw_dbh->{AutoCommit} = 0;
 
-$st_dbh->do(q{CREATE TEMP TABLE tmp_shadow(uid INTEGER PRIMARY KEY, gid INTEGER, username TEXT NOT NULL, passwd TEXT NOT NULL, gecos TEXT NOT NULL default '', shell TEXT NOT NULL,  homedir TEXT NOT NULL)})
-    or die("Unable to create temporary shadow table\n");
+$pw_dbh->do(q{CREATE TEMP TABLE tmp_passwd(uid INTEGER, gid INTEGER, username TEXT NOT NULL, gecos TEXT NOT NULL default '', shell TEXT NOT NULL,  homedir TEXT NOT NULL)})
+    or die("Unable to create temporary passwd table\n");
 
-$st_dbh->do(q{CREATE TEMP TABLE tmp_groups(gid INTEGER PRIMARY KEY, groupname TEXT NOT NULL, passwd TEXT NOT NULL DEFAULT '')})
+$pw_dbh->do(q{CREATE TEMP TABLE tmp_groups(gid INTEGER, groupname TEXT NOT NULL, passwd TEXT NOT NULL DEFAULT '')})
     or die("Unable to create temporary groups table\n");
 
-$st_dbh->do(q{CREATE TEMP TABLE tmp_user_group(uid INTEGER, gid INTEGER, CONSTRAINT pk_user_groups PRIMARY KEY(uid, gid))})
+$pw_dbh->do(q{CREATE TEMP TABLE tmp_user_group(uid INTEGER, gid INTEGER, CONSTRAINT pk_user_groups PRIMARY KEY(uid, gid))})
     or die("Unable to create temporary user_group table\n");
 
 
-my $select = q{SELECT u.uid, u.gid, u.username %s %s %s FROM vhffs_users u INNER JOIN vhffs_object o ON o.object_id = u.object_id WHERE o.state = 6};
-my $ssth = $pg_dbh->prepare(sprintf( $select, ($clearpass ? '' : ', u.passwd'), ($homedir ? '' : ', u.homedir'), ($shell ? '' : ', u.shell') )) 
+my $select = q{SELECT u.uid, u.gid, u.username %s %s FROM vhffs_users u INNER JOIN vhffs_object o ON o.object_id = u.object_id WHERE o.state = 6};
+my $ssth = $pg_dbh->prepare( sprintf($select, ($homedir ? '' : ', u.homedir'),
+    ($shell ? '' : ', u.shell') ) ) 
     or die("Unable to prepare users SELECT statement\n");
-my $sth = $st_dbh->prepare(q{INSERT INTO tmp_shadow(uid, gid, username, passwd, shell, homedir) VALUES(?, ?, ?, ?, ?, ?)})
-    or die("Unable to prepare shadow insert statement\n");;
+my $sth = $pw_dbh->prepare(q{INSERT INTO tmp_passwd(uid, gid, username, shell, homedir) VALUES(?, ?, ?, ?, ?)})
+    or die("Unable to prepare passwd insert statement\n");;
 
 $ssth->execute() or die("Unable to execute users SELECT statement\n");
 
 while(my $row = $ssth->fetchrow_hashref()) {
     $sth->execute($row->{uid}, $row->{gid}, $row->{username}, 
-        ($clearpass ? 'x' : $row->{passwd}), 
         ($shell ? $shell : $row->{shell}), ($homedir ? $homedir : $row->{homedir}))
-        or die("Unable to insert shadow\n");
+        or die('Unable to insert passwd entry #'.$row->{uid}."\n");
 }
 $sth->finish();
 
 $ssth = $pg_dbh->prepare(q{SELECT g.gid, g.groupname, g.passwd FROM vhffs_groups g INNER JOIN vhffs_object o ON o.object_id = g.object_id WHERE o.state = 6}) or die("Unable to prepare groups SELECT statement\n");
-$sth = $st_dbh->prepare(q{INSERT INTO tmp_groups(gid, groupname, passwd) VALUES(?, ?, ?)})
+$sth = $pw_dbh->prepare(q{INSERT INTO tmp_groups(gid, groupname, passwd) VALUES(?, ?, ?)})
     or die("Unable to prepare groups insert statement\n");
 
 $ssth->execute() or die("Unable to execute groups SELECT statement\n");
@@ -125,7 +135,7 @@
 
 $ssth = $pg_dbh->prepare(q{SELECT uid, gid FROM vhffs_user_group WHERE state = 6})
     or die("Unable to prepare user_group SELECT statement\n");
-$sth = $st_dbh->prepare(q{INSERT INTO tmp_user_group(uid, gid) VALUES(?, ?)})
+$sth = $pw_dbh->prepare(q{INSERT INTO tmp_user_group(uid, gid) VALUES(?, ?)})
     or die("Unable to prepare user_group insert statement\n");
 
 $ssth->execute();
@@ -136,19 +146,47 @@
 }
 $sth->finish();
 
+unless($skipshadow) {
+    $sp_dbh = DBI->connect("DBI:SQLite:dbname=$ST_SP_DB", '', '')
+        or die("Unable to open SQLite shadow connection\n");
+
+    $sp_dbh->do(q{CREATE TEMPORARY TABLE tmp_shadow
+        (username TEXT, passwd TEXT)})
+        or die("Unable to create temporary shadow table\n");
+
+    $ssth = $pg_dbh->prepare(q{SELECT username, passwd FROM vhffs_users u
+    INNER JOIN vhffs_object o ON o.object_id = u.object_id WHERE o.state = 6})
+        or die("Unable to prepare shadow SELECT statement\n");
+    $sth = $sp_dbh->prepare(q{INSERT INTO tmp_shadow(username, passwd)
+        VALUES(?, ?)})
+        or die("Unable to prepare shadow INSERT statement\n");
+
+    $ssth->execute();
+
+    while(my $row = $ssth->fetchrow_hashref()) {
+        $sth->execute($row->{username}, $row->{passwd})
+            or die('Unable to insert shadow user '.$row->{username}."\n");
+    }
+}
+
+
 # Required to avoid warning "closing dbh with active statement handles"
 undef $sth;
 
 $pg_dbh->disconnect();
 
-$st_dbh->do(q{DELETE FROM shadow WHERE uid NOT IN(SELECT uid FROM tmp_shadow)});
-$st_dbh->do(q{DELETE FROM groups WHERE gid NOT IN(SELECT gid FROM tmp_groups)});
-$st_dbh->do(q{DELETE FROM user_group WHERE NOT EXISTS(SELECT * FROM tmp_user_group 
+$pw_dbh->do(q{DELETE FROM passwd WHERE uid NOT IN(SELECT uid FROM tmp_passwd)});
+$pw_dbh->do(q{DELETE FROM groups WHERE gid NOT IN(SELECT gid FROM tmp_groups)});
+$pw_dbh->do(q{DELETE FROM user_group WHERE NOT EXISTS(SELECT * FROM tmp_user_group 
 WHERE tmp_user_group.uid = user_group.uid AND tmp_user_group.gid = user_group.gid)});
+$sp_dbh->do(q{DELETE FROM shadow WHERE username NOT IN(SELECT username FROM
+    tmp_shadow)}) unless($skipshadow);
 
-$st_dbh->do(q{INSERT OR REPLACE INTO shadow SELECT * FROM tmp_shadow});
-$st_dbh->do(q{INSERT OR REPLACE INTO groups SELECT * FROM tmp_groups});
-$st_dbh->do(q{INSERT OR IGNORE INTO user_group SELECT * FROM tmp_user_group});
+$pw_dbh->do(q{INSERT OR REPLACE INTO passwd SELECT * FROM tmp_passwd});
+$pw_dbh->do(q{INSERT OR REPLACE INTO groups SELECT * FROM tmp_groups});
+$pw_dbh->do(q{INSERT OR IGNORE INTO user_group SELECT * FROM tmp_user_group});
+$sp_dbh->do(q{INSERT OR REPLACE INTO shadow SELECT * FROM tmp_shadow})
+    unless($skipshadow);
 
-$st_dbh->commit();
-$st_dbh->disconnect();
+$pw_dbh->commit();
+$pw_dbh->disconnect();


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/