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_auto_extras,
56 $opt_disable_interactive,
57 $opt_distribution_label,
70 sub enable_extras (@);
72 sub disable_extras (@);
75 my @opt_disableextras;
77 exit 1 unless GetOptions(
78 'clean' => \&cmd_clean,
80 'update' => \&cmd_update,
82 'binary-dir=s' => \$opt_binary_dir,
83 'config-dir=s' => \$opt_config_dir,
84 'data-dir=s' => \$opt_data_dir,
85 'development' => \$opt_development,
86 'disable-auto-extras' => \$opt_disable_auto_extras,
87 'disable-interactive' => \$opt_disable_interactive,
88 'distribution-label=s' => \$opt_distribution_label,
90 'log-dir=s' => \$opt_log_dir,
91 'manual-dir=s' => \$opt_manual_dir,
92 'module-dir=s' => \$opt_module_dir,
93 'prefix=s' => \$opt_prefix,
94 'script-dir=s' => \$opt_script_dir,
95 'socketengine=s' => \$opt_socketengine,
96 'system' => \$opt_system,
99 # TODO: when the modulemanager rewrite is done these should be removed.
100 'disable-extras=s@' => \@opt_disableextras,
101 'enable-extras=s@' => \@opt_enableextras,
102 'list-extras' => sub { list_extras; exit 0; },
105 if (scalar(@opt_enableextras) + scalar(@opt_disableextras) > 0) {
106 @opt_enableextras = split /,/, join(',', @opt_enableextras);
107 @opt_disableextras = split /,/, join(',', @opt_disableextras);
108 enable_extras(@opt_enableextras);
109 disable_extras(@opt_disableextras);
111 print "Remember: YOU are responsible for making sure any libraries needed have been installed!\n";
115 our $interactive = !(
118 defined $opt_binary_dir ||
119 defined $opt_config_dir ||
120 defined $opt_data_dir ||
121 defined $opt_development ||
122 defined $opt_disable_auto_extras ||
123 defined $opt_disable_interactive ||
124 defined $opt_distribution_label ||
126 defined $opt_log_dir ||
127 defined $opt_manual_dir ||
128 defined $opt_module_dir ||
129 defined $opt_prefix ||
130 defined $opt_script_dir ||
131 defined $opt_socketengine ||
132 defined $opt_system ||
136 my %version = get_version $opt_distribution_label;
137 print_format "<|BOLD Configuring InspIRCd $version{FULL} on $^O.|>\n";
141 %config = read_config_file(CONFIGURE_CACHE_FILE);
142 run_test CONFIGURE_CACHE_FILE, %config;
143 if (!defined $config{VERSION}) {
144 $config{VERSION} = CONFIGURE_CACHE_VERSION;
145 } elsif ($config{VERSION} != CONFIGURE_CACHE_VERSION) {
146 print_warning "ignoring contents of ${\CONFIGURE_CACHE_FILE} as it was generated by an incompatible version of $0!";
147 %config = ('VERSION', CONFIGURE_CACHE_VERSION);
151 $config{CXX} = find_compiler($config{CXX} // $ENV{CXX});
152 unless ($config{CXX}) {
153 say 'A suitable C++ compiler could not be detected on your system!';
154 unless ($interactive) {
155 say 'Set the CXX environment variable to the path to a C++ compiler binary if this is incorrect.';
158 until ($config{CXX}) {
159 my $compiler_path = prompt_string 1, 'Please enter the path to a C++ compiler binary:', 'c++';
160 $config{CXX} = find_compiler $compiler_path;
163 my %compiler = get_compiler_info($config{CXX});
165 $config{HAS_ARC4RANDOM_BUF} = run_test 'arc4random_buf()', test_file($config{CXX}, 'arc4random_buf.cpp');
166 $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', $^O eq 'darwin' ? undef : '-lrt');
167 $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
170 push @socketengines, 'epoll' if run_test 'epoll', test_header $config{CXX}, 'sys/epoll.h';
171 push @socketengines, 'kqueue' if run_test 'kqueue', test_file $config{CXX}, 'kqueue.cpp';
172 push @socketengines, 'poll' if run_test 'poll', test_header $config{CXX}, 'poll.h';
173 push @socketengines, 'select';
175 if (defined $opt_socketengine) {
176 unless (grep { $_ eq $opt_socketengine } @socketengines) {
177 my $reason = -f "src/socketengines/socketengine_$opt_socketengine.cpp" ? 'is not available on this platform' : 'does not exist';
178 print_error "The socket engine you requested ($opt_socketengine) $reason!",
179 'Available socket engines are:',
180 map { " * $_" } @socketengines;
183 $config{SOCKETENGINE} = $opt_socketengine // $socketengines[0];
185 if (defined $opt_system) {
186 $config{BASE_DIR} = $opt_prefix // '/var/lib/inspircd';
187 $config{BINARY_DIR} = $opt_binary_dir // '/usr/sbin';
188 $config{CONFIG_DIR} = $opt_config_dir // '/etc/inspircd';
189 $config{DATA_DIR} = $opt_data_dir // '/var/inspircd';
190 $config{LOG_DIR} = $opt_log_dir // '/var/log/inspircd';
191 $config{MANUAL_DIR} = $opt_manual_dir // '/usr/share/man/man1';
192 $config{MODULE_DIR} = $opt_module_dir // '/usr/lib/inspircd';
193 $config{SCRIPT_DIR} = $opt_script_dir // '/usr/share/inspircd'
195 $config{BASE_DIR} = $opt_prefix // $config{BASE_DIR} // rel2abs 'run';
196 $config{BINARY_DIR} = $opt_binary_dir // $config{BINARY_DIR} // rel2abs $config{BASE_DIR} . '/bin';
197 $config{CONFIG_DIR} = $opt_config_dir // $config{CONFIG_DIR} // rel2abs $config{BASE_DIR} . '/conf';
198 $config{DATA_DIR} = $opt_data_dir // $config{DATA_DIR} // rel2abs $config{BASE_DIR} . '/data';
199 $config{LOG_DIR} = $opt_log_dir // $config{LOG_DIR} // rel2abs $config{BASE_DIR} . '/logs';
200 $config{MANUAL_DIR} = $opt_manual_dir // $config{MANUAL_DIR} // rel2abs $config{BASE_DIR} . '/manuals';
201 $config{MODULE_DIR} = $opt_module_dir // $config{MODULE_DIR} // rel2abs $config{BASE_DIR} . '/modules';
202 $config{SCRIPT_DIR} = $opt_script_dir // $config{SCRIPT_DIR} // $config{BASE_DIR};
205 # Parse --gid=123 or --gid=foo and extract the group id.
207 if (defined $opt_gid) {
208 @group = $opt_gid =~ /^\d+$/ ? getgrgid($opt_gid) : getgrnam($opt_gid);
209 print_error "there is no '$opt_gid' group on this system!" unless @group;
211 @group = $opt_system ? getgrnam('irc') : getgrgid($config{GID} // getgid());
212 print_error "you need to specify a group to run as using '--gid [id|name]'!" unless @group;
214 print_warning <<"EOW";
215 You are building as the privileged $group[0] group and have not specified
216 an unprivileged group to run InspIRCd as.
218 This is almost never what you should do. You should probably either create a new
219 unprivileged user/group to build and run as or pass the '--gid [id|name]' flag
220 to specify an unprivileged group to run as.
222 if (!prompt_bool $interactive, "Are you sure you want to build as the $group[0] group?", 0) {
223 say STDERR "If you are sure you want to build as the $group[0] group pass the --gid $group[2] flag." unless $interactive;
228 $config{GROUP} = $group[0];
229 $config{GID} = $group[2];
231 # Parse --uid=123 or --uid=foo and extract the user id.
233 if (defined $opt_uid) {
234 @user = $opt_uid =~ /^\d+$/ ? getpwuid($opt_uid) : getpwnam($opt_uid);
235 print_error "there is no '$opt_uid' user on this system!" unless @user;
237 @user = $opt_system ? getpwnam('irc') : getpwuid($config{UID} // getuid());
238 print_error "you need to specify a user to run as using '--uid [id|name]'!" unless @user;
240 print_warning <<"EOW";
241 You are building as the privileged $user[0] user and have not specified
242 an unprivileged user to run InspIRCd as.
244 This is almost never what you should do. You should probably either create a new
245 unprivileged user/group to build and run as or pass the '--uid [id|name]' flag
246 to specify an unprivileged user to run as.
248 if (!prompt_bool $interactive, "Are you sure you want to build as the $user[0] user?", 0) {
249 say STDERR "If you are sure you want to build as the $user[0] user pass the --uid $user[2] flag." unless $interactive;
254 $config{USER} = $user[0];
255 $config{UID} = $user[2];
257 # Warn the user about clock drifting when running on OpenVZ.
258 if (-e '/proc/user_beancounters' || -e '/proc/vz/vzaquota') {
259 print_warning <<'EOW';
260 You are building InspIRCd inside of an OpenVZ container. If you
261 plan to use InspIRCd in this container then you should make sure that NTP is
262 configured on the Hardware Node. Failure to do so may result in clock drifting!
266 # Check that the user actually wants this version.
267 if (defined $version{REAL_LABEL}) {
268 print_warning <<'EOW';
269 You are building a development version. This contains code which has
270 not been tested as heavily and may contain various faults which could seriously
271 affect the running of your server. It is recommended that you use a stable
274 You can obtain the latest stable version from https://www.inspircd.org or by
275 running `<|GREEN git checkout $(git describe --abbrev=0 --tags insp3)|>` if you are
278 if (!prompt_bool $interactive, 'I understand this warning and want to continue anyway.', $opt_development // 0) {
279 say STDERR 'If you understand this warning and still want to continue pass the --development flag.' unless $interactive;
284 # Configure directory settings.
285 my $question = <<"EOQ";
286 Currently, InspIRCd is configured with the following paths:
288 <|BOLD Base:|> $config{BASE_DIR}
289 <|BOLD Binary:|> $config{BINARY_DIR}
290 <|BOLD Config:|> $config{CONFIG_DIR}
291 <|BOLD Data:|> $config{DATA_DIR}
292 <|BOLD Log:|> $config{LOG_DIR}
293 <|BOLD Manual:|> $config{MANUAL_DIR}
294 <|BOLD Module:|> $config{MODULE_DIR}
295 <|BOLD Script:|> $config{SCRIPT_DIR}
297 Do you want to change these settings?
299 if (prompt_bool $interactive, $question, 0) {
300 my $original_base_dir = $config{BASE_DIR};
301 $config{BASE_DIR} = prompt_dir $interactive, 'In what directory do you wish to install the InspIRCd base?', $config{BASE_DIR};
302 foreach my $key (qw(BINARY_DIR CONFIG_DIR DATA_DIR LOG_DIR MANUAL_DIR MODULE_DIR SCRIPT_DIR)) {
303 $config{$key} =~ s/^\Q$original_base_dir\E/$config{BASE_DIR}/;
305 $config{BINARY_DIR} = prompt_dir $interactive, 'In what directory should the InspIRCd binary be placed?', $config{BINARY_DIR};
306 $config{CONFIG_DIR} = prompt_dir $interactive, 'In what directory are configuration files to be stored?', $config{CONFIG_DIR};
307 $config{DATA_DIR} = prompt_dir $interactive, 'In what directory are variable data files to be stored?', $config{DATA_DIR};
308 $config{LOG_DIR} = prompt_dir $interactive, 'In what directory are log files to be stored?', $config{LOG_DIR};
309 $config{MANUAL_DIR} = prompt_dir $interactive, 'In what directory are manual pages to be placed?', $config{MANUAL_DIR};
310 $config{MODULE_DIR} = prompt_dir $interactive, 'In what directory are modules to be placed?', $config{MODULE_DIR};
311 $config{SCRIPT_DIR} = prompt_dir $interactive, 'In what directory are scripts to be placed?', $config{SCRIPT_DIR};
314 # Configure module settings.
316 Currently, InspIRCd is configured to automatically enable all available extra modules.
318 Would you like to enable extra modules manually?
320 if (prompt_bool $interactive, $question, 0) {
321 foreach my $extra (<src/modules/extra/m_*.cpp>) {
322 my $module_name = basename $extra, '.cpp';
323 if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
324 enable_extras "$module_name.cpp";
327 } elsif (!defined $opt_disable_auto_extras) {
328 # TODO: finish modulemanager rewrite and replace this code with:
329 # system './modulemanager', 'enable', '--auto';
331 # Missing: m_ldap, m_regex_stdlib, m_ssl_mbedtls
332 'm_geo_maxmind.cpp' => 'pkg-config --exists libmaxminddb',
333 'm_mysql.cpp' => 'mysql_config --version',
334 'm_pgsql.cpp' => 'pg_config --version',
335 'm_regex_pcre.cpp' => 'pcre-config --version',
336 'm_regex_posix.cpp' => undef,
337 'm_regex_re2.cpp' => 'pkg-config --exists re2',
338 'm_regex_tre.cpp' => 'pkg-config --exists tre',
339 'm_sqlite3.cpp' => 'pkg-config --exists sqlite3',
340 'm_ssl_gnutls.cpp' => 'pkg-config --exists gnutls',
341 'm_ssl_openssl.cpp' => 'pkg-config --exists openssl',
342 'm_sslrehashsignal.cpp' => undef,
344 while (my ($module, $command) = each %modules) {
345 unless (defined $command && system "$command 1>/dev/null 2>/dev/null") {
346 enable_extras $module;
351 # Generate SSL certificates.
353 Would you like to generate a self-signed SSL certificate now? This certificate
354 can be used for testing but <|BOLD should not|> be used on a production network.
356 Note: you can get a <|BOLD free|> CA-signed certificate from Let's Encrypt. See
357 https://letsencrypt.org/getting-started/ for more details.
360 if (<src/modules/m_ssl_*.cpp> && prompt_bool $interactive, $question, $interactive) {
361 system './tools/genssl', 'auto';
364 # Cache the distribution label so that its not lost when --update is run.
365 $config{DISTRIBUTION} = $opt_distribution_label if $opt_distribution_label;
367 write_configure_cache %config;
368 parse_templates \%config, \%compiler, \%version;
370 print_format <<"EOM";
372 Configuration is complete! You have chosen to build with the following settings:
375 <|GREEN Binary:|> $config{CXX}
376 <|GREEN Name:|> $compiler{NAME}
377 <|GREEN Version:|> $compiler{VERSION}
379 <|GREEN Extra Modules:|>
382 for my $file (<src/modules/m_*>) {
383 my $module = basename $file, '.cpp';
384 say " * $module" if -l $file;
387 print_format <<"EOM";
390 <|GREEN Base:|> $config{BASE_DIR}
391 <|GREEN Binary:|> $config{BINARY_DIR}
392 <|GREEN Config:|> $config{CONFIG_DIR}
393 <|GREEN Data:|> $config{DATA_DIR}
394 <|GREEN Log:|> $config{LOG_DIR}
395 <|GREEN Manual:|> $config{MANUAL_DIR}
396 <|GREEN Module:|> $config{MODULE_DIR}
397 <|GREEN Script:|> $config{SCRIPT_DIR}
399 <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
400 <|GREEN Execution User:|> $config{USER} ($config{UID})
401 <|GREEN Socket Engine:|> $config{SOCKETENGINE}
403 To build with these settings run '<|GREEN make -j${\get_cpu_count} install|>' now.
407 # Routine to list out the extra/ modules that have been enabled.
408 # Note: when getting any filenames out and comparing, it's important to lc it if the
409 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
410 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
411 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
415 my $srcdir = File::Spec->catdir("src", "modules");
416 my $abs_srcdir = File::Spec->rel2abs($srcdir);
419 opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
420 my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
423 opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
424 my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
427 my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
429 EXTRA: for my $extra (@extras) {
430 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
431 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
432 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
433 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
434 if (-l $abs_source) {
435 # Symlink, is it in the right place?
436 my $targ = readlink($abs_source);
437 my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
438 if ($abs_targ eq $abs_extra) {
439 $extras{$extra} = "\e[32;1menabled\e[0m";
441 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
443 } elsif (-e $abs_source) {
444 my ($devext, $inoext) = stat($abs_extra);
445 my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
447 if ($devsrc == $devext && $inosrc == $inoext) {
448 $extras{$extra} = "\e[32;1menabled\e[0m";
450 $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
453 open my $extfd, "<", $abs_extra;
454 open my $srcfd, "<", $abs_source;
456 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
457 $extras{$extra} = "\e[32;1menabled\e[0m";
459 $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
463 $extras{$extra} = "\e[33;1mdisabled\e[0m";
466 # Now let's add dependency info
467 for my $extra (keys(%extras)) {
468 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
469 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
470 my @deps = split /\s+/, get_directive($abs_extra, 'ModDep', '');
471 for my $dep (@deps) {
472 if (exists($extras{$dep})) {
473 my $ref = \$extras{$dep}; # Take reference.
474 if ($$ref !~ m/needed by/) {
475 # First dependency found.
476 if ($$ref =~ m/enabled/) {
477 $$ref .= " (needed by \e[32;1m$extra\e[0m";
479 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
480 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
483 if ($$ref =~ m/enabled/) {
484 $$ref .= ", \e[32;1m$extra\e[0m";
486 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
492 for my $extra (sort {$a cmp $b} keys(%extras)) {
493 my $text = $extras{$extra};
494 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
495 printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
497 printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
500 return keys(%extras) if wantarray; # Can be used by manage_extras.
503 sub enable_extras (@) {
505 for my $extra (@extras) {
506 my $extrapath = "src/modules/extra/$extra";
507 if (!-e $extrapath) {
508 print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
511 my $source = "src/modules/$extra";
513 print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
516 # Get dependencies, and add them to be processed.
517 my @deps = split /\s+/, get_directive($extrapath, 'ModDep', '');
518 for my $dep (@deps) {
519 next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
520 if (!-e "src/modules/$dep" && !-e "include/$dep") {
521 if (-e "src/modules/extra/$dep") {
522 print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
525 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";
529 print "Enabling $extra ... \n";
530 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
534 sub disable_extras (@)
536 opendir my $dd, "src/modules/extra/";
537 my @files = readdir($dd);
540 EXTRA: for my $extra (@extras) {
541 my $extrapath = "src/modules/extra/$extra";
542 my $source = "src/modules/$extra";
543 if (!-e $extrapath) {
544 print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
547 if ((! -l $source) || readlink($source) ne "extra/$extra") {
548 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";
551 # Check if anything needs this.
552 for my $file (@files) {
553 my @deps = split /\s+/, get_directive("src/modules/extra/$file", 'ModDep', '');
554 # File depends on this extra...
555 if (scalar(grep { $_ eq $extra } @deps) > 0) {
556 # And is both enabled and not about to be disabled.
557 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
558 print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
564 print "Disabling $extra ... \n";
565 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";