/[useradm]/otvori_nove+db
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /otvori_nove+db

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Tue Apr 18 12:09:03 2000 UTC (19 years, 1 month ago) by dpavlin
Branch: MAIN
Changes since 1.2: +26 -9 lines
popravljen -d flag, podrka za preskakanje duplih accounta, bolji debug
output (sa ispisom broja zahtjeva)

1 dpavlin 1.1 #!/usr/local/bin/perl -w
2    
3     # Dobrica Pavlinusic <dpavlin@pliva.hr>
4     # 2000-01-31 DbP prva verzija
5     # 2000-02-14 DbP mozda i radi?
6     # 2000-03-06 DbP koristi DBD::pg
7     # 2000-03-09 DbP e-mail alias samo od malih slova
8     # 2000-03-13 DbP podrska za novu support/last_open tablicu
9     # 2000-03-16 DbP cuva stare aliase iz /etc/aliases
10     # 2000-03-20 DbP skida razmake prije/poslje imena/prezimena
11 dpavlin 1.2
12 dpavlin 1.1 use DBI;
13     use strict;
14    
15     my $debug=0;
16    
17 dpavlin 1.3 if (defined($ARGV[0]) && $ARGV[0] eq "-d") {
18     $debug++;
19     shift @ARGV;
20     }
21     my $open_only_id=shift @ARGV if (defined($ARGV[0]));
22 dpavlin 1.1
23     warn "DEBUG -- me kreira ni¹ta!" if ($debug);
24    
25     $|++;
26    
27    
28     #----
29    
30     sub xerror { print ERROR @_; print STDERR @_; }
31     sub xdebug { print DEBUG @_; print STDOUT @_ if ($debug); }
32     sub v { print STDOUT @_; print DEBUG @_; };
33    
34     open(ERROR,"> error-log") || die "can't open error-log: $!";
35     open(DEBUG,"> debug-log") || die "can't open debug-log: $!";
36    
37     my %etc_aliases;
38     my $mail_alias;
39     my $mail_login;
40    
41 dpavlin 1.3 my @mail_to; # array to main initial message to
42     open(WHO,"who am i |") || die "who am i? $!";
43     my $db_user=<WHO>;
44     $db_user=~s/\s.+$//g;
45     close(WHO);
46    
47     xdebug "koristim korinika $db_user\n";
48    
49    
50 dpavlin 1.1 v "/etc/aliases\n";
51     open(ALIAS,"/etc/aliases") || die "Can't open /etc/aliases: $!";
52     open(NALIAS,">aliases.new") || die "Can't open aliases.new: $!";
53     while(<ALIAS>) {
54     chop;
55     print NALIAS "$_\n";
56     next if (m!^#! || m!^$!);
57     my ($mail_alias,$mail_login);
58     if (m!\w+.\w+!) {
59     ($mail_alias,$mail_login) = split(/\s*:\s*/,$_,2);
60     $mail_alias =~ tr [A-Z] [a-z];
61     }
62     $etc_aliases{$mail_alias}=$mail_login;
63     # xdebug "$mail_alias:$mail_login\n";
64     }
65     close(ALIAS);
66    
67     my %etc_passwd;
68     my $uid; # current (or max) uid
69    
70     v "/etc/passwd\n";
71     open(PASSWD,"/etc/passwd") || die "Can't open /etc/passwd !";
72     open(NPASSWD,">passwd.new") || die "Can't open passwd.new !";
73     while(<PASSWD>) {
74     chop;
75     my ($login,$gid,$full_name,$dir,$shell);
76     ($login,undef,$uid,$gid,$full_name,$dir,$shell) = split(/:/,$_,7);
77     $etc_passwd{$login}=$full_name;
78     print NPASSWD "$_\n";
79     # xdebug "$login:$full_name\n";
80     }
81     close(PASSWD);
82    
83     my %group_id;
84    
85     v "/etc/group\n";
86     open(GROUP,"/etc/group") || die "Can't open /etc/group !";
87     while(<GROUP>) {
88     my ($group,undef,$gid,undef) = split (/:/,$_,4);
89     $group_id{$group}=$gid;
90     # xdebug "$group:$gid\n";
91     }
92     close(GROUP);
93    
94     sub nuke_chars {
95     my $foo=$_[0];
96     $foo =~ tr/šðè枊ÐÈƎ/¹ðè澩ÐÈÆ®/; # cp1250 -> iso88592
97     $foo =~ s/ð/dj/g;
98     $foo =~ s/Ð/Dj/g;
99     $foo =~ tr/¹ðè澩ÐÈÆ®/sdcczSDCCZ/; # nuke iso88592
100     $foo =~ s/^\s+//g;
101     $foo =~ s/\s+$//g;
102     return $foo;
103     }
104    
105     my $max_osoba_id=0;
106    
107     my $dbh = DBI->connect("DBI:Pg:dbname=informatika;host=support.pliva.hr;user=$db_user","","") || die $DBI::errstr;
108     my $sth = $dbh->prepare("select max(id) from last_open") || die $dbh->errstr();
109     $sth->execute() || die $sth->errstr();
110     my ($last_open)=$sth->fetchrow_array;
111 dpavlin 1.2 if (! defined($open_only_id)) {
112     $sth = $dbh->prepare("select id,ime,prezime,sifra,objekt,kat,soba,shell from view_otvori_nove where id > $last_open") || die $dbh->errstr();
113     } else {
114     $sth = $dbh->prepare("select id,ime,prezime,sifra,objekt,kat,soba,shell from view_otvori_nove where id=$open_only_id") || die $dbh->errstr();
115    
116     }
117 dpavlin 1.1 $sth->execute() || die $sth->errstr();
118 dpavlin 1.3
119    
120     my $sth2;
121    
122 dpavlin 1.1 while (my ($osoba_id,$ime,$prezime,$sifra,$objekt,$kat,$soba,$shell) = $sth->fetchrow_array() ) {
123    
124 dpavlin 1.3 $sth2 = $dbh->prepare("select max(status_tip_id) from status where osoba_id = $osoba_id") || die $dbh->errstr();
125     $sth2->execute() || die $sth2->errstr();
126     my ($max_status) = $sth2->fetchrow_array();
127     xdebug "max status zahtjeva $osoba_id je $max_status\n";
128     next if ($max_status > 3); # 3 .. otvoren
129    
130 dpavlin 1.1 next if ($ime eq "ime" || $prezime eq "prezime");
131    
132     if (!defined($ime) || !defined($prezime) || $ime eq "" || $prezime eq "") {
133     xerror "preskacem red '$_'\n";
134     next;
135     }
136    
137     $ime=nuke_chars($ime);
138     $prezime=nuke_chars($prezime);
139    
140     my $prezime_za_login;
141    
142     if ($prezime =~ /-/) { # dva prezimena prezime1-prezime2
143     ($prezime_za_login,undef) = split(/-/,$prezime,2);
144     } else {
145     $prezime_za_login = $prezime;
146     }
147    
148     my $add="";
149     my $login;
150    
151     do {
152 dpavlin 1.2 $login=substr($ime,0,1).substr($prezime_za_login,0,7-length($add)).$add;
153     $login =~ tr [A-Z] [a-z];
154     xdebug "$ime, $prezime login: $login\n";
155     $add++;
156 dpavlin 1.1 } while defined($etc_passwd{$login});
157    
158     my $email_alias;
159    
160     $add="";
161     do {
162     $email_alias="$ime.$prezime$add";
163     $email_alias =~ tr [A-Z] [a-z];
164     xdebug "\t$email_alias\n";
165     $add++;
166     } while defined($etc_aliases{$login});
167    
168    
169     my $group="users";
170    
171     if (defined($etc_passwd{$login})) {
172 dpavlin 1.3 xerror "preskocen: $ime $prezime osoba_id=$osoba_id (postoji isti login $login)\n";
173 dpavlin 1.1 $dbh->do("insert into status (osoba_id,kontakt_osoba_id,datum,status_tip_id) values ($osoba_id,3,'now'::datetime,4)") if (! $debug);
174     next;
175     }
176    
177     push @mail_to,$email_alias;
178    
179     my @saltch = ( 'a'..'z','0'..'9','A'..'Z' );
180     my @passwdch = ( 'a'..'k','m'..'z','2'..'9','A'..'H','J'..'N','P'..'Z',
181     '!','#','$','%','&','(',')','=','-','/','?' );
182    
183     my $passwd="";
184     for (1..8) { $passwd.=$passwdch[int(rand($#passwdch))] };
185    
186     my $salt = $saltch[int(rand($#saltch))].$saltch[int(rand($#saltch))];
187     my $crypt_passwd = crypt($passwd,$salt);
188    
189     $uid++;
190     my $loc="";
191     $loc.=$objekt if (defined($objekt));
192     $loc.=" $kat" if (defined($kat));
193     $loc.=" $soba" if (defined($soba));
194     my $full_name="$ime $prezime,$loc,$sifra";
195     $full_name=~s/, +/,/g;
196     $full_name=~s/ +:/:/g;
197     $full_name=~s/ +/ /g;
198     $full_name=nuke_chars($full_name);
199     if ($shell eq "t") {
200     $shell="/usr/local/bin/bash";
201     } else {
202     $shell="/usr/bin/false";
203     }
204     my $dir="/usr/users/$login";
205    
206     die "Nema grupe $group!" if (! defined($group_id{$group}));
207     xdebug "$login:$passwd:$uid:$group_id{$group}:$full_name:$dir:$shell\n";
208    
209     print NPASSWD "$login:$crypt_passwd:$uid:$group_id{$group}:$full_name:$dir:$shell\n";
210     $dbh->do("insert into racuni (osoba_id,login,passwd) values ($osoba_id,'$login','$passwd')") if (! $debug);
211     $etc_passwd{$login}=$full_name;
212    
213     if (defined($etc_aliases{$email_alias})) {
214 dpavlin 1.3 xerror "upozorenje: $ime $prezime osoba_id=$osoba_id (postoji e-mail alias) -- $email_alias\n";
215 dpavlin 1.1 print NALIAS "$email_alias:\t$login,$etc_aliases{$email_alias}\n";
216     } else {
217     print NALIAS "$email_alias:\t$login\n";
218     }
219    
220     $dbh->do("insert into e_mail (osoba_id,alias) values ($osoba_id,'$email_alias')") if (! $debug);
221     $etc_aliases{$email_alias}=$login;
222    
223     #---- C2 security
224     # if (!$debug || 1) {
225     # open(AUTH,">> $0.auth");
226     # print AUTH "$login:u_name=$login:u_id#$uid:u_pwd=$crypt_passwd:u_succhg#",time,":u_lock\@:chkent:\n";
227     # close(AUTH);
228     # }
229    
230     system "mkdir -p $dir ; mkdir $dir/bin ; \
231     chown -R $uid:$group_id{$group} $dir ; chmod 755 $dir" if (! $debug);
232    
233     $dbh->do("insert into status (osoba_id,kontakt_osoba_id,datum,status_tip_id) values ($osoba_id,3,'now'::datetime,3)") if (! $debug);
234    
235     $max_osoba_id = $osoba_id if ($max_osoba_id < $osoba_id) ;
236     }
237    
238     $dbh->do("insert into last_open values ('now',$max_osoba_id)") if (! $debug);
239    
240     close(NPASSWD);
241     close(NALIAS);
242    
243     undef $sth;
244     $dbh->disconnect;
245    
246     system "mv /etc/passwd passwd.orig" if (! $debug);
247     system "cp passwd.new /etc/passwd" if (! $debug);
248     system "chmod 644 /etc/passwd" if (! $debug);
249     system "/usr/sbin/mkpasswd /etc/passwd" if (! $debug);
250     system "mv /etc/aliases aliases.orig" if (! $debug);
251     system "cp aliases.new /etc/aliases" if (! $debug);
252     system "chmod 644 /etc/aliases" if (! $debug);
253     system "/usr/sbin/newaliases" if (! $debug);
254     #system "/tcb/bin/edauth -s < $0.auth" if (! $debug);
255    
256     foreach my $alias (@mail_to) {
257     system "/usr/lib/sendmail $alias < mail.txt" if (! $debug);
258     }

  ViewVC Help
Powered by ViewVC 1.1.26