/[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.2 - (show annotations)
Wed Apr 12 09:10:20 2000 UTC (19 years, 2 months ago) by dpavlin
Branch: MAIN
Changes since 1.1: +12 -6 lines
podrka za otvaranje samo jednog accounta, popravljeno traenje novih logina

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

  ViewVC Help
Powered by ViewVC 1.1.26