main.c 18 KB

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