/[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.1 - (show annotations)
Wed Apr 12 08:19:17 2000 UTC (19 years, 2 months ago) by dpavlin
Branch: MAIN
Branch point for: PLIVA
Initial revision

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

  ViewVC Help
Powered by ViewVC 1.1.26