/[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

Contents of /otvori_nove+db

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Wed May 31 13:57:49 2000 UTC (19 years ago) by dpavlin
Branch: MAIN
Changes since 1.6: +17 -2 lines
remap unix korisnika i hosta u kontakt_osoba_id

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
12 use DBI;
13 use strict;
14
15 my $debug=0;
16
17 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
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 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 chomp $db_user;
45 $db_user=~s/\s.+$//g;
46 close(WHO);
47
48 xdebug "koristim korinika $db_user\n";
49
50 open(HOSTNAME,"hostname |") || die "hostname: $!";
51 my $hostname=<HOSTNAME>;
52 chomp $hostname;
53 close(HOSTNAME);
54
55 xdebug "koristim hostname $hostname\n";
56
57 v "/etc/aliases\n";
58 open(ALIAS,"/etc/aliases") || die "Can't open /etc/aliases: $!";
59 open(NALIAS,">aliases.new") || die "Can't open aliases.new: $!";
60 while(<ALIAS>) {
61 chop;
62 print NALIAS "$_\n";
63 next if (m!^#! || m!^$!);
64 my ($mail_alias,$mail_login);
65 if (m!\w+.\w+!) {
66 ($mail_alias,$mail_login) = split(/\s*:\s*/,$_,2);
67 $mail_alias =~ tr [A-Z] [a-z];
68 }
69 $etc_aliases{$mail_alias}=$mail_login;
70 # xdebug "$mail_alias:$mail_login\n";
71 }
72 close(ALIAS);
73
74 my %etc_passwd;
75 my $uid; # current (or max) uid
76
77 v "/etc/passwd\n";
78 open(PASSWD,"/etc/passwd") || die "Can't open /etc/passwd !";
79 open(NPASSWD,">passwd.new") || die "Can't open passwd.new !";
80 while(<PASSWD>) {
81 chop;
82 my ($login,$gid,$full_name,$dir,$shell);
83 ($login,undef,$uid,$gid,$full_name,$dir,$shell) = split(/:/,$_,7);
84 $etc_passwd{$login}=$full_name;
85 print NPASSWD "$_\n";
86 # xdebug "$login:$full_name\n";
87 }
88 close(PASSWD);
89
90 my %group_id;
91
92 v "/etc/group\n";
93 open(GROUP,"/etc/group") || die "Can't open /etc/group !";
94 while(<GROUP>) {
95 my ($group,undef,$gid,undef) = split (/:/,$_,4);
96 $group_id{$group}=$gid;
97 # xdebug "$group:$gid\n";
98 }
99 close(GROUP);
100
101 sub nuke_chars {
102 my $foo=$_[0];
103 $foo =~ tr/šðè枊ÐÈƎ/¹ðè澩ÐÈÆ®/; # cp1250 -> iso88592
104 $foo =~ s/ð/dj/g;
105 $foo =~ s/Ð/Dj/g;
106 $foo =~ tr/¹ðè澩ÐÈÆ®/sdcczSDCCZ/; # nuke iso88592
107 $foo =~ s/^\s+//g;
108 $foo =~ s/\s+$//g;
109 return $foo;
110 }
111
112 my $max_osoba_id=0;
113
114 my $dbh = DBI->connect("DBI:Pg:dbname=informatika;host=support.pliva.hr;user=$db_user","","") || die $DBI::errstr;
115
116 my $sth = $dbh->prepare("select ko_id from unix2ko where login='$db_user' and host='$hostname'") || die $dbh->errstr();
117 $sth->execute() || die $sth->errstr();
118 my ($ko_id)=$sth->fetchrow_array;
119
120 xdebug "upotrebljavam ID kontakt osobe: $ko_id\n";
121
122 $sth = $dbh->prepare("select max(id) from last_open") || die $dbh->errstr();
123 $sth->execute() || die $sth->errstr();
124 my ($last_open)=$sth->fetchrow_array;
125
126 if (! defined($open_only_id)) {
127 $sth = $dbh->prepare("select id,ime,prezime,sifra,objekt,kat,soba,shell from view_otvori_nove where id > $last_open") || die $dbh->errstr();
128 } else {
129 $sth = $dbh->prepare("select id,ime,prezime,sifra,objekt,kat,soba,shell from view_otvori_nove where id=$open_only_id") || die $dbh->errstr();
130
131 }
132 $sth->execute() || die $sth->errstr();
133
134
135 my $sth2;
136
137 while (my ($osoba_id,$ime,$prezime,$sifra,$objekt,$kat,$soba,$shell) = $sth->fetchrow_array() ) {
138
139 $sth2 = $dbh->prepare("select max(status_tip_id) from status where osoba_id = $osoba_id") || die $dbh->errstr();
140 $sth2->execute() || die $sth2->errstr();
141 my ($max_status) = $sth2->fetchrow_array();
142 xdebug "max status zahtjeva $osoba_id je $max_status\n";
143 next if ($max_status > 3); # 3 .. otvoren
144
145 next if ($ime eq "ime" || $prezime eq "prezime");
146
147 if (!defined($ime) || !defined($prezime) || $ime eq "" || $prezime eq "") {
148 xerror "preskacem red '$_'\n";
149 next;
150 }
151
152 $ime=nuke_chars($ime);
153 $prezime=nuke_chars($prezime);
154
155 my ($prezime_za_login,$ime_za_login);
156
157 if ($prezime =~ /[- ]/) { # dva prezimena prezime1-prezime2
158 ($prezime_za_login,undef) = split(/[- ]/,$prezime,2);
159 } else {
160 $prezime_za_login = $prezime;
161 }
162
163 if ($ime =~ /[- ]/) { # dva imena ime1-ime2
164 ($ime_za_login,undef) = split(/[- ]/,$ime,2);
165 } else {
166 $ime_za_login = $ime;
167 }
168
169 my $add="";
170 my $login;
171
172 do {
173 $login=substr($ime_za_login,0,1).substr($prezime_za_login,0,7-length($add)).$add;
174 $login =~ tr [A-Z] [a-z];
175 xdebug "$ime, $prezime login: $login\n";
176 $add++;
177 } while defined($etc_passwd{$login});
178
179 my $email_alias;
180
181 $add="";
182 do {
183 $email_alias="$ime.$prezime$add";
184 $email_alias =~ s/ /./g; # space -> dot
185 $email_alias =~ tr [A-Z] [a-z];
186 xdebug "\t$email_alias\n";
187 $add++;
188 } while defined($etc_aliases{$login});
189
190
191 my $group="users";
192
193 if (defined($etc_passwd{$login})) {
194 xerror "preskocen: $ime $prezime osoba_id=$osoba_id (postoji isti login $login)\n";
195 $dbh->do("insert into status (osoba_id,kontakt_osoba_id,datum,status_tip_id) values ($osoba_id,3,'now'::datetime,4)") if (! $debug);
196 next;
197 }
198
199 push @mail_to,$email_alias;
200
201 my @saltch = ( 'a'..'z','0'..'9','A'..'Z' );
202 my @passwdch = ( 'a'..'k','m'..'z','2'..'9','A'..'H','J'..'N','P'..'Z',
203 '!','#','$','%','&','(',')','=','-','/','?' );
204
205 my $passwd="";
206 for (1..8) { $passwd.=$passwdch[int(rand($#passwdch))] };
207
208 my $salt = $saltch[int(rand($#saltch))].$saltch[int(rand($#saltch))];
209 my $crypt_passwd = crypt($passwd,$salt);
210
211 $uid++;
212 my $loc="";
213 $loc.=$objekt if (defined($objekt));
214 $loc.=" $kat" if (defined($kat));
215 $loc.=" $soba" if (defined($soba));
216 my $full_name="$ime $prezime,$loc,$sifra";
217 $full_name=~s/, +/,/g;
218 $full_name=~s/ +:/:/g;
219 $full_name=~s/ +/ /g;
220 $full_name=nuke_chars($full_name);
221 if ($shell) {
222 $shell="/usr/local/bin/bash";
223 } else {
224 $shell="/usr/bin/false";
225 }
226 my $dir="/usr/users/$login";
227
228 die "Nema grupe $group!" if (! defined($group_id{$group}));
229 xdebug "$login:$passwd:$uid:$group_id{$group}:$full_name:$dir:$shell\n";
230
231 print NPASSWD "$login:$crypt_passwd:$uid:$group_id{$group}:$full_name:$dir:$shell\n";
232 $dbh->do("insert into racuni (osoba_id,login,passwd) values ($osoba_id,'$login','$passwd')") if (! $debug);
233 $etc_passwd{$login}=$full_name;
234
235 if (defined($etc_aliases{$email_alias})) {
236 xerror "upozorenje: $ime $prezime osoba_id=$osoba_id (postoji e-mail alias) -- $email_alias\n";
237 print NALIAS "$email_alias:\t$login,$etc_aliases{$email_alias}\n";
238 } else {
239 print NALIAS "$email_alias:\t$login\n";
240 }
241
242 $dbh->do("insert into e_mail (osoba_id,alias) values ($osoba_id,'$email_alias')") if (! $debug);
243 $etc_aliases{$email_alias}=$login;
244
245 #---- C2 security
246 # if (!$debug || 1) {
247 # open(AUTH,">> $0.auth");
248 # print AUTH "$login:u_name=$login:u_id#$uid:u_pwd=$crypt_passwd:u_succhg#",time,":u_lock\@:chkent:\n";
249 # close(AUTH);
250 # }
251
252 system "mkdir -p $dir ; mkdir $dir/bin ; \
253 chown -R $uid:$group_id{$group} $dir ; chmod 755 $dir" if (! $debug);
254
255 $dbh->do("insert into status (osoba_id,kontakt_osoba_id,datum,status_tip_id) values ($osoba_id,$ko_id,'now'::datetime,3)") if (! $debug);
256
257 $max_osoba_id = $osoba_id if ($max_osoba_id < $osoba_id) ;
258 }
259
260 $dbh->do("insert into last_open values ('now',$max_osoba_id)") if (! $debug);
261
262 close(NPASSWD);
263 close(NALIAS);
264
265 undef $sth,$sth2;
266 $dbh->disconnect;
267
268 system "mv /etc/passwd passwd.orig" if (! $debug);
269 system "cp passwd.new /etc/passwd" if (! $debug);
270 system "chmod 644 /etc/passwd" if (! $debug);
271 system "/usr/sbin/mkpasswd /etc/passwd" if (! $debug);
272 system "mv /etc/aliases aliases.orig" if (! $debug);
273 system "cp aliases.new /etc/aliases" if (! $debug);
274 system "chmod 644 /etc/aliases" if (! $debug);
275 system "/usr/sbin/newaliases" if (! $debug);
276 #system "/tcb/bin/edauth -s < $0.auth" if (! $debug);
277
278 foreach my $alias (@mail_to) {
279 system "/usr/lib/sendmail $alias < mail.txt" if (! $debug);
280 }

  ViewVC Help
Powered by ViewVC 1.1.26