/[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.4 - (hide annotations)
Tue Apr 18 12:19:24 2000 UTC (19 years, 4 months ago) by dpavlin
Branch: MAIN
Changes since 1.3: +11 -4 lines
bolja podrka za dva imena/prezimena

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 dpavlin 1.4 my $prezime_za_login,$ime_za_login;
141 dpavlin 1.1
142 dpavlin 1.4 if ($prezime =~ /[- ]/) { # dva prezimena prezime1-prezime2
143     ($prezime_za_login,undef) = split(/[- ]/,$prezime,2);
144 dpavlin 1.1 } else {
145     $prezime_za_login = $prezime;
146     }
147    
148 dpavlin 1.4 if ($ime =~ /[- ]/) { # dva imena ime1-ime2
149     ($ime_za_login,undef) = split(/[- ]/,$ime,2);
150     } else {
151     $ime_za_login = $ime;
152     }
153    
154 dpavlin 1.1 my $add="";
155     my $login;
156    
157     do {
158 dpavlin 1.4 $login=substr($ime_za_login,0,1).substr($prezime_za_login,0,7-length($add)).$add;
159 dpavlin 1.2 $login =~ tr [A-Z] [a-z];
160     xdebug "$ime, $prezime login: $login\n";
161     $add++;
162 dpavlin 1.1 } while defined($etc_passwd{$login});
163    
164     my $email_alias;
165    
166     $add="";
167     do {
168     $email_alias="$ime.$prezime$add";
169 dpavlin 1.4 $email_alias =~ s/ /./g; # space -> dot
170 dpavlin 1.1 $email_alias =~ tr [A-Z] [a-z];
171     xdebug "\t$email_alias\n";
172     $add++;
173     } while defined($etc_aliases{$login});
174    
175    
176     my $group="users";
177    
178     if (defined($etc_passwd{$login})) {
179 dpavlin 1.3 xerror "preskocen: $ime $prezime osoba_id=$osoba_id (postoji isti login $login)\n";
180 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);
181     next;
182     }
183    
184     push @mail_to,$email_alias;
185    
186     my @saltch = ( 'a'..'z','0'..'9','A'..'Z' );
187     my @passwdch = ( 'a'..'k','m'..'z','2'..'9','A'..'H','J'..'N','P'..'Z',
188     '!','#','$','%','&','(',')','=','-','/','?' );
189    
190     my $passwd="";
191     for (1..8) { $passwd.=$passwdch[int(rand($#passwdch))] };
192    
193     my $salt = $saltch[int(rand($#saltch))].$saltch[int(rand($#saltch))];
194     my $crypt_passwd = crypt($passwd,$salt);
195    
196     $uid++;
197     my $loc="";
198     $loc.=$objekt if (defined($objekt));
199     $loc.=" $kat" if (defined($kat));
200     $loc.=" $soba" if (defined($soba));
201     my $full_name="$ime $prezime,$loc,$sifra";
202     $full_name=~s/, +/,/g;
203     $full_name=~s/ +:/:/g;
204     $full_name=~s/ +/ /g;
205     $full_name=nuke_chars($full_name);
206     if ($shell eq "t") {
207     $shell="/usr/local/bin/bash";
208     } else {
209     $shell="/usr/bin/false";
210     }
211     my $dir="/usr/users/$login";
212    
213     die "Nema grupe $group!" if (! defined($group_id{$group}));
214     xdebug "$login:$passwd:$uid:$group_id{$group}:$full_name:$dir:$shell\n";
215    
216     print NPASSWD "$login:$crypt_passwd:$uid:$group_id{$group}:$full_name:$dir:$shell\n";
217     $dbh->do("insert into racuni (osoba_id,login,passwd) values ($osoba_id,'$login','$passwd')") if (! $debug);
218     $etc_passwd{$login}=$full_name;
219    
220     if (defined($etc_aliases{$email_alias})) {
221 dpavlin 1.3 xerror "upozorenje: $ime $prezime osoba_id=$osoba_id (postoji e-mail alias) -- $email_alias\n";
222 dpavlin 1.1 print NALIAS "$email_alias:\t$login,$etc_aliases{$email_alias}\n";
223     } else {
224     print NALIAS "$email_alias:\t$login\n";
225     }
226    
227     $dbh->do("insert into e_mail (osoba_id,alias) values ($osoba_id,'$email_alias')") if (! $debug);
228     $etc_aliases{$email_alias}=$login;
229    
230     #---- C2 security
231     # if (!$debug || 1) {
232     # open(AUTH,">> $0.auth");
233     # print AUTH "$login:u_name=$login:u_id#$uid:u_pwd=$crypt_passwd:u_succhg#",time,":u_lock\@:chkent:\n";
234     # close(AUTH);
235     # }
236    
237     system "mkdir -p $dir ; mkdir $dir/bin ; \
238     chown -R $uid:$group_id{$group} $dir ; chmod 755 $dir" if (! $debug);
239    
240     $dbh->do("insert into status (osoba_id,kontakt_osoba_id,datum,status_tip_id) values ($osoba_id,3,'now'::datetime,3)") if (! $debug);
241    
242     $max_osoba_id = $osoba_id if ($max_osoba_id < $osoba_id) ;
243     }
244    
245     $dbh->do("insert into last_open values ('now',$max_osoba_id)") if (! $debug);
246    
247     close(NPASSWD);
248     close(NALIAS);
249    
250     undef $sth;
251     $dbh->disconnect;
252    
253     system "mv /etc/passwd passwd.orig" if (! $debug);
254     system "cp passwd.new /etc/passwd" if (! $debug);
255     system "chmod 644 /etc/passwd" if (! $debug);
256     system "/usr/sbin/mkpasswd /etc/passwd" if (! $debug);
257     system "mv /etc/aliases aliases.orig" if (! $debug);
258     system "cp aliases.new /etc/aliases" if (! $debug);
259     system "chmod 644 /etc/aliases" if (! $debug);
260     system "/usr/sbin/newaliases" if (! $debug);
261     #system "/tcb/bin/edauth -s < $0.auth" if (! $debug);
262    
263     foreach my $alias (@mail_to) {
264     system "/usr/lib/sendmail $alias < mail.txt" if (! $debug);
265     }

  ViewVC Help
Powered by ViewVC 1.1.26