--- otvori_nove+db 2000/04/12 08:19:17 1.1 +++ otvori_nove+db 2001/01/26 07:44:36 1.9 @@ -8,23 +8,24 @@ # 2000-03-13 DbP podrska za novu support/last_open tablicu # 2000-03-16 DbP cuva stare aliase iz /etc/aliases # 2000-03-20 DbP skida razmake prije/poslje imena/prezimena - + use DBI; use strict; my $debug=0; -$debug++ if (defined($ARGV[0]) && $ARGV[0] eq "-d"); +my $res_uids=60000; # uids larger than this are for system use + +if (defined($ARGV[0]) && $ARGV[0] eq "-d") { + $debug++; + shift @ARGV; +} +my $open_only_id=shift @ARGV if (defined($ARGV[0])); -warn "DEBUG -- me kreira ništa!" if ($debug); +warn "DEBUG -- ne kreira ništa!" if ($debug); $|++; -my @mail_to; # array to main initial message to -open(WHO,"who am i |") || die "who am i? $!"; -my $db_user=; -$db_user=~s/\s.+$//g; -close(WHO); #---- @@ -39,6 +40,22 @@ my $mail_alias; my $mail_login; +my @mail_to; # array to main initial message to +open(WHO,"who am i |") || die "who am i? $!"; +my $db_user=; +chomp $db_user; +$db_user=~s/\s.+$//g; +close(WHO); + +xdebug "koristim korisnika $db_user\n"; + +open(HOSTNAME,"hostname |") || die "hostname: $!"; +my $hostname=; +chomp $hostname; +close(HOSTNAME); + +xdebug "koristim hostname $hostname\n"; + v "/etc/aliases\n"; open(ALIAS,"/etc/aliases") || die "Can't open /etc/aliases: $!"; open(NALIAS,">aliases.new") || die "Can't open aliases.new: $!"; @@ -57,18 +74,19 @@ close(ALIAS); my %etc_passwd; -my $uid; # current (or max) uid +my $max_uid = 0; # maximum uid v "/etc/passwd\n"; open(PASSWD,"/etc/passwd") || die "Can't open /etc/passwd !"; open(NPASSWD,">passwd.new") || die "Can't open passwd.new !"; while() { chop; - my ($login,$gid,$full_name,$dir,$shell); + my ($login,$uid,$gid,$full_name,$dir,$shell); ($login,undef,$uid,$gid,$full_name,$dir,$shell) = split(/:/,$_,7); $etc_passwd{$login}=$full_name; print NPASSWD "$_\n"; # xdebug "$login:$full_name\n"; + $max_uid = $uid if ($max_uid < $uid && $uid < $res_uids); } close(PASSWD); @@ -97,13 +115,36 @@ my $max_osoba_id=0; my $dbh = DBI->connect("DBI:Pg:dbname=informatika;host=support.pliva.hr;user=$db_user","","") || die $DBI::errstr; -my $sth = $dbh->prepare("select max(id) from last_open") || die $dbh->errstr(); + +my $sth = $dbh->prepare("select ko_id from unix2ko where login='$db_user' and host='$hostname'") || die $dbh->errstr(); +$sth->execute() || die $sth->errstr(); +my ($ko_id)=$sth->fetchrow_array; + +xdebug "upotrebljavam ID kontakt osobe: $ko_id\n"; + +$sth = $dbh->prepare("select max(id) from last_open") || die $dbh->errstr(); $sth->execute() || die $sth->errstr(); my ($last_open)=$sth->fetchrow_array; -$sth = $dbh->prepare("select id,ime,prezime,sifra,objekt,kat,soba,shell from view_otvori_nove where id > $last_open") || die $dbh->errstr(); + +if (! defined($open_only_id)) { + $sth = $dbh->prepare("select id,ime,prezime,sifra,objekt,kat,soba,shell from view_otvori_nove where id > $last_open") || die $dbh->errstr(); +} else { + $sth = $dbh->prepare("select id,ime,prezime,sifra,objekt,kat,soba,shell from view_otvori_nove where id=$open_only_id") || die $dbh->errstr(); + +} $sth->execute() || die $sth->errstr(); + + +my $sth2; + while (my ($osoba_id,$ime,$prezime,$sifra,$objekt,$kat,$soba,$shell) = $sth->fetchrow_array() ) { + $sth2 = $dbh->prepare("select max(status_tip_id) from status where osoba_id = $osoba_id") || die $dbh->errstr(); + $sth2->execute() || die $sth2->errstr(); + my ($max_status) = $sth2->fetchrow_array(); + xdebug "max status zahtjeva $osoba_id je $max_status\n"; + next if ($max_status > 3); # 3 .. otvoren + next if ($ime eq "ime" || $prezime eq "prezime"); if (!defined($ime) || !defined($prezime) || $ime eq "" || $prezime eq "") { @@ -114,29 +155,36 @@ $ime=nuke_chars($ime); $prezime=nuke_chars($prezime); - my $prezime_za_login; + my ($prezime_za_login,$ime_za_login); - if ($prezime =~ /-/) { # dva prezimena prezime1-prezime2 - ($prezime_za_login,undef) = split(/-/,$prezime,2); + if ($prezime =~ /[- ]/) { # dva prezimena prezime1-prezime2 + ($prezime_za_login,undef) = split(/[- ]/,$prezime,2); } else { $prezime_za_login = $prezime; } + if ($ime =~ /[- ]/) { # dva imena ime1-ime2 + ($ime_za_login,undef) = split(/[- ]/,$ime,2); + } else { + $ime_za_login = $ime; + } + my $add=""; my $login; do { - $login=substr($ime,0,1).substr($prezime_za_login,0,7-length($add)).$add; - xdebug "$ime, $prezime login: $login\n"; - $add++; + $login=substr($ime_za_login,0,1).substr($prezime_za_login,0,7-length($add)).$add; + $login =~ tr [A-Z] [a-z]; + xdebug "$ime, $prezime login: $login\n"; + $add++; } while defined($etc_passwd{$login}); - $login =~ tr [A-Z] [a-z]; my $email_alias; $add=""; do { $email_alias="$ime.$prezime$add"; + $email_alias =~ s/ /./g; # space -> dot $email_alias =~ tr [A-Z] [a-z]; xdebug "\t$email_alias\n"; $add++; @@ -146,7 +194,7 @@ my $group="users"; if (defined($etc_passwd{$login})) { - xerror "preskocen: $ime $prezime (postoji isti login $login)\n"; + xerror "preskocen: $ime $prezime osoba_id=$osoba_id (postoji isti login $login)\n"; $dbh->do("insert into status (osoba_id,kontakt_osoba_id,datum,status_tip_id) values ($osoba_id,3,'now'::datetime,4)") if (! $debug); next; } @@ -163,7 +211,7 @@ my $salt = $saltch[int(rand($#saltch))].$saltch[int(rand($#saltch))]; my $crypt_passwd = crypt($passwd,$salt); - $uid++; + $max_uid++; my $loc=""; $loc.=$objekt if (defined($objekt)); $loc.=" $kat" if (defined($kat)); @@ -173,7 +221,7 @@ $full_name=~s/ +:/:/g; $full_name=~s/ +/ /g; $full_name=nuke_chars($full_name); - if ($shell eq "t") { + if ($shell) { $shell="/usr/local/bin/bash"; } else { $shell="/usr/bin/false"; @@ -181,14 +229,14 @@ my $dir="/usr/users/$login"; die "Nema grupe $group!" if (! defined($group_id{$group})); - xdebug "$login:$passwd:$uid:$group_id{$group}:$full_name:$dir:$shell\n"; + xdebug "$login:$passwd:$max_uid:$group_id{$group}:$full_name:$dir:$shell\n"; - print NPASSWD "$login:$crypt_passwd:$uid:$group_id{$group}:$full_name:$dir:$shell\n"; + print NPASSWD "$login:$crypt_passwd:$max_uid:$group_id{$group}:$full_name:$dir:$shell\n"; $dbh->do("insert into racuni (osoba_id,login,passwd) values ($osoba_id,'$login','$passwd')") if (! $debug); $etc_passwd{$login}=$full_name; if (defined($etc_aliases{$email_alias})) { - xerror "upozorenje: $ime $prezime (postoji e-mail alias) -- $email_alias\n"; + xerror "upozorenje: $ime $prezime osoba_id=$osoba_id (postoji e-mail alias) -- $email_alias\n"; print NALIAS "$email_alias:\t$login,$etc_aliases{$email_alias}\n"; } else { print NALIAS "$email_alias:\t$login\n"; @@ -200,14 +248,14 @@ #---- C2 security # if (!$debug || 1) { # open(AUTH,">> $0.auth"); -# print AUTH "$login:u_name=$login:u_id#$uid:u_pwd=$crypt_passwd:u_succhg#",time,":u_lock\@:chkent:\n"; +# print AUTH "$login:u_name=$login:u_id#$max_uid:u_pwd=$crypt_passwd:u_succhg#",time,":u_lock\@:chkent:\n"; # close(AUTH); # } system "mkdir -p $dir ; mkdir $dir/bin ; \ - chown -R $uid:$group_id{$group} $dir ; chmod 755 $dir" if (! $debug); + chown -R $max_uid:$group_id{$group} $dir ; chmod 755 $dir" if (! $debug); - $dbh->do("insert into status (osoba_id,kontakt_osoba_id,datum,status_tip_id) values ($osoba_id,3,'now'::datetime,3)") if (! $debug); + $dbh->do("insert into status (osoba_id,kontakt_osoba_id,datum,status_tip_id) values ($osoba_id,$ko_id,'now'::datetime,3)") if (! $debug); $max_osoba_id = $osoba_id if ($max_osoba_id < $osoba_id) ; } @@ -217,7 +265,12 @@ close(NPASSWD); close(NALIAS); +$sth->finish; undef $sth; +if (defined($sth2)) { + $sth2->finish; + undef $sth2; +} $dbh->disconnect; system "mv /etc/passwd passwd.orig" if (! $debug);