#include <fcntl.h>
#include <fnmatch.h>
#include <time.h>
+#include <libgen.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/socket.h>
}
log_init(debug);
- priv_init(
+
#ifdef USE_SNMP
- snmp
-#endif
-);
+ if (NETSNMP_AGENTX_SOCKET[0] == '/') {
+ /* AgentX socket is a file, we need to mangle it to be able to chroot */
+ char *caxsocket;
+ char *chrootdir;
+ char *axsocket;
+
+ /* We chroot into the directory containing the socket. At this
+ * point of the program, no config file has been read. If the
+ * socket is not in the default directory, this won't work. */
+ caxsocket = strdup(NETSNMP_AGENTX_SOCKET);
+ chrootdir = strdup(dirname(caxsocket));
+ free(caxsocket);
+ priv_init(chrootdir);
+ free(chrootdir);
+
+ /* We mangle the name of the socket since it is in the current directory */
+ caxsocket = strdup(NETSNMP_AGENTX_SOCKET);
+ axsocket = strdup(basename(caxsocket));
+ free(caxsocket);
+ netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
+ NETSNMP_DS_AGENT_X_SOCKET,
+ axsocket);
+ free(axsocket);
+ } else
+ /* Let's suppose that we can chroot normally */
+ priv_init(PRIVSEP_CHROOT);
+#else
+ priv_init(PRIVSEP_CHROOT);
+#endif
if (probe == 0) probe = LLDPD_TTL;
/* Initialization */
void
-#ifdef USE_SNMP
-priv_init(int snmp)
-#else
-priv_init()
-#endif
+priv_init(char *chrootdir)
{
int pair[2];
struct passwd *user;
switch (monitored) {
case 0:
/* We are in the children, drop privileges */
- if (chroot(PRIVSEP_CHROOT) == -1)
+ if (chroot(chrootdir) == -1)
fatal("[priv]: unable to chroot");
if (chdir("/") != 0)
fatal("[priv]: unable to chdir");