main.c 17 KB

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