File: postinst

package info (click to toggle)
gnudip 2.1.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 252 kB
  • ctags: 24
  • sloc: perl: 2,088; sh: 95; makefile: 43
file content (274 lines) | stat: -rw-r--r-- 9,369 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/perl
# Adapted from the original install script, but modified extensively 
#   for Debian by Randolph Chung <tausq@debian.org> Feb 20, 1999
# (c) 1999 GPL

use strict;
use DBI;
use MD5;

require '/usr/share/gnudip/gnudip-lib.pl';

my ($sth, $dbtype, $onboot, $initdir, $isinuser, $isindb);
my ($mysqlmajor, $mysqlminor, $dbhost, $dontcreatedb);
my $VER = "2.0.6";

exit 0 if ($ARGV[0] ne "configure");

if (-f "/etc/gnudip.conf") {
  print "/etc/gnudip.conf already exists, not reconfiguring";
  exit 0;
}

# /usr/doc -> /usr/share/doc transition
if (-d "/usr/doc/gnudip") {
  system("rm -rf /usr/doc/gnudip");
}

if ((-d "/usr/doc") && (! -e "/usr/doc/gnudip") && (-d "/usr/share/doc/gnudip")) {
  symlink("/usr/share/doc/gnudip", "/usr/doc/gnudip") || die $!;
}

# Chmod the config file
my ($uid, $gid) = ((getpwnam("gnudip"))[2], (getgrnam("root"))[2]);
chown $uid, $gid, "/etc/gnudip.conf";

print "\nThe Debian GnuDIP package contains both client and server software.\n";
print "If you are just using the gdipc client, you may skip the rest of the\n";
print "installation (server-specific). Continue? (Y/n)";
my $continue=<STDIN>;
chomp($continue);
exit 0 if (($continue eq "N") || ($continue eq "n"));

print "\n-Setting up database for GnuDIP version $VER-\n\n";

print "Detecting MySQL version installed... ";
$mysqlmajor = `mysqld --version`;
chop($mysqlmajor);
$mysqlmajor =~ s/^[^\d]*(\d+\.\d+\.\d+).*$/$1/;
if ($mysqlmajor) {
  print "$mysqlmajor ";
  $mysqlminor = $mysqlmajor;
  $mysqlminor =~ s/^\d+\.(\d+\.\d+).*$/$1/;
  $mysqlmajor =~ s/^(\d+)\.\d+\.\d+.*$/$1/;
  if ($mysqlmajor < 3 or ($mysqlmajor == 3 and $mysqlminor < 22.11)) {
    $dbtype = 1;
    print "(old format)\n";
  } else {
    $dbtype = 2;
    print "(new format)\n";
  }

  if ( `grep -c '^ *skip-networking' /etc/mysql/my.cnf` ) {
      $dbhost = 'localhost';
      print "Using unix sockets to connect to the database\n" ;
  } else {
      $dbhost = '127.0.0.1';
      print "Using TCP sockets to connect to the database\n" ;
  }

  if (-e "/var/lib/mysql/gnudip2") {
    print "Looks like you already have tables set up.... \n";
    print "         ********* WARNING ************ WARNING ***********     \n";
    print " This version of GnuDIP breaks backward compatibility. Your databases\n";
    print " are being left alone right now. When you are ready to upgrade, run\n";
    print " the upgrade_gnudip script which will create a blank database with the\n";
    print " new schema\n\n";
    $dontcreatedb = 1;
  }
} else {
  print "sorry, i can't figure it out....\n";
  print "On which host is the database located? [127.0.0.1] ";
  $dbhost = <STDIN>;
  chomp($dbhost);
  $dbhost = '127.0.0.1' if ($dbhost eq "");
  while ($dbtype ne "1" && $dbtype ne "2") {
    print "Please select database type \n\t\t 1 - MySQL < 3.22.11\n\t\t[2 - MySQL >= 3.22.11] ";
    $dbtype = <STDIN>;
    chop($dbtype);
    if ($dbtype eq "") {
      $dbtype = "2";
    }
  }
}

my $gnudipuser = 'gnudip';
print "GnuDIP uses a password to authenticate transactions with the MySQL database.\nEnter the password you wish to use for this purpose: ";
my $gnudippass = &ReadPassword;

# write some stuff to the config file....
open (ETC, "/etc/gnudip.conf") || die $!;
my @etc = <ETC>;
close ETC;
open (ETC, ">/etc/gnudip.conf") || die $!;
foreach (@etc) {
  if (/^gnudippassword\s*=/) {
    print ETC "gnudippassword = $gnudippass\n";
  } elsif (/^gnudiphost\s*=/) {
    print ETC "gnudiphost = $dbhost\n";
  } else {
    print ETC;
  }
}
close ETC;
my @pwent;
@pwent = getpwnam('gnudip');
my $fileuid = $pwent[2];
@pwent = getpwnam('www-data');
my $filegid = $pwent[3];
chown($fileuid, $filegid, '/etc/gnudip.conf') || die "chown";
chmod(0640, '/etc/gnudip.conf') || die "chmod";

if (!$dontcreatedb) {
  print "\n\nIn order to set up tables in the database, we need a user with permissions\n";
  print "to create tables in the database.\nPlease enter the name of such a user [root]: ";
  my $rootmysqluser = <STDIN>;
  chomp($rootmysqluser);
  if ($rootmysqluser eq "") {
    $rootmysqluser = "root";
  }

  print "Enter $rootmysqluser\'s mysql password: ";
  my $rootmysqlpass = &ReadPassword;
  
  print "\n\nFinally, we need to set up a password for an admin who can change\n";
  print "settings online: ";
  my $adminpassword = &ReadPassword;

  my $mysqlpassstring;
  if ($rootmysqlpass eq '') {
    $mysqlpassstring = "";
  } else {
    $mysqlpassstring = "-p$rootmysqlpass";
  }

  ######### DO INSTALL STUFF ################
  print "\n\n*********** CREATING TABLES *************\n";

  system("mysqladmin -u$rootmysqluser -h$dbhost $mysqlpassstring create gnudip2") && die "mysqldmin";
  system("mysql -u$rootmysqluser -h$dbhost $mysqlpassstring gnudip2 < /usr/share/doc/gnudip/gnudip2.db") && die "mysql";

  my $dbh = DBI->connect("DBI:mysql:database=mysql;host=$dbhost",$rootmysqluser, $rootmysqlpass) || die "Could not connect to database server. Invalid login?\n";
  $sth = $dbh->prepare("SELECT user
                          FROM user
                         WHERE user = ?");
  $sth->execute($gnudipuser) || die $dbh->errstr;
  $isinuser = $sth->fetchrow_array;
  $sth = $dbh->prepare("SELECT user
                          FROM db
                         WHERE user = ? AND db = ?");
  $sth->execute($gnudipuser,"gnudip2") || die $dbh->errstr;
  $isindb = $sth->fetchrow_array;

  if ($dbtype eq "1") {
    if ($isinuser eq '') {
      $sth = $dbh->do("INSERT
                         INTO user (host, user, password)
                       VALUES ('localhost', '$gnudipuser',
                                password('$gnudippass'))")
             || die "Could not add user to MySQL database\n";
    } else {
      $sth = $dbh->do("UPDATE user
                          SET password = password('$gnudippass')
                        WHERE user = '$gnudipuser'")
             || die "Could not add user to MySQL database\n";
    }
    if ($isindb eq '') {
      $sth = $dbh->do("INSERT
                         INTO db (host, db, user, select_priv, insert_priv,
                                  update_priv, delete_priv, create_priv,
                                  drop_priv)
                       VALUES ('localhost', 'gnudip2', '$gnudipuser',
                               'Y', 'Y', 'Y', 'Y', 'N', 'N')")
             || die "Could not add database rule entry\n";
    }

  } elsif ($dbtype eq "2") {
    if ($isinuser eq '') {
      $sth = $dbh->do("INSERT
                         INTO user (host, user, password)
                       VALUES ('localhost', '$gnudipuser',
                               password('$gnudippass'))")
             || die "Could not add user to MySQL database\n";
    } else {
      $sth = $dbh->do("UPDATE user
                          SET password = password('$gnudippass')
                        WHERE user = '$gnudipuser'")
             || die "Could not add user to MySQL database\n";
    }
    if ($isindb eq '') {
      $sth = $dbh->do("INSERT
                         INTO db (host, db, user, select_priv, insert_priv,
                                  update_priv, delete_priv, create_priv,
                                  drop_priv, grant_priv, references_priv,
                                  index_priv, alter_priv)
                       VALUES ('localhost', 'gnudip2', '$gnudipuser',
                               'Y', 'Y', 'Y', 'Y', 'N', 'N',
                               'N', 'N', 'N', 'N')")
             || die "Could not add database rule entry\n";
    }
  }
  undef $sth;
  $dbh->disconnect;

  $dbh = DBI->connect("DBI:mysql:database=gnudip2;host=$dbhost",$rootmysqluser, $rootmysqlpass) || die "Could not connect to database server. Invalid login?\n";
  my $encpass = gensum($adminpassword);
  $sth = $dbh->do("UPDATE users
  		      SET username 	= 'admin',
			  password 	= '$encpass',
			  forwardurl 	= 'http://gnudip.cheapnet.net',
			  updated 	= '1999-01-18 14:33:04',
			  level		= 'ADMIN',
			  currentip	= '1.1.1.1',
			  autourlon	= 'NO'
		    WHERE id = 1")
	|| die "Cannot update admin user ".$dbh->errstr;
  undef $sth;

  system("mysqladmin -u$rootmysqluser $mysqlpassstring reload") && die "reload";

  $dbh->disconnect;
}

my $hostname = `hostname --fqdn`;
chomp($hostname);
print <<EOF;
After the server has started, go to 
      http://$hostname/cgi-bin/gnudip2.cgi
and log in as 'admin' to change your settings.

If you do not already have mysql server and bind setup, you should do that 
before attempting to use this package. They can run on different machines than 
the one on which gnudip runs.

You also need to set up a DNS zone file for your dynamic domain. Please
see /usr/share/doc/gnudip/README for details.
EOF

# Set up the init.d stuff...
system ("update-rc.d gnudip defaults 99 49 >/dev/null") && die "update-rc.d";
system ("/etc/init.d/gnudip start") && die "Cannot start gnudip";

exit 0;

sub ReadPassword {
  system("/bin/stty -echo") && die "stty";
  my $r = <STDIN>;
  chomp($r);
  system("/bin/stty echo") && die "stty";
  print "\n";
  return $r;
}

sub TestQuery {
  # returns true if the query runs successfully  
  my $dbhost; my $db = shift; my $sql = shift;

  my $dbh = DBI->connect("DBI:mysql:database=$dbhost");
  return 0 if (!$dbh);
  my $sth = $dbh->do($sql);
  $dbh->disconnect;
  return 0 if (!$sth);
  undef $sth;
  return ($sth->rows > 0);
}