4 # InspIRCd -- Internet Relay Chat Daemon
6 # Copyright (C) 2012-2017 Peter Powell <petpow@saberuk.com>
7 # Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8 # Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
9 # Copyright (C) 2003, 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
10 # Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
11 # Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
12 # Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
13 # Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
14 # Copyright (C) 2003-2006 Craig McLure <craig@chatspike.net>
16 # This file is part of InspIRCd. InspIRCd is free software: you can
17 # redistribute it and/or modify it under the terms of the GNU General Public
18 # License as published by the Free Software Foundation, version 2.
20 # This program is distributed in the hope that it will be useful, but WITHOUT
21 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25 # You should have received a copy of the GNU General Public License
26 # along with this program. If not, see <http://www.gnu.org/licenses/>.
36 use warnings FATAL => qw(all);
38 use File::Basename qw(basename);
40 use File::Spec::Functions qw(rel2abs);
41 use FindBin qw($RealDir);
42 use Getopt::Long qw(GetOptions);
43 use POSIX qw(getgid getuid);
55 $opt_disable_interactive,
56 $opt_distribution_label,
69 sub enable_extras (@);
71 sub disable_extras (@);
74 my @opt_disableextras;
77 'clean' => \&cmd_clean,
79 'update' => \&cmd_update,
81 'development' => \$opt_development,
82 'disable-interactive' => \$opt_disable_interactive,
83 'distribution-label=s' => \$opt_distribution_label,
84 'binary-dir=s' => \$opt_binary_dir,
85 'config-dir=s' => \$opt_config_dir,
86 'data-dir=s' => \$opt_data_dir,
88 'log-dir=s' => \$opt_log_dir,
89 'manual-dir=s' => \$opt_manual_dir,
90 'module-dir=s' => \$opt_module_dir,
91 'prefix=s' => \$opt_prefix,
92 'script-dir=s' => \$opt_script_dir,
93 'socketengine=s' => \$opt_socketengine,
94 'system' => \$opt_system,
97 # TODO: when the modulemanager rewrite is done these should be removed.
98 'disable-extras=s@' => \@opt_disableextras,
99 'enable-extras=s@' => \@opt_enableextras,
100 'list-extras' => sub { list_extras; exit 0; },
103 if (scalar(@opt_enableextras) + scalar(@opt_disableextras) > 0) {
104 @opt_enableextras = split /,/, join(',', @opt_enableextras);
105 @opt_disableextras = split /,/, join(',', @opt_disableextras);
106 enable_extras(@opt_enableextras);
107 disable_extras(@opt_disableextras);
109 print "Remember: YOU are responsible for making sure any libraries needed have been installed!\n";
113 our $interactive = !(
116 defined $opt_binary_dir ||
117 defined $opt_config_dir ||
118 defined $opt_data_dir ||
119 defined $opt_development ||
120 defined $opt_disable_interactive ||
121 defined $opt_distribution_label ||
123 defined $opt_log_dir ||
124 defined $opt_manual_dir ||
125 defined $opt_module_dir ||
126 defined $opt_prefix ||
127 defined $opt_script_dir ||
128 defined $opt_socketengine ||
129 defined $opt_system ||
133 my %version = get_version $opt_distribution_label;
134 print_format "<|BOLD Configuring InspIRCd $version{FULL} on $^O.|>\n";
138 %config = read_config_file(CONFIGURE_CACHE_FILE);
139 run_test CONFIGURE_CACHE_FILE, %config;
140 if (!defined $config{VERSION}) {
141 $config{VERSION} = CONFIGURE_CACHE_VERSION;
142 } elsif ($config{VERSION} != CONFIGURE_CACHE_VERSION) {
143 print_warning "ignoring contents of ${\CONFIGURE_CACHE_FILE} as it was generated by an incompatible version of $0!";
144 %config = ('VERSION', CONFIGURE_CACHE_VERSION);
148 $config{CXX} = find_compiler($config{CXX} // $ENV{CXX});
149 unless ($config{CXX}) {
150 say 'A suitable C++ compiler could not be detected on your system!';
151 unless ($interactive) {
152 say 'Set the CXX environment variable to the path to a C++ compiler binary if this is incorrect.';
155 until ($config{CXX}) {
156 my $compiler_path = prompt_string 1, 'Please enter the path to a C++ compiler binary:', 'c++';
157 $config{CXX} = find_compiler $compiler_path;
160 my %compiler = get_compiler_info($config{CXX});
162 $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', $^O eq 'darwin' ? undef : '-lrt');
163 $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
166 push @socketengines, 'epoll' if run_test 'epoll', test_header $config{CXX}, 'sys/epoll.h';
167 push @socketengines, 'kqueue' if run_test 'kqueue', test_file $config{CXX}, 'kqueue.cpp';
168 push @socketengines, 'poll' if run_test 'poll', test_header $config{CXX}, 'poll.h';
169 push @socketengines, 'select';
171 if (defined $opt_socketengine) {
172 unless (grep { $_ eq $opt_socketengine } @socketengines) {
173 my $reason = -f "src/socketengines/socketengine_$opt_socketengine.cpp" ? 'is not available on this platform' : 'does not exist';
174 print_error "The socket engine you requested ($opt_socketengine) $reason!",
175 'Available socket engines are:',
176 map { " * $_" } @socketengines;
179 $config{SOCKETENGINE} = $opt_socketengine // $socketengines[0];
181 if (defined $opt_system) {
182 $config{BASE_DIR} = $opt_prefix // '/var/lib/inspircd';
183 $config{BINARY_DIR} = $opt_binary_dir // '/usr/sbin';
184 $config{CONFIG_DIR} = $opt_config_dir // '/etc/inspircd';
185 $config{DATA_DIR} = $opt_data_dir // '/var/inspircd';
186 $config{LOG_DIR} = $opt_module_dir // '/var/log/inspircd';
187 $config{MANUAL_DIR} = $opt_manual_dir // '/usr/share/man/man1';
188 $config{MODULE_DIR} = $opt_module_dir // '/usr/lib/inspircd';
189 $config{SCRIPT_DIR} = $opt_script_dir // '/usr/share/inspircd'
191 $config{BASE_DIR} = $opt_prefix // $config{BASE_DIR} // rel2abs 'run';
192 $config{BINARY_DIR} = $opt_binary_dir // $config{BINARY_DIR} // rel2abs $config{BASE_DIR} . '/bin';
193 $config{CONFIG_DIR} = $opt_config_dir // $config{CONFIG_DIR} // rel2abs $config{BASE_DIR} . '/conf';
194 $config{DATA_DIR} = $opt_data_dir // $config{DATA_DIR} // rel2abs $config{BASE_DIR} . '/data';
195 $config{LOG_DIR} = $opt_log_dir // $config{LOG_DIR} // rel2abs $config{BASE_DIR} . '/logs';
196 $config{MANUAL_DIR} = $opt_manual_dir // $config{MANUAL_DIR} // rel2abs $config{BASE_DIR} . '/manuals';
197 $config{MODULE_DIR} = $opt_module_dir // $config{MODULE_DIR} // rel2abs $config{BASE_DIR} . '/modules';
198 $config{SCRIPT_DIR} = $opt_script_dir // $config{SCRIPT_DIR} // $config{BASE_DIR};
201 # Parse --gid=123 or --gid=foo and extract the group id.
203 if (defined $opt_gid) {
204 @group = $opt_gid =~ /^\d+$/ ? getgrgid($opt_gid) : getgrnam($opt_gid);
205 print_error "there is no '$opt_gid' group on this system!" unless @group;
207 @group = $opt_system ? getgrnam('irc') : getgrgid($config{GID} // getgid());
208 print_error "you need to specify a group to run as using '--gid [id|name]'!" unless @group;
210 $config{GROUP} = $group[0];
211 $config{GID} = $group[2];
213 # Parse --uid=123 or --uid=foo and extract the user id.
215 if (defined $opt_uid) {
216 @user = $opt_uid =~ /^\d+$/ ? getpwuid($opt_uid) : getpwnam($opt_uid);
217 print_error "there is no '$opt_uid' user on this system!" unless @user;
219 @user = $opt_system ? getpwnam('irc') : getpwuid($config{UID} // getuid());
220 print_error "you need to specify a user to run as using '--uid [id|name]'!" unless @user;
222 $config{USER} = $user[0];
223 $config{UID} = $user[2];
226 system 'tput', 'clear' if $interactive;
228 # Warn the user about clock drifting when running on OpenVZ.
229 if (-e '/proc/user_beancounters' || -e '/proc/vz/vzaquota') {
230 print_warning <<'EOW';
231 You are building InspIRCd inside of an an OpenVZ container. If you
232 plan to use InspIRCd in this container then you should make sure that NTP is
233 configured on the Hardware Node. Failure to do so may result in clock drifting!
237 # Check that the user actually wants this version.
238 if ($version{LABEL} ne 'release') {
239 print_warning <<'EOW';
240 You are building a development version. This contains code which has
241 not been tested as heavily and may contain various faults which could seriously
242 affect the running of your server. It is recommended that you use a stable
245 You can obtain the latest stable version from http://www.inspircd.org/ or by
246 running `git checkout insp20` if you are installing from Git.
248 if (!prompt_bool $interactive, 'I understand this warning and want to continue anyway.', $opt_development // 0) {
249 say STDERR 'If you understand this warning and still want to continue pass the --development flag.' unless $interactive;
254 # Configure directory settings.
255 my $question = <<"EOQ";
256 Currently, InspIRCd is configured with the following paths:
258 <|BOLD Base:|> $config{BASE_DIR}
259 <|BOLD Binary:|> $config{BINARY_DIR}
260 <|BOLD Config:|> $config{CONFIG_DIR}
261 <|BOLD Data:|> $config{DATA_DIR}
262 <|BOLD Log:|> $config{LOG_DIR}
263 <|BOLD Manual:|> $config{MANUAL_DIR}
264 <|BOLD Module:|> $config{MODULE_DIR}
265 <|BOLD Script:|> $config{SCRIPT_DIR}
267 Do you want to change these settings?
269 if (prompt_bool $interactive, $question, 0) {
270 my $original_base_dir = $config{BASE_DIR};
271 $config{BASE_DIR} = prompt_dir $interactive, 'In what directory do you wish to install the InspIRCd base?', $config{BASE_DIR};
272 foreach my $key (qw(BINARY_DIR CONFIG_DIR DATA_DIR LOG_DIR MANUAL_DIR MODULE_DIR SCRIPT_DIR)) {
273 $config{$key} =~ s/^\Q$original_base_dir\E/$config{BASE_DIR}/;
275 $config{BINARY_DIR} = prompt_dir $interactive, 'In what directory should the InspIRCd binary be placed?', $config{BINARY_DIR};
276 $config{CONFIG_DIR} = prompt_dir $interactive, 'In what directory are configuration files to be stored?', $config{CONFIG_DIR};
277 $config{DATA_DIR} = prompt_dir $interactive, 'In what directory are variable data files to be stored?', $config{DATA_DIR};
278 $config{LOG_DIR} = prompt_dir $interactive, 'In what directory are log files to be stored?', $config{LOG_DIR};
279 $config{MANUAL_DIR} = prompt_dir $interactive, 'In what directory are manual pages to be placed?', $config{MANUAL_DIR};
280 $config{MODULE_DIR} = prompt_dir $interactive, 'In what directory are modules to be placed?', $config{MODULE_DIR};
281 $config{SCRIPT_DIR} = prompt_dir $interactive, 'In what directory are scripts to be placed?', $config{SCRIPT_DIR};
284 # Configure module settings.
286 Currently, InspIRCd is configured to automatically enable all available extra modules.
288 Would you like to enable extra modules manually?
290 if (prompt_bool $interactive, $question, 0) {
291 foreach my $extra (<src/modules/extra/m_*.cpp>) {
292 my $module_name = basename $extra, '.cpp';
293 if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
294 enable_extras "$module_name.cpp";
298 # TODO: finish modulemanager rewrite and replace this code with:
299 # system './modulemanager', 'enable', '--auto';
300 enable_extras 'm_ssl_gnutls.cpp' unless system 'pkg-config --exists gnutls >/dev/null 2>&1';
301 enable_extras 'm_ssl_mbedtls.cpp' if -e '/usr/include/mbedtls/ssl.h';
302 enable_extras 'm_ssl_openssl.cpp' unless system 'pkg-config --exists openssl >/dev/null 2>&1';
305 # Generate SSL certificates.
306 if (<src/modules/m_ssl_*.cpp> && prompt_bool $interactive, 'Would you like to generate SSL certificates now?', $interactive) {
307 system './tools/genssl', 'auto';
310 # Cache the distribution label so that its not lost when --update is run.
311 $config{DISTRIBUTION} = $opt_distribution_label if $opt_distribution_label;
313 write_configure_cache %config;
314 parse_templates \%config, \%compiler, \%version;
316 print_format <<"EOM";
318 Configuration is complete! You have chosen to build with the following settings:
321 <|GREEN Binary:|> $config{CXX}
322 <|GREEN Name:|> $compiler{NAME}
323 <|GREEN Version:|> $compiler{VERSION}
325 <|GREEN Extra Modules:|>
328 for my $file (<src/modules/m_*>) {
329 my $module = basename $file, '.cpp';
330 say " * $module" if -l $file;
333 print_format <<"EOM";
336 <|GREEN Base:|> $config{BASE_DIR}
337 <|GREEN Binary:|> $config{BINARY_DIR}
338 <|GREEN Config:|> $config{CONFIG_DIR}
339 <|GREEN Data:|> $config{DATA_DIR}
340 <|GREEN Log:|> $config{LOG_DIR}
341 <|GREEN Manual:|> $config{MANUAL_DIR}
342 <|GREEN Module:|> $config{MODULE_DIR}
343 <|GREEN Script:|> $config{SCRIPT_DIR}
345 <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
346 <|GREEN Execution User:|> $config{USER} ($config{UID})
347 <|GREEN Socket Engine:|> $config{SOCKETENGINE}
349 To build with these settings run '<|GREEN make -j${\get_cpu_count}|>' now.
353 # Routine to list out the extra/ modules that have been enabled.
354 # Note: when getting any filenames out and comparing, it's important to lc it if the
355 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
356 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
357 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
361 my $srcdir = File::Spec->catdir("src", "modules");
362 my $abs_srcdir = File::Spec->rel2abs($srcdir);
365 opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
366 my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
369 opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
370 my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
373 my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
375 EXTRA: for my $extra (@extras) {
376 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
377 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
378 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
379 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
380 if (-l $abs_source) {
381 # Symlink, is it in the right place?
382 my $targ = readlink($abs_source);
383 my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
384 if ($abs_targ eq $abs_extra) {
385 $extras{$extra} = "\e[32;1menabled\e[0m";
387 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
389 } elsif (-e $abs_source) {
390 my ($devext, $inoext) = stat($abs_extra);
391 my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
393 if ($devsrc == $devext && $inosrc == $inoext) {
394 $extras{$extra} = "\e[32;1menabled\e[0m";
396 $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
399 open my $extfd, "<", $abs_extra;
400 open my $srcfd, "<", $abs_source;
402 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
403 $extras{$extra} = "\e[32;1menabled\e[0m";
405 $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
409 $extras{$extra} = "\e[33;1mdisabled\e[0m";
412 # Now let's add dependency info
413 for my $extra (keys(%extras)) {
414 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
415 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
416 my @deps = split /\s+/, get_directive($abs_extra, 'ModDep', '');
417 for my $dep (@deps) {
418 if (exists($extras{$dep})) {
419 my $ref = \$extras{$dep}; # Take reference.
420 if ($$ref !~ m/needed by/) {
421 # First dependency found.
422 if ($$ref =~ m/enabled/) {
423 $$ref .= " (needed by \e[32;1m$extra\e[0m";
425 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
426 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
429 if ($$ref =~ m/enabled/) {
430 $$ref .= ", \e[32;1m$extra\e[0m";
432 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
438 for my $extra (sort {$a cmp $b} keys(%extras)) {
439 my $text = $extras{$extra};
440 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
441 printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
443 printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
446 return keys(%extras) if wantarray; # Can be used by manage_extras.
449 sub enable_extras (@) {
451 for my $extra (@extras) {
452 my $extrapath = "src/modules/extra/$extra";
453 if (!-e $extrapath) {
454 print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
457 my $source = "src/modules/$extra";
459 print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
462 # Get dependencies, and add them to be processed.
463 my @deps = split /\s+/, get_directive($extrapath, 'ModDep', '');
464 for my $dep (@deps) {
465 next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
466 if (!-e "src/modules/$dep" && !-e "include/$dep") {
467 if (-e "src/modules/extra/$dep") {
468 print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
471 print STDERR "\e[33;1mWARNING:\e[0m module \e[32;1m$extra\e[0m might be missing dependency \e[32;1m$dep\e[0m - YOU are responsible for satisfying it!\n";
475 print "Enabling $extra ... \n";
476 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
480 sub disable_extras (@)
482 opendir my $dd, "src/modules/extra/";
483 my @files = readdir($dd);
486 EXTRA: for my $extra (@extras) {
487 my $extrapath = "src/modules/extra/$extra";
488 my $source = "src/modules/$extra";
489 if (!-e $extrapath) {
490 print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
493 if ((! -l $source) || readlink($source) ne "extra/$extra") {
494 print STDERR "Cannot disable \e[32;1m$extra\e[0m : Source is not a link or doesn't refer to the right file. Remove manually if this is in error.\n";
497 # Check if anything needs this.
498 for my $file (@files) {
499 my @deps = split /\s+/, get_directive("src/modules/extra/$file", 'ModDep', '');
500 # File depends on this extra...
501 if (scalar(grep { $_ eq $extra } @deps) > 0) {
502 # And is both enabled and not about to be disabled.
503 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
504 print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
510 print "Disabling $extra ... \n";
511 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";