--- otvori_nove+db 2000/04/18 12:09:03 1.3 +++ otvori_nove+db 2001/01/26 07:44:36 1.9 @@ -14,13 +14,15 @@ my $debug=0; +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); $|++; @@ -41,11 +43,18 @@ 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 korinika $db_user\n"; +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: $!"; @@ -65,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); @@ -105,9 +115,17 @@ 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; + 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 { @@ -137,19 +155,25 @@ $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; + $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++; @@ -160,6 +184,7 @@ $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++; @@ -186,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)); @@ -196,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"; @@ -204,9 +229,9 @@ 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; @@ -223,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) ; } @@ -240,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);