main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. /* ldif_read_record lineno argument type depends on openldap version */
  209. #if defined(USE_OPENLDAP)
  210. #if LDAP_VENDOR_VERSION >= 20434 /* changed in 2.4.34 */
  211. typedef unsigned long int ldif_record_lineno_t;
  212. #else
  213. typedef int ldif_record_lineno_t;
  214. #endif
  215. #endif
  216. /************************************************************************
  217. * load_config
  218. *
  219. * Loads subagent config file and reads directory server config files.
  220. */
  221. void
  222. load_config(char *conf_path)
  223. {
  224. server_instance *serv_p = NULL;
  225. FILE *conf_file = NULL;
  226. #if defined(USE_OPENLDAP)
  227. LDIFFP *dse_fp = NULL;
  228. int buflen = 0;
  229. ldif_record_lineno_t lineno = 0;
  230. #else
  231. FILE *dse_fp = NULL;
  232. int lineno = 0;
  233. #endif
  234. char line[MAXLINE];
  235. char *p = NULL;
  236. int error = 0;
  237. /* Make sure we are getting an absolute path */
  238. if (*conf_path != '/') {
  239. printf("ldap-agent: Error opening config file: %s\n", conf_path);
  240. printf("ldap-agent: You must specify the absolute path to your config file\n");
  241. error = 1;
  242. goto close_and_exit;
  243. }
  244. /* Open config file */
  245. if ((conf_file = fopen(conf_path, "r")) == NULL) {
  246. printf("ldap-agent: Error opening config file: %s\n", conf_path);
  247. error = 1;
  248. goto close_and_exit;
  249. }
  250. /* set pidfile path */
  251. if ((pidfile = malloc(strlen(LOCALSTATEDIR) + strlen("/run/") +
  252. strlen(LDAP_AGENT_PIDFILE) + 1)) != NULL) {
  253. strncpy(pidfile, LOCALSTATEDIR, strlen(LOCALSTATEDIR));
  254. /* The above will likely not be NULL terminated, but we need to
  255. * be sure that we're properly NULL terminated for the below
  256. * strcat() to work properly. */
  257. pidfile[strlen(LOCALSTATEDIR)] = (char)0;
  258. strcat(pidfile, "/run/");
  259. strcat(pidfile, LDAP_AGENT_PIDFILE);
  260. } else {
  261. printf("ldap-agent: malloc error processing config file\n");
  262. error = 1;
  263. goto close_and_exit;
  264. }
  265. /* set default logdir to location of config file */
  266. for (p = (conf_path + strlen(conf_path) - 1); p >= conf_path; p--) {
  267. if (*p == '/') {
  268. if ((agent_logdir = malloc((p - conf_path) + 1)) != NULL) {
  269. strncpy(agent_logdir, conf_path, (p - conf_path));
  270. agent_logdir[(p - conf_path)] = (char)0;
  271. break;
  272. } else {
  273. printf("ldap-agent: malloc error processing config file\n");
  274. error = 1;
  275. goto close_and_exit;
  276. }
  277. }
  278. }
  279. while (fgets(line, MAXLINE, conf_file) != NULL) {
  280. /* Ignore comment lines in config file */
  281. if (line[0] == '#')
  282. continue;
  283. if ((p = strstr(line, "agentx-master")) != NULL) {
  284. /* load agentx-master setting */
  285. p = p + 13;
  286. if ((p = strtok(p, " \t\n")) != NULL) {
  287. if (agentx_master){
  288. free(agentx_master);
  289. }
  290. if ((agentx_master = (char *) malloc(strlen(p) + 1)) != NULL)
  291. strcpy(agentx_master, p);
  292. }
  293. } else if ((p = strstr(line, "agent-logdir")) != NULL) {
  294. /* free the default logdir setting */
  295. if (agent_logdir != NULL) {
  296. free(agent_logdir);
  297. }
  298. /* load agent-logdir setting */
  299. p = p + 12;
  300. if ((p = strtok(p, " \t\n")) != NULL) {
  301. if ((agent_logdir = (char *) malloc(strlen(p) + 1)) != NULL)
  302. strcpy(agent_logdir, p);
  303. }
  304. } else if ((p = strstr(line, "server")) != NULL) {
  305. int got_port = 0;
  306. int got_rundir = 0;
  307. char *entry = NULL;
  308. char *instancename = NULL;
  309. lineno = 0;
  310. /* Allocate a server_instance */
  311. if ((serv_p = malloc(sizeof(server_instance))) == NULL) {
  312. printf("ldap-agent: malloc error processing config file\n");
  313. error = 1;
  314. goto close_and_exit;
  315. }
  316. /* load server setting */
  317. p = p + 6;
  318. if ((p = strtok(p, " \t\n")) != NULL) {
  319. /* first token is the instance name */
  320. instancename = strdup(p);
  321. serv_p->dse_ldif = malloc(strlen(p) + strlen(SYSCONFDIR) +
  322. strlen(PACKAGE_NAME) + 12);
  323. if (serv_p->dse_ldif != NULL) {
  324. snprintf(serv_p->dse_ldif, strlen(p) + strlen(SYSCONFDIR) +
  325. strlen(PACKAGE_NAME) + 12, "%s/%s/%s/dse.ldif",
  326. SYSCONFDIR, PACKAGE_NAME, p);
  327. serv_p->dse_ldif[(strlen(p) + strlen(SYSCONFDIR) +
  328. strlen(PACKAGE_NAME) + 11)] = (char)0;
  329. } else {
  330. printf("ldap-agent: malloc error processing config file\n");
  331. error = 1;
  332. free(instancename);
  333. instancename = NULL;
  334. goto close_and_exit;
  335. }
  336. /* set the semaphore name */
  337. /* "/" + ".stats" + \0 = 8 */
  338. serv_p->stats_sem_name = malloc(strlen(p) + 8);
  339. if (serv_p->stats_sem_name != NULL) {
  340. snprintf(serv_p->stats_sem_name, strlen(p) + 8, "/%s.stats", p);
  341. } else {
  342. printf("ldap-agent: malloc error processing config file\n");
  343. error = 1;
  344. free(instancename);
  345. instancename = NULL;
  346. goto close_and_exit;
  347. }
  348. } else {
  349. printf("ldap-agent: missing instance name\n");
  350. error = 1;
  351. goto close_and_exit;
  352. }
  353. /* Open dse.ldif */
  354. #if defined(USE_OPENLDAP)
  355. dse_fp = ldif_open(serv_p->dse_ldif, "r");
  356. buflen = 0;
  357. #else
  358. dse_fp = fopen(serv_p->dse_ldif, "r");
  359. #endif
  360. if (dse_fp == NULL) {
  361. printf("ldap-agent: Error opening server config file: %s\n",
  362. serv_p->dse_ldif);
  363. error = 1;
  364. free(instancename);
  365. instancename = NULL;
  366. goto close_and_exit;
  367. }
  368. /* ldif_get_entry will realloc the entry if it's not null,
  369. * so we can just free it when we're done fetching entries
  370. * from the dse.ldif. Unfortunately, ldif_getline moves
  371. * the pointer that is passed to it, so we need to save a
  372. * pointer to the beginning of the entry so we can free it
  373. * later. */
  374. #if defined(USE_OPENLDAP)
  375. while (ldif_read_record(dse_fp, &lineno, &entry, &buflen))
  376. #else
  377. while ((entry = ldif_get_entry(dse_fp, &lineno)) != NULL)
  378. #endif
  379. {
  380. char *entryp = entry;
  381. char *attr = NULL;
  382. char *val = NULL;
  383. #if defined(USE_OPENLDAP)
  384. ber_len_t vlen;
  385. #else
  386. int vlen;
  387. #endif
  388. /* Check if this is the cn=config entry */
  389. if (ldif_parse_line(ldif_getline(&entryp), &attr, &val, &vlen)) {
  390. printf("ldap-agent: error parsing ldif line from [%s]\n", serv_p->dse_ldif);
  391. }
  392. if ((strcmp(attr, "dn") == 0) &&
  393. (strcmp(val, "cn=config") == 0)) {
  394. char *dse_line = NULL;
  395. /* Look for port and rundir attributes */
  396. while ((dse_line = ldif_getline(&entryp)) != NULL) {
  397. ldif_parse_line(dse_line, &attr, &val, &vlen);
  398. if (strcmp(attr, "nsslapd-port") == 0) {
  399. serv_p->port = atol(val);
  400. got_port = 1;
  401. } else if (strcmp(attr, "nsslapd-rundir") == 0) {
  402. /* 8 = "/" + ".stats" + \0 */
  403. serv_p->stats_file = malloc(vlen + (instancename ? strlen(instancename) : 0) + 8);
  404. if (serv_p->stats_file && instancename) {
  405. snprintf(serv_p->stats_file, vlen + strlen(instancename) + 8,
  406. "%s/%s.stats", val, instancename);
  407. serv_p->stats_file[(vlen + strlen(instancename) + 7)] = (char)0;
  408. } else {
  409. printf("ldap-agent: malloc error processing config file\n");
  410. free(entry);
  411. error = 1;
  412. free(instancename);
  413. instancename = NULL;
  414. goto close_and_exit;
  415. }
  416. got_rundir = 1;
  417. }
  418. /* Stop processing this entry if we found the
  419. * port and rundir settings */
  420. if (got_port && got_rundir) {
  421. break;
  422. }
  423. }
  424. /* The port and rundir settings must be in the
  425. * cn=config entry, so we can stop reading through
  426. * the dse.ldif now. */
  427. break;
  428. }
  429. }
  430. free(instancename);
  431. instancename = NULL;
  432. /* We're done reading entries from dse_ldif now, so
  433. * we can free entry */
  434. free(entry);
  435. /* Make sure we were able to read the port and
  436. * location of the stats file. */
  437. if (!got_port) {
  438. printf("ldap-agent: Error reading nsslapd-port from "
  439. "server config file: %s\n", serv_p->dse_ldif);
  440. error = 1;
  441. goto close_and_exit;
  442. } else if (!got_rundir) {
  443. printf("ldap-agent: Error reading nsslapd-rundir from "
  444. "server config file: %s\n", serv_p->dse_ldif);
  445. error = 1;
  446. goto close_and_exit;
  447. }
  448. /* Insert server instance into linked list */
  449. serv_p->next = server_head;
  450. server_head = serv_p;
  451. }
  452. }
  453. /* check for at least one directory server instance */
  454. if (server_head == NULL) {
  455. printf("ldap-agent: No server instances defined in config file\n");
  456. error = 1;
  457. goto close_and_exit;
  458. }
  459. close_and_exit:
  460. if (conf_file)
  461. fclose(conf_file);
  462. if (dse_fp) {
  463. #if defined(USE_OPENLDAP)
  464. ldif_close(dse_fp);
  465. #else
  466. fclose(dse_fp);
  467. #endif
  468. }
  469. if (error)
  470. exit(error);
  471. }
  472. /************************************************************************
  473. * exit_usage
  474. *
  475. * Prints usage message and exits program.
  476. */
  477. void
  478. exit_usage()
  479. {
  480. printf("Usage: ldap-agent [-D] configfile\n");
  481. printf(" -D Enable debug logging\n");
  482. exit(1);
  483. }