main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * Copyright (C) 2005 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * --- END COPYRIGHT BLOCK --- */
  8. #ifdef HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include <signal.h>
  16. #include <sys/stat.h>
  17. #include "ldap-agent.h"
  18. #include "ldap.h"
  19. #include "ldif.h"
  20. static char *agentx_master = NULL;
  21. static char *agent_logdir = NULL;
  22. static char *pidfile = NULL;
  23. server_instance *server_head = NULL;
  24. static int keep_running;
  25. RETSIGTYPE
  26. stop_server(int signum) {
  27. if (signum == SIGUSR1) {
  28. snmp_log(LOG_WARNING, "Detected attempt to start ldap-agent again.\n");
  29. } else {
  30. snmp_log(LOG_WARNING, "Received stop signal. Stopping ldap-agent...\n");
  31. keep_running = 0;
  32. }
  33. }
  34. int
  35. main (int argc, char *argv[]) {
  36. char *config_file = NULL;
  37. netsnmp_log_handler *log_hdl = NULL;
  38. int c, log_level = LOG_WARNING;
  39. struct stat logdir_s;
  40. pid_t child_pid;
  41. FILE *pid_fp;
  42. /* Load options */
  43. while ((--argc > 0) && ((*++argv)[0] == '-')) {
  44. while ((c = *++argv[0])) {
  45. switch (c) {
  46. case 'D':
  47. log_level = LOG_DEBUG;
  48. break;
  49. default:
  50. printf("ldap-agent: illegal option %c\n", c);
  51. exit_usage();
  52. }
  53. }
  54. }
  55. if (argc != 1)
  56. exit_usage();
  57. /* load config file */
  58. if ((config_file = strdup(*argv)) == NULL) {
  59. printf("ldap-agent: Memory error loading config file\n");
  60. exit(1);
  61. }
  62. load_config(config_file);
  63. /* check if we're already running as another process */
  64. if ((pid_fp = fopen(pidfile, "r")) != NULL) {
  65. fscanf(pid_fp, "%d", &child_pid);
  66. fclose(pid_fp);
  67. if (kill(child_pid, SIGUSR1) == 0) {
  68. printf("ldap-agent: Already running as pid %d!\n", child_pid);
  69. exit(1);
  70. } else {
  71. /* old pidfile exists, but the process doesn't. Cleanup pidfile */
  72. remove(pidfile);
  73. }
  74. }
  75. /* start logging */
  76. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
  77. NETSNMP_DS_LIB_LOG_TIMESTAMP, 1);
  78. if ((log_hdl = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_FILE,
  79. log_level)) != NULL) {
  80. if (agent_logdir != NULL) {
  81. /* Verify agent-logdir setting */
  82. if (stat(agent_logdir, &logdir_s) < 0) {
  83. printf("ldap-agent: Error reading logdir: %s\n", agent_logdir);
  84. exit(1);
  85. } else {
  86. /* Is it a directory? */
  87. if (S_ISDIR(logdir_s.st_mode)) {
  88. /* Can we write to it? */
  89. if (access(agent_logdir, W_OK) < 0) {
  90. printf("ldap-agent: Unable to write to logdir: %s\n",
  91. agent_logdir);
  92. exit(1);
  93. }
  94. } else {
  95. printf("ldap-agent: agent-logdir setting must point to a directory.\n");
  96. exit(1);
  97. }
  98. }
  99. /* agent-logdir setting looks ok */
  100. if ((log_hdl->token = malloc(strlen(agent_logdir) +
  101. strlen(LDAP_AGENT_LOGFILE) + 2)) != NULL) {
  102. strncpy((char *) log_hdl->token, agent_logdir, strlen(agent_logdir) + 1);
  103. /* add a trailing slash if needed */
  104. if (*(agent_logdir + strlen(agent_logdir)) != '/')
  105. strcat((char *) log_hdl->token, "/");
  106. strcat((char *) log_hdl->token, LDAP_AGENT_LOGFILE);
  107. ((char*)log_hdl->token)[(strlen(agent_logdir) + strlen(LDAP_AGENT_LOGFILE) + 1)] = (char)0;
  108. }
  109. } else {
  110. /* agent-logdir not set */
  111. printf("ldap-agent: Error determining log directory.\n");
  112. exit(1);
  113. }
  114. snmp_enable_filelog((char*)log_hdl->token, 1);
  115. } else {
  116. printf("Error starting logging.");
  117. exit(1);
  118. }
  119. snmp_log(LOG_WARNING, "Starting ldap-agent...\n");
  120. /* setup agentx master */
  121. netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
  122. NETSNMP_DS_AGENT_ROLE, 1);
  123. if (agentx_master)
  124. netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
  125. NETSNMP_DS_AGENT_X_SOCKET, agentx_master);
  126. /* run as a daemon */
  127. if (netsnmp_daemonize(0, 0)) {
  128. int i;
  129. /* sleep to allow pidfile to be created by child */
  130. for (i=0; i < 3; i++) {
  131. sleep(5);
  132. if((pid_fp = fopen(pidfile,"r")) != NULL) {
  133. break;
  134. }
  135. }
  136. if(!pid_fp) {
  137. printf("ldap-agent: Not started after 15 seconds! Check log file for details.\n");
  138. exit(1);
  139. }
  140. fscanf(pid_fp, "%d", &child_pid);
  141. fclose(pid_fp);
  142. printf("ldap-agent: Started as pid %d\n", child_pid);
  143. exit(0);
  144. }
  145. /* initialize the agent */
  146. init_agent("ldap-agent");
  147. init_ldap_agent();
  148. init_snmp("ldap-agent");
  149. /* listen for signals */
  150. keep_running = 1;
  151. signal(SIGUSR1, stop_server);
  152. signal(SIGTERM, stop_server);
  153. signal(SIGINT, stop_server);
  154. /* create pidfile */
  155. child_pid = getpid();
  156. if ((pid_fp = fopen(pidfile, "w")) == NULL) {
  157. snmp_log(LOG_ERR, "Error creating pid file: %s\n", pidfile);
  158. exit(1);
  159. } else {
  160. if (fprintf(pid_fp, "%d", child_pid) < 0) {
  161. snmp_log(LOG_ERR, "Error writing pid file: %s\n", pidfile);
  162. exit(1);
  163. }
  164. fclose(pid_fp);
  165. }
  166. /* we're up and running! */
  167. snmp_log(LOG_WARNING, "Started ldap-agent as pid %d\n", child_pid);
  168. /* loop here until asked to stop */
  169. while(keep_running) {
  170. agent_check_and_process(1);
  171. }
  172. /* say goodbye */
  173. snmp_shutdown("ldap-agent");
  174. snmp_log(LOG_WARNING, "ldap-agent stopped.\n");
  175. /* remove pidfile */
  176. remove(pidfile);
  177. return 0;
  178. }
  179. /* ldif_read_record lineno argument type depends on openldap version */
  180. #if defined(USE_OPENLDAP)
  181. #if LDAP_VENDOR_VERSION >= 20434 /* changed in 2.4.34 */
  182. typedef unsigned long int ldif_record_lineno_t;
  183. #else
  184. typedef int ldif_record_lineno_t;
  185. #endif
  186. #endif
  187. /************************************************************************
  188. * load_config
  189. *
  190. * Loads subagent config file and reads directory server config files.
  191. */
  192. void
  193. load_config(char *conf_path)
  194. {
  195. server_instance *serv_p = NULL;
  196. FILE *conf_file = NULL;
  197. #if defined(USE_OPENLDAP)
  198. LDIFFP *dse_fp = NULL;
  199. int buflen = 0;
  200. ldif_record_lineno_t lineno = 0;
  201. #else
  202. FILE *dse_fp = NULL;
  203. int lineno = 0;
  204. #endif
  205. char line[MAXLINE];
  206. char *p = NULL;
  207. int error = 0;
  208. /* Make sure we are getting an absolute path */
  209. if (*conf_path != '/') {
  210. printf("ldap-agent: Error opening config file: %s\n", conf_path);
  211. printf("ldap-agent: You must specify the absolute path to your config file\n");
  212. error = 1;
  213. goto close_and_exit;
  214. }
  215. /* Open config file */
  216. if ((conf_file = fopen(conf_path, "r")) == NULL) {
  217. printf("ldap-agent: Error opening config file: %s\n", conf_path);
  218. error = 1;
  219. goto close_and_exit;
  220. }
  221. /* set pidfile path */
  222. if ((pidfile = malloc(strlen(LOCALSTATEDIR) + strlen("/run/") +
  223. strlen(LDAP_AGENT_PIDFILE) + 1)) != NULL) {
  224. strncpy(pidfile, LOCALSTATEDIR, strlen(LOCALSTATEDIR));
  225. /* The above will likely not be NULL terminated, but we need to
  226. * be sure that we're properly NULL terminated for the below
  227. * strcat() to work properly. */
  228. pidfile[strlen(LOCALSTATEDIR)] = (char)0;
  229. strcat(pidfile, "/run/");
  230. strcat(pidfile, LDAP_AGENT_PIDFILE);
  231. } else {
  232. printf("ldap-agent: malloc error processing config file\n");
  233. error = 1;
  234. goto close_and_exit;
  235. }
  236. /* set default logdir to location of config file */
  237. for (p = (conf_path + strlen(conf_path) - 1); p >= conf_path; p--) {
  238. if (*p == '/') {
  239. if ((agent_logdir = malloc((p - conf_path) + 1)) != NULL) {
  240. strncpy(agent_logdir, conf_path, (p - conf_path));
  241. agent_logdir[(p - conf_path)] = (char)0;
  242. break;
  243. } else {
  244. printf("ldap-agent: malloc error processing config file\n");
  245. error = 1;
  246. goto close_and_exit;
  247. }
  248. }
  249. }
  250. while (fgets(line, MAXLINE, conf_file) != NULL) {
  251. /* Ignore comment lines in config file */
  252. if (line[0] == '#')
  253. continue;
  254. if ((p = strstr(line, "agentx-master")) != NULL) {
  255. /* load agentx-master setting */
  256. p = p + 13;
  257. if ((p = strtok(p, " \t\n")) != NULL) {
  258. if (agentx_master){
  259. free(agentx_master);
  260. }
  261. if ((agentx_master = (char *) malloc(strlen(p) + 1)) != NULL)
  262. strcpy(agentx_master, p);
  263. }
  264. } else if ((p = strstr(line, "agent-logdir")) != NULL) {
  265. /* free the default logdir setting */
  266. if (agent_logdir != NULL) {
  267. free(agent_logdir);
  268. }
  269. /* load agent-logdir setting */
  270. p = p + 12;
  271. if ((p = strtok(p, " \t\n")) != NULL) {
  272. if ((agent_logdir = (char *) malloc(strlen(p) + 1)) != NULL)
  273. strcpy(agent_logdir, p);
  274. }
  275. } else if ((p = strstr(line, "server")) != NULL) {
  276. int got_port = 0;
  277. int got_rundir = 0;
  278. int got_snmp_index = 0;
  279. long snmp_index = 0;
  280. char *entry = NULL;
  281. char *instancename = NULL;
  282. lineno = 0;
  283. /* Allocate a server_instance */
  284. if ((serv_p = malloc(sizeof(server_instance))) == NULL) {
  285. printf("ldap-agent: malloc error processing config file\n");
  286. error = 1;
  287. goto close_and_exit;
  288. }
  289. /* load server setting */
  290. p = p + 6;
  291. if ((p = strtok(p, " \t\n")) != NULL) {
  292. /* first token is the instance name */
  293. instancename = strdup(p);
  294. serv_p->dse_ldif = malloc(strlen(p) + strlen(SYSCONFDIR) +
  295. strlen(PACKAGE_NAME) + 12);
  296. if (serv_p->dse_ldif != NULL) {
  297. snprintf(serv_p->dse_ldif, strlen(p) + strlen(SYSCONFDIR) +
  298. strlen(PACKAGE_NAME) + 12, "%s/%s/%s/dse.ldif",
  299. SYSCONFDIR, PACKAGE_NAME, p);
  300. serv_p->dse_ldif[(strlen(p) + strlen(SYSCONFDIR) +
  301. strlen(PACKAGE_NAME) + 11)] = (char)0;
  302. } else {
  303. printf("ldap-agent: malloc error processing config file\n");
  304. error = 1;
  305. free(instancename);
  306. instancename = NULL;
  307. goto close_and_exit;
  308. }
  309. /* set the semaphore name */
  310. /* "/" + ".stats" + \0 = 8 */
  311. serv_p->stats_sem_name = malloc(strlen(p) + 8);
  312. if (serv_p->stats_sem_name != NULL) {
  313. snprintf(serv_p->stats_sem_name, strlen(p) + 8, "/%s.stats", p);
  314. } else {
  315. printf("ldap-agent: malloc error processing config file\n");
  316. error = 1;
  317. free(instancename);
  318. instancename = NULL;
  319. goto close_and_exit;
  320. }
  321. } else {
  322. printf("ldap-agent: missing instance name\n");
  323. error = 1;
  324. goto close_and_exit;
  325. }
  326. /* Open dse.ldif */
  327. #if defined(USE_OPENLDAP)
  328. dse_fp = ldif_open(serv_p->dse_ldif, "r");
  329. buflen = 0;
  330. #else
  331. dse_fp = fopen(serv_p->dse_ldif, "r");
  332. #endif
  333. if (dse_fp == NULL) {
  334. printf("ldap-agent: Error opening server config file: %s\n",
  335. serv_p->dse_ldif);
  336. error = 1;
  337. free(instancename);
  338. instancename = NULL;
  339. goto close_and_exit;
  340. }
  341. /* ldif_get_entry will realloc the entry if it's not null,
  342. * so we can just free it when we're done fetching entries
  343. * from the dse.ldif. Unfortunately, ldif_getline moves
  344. * the pointer that is passed to it, so we need to save a
  345. * pointer to the beginning of the entry so we can free it
  346. * later. */
  347. #if defined(USE_OPENLDAP)
  348. while (ldif_read_record(dse_fp, &lineno, &entry, &buflen))
  349. #else
  350. while ((entry = ldif_get_entry(dse_fp, &lineno)) != NULL)
  351. #endif
  352. {
  353. char *entryp = entry;
  354. char *attr = NULL;
  355. char *val = NULL;
  356. #if defined(USE_OPENLDAP)
  357. ber_len_t vlen;
  358. #else
  359. int vlen;
  360. #endif
  361. /* Check if this is the cn=config entry */
  362. if (ldif_parse_line(ldif_getline(&entryp), &attr, &val, &vlen)) {
  363. printf("ldap-agent: error parsing ldif line from [%s]\n", serv_p->dse_ldif);
  364. }
  365. if ((strcmp(attr, "dn") == 0) &&
  366. (strcmp(val, "cn=config") == 0)) {
  367. char *dse_line = NULL;
  368. /* Look for port and rundir attributes */
  369. while ((dse_line = ldif_getline(&entryp)) != NULL) {
  370. ldif_parse_line(dse_line, &attr, &val, &vlen);
  371. if (strcmp(attr, "nsslapd-snmp-index") == 0) {
  372. snmp_index = atol(val);
  373. got_snmp_index = 1;
  374. } else if (strcmp(attr, "nsslapd-port") == 0) {
  375. serv_p->port = atol(val);
  376. got_port = 1;
  377. } else if (strcmp(attr, "nsslapd-rundir") == 0) {
  378. /* 8 = "/" + ".stats" + \0 */
  379. serv_p->stats_file = malloc(vlen + (instancename ? strlen(instancename) : 0) + 8);
  380. if (serv_p->stats_file && instancename) {
  381. snprintf(serv_p->stats_file, vlen + strlen(instancename) + 8,
  382. "%s/%s.stats", val, instancename);
  383. serv_p->stats_file[(vlen + strlen(instancename) + 7)] = (char)0;
  384. } else {
  385. printf("ldap-agent: malloc error processing config file\n");
  386. free(entry);
  387. error = 1;
  388. free(instancename);
  389. instancename = NULL;
  390. goto close_and_exit;
  391. }
  392. got_rundir = 1;
  393. }
  394. /* Stop processing this entry if we found the
  395. * port and rundir and snmp_index settings */
  396. if (got_port && got_rundir && got_snmp_index) {
  397. break;
  398. }
  399. }
  400. /* The port and rundir settings must be in the
  401. * cn=config entry, so we can stop reading through
  402. * the dse.ldif now. */
  403. break;
  404. }
  405. }
  406. free(instancename);
  407. instancename = NULL;
  408. /* We're done reading entries from dse_ldif now, so
  409. * we can free entry */
  410. free(entry);
  411. /* Make sure we were able to read the port and
  412. * location of the stats file. */
  413. if (!got_port) {
  414. printf("ldap-agent: Error reading nsslapd-port from "
  415. "server config file: %s\n", serv_p->dse_ldif);
  416. error = 1;
  417. goto close_and_exit;
  418. } else if (!got_rundir) {
  419. printf("ldap-agent: Error reading nsslapd-rundir from "
  420. "server config file: %s\n", serv_p->dse_ldif);
  421. error = 1;
  422. goto close_and_exit;
  423. }
  424. /* in case a snmp index is specified, it replace the nsslapd-port
  425. * This would allow to give an index to a snmp report, rather than using
  426. * the TCP interface port number (because the same port may be listen on multiple interfaces).
  427. * For snmp_index values <= 0 (disabled), let's keep the port
  428. */
  429. if (got_snmp_index && (snmp_index > 0)) {
  430. serv_p->port = snmp_index;
  431. }
  432. /* Insert server instance into linked list */
  433. serv_p->next = server_head;
  434. server_head = serv_p;
  435. }
  436. }
  437. /* check for at least one directory server instance */
  438. if (server_head == NULL) {
  439. printf("ldap-agent: No server instances defined in config file\n");
  440. error = 1;
  441. goto close_and_exit;
  442. }
  443. close_and_exit:
  444. if (conf_file)
  445. fclose(conf_file);
  446. if (dse_fp) {
  447. #if defined(USE_OPENLDAP)
  448. ldif_close(dse_fp);
  449. #else
  450. fclose(dse_fp);
  451. #endif
  452. }
  453. if (error)
  454. exit(error);
  455. }
  456. /************************************************************************
  457. * exit_usage
  458. *
  459. * Prints usage message and exits program.
  460. */
  461. void
  462. exit_usage()
  463. {
  464. printf("Usage: ldap-agent [-D] configfile\n");
  465. printf(" -D Enable debug logging\n");
  466. exit(1);
  467. }