1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
11 #include "agentlessd.h"
12 #include "config/config.h"
15 static void help_agentlessd(void) __attribute__((noreturn));
18 /* Print help statement */
19 static void help_agentlessd()
22 print_out(" %s: -[Vhdtf] [-u user] [-g group] [-c config] [-D dir]", ARGV0);
23 print_out(" -V Version and license message");
24 print_out(" -h This help message");
25 print_out(" -d Execute in debug mode. This parameter");
26 print_out(" can be specified multiple times");
27 print_out(" to increase the debug level.");
28 print_out(" -t Test configuration");
29 print_out(" -f Run in foreground");
30 print_out(" -u <user> User to run as (default: %s)", USER);
31 print_out(" -g <group> Group to run as (default: %s)", GROUPGLOBAL);
32 print_out(" -c <config> Configuration file to use (default: %s)", DEFAULTCPATH);
33 print_out(" -D <dir> Directory to chroot into (default: %s)", DEFAULTDIR);
38 int main(int argc, char **argv)
40 int c, test_config = 0, run_foreground = 0;
43 const char *dir = DEFAULTDIR;
44 const char *user = USER;
45 const char *group = GROUPGLOBAL;
46 const char *cfg = DEFAULTCPATH;
51 while ((c = getopt(argc, argv, "Vdhtfu:g:D:c:")) != -1) {
67 ErrorExit("%s: -u needs an argument", ARGV0);
73 ErrorExit("%s: -g needs an argument", ARGV0);
79 ErrorExit("%s: -D needs an argument", ARGV0);
85 ErrorExit("%s: -c needs an argument", ARGV0);
99 debug1(STARTED_MSG, ARGV0);
101 /* Check if the user/group given are valid */
102 uid = Privsep_GetUser(user);
103 gid = Privsep_GetGroup(group);
104 if (uid == (uid_t) - 1 || gid == (gid_t) - 1) {
105 ErrorExit(USER_ERROR, ARGV0, user, group);
111 lessdc.entries = NULL;
114 if (ReadConfig(c, cfg, &lessdc, NULL) < 0) {
115 ErrorExit(XML_INV_AGENTLESS, ARGV0);
118 /* Exit here if test config is set */
123 /* Continue in daemon mode */
124 if (!run_foreground) {
129 if (chdir(dir) == -1) {
130 ErrorExit(CHDIR_ERROR, ARGV0, dir, errno, strerror(errno));
133 /* Exit if not configured */
134 if (!lessdc.entries) {
135 verbose("%s: INFO: Not configured. Exiting.", ARGV0);
139 /* Privilege separation */
140 if (Privsep_SetGroup(gid) < 0) {
141 ErrorExit(SETGID_ERROR, ARGV0, group, errno, strerror(errno));
145 if (Privsep_SetUser(uid) < 0) {
146 ErrorExit(SETUID_ERROR, ARGV0, user, errno, strerror(errno));
149 debug1(CHROOT_MSG, ARGV0, dir);
150 debug1(PRIVSEP_MSG, ARGV0, user);
152 /* Signal manipulation */
155 /* Create PID files */
156 if (CreatePID(ARGV0, getpid()) < 0) {
157 ErrorExit(PID_ERROR, ARGV0);
160 /* Start up message */
161 verbose(STARTUP_MSG, ARGV0, (int)getpid());
163 /* The real daemon now */