main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. /* sleep to allow pidfile to be created by child */
  157. sleep(5);
  158. if((pid_fp = fopen(pidfile,"r")) == NULL) {
  159. printf("ldap-agent: Not started! Check log file for details.\n");
  160. exit(1);
  161. } else {
  162. fscanf(pid_fp, "%d", &child_pid);
  163. fclose(pid_fp);
  164. }
  165. printf("ldap-agent: Started as pid %d\n", child_pid);
  166. exit(1);
  167. }
  168. /* initialize the agent */
  169. init_agent("ldap-agent");
  170. init_ldap_agent();
  171. init_snmp("ldap-agent");
  172. /* listen for signals */
  173. keep_running = 1;
  174. signal(SIGUSR1, stop_server);
  175. signal(SIGTERM, stop_server);
  176. signal(SIGINT, stop_server);
  177. /* create pidfile in config file dir */
  178. child_pid = getpid();
  179. if ((pid_fp = fopen(pidfile, "w")) == NULL) {
  180. snmp_log(LOG_ERR, "Error creating pid file: %s\n", pidfile);
  181. exit(1);
  182. } else {
  183. if (fprintf(pid_fp, "%d", child_pid) < 0) {
  184. snmp_log(LOG_ERR, "Error writing pid file: %s\n", pidfile);
  185. exit(1);
  186. }
  187. fclose(pid_fp);
  188. }
  189. /* we're up and running! */
  190. snmp_log(LOG_WARNING, "Started ldap-agent as pid %d\n", child_pid);
  191. /* loop here until asked to stop */
  192. while(keep_running) {
  193. agent_check_and_process(1);
  194. }
  195. /* say goodbye */
  196. snmp_shutdown("ldap-agent");
  197. snmp_log(LOG_WARNING, "ldap-agent stopped.\n");
  198. /* remove pidfile */
  199. remove(pidfile);
  200. return 0;
  201. }
  202. /************************************************************************
  203. * load_config
  204. *
  205. * Loads subagent config file and reads directory server config files.
  206. */
  207. void
  208. load_config(char *conf_path)
  209. {
  210. server_instance *serv_p = NULL;
  211. FILE *conf_file = NULL;
  212. FILE *dse_fp = NULL;
  213. char line[MAXLINE];
  214. char *p = NULL;
  215. int error = 0;
  216. /* Make sure we are getting an absolute path */
  217. if (*conf_path != '/') {
  218. printf("ldap-agent: Error opening config file: %s\n", conf_path);
  219. printf("ldap-agent: You must specify the absolute path to your config file\n");
  220. error = 1;
  221. goto close_and_exit;
  222. }
  223. /* Open config file */
  224. if ((conf_file = fopen(conf_path, "r")) == NULL) {
  225. printf("ldap-agent: Error opening config file: %s\n", conf_path);
  226. error = 1;
  227. goto close_and_exit;
  228. }
  229. /* set pidfile path */
  230. for (p = (conf_path + strlen(conf_path) - 1); p >= conf_path; p--) {
  231. if (*p == '/') {
  232. /* set pidfile path */
  233. if ((pidfile = malloc((p - conf_path) +
  234. strlen(LDAP_AGENT_PIDFILE) + 2)) != NULL) {
  235. strncpy(pidfile, conf_path, (p - conf_path + 1));
  236. strcat(pidfile, LDAP_AGENT_PIDFILE);
  237. pidfile[((p - conf_path) + strlen(LDAP_AGENT_PIDFILE) + 1)] = (char)0;
  238. } else {
  239. printf("ldap-agent: malloc error processing config file\n");
  240. error = 1;
  241. goto close_and_exit;
  242. }
  243. /* set default logdir to location of config file */
  244. if ((agent_logdir = malloc((p - conf_path) + 1)) != NULL) {
  245. strncpy(agent_logdir, conf_path, (p - conf_path));
  246. agent_logdir[(p - conf_path)] = (char)0;
  247. break;
  248. } else {
  249. printf("ldap-agent: malloc error processing config file\n");
  250. error = 1;
  251. goto close_and_exit;
  252. }
  253. }
  254. }
  255. while (fgets(line, MAXLINE, conf_file) != NULL) {
  256. /* Ignore comment lines in config file */
  257. if (line[0] == '#')
  258. continue;
  259. if ((p = strstr(line, "agentx-master")) != NULL) {
  260. /* load agentx-master setting */
  261. p = p + 13;
  262. if ((p = strtok(p, " \t\n")) != NULL) {
  263. if ((agentx_master = (char *) malloc(strlen(p) + 1)) != NULL)
  264. strcpy(agentx_master, p);
  265. }
  266. } else if ((p = strstr(line, "agent-logdir")) != NULL) {
  267. /* free the default logdir setting */
  268. if (agent_logdir != NULL) {
  269. free(agent_logdir);
  270. }
  271. /* load agent-logdir setting */
  272. p = p + 12;
  273. if ((p = strtok(p, " \t\n")) != NULL) {
  274. if ((agent_logdir = (char *) malloc(strlen(p) + 1)) != NULL)
  275. strcpy(agent_logdir, p);
  276. }
  277. } else if ((p = strstr(line, "server")) != NULL) {
  278. int got_port = 0;
  279. int got_tmpdir = 0;
  280. int lineno = 0;
  281. char *entry = NULL;
  282. /* Allocate a server_instance */
  283. if ((serv_p = malloc(sizeof(server_instance))) == NULL) {
  284. printf("ldap-agent: malloc error processing config file\n");
  285. error = 1;
  286. goto close_and_exit;
  287. }
  288. /* load server setting */
  289. p = p + 6;
  290. if ((p = strtok(p, " \t\n")) != NULL) {
  291. /* first token is the instance name */
  292. serv_p->dse_ldif = malloc(strlen(p) + strlen(SYSCONFDIR) +
  293. strlen(PACKAGE_NAME) + 12);
  294. if (serv_p->dse_ldif != NULL) {
  295. snprintf(serv_p->dse_ldif, strlen(p) + strlen(SYSCONFDIR) +
  296. strlen(PACKAGE_NAME) + 12, "%s/%s/%s/dse.ldif",
  297. SYSCONFDIR, PACKAGE_NAME, p);
  298. serv_p->dse_ldif[(strlen(p) + strlen(SYSCONFDIR) +
  299. strlen(PACKAGE_NAME) + 11)] = (char)0;
  300. } else {
  301. printf("ldap-agent: malloc error processing config file\n");
  302. error = 1;
  303. goto close_and_exit;
  304. }
  305. }
  306. /* Open dse.ldif */
  307. if ((dse_fp = fopen(serv_p->dse_ldif, "r")) == NULL) {
  308. printf("ldap-agent: Error opening server config file: %s\n",
  309. serv_p->dse_ldif);
  310. error = 1;
  311. goto close_and_exit;
  312. }
  313. /* ldif_get_entry will realloc the entry if it's not null,
  314. * so we can just free it when we're done fetching entries
  315. * from the dse.ldif. Unfortunately, ldif_getline moves
  316. * the pointer that is passed to it, so we need to save a
  317. * pointer to the beginning of the entry so we can free it
  318. * later. */
  319. while ((entry = ldif_get_entry(dse_fp, &lineno)) != NULL) {
  320. char *entryp = entry;
  321. char *attr = NULL;
  322. char *val = NULL;
  323. int vlen;
  324. /* Check if this is the cn=config entry */
  325. ldif_parse_line(ldif_getline(&entryp), &attr, &val, &vlen);
  326. if ((strcmp(attr, "dn") == 0) &&
  327. (strcmp(val, "cn=config") == 0)) {
  328. char *dse_line = NULL;
  329. /* Look for port and tmpdir attributes */
  330. while ((dse_line = ldif_getline(&entryp)) != NULL) {
  331. ldif_parse_line(dse_line, &attr, &val, &vlen);
  332. if (strcmp(attr, "nsslapd-port") == 0) {
  333. serv_p->port = atol(val);
  334. got_port = 1;
  335. } else if (strcmp(attr, "nsslapd-tmpdir") == 0) {
  336. serv_p->stats_file = malloc(vlen + 13);
  337. if (serv_p->stats_file != NULL) {
  338. snprintf(serv_p->stats_file, vlen + 13,
  339. "%s/slapd.stats", val);
  340. serv_p->stats_file[(vlen + 12)] = (char)0;
  341. } else {
  342. printf("ldap-agent: malloc error processing config file\n");
  343. free(entry);
  344. error = 1;
  345. goto close_and_exit;
  346. }
  347. got_tmpdir = 1;
  348. }
  349. /* Stop processing this entry if we found the
  350. * port and tmpdir settings */
  351. if (got_port && got_tmpdir) {
  352. break;
  353. }
  354. }
  355. /* The port and tmpdir settings must be in the
  356. * cn=config entry, so we can stop reading through
  357. * the dse.ldif now. */
  358. break;
  359. }
  360. }
  361. /* We're done reading entries from dse_ldif now, so
  362. * we can free entry */
  363. free(entry);
  364. /* Make sure we were able to read the port and
  365. * location of the stats file. */
  366. if (!got_port) {
  367. printf("ldap-agent: Error reading nsslapd-port from "
  368. "server config file: %s\n", serv_p->dse_ldif);
  369. error = 1;
  370. goto close_and_exit;
  371. } else if (!got_tmpdir) {
  372. printf("ldap-agent: Error reading nsslapd-tmpdir from "
  373. "server config file: %s\n", serv_p->dse_ldif);
  374. error = 1;
  375. goto close_and_exit;
  376. }
  377. /* Insert server instance into linked list */
  378. serv_p->next = server_head;
  379. server_head = serv_p;
  380. }
  381. }
  382. /* check for at least one directory server instance */
  383. if (server_head == NULL) {
  384. printf("ldap-agent: No server instances defined in config file\n");
  385. error = 1;
  386. goto close_and_exit;
  387. }
  388. close_and_exit:
  389. if (conf_file)
  390. fclose(conf_file);
  391. if (dse_fp)
  392. fclose(dse_fp);
  393. if (error)
  394. exit(error);
  395. }
  396. /************************************************************************
  397. * exit_usage
  398. *
  399. * Prints usage message and exits program.
  400. */
  401. void
  402. exit_usage()
  403. {
  404. printf("Usage: ldap-agent [-D] configfile\n");
  405. printf(" -D Enable debug logging\n");
  406. exit(1);
  407. }