~ruvolof/cvmfs-test/trunk

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
package Functions::Setup;

####################################################
# Here will be stored all the functions needed to setup the environment
# for the daemon
####################################################

use strict;
use warnings;
use Fcntl ':mode';
use Functions::Shell qw(check_daemon);

# The next line is here to help me find the directory of the script
# if you have a better method, let me know.
use FindBin qw($Bin);

# Next lines are needed to export subroutines to the main package
use base 'Exporter';
use vars qw/ @EXPORT_OK /;
@EXPORT_OK = qw(setup fixperm);

sub setup {
	if (!Functions::Shell::check_daemon()){
		print "You will need to be in the sudoers file to complete the setup process. Are you? [N,y]";
		my $answer = <STDIN>;
		
		my $compile_success = compile_wrapper();
		my $user_added = create_user();
		my $wrapper_chown = chown_wrapper();
		my $wrapper_setuid = setuid_wrapper();
		my $log_folder = create_log_folder();
		my $add_to_sudoers = add_to_sudoers();
	
		print "\n";
		print '_' x 80 . "\n";
		print "Setup complete. Run 'fixperm' to be sure all permission are set correctly.\n";
		print "You can type 'help fixperm' to show what this command does.\n";
		print '_' x 80 . "\n";
		
	}
	else {
		print "The daemon is running. Can't run setup while the daemon is running.\nStop it and retry.\n";
	}
}

# This function will try to compile the c wrapper for the script. It will return 1 on success and 0 on failure.
sub compile_wrapper {
	if (-e '/usr/bin/gcc') {
		print "Compiling the wrapper... \n";
		my $compile = system("/usr/bin/gcc -o $Bin/cvmfs-testdwrapper $Bin/cvmfs-testdwrapper.c");
		if ($compile == -1){
			print "FAILED: $!\n";
			return 0;
		}
		else {
			print ("Done.\n");
			return 1;
		}
	}
	else {
		print "It seems you don't have gcc installed on your system. Install it and retry.\n";
		return 0;
	}
}

# This function will try to create a new user for the daemon, return 1 on success and 0 on failure
sub create_user {
	# Checking if the user exists in the system
	my $user = `cat /etc/passwd | grep cvmfs-test`;
	# If it doesn't, create it
	if (!$user) {
		if (-e '/usr/sbin/useradd') {
			print "Adding user 'cvmfs-test'.\n";
			my $added = system('sudo /usr/sbin/useradd -r --key UMASK=0000 cvmfs-test');
			if ($added == -1) {
				print "FAILED: $!\n";
				return 0;
			}
			else {
				print "User 'cvmfs-test' added.\n";
				return 1;
			}
		}
		else {
			print "It seems you don't have useradd on your system. Install it and retry.\n";
			return 0;
		}
	}
	else {
		print "User already present on the system.\n";
		return 1;
	}
}

# This function will check if the wrapper is owned by the user cvmfs-test, return 1 on success and 0 on failure
sub chown_wrapper {
	# Checking if the user own the daemon wrapper
	my $uid = (stat("$Bin/cvmfs-testdwrapper"))[4];
	my $owner = (getpwuid($uid))[0];
	if($owner ne 'root') {
		print "Changing the owner of the wrapper... \n";
		my $chowned = system("sudo chown root:root $Bin/cvmfs-testdwrapper");
		if ($chowned == -1){
			print "FAILED: $!\n";
			return 0;
		}
		else {
			print "Done.\n";
			return 1;
		}
	}
	else {
		print "Wrapper already owned by cvmfs-test.\n";
		return 1;
	}
}

# This function will add the setuid byte to wrapper permission
sub setuid_wrapper {
	# Checking if the file has the setuid bit
	my $mode = (stat("$Bin/cvmfs-testdwrapper"))[2];
	my $suid = $mode & S_ISUID;
	
	if($suid) {
		print "setuid byte already set on the wrapper.\n";
		return 1;
	}
	else {
		print "Adding setuid byte... \n";
		my $setuid = system("sudo chmod u+s $Bin/cvmfs-testdwrapper");
		if ($setuid == -1){
			print "FAILED: $!\n";
			return 0;
		}
		else {
			print "Done.\n";
			return 1;
		}
	}
}

# This function will set all permission for files and directories
sub fixperm {
	print 'Setting permission for modules to 644... ';
	system("find -type f -name \"*.pm\" -exec chmod 644 {} +");
	print "Done.\n";
	
	print 'Setting permission for executables to 755... ';
	system("find -type f -name \"*.pl\" -exec chmod 755 {} +");
	print "Done.\n";
	
	print 'Setting permission for directories to 755... ';
	system("find -type d -exec chmod 755 {} +");
	print "Done.\n";
	
	print 'Setting permission for help files to 644... ';
	system("find -type f -name \"*help\" -exec chmod 644 {} +");
	print "Done.\n";
	
	print 'Setting permission for bash script to 755...';
	system("find -type f -name \"*.sh\" -exec chmod 755 {} +");
	print "Done.\n";
}

sub create_log_folder {
	unless( -e '/var/log/cvmfs-test' ) {
		print 'Creating /var/log/cvmfs-test... ';
		system("sudo mkdir -p /var/log/cvmfs-test");
		print "Done.\n";
	}
	else {
		print "Log folder already present on the system.\n";
	}
	
	my $uid = (stat('/var/log/cvmfs-test'))[4];
	my $owner = (getpwuid($uid))[0];
	
	unless ($uid eq 'cvmfs-test') {
		print 'Setting owner and permission on the log folder...';
		system("sudo chown cvmfs-test /var/log/cvmfs-test");
		system("sudo chmod 777 /var/log/cvmfs-test");
		print "Done.\n";
	}
	else {
		print "Log folder already owned by user cvmfs-test.\n";
	}
}

sub add_to_sudoers {
	my $sudoers = `sudo cat /etc/sudoers | grep "cvmfs-test ALL=(ALL) NOPASSWD: ALL"`;
	if ($sudoers) {
		print "User cvmfs-test is already in the /etc/sudoers file.\n";
	}
	else {
		print 'Adding cvmfs-test to the /etc/sudoers file...';
		system ('sudo sh -c "echo \"cvmfs-test ALL=(ALL) NOPASSWD: ALL\" >> /etc/sudoers"');
		print "Done.\n";
	}
}

1;