summaryrefslogtreecommitdiffstats
path: root/nisautofs_wizard/Nisautofs.pm
blob: 13f3c5e0961a7f603e758d39394b11301953f8fe (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#/usr/bin/perl -w
# version 0.3
#
# Copyright (C) 2002,2003 Mandrakesoft
#
# Author: aginies _at_ mandrakesoft.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

package MDK::Wizard::Nisautofs;
use lib qw(/usr/lib/libDrakX);
use strict;
#use standalone; 
use common;
use services;
#use MDK::Wizard::Varspaceval;
use MDK::Wizard::Wizcommon;


# test root capa
my $wiz = new MDK::Wizard::Wizcommon;

#my $NISDOMAIN = $wiz->{net}->network_get("NISDOMAIN");
my $NISDOMAIN = chomp_(`dnsdomainname`);
my @HOST = split('\.', $wiz->{net}->network_get("HOSTNAME"));
my $HOST = @HOST[0]; # just need basename and not full qualified computer hostname (with domainaname)
my $NIS_DIRMAKEFILE = "/var/yp/";
my $HOMENIS = "/home/nis";
my $NETWORKFILE = "/etc/sysconfig/network";
my $NFSEXPORTS = "/etc/exports";
my $AUTOMASTER = "/etc/auto.master";
my $AUTOHOME = "/etc/auto.home";
my $YPCONF = "/etc/yp.conf";

my $o = {
	 name => 'NIS Configuration Wizard',
	 var => {
		 NISDOMAIN => chomp_($NISDOMAIN),
		 NISSERVER => chomp_($HOST),
		 NIS_DIRMAKEFILE => $NIS_DIRMAKEFILE,
		 HOMENIS => $HOMENIS,
		 NETWORKFILE => $NETWORKFILE,
		 NFSEXPORTS => $NFSEXPORTS,
		 AUTOMASTER => $AUTOMASTER,
		 AUTOHOME => $AUTOHOME,
		},
	 needed_rpm => [ 'ypbind', 'nfs-utils', 'yp-tools', 'nfs-utils-clients' ],
	 defaultimage => "$ENV{__WIZ_HOME__}/nisautofs_wizard/images/nisautofs.png",
	 init => sub {
	   if (member($NISDOMAIN, qw(localdomain (none)))) {
             return 0, N("You need to readjust your NIS domainname. For a NIS server you need a correct NIS domainname, not equal to localdomain or none.")
           }
           1
	 },
	};

my %level = (
             1 => N("NIS server with autofs map"),
             2 => N("NIS client"),
             );

$o->{pages} = {
	       welcome => {
			   name => N("NIS server with autofs map") . "\n\n" . N("Setup a NIS server with autofs map, auto.home and auto.master files.") . "\n\n" . N("Users automatically mount their home directory from server, when they log on a NIS client computer network."),
			   no_back => 1,
			   pre => sub { $o->{var}{wiz_level} ||= 1 },
			   post => sub { if ($o->{var}{wiz_level} == 2) {
			     return 'nis_client'
			   }
				       },
			   data => [
				    { label => N("What do you want to do ?"), val => \$o->{var}{wiz_level}, list => [ keys %level ], format => sub { $level{$_[0]} } },
				   ],
			   next => 'nis_server',
			  },
	       nis_client => {
			      name => N("Configure computer to be a NIS client") . "\n\n" . N("You just have to put nisdomain and nisserver."),
			      data => [
				       { label => N("NIS server:"), val => \$o->{var}{NISSERVER} },
				       { label => N("NIS domain:"), val => \$o->{var}{NISDOMAIN} },
				      ],
				  next => 'summaryclient',
			     },
	       nis_server => {
			      name => N("NIS server with autofs map") . "\n\n" . N("A NIS server is useful to create user, hostname database. The wizard also build autofs map, so it provides the capabilities for NIS user to automount their home directory on a NIS client computer.") . "\n\n" . N("NIS server: name of your computer.")  . "\n" . N("Home NIS: home base directory for users on NIS server. This directory will be exported through NFS server.") . "\n" . N("NIS domain: NIS domain to use (generally same as your DNS domain name)."),
			      data => [
				       { label => N("NIS server:"), val => $HOST },
				       { label => N("Home NIS:"), val => \$o->{var}{HOMENIS} },
				       { label => N("NIS domainname:"), val => \$o->{var}{NISDOMAIN} },
				      ],
			      post => \&test_set,
			      next => 'summaryserver',
			     },
	       summaryserver => {
				 name => N("The wizard will set your NIS server with autofs map"),
				 data => [
					  { label => N("NIS server:"), fixed_val => \$HOST },
					  { label => N("Home NIS:"), fixed_val => \$o->{var}{HOMENIS} },
					  { label => N("NIS domainname:"), fixed_val => \$o->{var}{NISDOMAIN} },
					  { label => N("NIS directory:"), fixed_val => \$o->{var}{NIS_DIRMAKEFILE} },
#					  { label => N("Network file:"), fixed_val => \$o->{var}{NETWORKFILE} },
#					  { label => N("NFS exports:"), fixed_val => \$o->{var}{NFSEXPORTS} },
#					  { label => N("Auto master:"), fixed_val => \$o->{var}{AUTOMASTER} },
#					  { label => N("Auto home:"), fixed_val => \$o->{var}{AUTOHOME} },
					 ],
				 post => \&do_it_server,
				     next => 'endserver',
				},
	       summaryclient => {
				 name => N("The YPBIND daemon finds the server for NIS domains and maintains the NIS binding information.") . "\n\n" . N("NIS server: hostname of the NIS server.") . "\n" . N("NIS domainname: name of NIS domain."),
				 data => [
					  { label => N("NIS server:"), fixed_val => \$o->{var}{NISSERVER} },
					  { label => N("NIS domainname:"), fixed_val => \$o->{var}{NISDOMAIN} },
					 ],
				 post => \&do_it_client,
				 next => 'endclient',
				},
	       error_homedir => {
				 name => N("Error: should be a directory."),
				 next => 'nis_server',
				},
	       error_nisd => {
			      name => N("Error: nisdomainame should not be 'none' or 'localdomain'.") . "\n\n" . N("Please adjust it."),
			      end => 1,
			      next => 0,
			     },
	       endclient => {
			     name => N("Congratulations"),
			     data => [ { label => N("The wizard successfully configured your machine to be a NIS client.") } ],
			     no_back => 1,
			     end => 1,
			     next => 0
			    },
	       endserver => {
			     name => N("Congratulations"),
			     data => [ { label => N("The wizard successfully configured your machine to be a NIS server with autofs map.") } ],
			     no_back => 1,
			     end => 1,
			     next => 0
			    },
	       error_end => {
			     name => N("Failed"),
                             data => [ { label => N("Relaunch drakwizard, and try to change some parameters.") } ],
                             no_back => 1,
                             end => 1,
                             next => 0,
                            },
	      };


sub test_set {
  if (member($o->{var}{NISDOMAIN}, qw(localdomain (none)))) { return 'error_nisd' }
  !-d $o->{var}{HOMENIS} or return 'error_dir';
  !-d $o->{var}{NIS_DIRMAKEFILE} or return 'error_dir';
}
# update /etc/sysconfig/network
sub update_network {
  save_config($NETWORKFILE);
  if (any { /NISDOMAIN/ } cat_($NETWORKFILE)) {
    substInFile { s/NISDOMAIN.*/NISDOMAIN=$o->{var}{NISDOMAIN}/g } $NETWORKFILE;
  } else {
    append_to_file($NETWORKFILE, "NISDOMAIN=$o->{var}{NISDOMAIN}\n");
  }
}

# Setting nisdomainname
sub set_nisdomain {
  system("nisdomainname $o->{var}{NISDOMAIN}");
}


sub nfs_home_nis {
  # test directory
  $HOMENIS = $o->{var}{HOMENIS};
  if (-d $HOMENIS) {
    print " - $HOMENIS directory exist\n";
  } else {
    print " - Creating $HOMENIS directory\n";
    mkdir_p($HOMENIS);
  }

  # check nis home
  if (any { /$HOMENIS/ } cat_($NFSEXPORTS)) {
    print " - " . $NFSEXPORTS . " ready\n";
  } else {
    print " - Adjusting " . $NFSEXPORTS . "\n";
    append_to_file($NFSEXPORTS, "$HOMENIS 	*(async,rw,no_root_squash)\n");
  }
}

# configure /etc/auto.*
sub configure_auto {
  save_config($AUTOMASTER);
  my $nisserver = chomp_($o->{var}{NISSERVER});
  output($AUTOMASTER, <<EOF);
$HOMENIS auto.home     --timeout=60
EOF

  save_config($AUTOHOME);
  output($AUTOHOME, <<EOF);
* -rw,nfs,soft,intr,nosuid,rsize=8192,wsize=8192       $nisserver:$HOMENIS/&
EOF
}

sub adjust_makefile {
# Makefile parameter
  print " - Updating " . $NIS_DIRMAKEFILE . " to add autofs\n";
  mkdir_p('/etc/mail');
  system('touch /etc/mail/aliases');
  substInFile {
    s!^ALIASES!#ALIASES=/etc/aliases!g;
    s/^all.*/all:  passwd group hosts rpc services netid protocols auto.master auto.home/g;
  } $NIS_DIRMAKEFILE . '/Makefile';
}

# create yp base
sub make_yp {
  system("make -C $NIS_DIRMAKEFILE");
}

sub needed_service {
  my ($service) = @_;
  if (services::is_service_running($service)) {
    services::restart($service)
    } else { services::start($service) }
}

sub get_nis_users {
  my @unwanted = qw(install maui nobody mpi);
  my @users;
  local *PASS;
  open(PASS, "ypcat passwd|") or die " cant exec ypcat passwd!";
  while (<PASS>) {
    my ($login) = split(':');
    if (!member($login, @unwanted)) {
      push(@users, $login);
    }
  }
  close(PASS);
  [ @users ];
}

sub update_yp {
  my ($nd, $yps) = @_;
  save_config($YPCONF);
  output($YPCONF, <<EOF);
# /etc/yp.conf - ypbind configuration file
# domain NISDOMAIN server HOSTNAME
# Use server HOSTNAME for the domain NISDOMAIN.
#domain NISDOMAIN broadcast
#       Use  broadcast  on  the local net for domain NISDOMAIN
#
#ypserver HOSTNAME
#       Use server HOSTNAME for the  local  domain.  The
#       IP-address of server must be listed in /etc/hosts.
#
domain $nd server $yps
EOF
}

sub test_autofile {
    map { if (-e $_) { rm_rf($_) }; $_ } $AUTOHOME, $AUTOMASTER;
}

sub save_config {
  my ($old) = @_;
  my $DATE = chomp_(`date +%d-%m-20%y`);
  if (-f $old) {
    cp_af($old, $old . '.' . $DATE);
  }
  return($old . '.' . $DATE);
}

sub update_network_nisdomain {
  my ($nd) = @_;
  my $nf = cluster_commonconf::system_network()->{NETWORKFILE};
  my %conf = getVarsFromSh($nf);
  save_config($nf);
  setVarsInSh($nf, { %conf, NISDOMAIN => $nd });
}

sub update_nsswitch {
  substInFile {
    s/automount:.*/automount:  nis nisplus files/g;
    s/hosts.*/hosts:      dns nis files/;
  } '/etc/nsswitch.conf';
}

sub uninstall_rpm {
    my ($rpm) = @_;
    system("urpme $rpm --auto");# if (system('/bin/rpm', '>/dev/null', '-q', $rpm));
}

sub do_it_server {
    return if $::testing;
    my $in = 'interactive'->vnew('su', 'nisautofsconfig');
    my $w = $in->wait_message(N("NIS with Autofs map"), N("Configuring your system to be a NIS server with Autofs map..."));
#    system("urpme --auto autofs") if (system("/bin/rpm -q autofs >/dev/null"));
    uninstall_rpm('autofs');
    system("urpmi --auto ypserv");
    needed_service('ypserv');
    # configure autofs to fit nis
    configure_auto();
    # set correct nis
    set_nisdomain();
    # create all make file needed
    adjust_makefile();
    make_yp();
    # update client conf file (also on server)
    update_yp(chomp_($o->{var}{NISDOMAIN}), chomp_($HOST));
    # update conf file
    update_network();
    # update nsswitch.conf
    update_nsswitch();
    # update nfs to fit nis
    nfs_home_nis();
    # restart all needed services
    needed_service($_) foreach qw(ypserv nfs);
    undef $w;
    check_started($_) foreach qw(ypserv nfsd);
}

sub do_it_client {
    return if $::testing;
    my $in = 'interactive'->vnew('su', 'nisautofsclient');
    my $w = $in->wait_message(N("NIS client"), N("Configuring your system as NIS client ..."));
    uninstall_rpm('ypserv');
    system("urpmi --auto autofs");
    test_autofile();
    # set correct nis
    set_nisdomain();
    # update client conf file (also on server)
    update_yp(chomp_($o->{var}{NISDOMAIN}), chomp_($o->{var}{NISSERVER}));
    # update conf file
    update_network();
    needed_service($_) foreach qw(ypbind autofs);
    undef $w;
    check_started($_) foreach qw(ypbind autofs);
}

sub new {
    my ($class) = @_;
    bless {
            o   => $o,
       }, $class;
}

1;