sync_passwd_file.pl
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:8k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. #!/usr/bin/perl
  2. #
  3. # Syncronizes a /etc/passwd file with a remote prometheus database.
  4. #
  5. use strict;
  6. use DBI;
  7. use DBD::mysql;
  8. my $prometheus_db_type        = 'mysql';
  9. my $prometheus_db_host        = 'localhost';
  10. my $prometheus_db_username    = 'prouser';
  11. my $prometheus_db_password    = 'prouser';
  12. my $imap_server               = 'localhost';
  13. my $prometheus_domain         = 'test.com';
  14. # Default list of users to mask out..
  15. my @system_users = (
  16.    'root',
  17.    'mail',
  18.    'gopher',
  19.    'ftp',
  20.    'halt',
  21.    'nobody',
  22.    'xfs',
  23.    'games',
  24.    'adm',
  25.    'operator',
  26.    'daemon',
  27.    'gdm',
  28.    'lp',
  29.    'bin',
  30.    'shutdown',
  31.    'uucp',
  32.    'news',
  33.    'sync',
  34.    # Qmail users for qmail hosts
  35.    'qmailp',
  36.    'qmailr',
  37.    'qmails',
  38.    'vpopmail',
  39.    'alias',
  40. );
  41. ################################################################################
  42. #
  43. # Main line, you shouldn't need to modify things down here
  44. #
  45. my $db_connection = mysql_open_connection( 'prometheus_domains' );
  46. if ( $db_connection == undef ) {
  47.    die "Database connection open failed : $!n";
  48. }
  49. my $domain_id = get_domain_id ( $db_connection, $prometheus_domain );
  50. if ( $domain_id == -1 ) {
  51.    create_domain( $db_connection, $prometheus_domain );
  52.    $domain_id = get_domain_id ( $db_connection, $prometheus_domain );
  53. }
  54. if ( $domain_id == -1 ) {
  55.    $db_connection->disconnect();
  56.    die "DOMAIN NOT FOUND : $prometheus_domainn";
  57. }
  58. print "DOMAIN : $prometheus_domain - $domain_idn";
  59. my %users = shadow_aware_getpwent();
  60. # Close the old connection and open up the connection
  61. # to the user database.
  62. $db_connection->disconnect();
  63. my $db_connection = mysql_open_connection( 'prometheus_users' );
  64. foreach my $user ( keys %users ) {
  65.    my $enc_passwd = $users{ $user };
  66.    if ( $enc_passwd eq '!!' || $enc_passwd eq '*' ) {
  67.       #print "DENYING SYNC FOR $usern"; 
  68.       next;
  69.    }
  70.    print "USER : $user - $enc_passwdn";
  71.    my ( $user_id, $password ) = get_user_information( $db_connection, $user, $domain_id );
  72.    if ( $user_id == -1 ) {
  73.       print "Adding user to system : $usern";
  74.       add_user( $user, $enc_passwd, $domain_id );
  75.    }
  76.    if ( $password ne $enc_passwd && $user_id != -1 ) {
  77.       # They have changed their password on the unix side
  78.       # we need to update the prometheus lib database
  79.       print "Updating password info : $user_idn";
  80.       update_password( $db_connection, $user_id, $enc_passwd );
  81.    }
  82. }
  83. $db_connection->disconnect();
  84. # Exit
  85. exit();
  86. ################################################################################
  87. sub update_password {
  88.    my $db_connection = shift;
  89.    my $user_id       = shift;
  90.    my $password      = shift;
  91.    my $user_update = $db_connection->prepare( 
  92. 'UPDATE user_table SET password = ? WHERE user_id = ?' 
  93. ) or die "Could not prepare the update statement handle for $user_id : $!n";
  94.    $user_update->execute( $password, $user_id )
  95.       or die "Could not execute the update statement for $user_id : $!n";
  96.    $user_update->finish();
  97. }
  98. sub shadow_aware_getpwent {
  99.    my $dont_trust_getpwent = 0;
  100.    if ( -f '/etc/shadow' ) {
  101.       $dont_trust_getpwent = 1;
  102.    }
  103.    my %users = ();
  104.    while( my( $user, $enc_passwd ) = getpwent() ) {
  105.       if ( is_system_user( $user ) ) { next; }
  106.       $users{ $user } = $enc_passwd;
  107.    }
  108.    if ( $dont_trust_getpwent ) {
  109.       open( SHADOW, '/etc/shadow' ) 
  110.          or die "Couldnt open shadow : $!n";
  111.       while( my $line = <SHADOW> ) {
  112.          my( $user, $enc_passwd ) = split( ':', $line );
  113.          if ( is_system_user( $user ) ) { next; }
  114.          $users{ $user } = $enc_passwd;
  115.       }
  116.       close( SHADOW );
  117.    }
  118.    return %users;
  119. }
  120. sub is_system_user {
  121.    my $username = shift;
  122.    foreach my $user ( @system_users ) {
  123.       if ( $user eq $username ) {
  124.          return 1;
  125.       }
  126.    }
  127.    return 0;
  128. }
  129. sub get_domain_id {
  130.    my $db_connection    = shift;
  131.    my $domain_name      = shift;
  132.    if ( $domain_name eq 'default' ) {
  133.       # The default domain is 0
  134.       return 0;
  135.    }
  136.    my $csr = $db_connection->prepare(
  137.       'SELECT domain_id FROM domain_table WHERE domain_name = ?'
  138.    ) or die "Could not prepare select domain : $!n";
  139.    $csr->execute( $domain_name ) or "Die execute select domain puked : $!n";
  140.    my ( $domain_id ) = $csr->fetchrow();
  141.    if ( $domain_id == undef ) {
  142.       $csr->finish();
  143.       return -1;
  144.    } else {
  145.       $csr->finish();
  146.       return $domain_id;
  147.    }
  148.    $csr->finish();
  149.    return -1;
  150. }
  151. sub create_domain {
  152.    my $db_connection    = shift;
  153.    my $domain_name      = shift;
  154.    if ( $domain_name eq 'default' ) {
  155.       return 0;
  156.    }
  157.    my $csr = $db_connection->prepare( 
  158.       'INSERT INTO domain_table ( domain_name ) VALUES ( ? )'
  159.    ) or die "Could not insert into domain table : $!n";
  160.    $csr->execute( $domain_name ) or die "Die execute create domani puked: $!n";
  161.    $csr->finish();
  162.    return 1;
  163. }
  164. sub get_user_information {
  165.    my $db_connection = shift;
  166.    my $user_name = shift;
  167.    my $domain_id = shift;
  168.    my $csr = $db_connection->prepare(
  169.       'SELECT user_id, password FROM user_table WHERE user_name = ? AND domain_id = ?'
  170.    ) or die "Could not prepare select user_table : $!n";
  171.    $csr->execute( $user_name, $domain_id ) or die "Execute select user information failed : $!n";
  172.    my( $user_id, $password ) = $csr->fetchrow();
  173.    $csr->finish();
  174.    if ( $user_id < 0 || $user_id == undef ) {
  175.       # User not found
  176.       $user_id    = -1;
  177.       $password   = '';
  178.    }
  179.    return ( $user_id, $password );
  180. }
  181. sub add_user {
  182.    my $user_name = shift;
  183.    my $password  = shift;
  184.    my $domain_id = shift;
  185.    my $t_conn = mysql_open_connection( 'prometheus_users' );
  186.    if ( $t_conn == undef ) {
  187.       die "unable to connect to the prometheus_users database : $!n";
  188.    }
  189.    my $create_user = $t_conn->prepare(
  190. '
  191. INSERT INTO user_table ( user_name, password, login_deny, domain_id ) 
  192. VALUES ( ?, ?, ?, ? )
  193. ' ) or die "Could not create the insert handle for a user : $!n";
  194.    $create_user->execute( $user_name, $password, 0, $domain_id )
  195.       or die "Failed to insert $user_name : $!n";
  196.    $create_user->finish();
  197.    my ($user_id, $password ) = get_user_information( $t_conn, $user_name, $domain_id );
  198.    $t_conn->disconnect();
  199.    if ( $user_id == -1 ) {
  200.       # Something has gone teriablly wrong 
  201.       die "Tried to add a user and retained a false positive via DBD and such : $!n";
  202.    }
  203.    $t_conn = mysql_open_connection( 'prometheus_privileges' );
  204.    my $create_privileges = $t_conn->prepare(
  205. 'INSERT INTO 
  206.    admin_privileges_table 
  207. ( user_id, add_users, edit_users, delete_users )
  208. VALUES 
  209. ( ?, ?, ?, ? )
  210. ' ) or die "Could not create the insert hadnle for user privs $user_name : $!n";
  211.    $create_privileges->execute( $user_id, 0, 0, 0 ) 
  212.       or die "Could not execute insert user privs : $user_name : $!n";
  213.    $create_privileges->finish();
  214.    $t_conn->disconnect();
  215.    # Add a entry for the pimp settings to passthrough
  216.    $t_conn = mysql_open_connection( 'mail_settings' );
  217.    my $add_mail_settings = $t_conn->prepare(
  218. 'INSERT INTO
  219.    server_settings_table
  220. ( user_id, server_name )
  221. VALUES
  222. ( ?, ? )' ) or die "Could not prepare a insert for the server settings : $!n";
  223.    $add_mail_settings->execute( $user_id, $imap_server )
  224.       or die "Could not execute the insert for server settings : $!n";
  225.    $add_mail_settings->finish();
  226.    $t_conn->disconnect();
  227. }
  228. ### -- Mysql driver component -- ###
  229. sub mysql_open_connection {
  230.    my $database = shift;
  231.    return
  232.       DBI->connect( 
  233.          'DBI:mysql:database=' . $database . ';host=' . $prometheus_db_host,
  234.          $prometheus_db_username, $prometheus_db_password,
  235.          { 'RaiseError' => 1 }
  236.       ) or die "Could not connect to $databasen";
  237. }