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
|
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <signal.h>
#include "znc.h"
#include "Modules.h"
#include "MD5.h"
static struct option g_LongOpts[] = {
{ "help", 0, NULL, 0 },
{ "version", 0, NULL, 0 },
{ "makeconf", 0, NULL, 0 },
{ "makepass", 0, NULL, 0 },
#ifdef HAVE_LIBSSL
{ "makepem", 0, NULL, 0 },
{ "encrypt-pem", 0, NULL, 0 },
#endif /* HAVE_LIBSSL */
{ NULL }
};
void GenerateHelp(const char *appname) {
CUtils::PrintMessage("USAGE: " + CString(appname) + " [options] [config]");
CUtils::PrintMessage("Options are:");
CUtils::PrintMessage("\t--help)");
CUtils::PrintMessage("\t--version Output version information and exit");
CUtils::PrintMessage("\t--makeconf Interactively create a new config");
CUtils::PrintMessage("\t--makepass Generates a password for use in config");
#ifdef HAVE_LIBSSL
CUtils::PrintMessage("\t--makepem Generates a pemfile for use with SSL");
CUtils::PrintMessage("\t--encrypt-pem Encrypts the pemfile");
#endif /* HAVE_LIBSSL */
}
void die(int sig) {
signal(SIGSEGV, SIG_DFL);
signal(SIGABRT, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
#ifdef _DEBUG
CUtils::PrintMessage("Exiting on SIG [" + CString::ToString(sig) + "]");
if ((sig == SIGABRT) || (sig == SIGSEGV)) {
abort();
}
#endif /* _DEBUG */
delete &CZNC::Get();
exit(sig);
}
int main(int argc, char** argv, char** envp) {
CString sConfig;
#ifdef HAVE_LIBSSL
// initialize ssl, allow client to have compression enabled if desired
InitSSL(CT_ZLIB);
#endif /* HAVE_LIBSSL */
int iArg, iOptIndex = -1;
bool bMakeConf = false;
bool bMakePass = false;
#ifdef HAVE_LIBSSL
bool bMakePem = false;
bool bEncPem = false;
#endif /* HAVE_LIBSSL */
while ((iArg = getopt_long(argc, argv, "c|h", g_LongOpts, &iOptIndex) != -1)) {
switch (iArg) {
case 1: { // long options
if (iOptIndex >= 0) {
CString sOption = Lower(g_LongOpts[iOptIndex].name);
if (sOption == "version") {
cout << CZNC::GetTag() << endl;
return 0;
} else if (sOption == "makeconf") {
bMakeConf = true;
} else if (sOption == "makepass") {
bMakePass = true;
#ifdef HAVE_LIBSSL
} else if (sOption == "makepem") {
bMakePem = true;
} else if (sOption == "encrypt-pem") {
bEncPem = true;
#endif /* HAVE_LIBSSL */
} else if (sOption == "help") {
GenerateHelp(argv[0]);
return 0;
}
} else {
GenerateHelp(argv[0]);
return 1;
}
break;
}
case 'h':
GenerateHelp(argv[0]);
return 0;
default: {
GenerateHelp(argv[0]);
return 1;
}
}
}
if (optind < argc) {
sConfig = argv[optind];
} else {
sConfig = "znc.conf";
}
if (bMakeConf) {
CZNC& ZNC = CZNC::Get();
ZNC.InitDirs("");
if (ZNC.WriteNewConfig(sConfig)) {
char* args[3];
if (argc > 2) {
args[0] = argv[0];
args[1] = argv[optind];
args[2] = NULL;
} else if (argc > 1) {
args[0] = argv[0];
args[1] = NULL;
args[2] = NULL;
} else {
CUtils::PrintError("Unable to launch znc [Try manually restarting]");
return 1;
}
if (execve(*argv, args, envp) == -1) {
CUtils::PrintError("Unable to launch znc [" + CString(strerror(errno)) + "]");
return 1;
}
}
return 0;
}
#ifdef HAVE_LIBSSL
if (bMakePem) {
CZNC* pZNC = &CZNC::Get();
pZNC->InitDirs("");
pZNC->WritePemFile();
delete pZNC;
return 0;
}
#endif /* HAVE_LIBSSL */
if (bMakePass) {
CString sHash = CUtils::GetHashPass();
CUtils::PrintMessage("Use this in the <User> section of your config:");
CUtils::PrintMessage("Pass = " + sHash + " -");
return 0;
}
CZNC* pZNC = &CZNC::Get();
pZNC->InitDirs(((argc) ? argv[0] : ""));
if (!pZNC->ParseConfig(sConfig)) {
CUtils::PrintError("Unrecoverable config error.");
delete pZNC;
return 1;
}
if (!pZNC->GetListeners().size()) {
CUtils::PrintError("You must supply at least one Listen port in your config.");
delete pZNC;
return 1;
}
if (!pZNC->OnBoot()) {
CUtils::PrintError("Exiting due to module boot errors.");
delete pZNC;
return 1;
}
#ifdef _DEBUG
CUtils::PrintMessage("Staying open for debugging");
#else
CUtils::PrintAction("Forking into the background");
int iPid = fork();
if (iPid == -1) {
CUtils::PrintStatus(false, strerror(errno));
delete pZNC;
exit(1);
}
if (iPid > 0) {
CUtils::PrintStatus(true, "[pid: " + CString::ToString(iPid) + "]");
pZNC->WritePidFile(iPid);
CUtils::PrintMessage(CZNC::GetTag(false));
exit(0);
}
// Redirect std in/out/err to /dev/null
close(0); open("/dev/null", O_RDONLY);
close(1); open("/dev/null", O_WRONLY);
close(2); open("/dev/null", O_WRONLY);
#endif
struct sigaction sa;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, (struct sigaction*) NULL);
sa.sa_handler = die;
sigaction(SIGINT, &sa, (struct sigaction*) NULL);
sigaction(SIGILL, &sa, (struct sigaction*) NULL);
sigaction(SIGQUIT, &sa, (struct sigaction*) NULL);
sigaction(SIGBUS, &sa, (struct sigaction*) NULL);
sigaction(SIGSEGV, &sa, (struct sigaction*) NULL);
sigaction(SIGTERM, &sa, (struct sigaction*) NULL);
int iRet = 0;
try {
iRet = pZNC->Loop();
} catch (CException e) {
// EX_Shutdown is thrown to exit
switch (e.GetType()) {
case CException::EX_Shutdown:
iRet = 0;
default:
iRet = 1;
}
}
delete pZNC;
return iRet;
}
|