main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * Copyright (C) 2005 Red Hat, Inc.
  3. * All rights reserved.
  4. * --- END COPYRIGHT BLOCK --- */
  5. #include <signal.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10. #include <sys/stat.h>
  11. #include <ldap-agent.h>
  12. static char *agentx_master = NULL;
  13. static char *agent_logdir = NULL;
  14. static char *pidfile = NULL;
  15. server_instance *server_head = NULL;
  16. static int keep_running;
  17. RETSIGTYPE
  18. stop_server(int signum) {
  19. if (signum == SIGUSR1) {
  20. snmp_log(LOG_INFO, "Detected attempt to start ldap-agent again.\n");
  21. } else {
  22. snmp_log(LOG_INFO, "Received stop signal. Stopping ldap-agent...\n");
  23. keep_running = 0;
  24. }
  25. }
  26. int
  27. main (int argc, char *argv[]) {
  28. char *config_file = NULL;
  29. netsnmp_log_handler *log_hdl = NULL;
  30. int c, log_level = LOG_INFO;
  31. struct stat logdir_s;
  32. pid_t child_pid;
  33. FILE *pid_fp;
  34. /* Load options */
  35. while ((--argc > 0) && ((*++argv)[0] == '-')) {
  36. while (c = *++argv[0]) {
  37. switch (c) {
  38. case 'D':
  39. log_level = LOG_DEBUG;
  40. break;
  41. default:
  42. printf("ldap-agent: illegal option %c\n", c);
  43. exit_usage();
  44. }
  45. }
  46. }
  47. if (argc != 1)
  48. exit_usage();
  49. /* load config file */
  50. if ((config_file = strdup(*argv)) == NULL) {
  51. printf("ldap-agent: Memory error loading config file\n");
  52. exit(1);
  53. }
  54. load_config(config_file);
  55. /* check if we're already running as another process */
  56. if ((pid_fp = fopen(pidfile, "r")) != NULL) {
  57. fscanf(pid_fp, "%d", &child_pid);
  58. fclose(pid_fp);
  59. if (kill(child_pid, SIGUSR1) == 0) {
  60. printf("ldap-agent: Already running as pid %d!\n", child_pid);
  61. exit(1);
  62. } else {
  63. /* old pidfile exists, but the process doesn't. Cleanup pidfile */
  64. remove(pidfile);
  65. }
  66. }
  67. /* start logging */
  68. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
  69. NETSNMP_DS_LIB_LOG_TIMESTAMP, 1);
  70. if ((log_hdl = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_FILE,
  71. log_level)) != NULL) {
  72. if (agent_logdir != NULL) {
  73. /* Verify agent-logdir setting */
  74. if (stat(agent_logdir, &logdir_s) < 0) {
  75. printf("ldap-agent: Error reading logdir: %s\n", agent_logdir);
  76. exit(1);
  77. } else {
  78. /* Is it a directory? */
  79. if (S_ISDIR(logdir_s.st_mode)) {
  80. /* Can we write to it? */
  81. if (access(agent_logdir, W_OK) < 0) {
  82. printf("ldap-agent: Unable to write to logdir: %s\n",
  83. agent_logdir);
  84. exit(1);
  85. }
  86. } else {
  87. printf("ldap-agent: agent-logdir setting must point to a directory.\n");
  88. exit(1);
  89. }
  90. }
  91. /* agent-logdir setting looks ok */
  92. if ((log_hdl->token = malloc(strlen(agent_logdir) +
  93. strlen(LDAP_AGENT_LOGFILE) + 2)) != NULL) {
  94. strncpy((char *) log_hdl->token, agent_logdir, strlen(agent_logdir) + 1);
  95. /* add a trailing slash if needed */
  96. if (*(agent_logdir + strlen(agent_logdir)) != '/')
  97. strcat((char *) log_hdl->token, "/");
  98. strcat((char *) log_hdl->token, LDAP_AGENT_LOGFILE);
  99. }
  100. } else {
  101. /* agent-logdir not set, so write locally */
  102. log_hdl->token = strdup(LDAP_AGENT_LOGFILE);
  103. }
  104. netsnmp_enable_filelog(log_hdl, 1);
  105. } else {
  106. printf("Error starting logging.");
  107. exit(1);
  108. }
  109. snmp_log(LOG_INFO, "Starting ldap-agent...\n");
  110. /* setup agentx master */
  111. netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
  112. NETSNMP_DS_AGENT_ROLE, 1);
  113. if (agentx_master)
  114. netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
  115. NETSNMP_DS_AGENT_X_SOCKET, agentx_master);
  116. /* run as a daemon */
  117. if (netsnmp_daemonize(0, 0)) {
  118. /* sleep to allow pidfile to be created by child */
  119. sleep(3);
  120. if((pid_fp = fopen(pidfile,"r")) == NULL) {
  121. printf("ldap-agent: Not started! Check log file for details.\n");
  122. exit(1);
  123. } else {
  124. fscanf(pid_fp, "%d", &child_pid);
  125. fclose(pid_fp);
  126. }
  127. printf("ldap-agent: Started as pid %d\n", child_pid);
  128. exit(1);
  129. }
  130. /* initialize the agent */
  131. init_agent("ldap-agent");
  132. init_ldap_agent();
  133. init_snmp("ldap-agent");
  134. /* listen for signals */
  135. keep_running = 1;
  136. signal(SIGUSR1, stop_server);
  137. signal(SIGTERM, stop_server);
  138. signal(SIGINT, stop_server);
  139. /* create pidfile in config file dir */
  140. child_pid = getpid();
  141. if ((pid_fp = fopen(pidfile, "w")) == NULL) {
  142. snmp_log(LOG_ERR, "Error creating pid file: %s\n", pidfile);
  143. exit(1);
  144. } else {
  145. if (fprintf(pid_fp, "%d", child_pid) < 0) {
  146. snmp_log(LOG_ERR, "Error writing pid file: %s\n", pidfile);
  147. exit(1);
  148. }
  149. fclose(pid_fp);
  150. }
  151. /* we're up and running! */
  152. snmp_log(LOG_INFO, "Started ldap-agent as pid %d\n", child_pid);
  153. /* loop here until asked to stop */
  154. while(keep_running) {
  155. agent_check_and_process(1);
  156. }
  157. /* say goodbye */
  158. snmp_shutdown("ldap-agent");
  159. snmp_log(LOG_INFO, "ldap-agent stopped.\n");
  160. /* remove pidfile */
  161. remove(pidfile);
  162. return 0;
  163. }
  164. /************************************************************************
  165. * load_config
  166. *
  167. * Loads subagent config file and reads directory server config files.
  168. */
  169. void
  170. load_config(char *conf_path)
  171. {
  172. server_instance *serv_p = NULL;
  173. FILE *conf_file = NULL;
  174. FILE *dse_fp = NULL;
  175. char line[MAXLINE];
  176. char *p = NULL;
  177. char *p2 = NULL;
  178. /* Open config file */
  179. if ((conf_file = fopen(conf_path, "r")) == NULL) {
  180. printf("ldap-agent: Error opening config file: %s\n", conf_path);
  181. exit(1);
  182. }
  183. /* set pidfile path */
  184. for (p = (conf_path + strlen(conf_path) - 1); p >= conf_path; p--) {
  185. if (*p == '/') {
  186. if ((pidfile = malloc((p - conf_path) +
  187. strlen(LDAP_AGENT_PIDFILE) + 2)) != NULL) {
  188. strncpy(pidfile, conf_path, (p - conf_path + 1));
  189. strcat(pidfile, LDAP_AGENT_PIDFILE);
  190. break;
  191. } else {
  192. printf("ldap-agent: malloc error processing config file\n");
  193. exit(1);
  194. }
  195. }
  196. }
  197. while (fgets(line, MAXLINE, conf_file) != NULL) {
  198. /* Ignore comment lines in config file */
  199. if (line[0] == '#')
  200. continue;
  201. if ((p = strstr(line, "agentx-master")) != NULL) {
  202. /* load agentx-master setting */
  203. p = p + 13;
  204. if ((p = strtok(p, " \t\n")) != NULL) {
  205. if ((agentx_master = (char *) malloc(strlen(p) + 1)) != NULL)
  206. strcpy(agentx_master, p);
  207. }
  208. } else if ((p = strstr(line, "agent-logdir")) != NULL) {
  209. /* load agent-logdir setting */
  210. p = p + 12;
  211. if ((p = strtok(p, " \t\n")) != NULL) {
  212. if ((agent_logdir = (char *) malloc(strlen(p) + 1)) != NULL)
  213. strcpy(agent_logdir, p);
  214. }
  215. } else if ((p = strstr(line, "server")) != NULL) {
  216. /* Allocate a server_instance */
  217. if ((serv_p = malloc(sizeof(server_instance))) == NULL) {
  218. printf("ldap-agent: malloc error processing config file\n");
  219. exit(1);
  220. }
  221. /* load server setting */
  222. p = p + 6;
  223. if ((p = strtok_r(p, " :\t\n", &p2)) != NULL) {
  224. /* first token is the instance root */
  225. if ((serv_p->stats_file = malloc(strlen(p) + 18)) != NULL)
  226. snprintf(serv_p->stats_file, strlen(p) + 18,
  227. "%s/logs/slapd.stats", p);
  228. if ((serv_p->dse_ldif = malloc(strlen(p) + 17)) != NULL) {
  229. snprintf(serv_p->dse_ldif, strlen(p) + 17, "%s/config/dse.ldif", p);
  230. }
  231. /* second token is the name */
  232. p = p2;
  233. if((p2 = strchr(p, ':')) != NULL) {
  234. *p2 = '\0';
  235. ++p2;
  236. if ((serv_p->name = malloc(strlen(p) + 1)) != NULL)
  237. snprintf(serv_p->name, strlen(p) + 1, "%s", p);
  238. } else {
  239. printf("ldap-agent: Invalid config file\n");
  240. exit(1);
  241. }
  242. /* third token is the description */
  243. p = p2;
  244. if((p2 = strchr(p, ':')) != NULL) {
  245. *p2 = '\0';
  246. ++p2;
  247. if ((serv_p->description = malloc(strlen(p) + 1)) != NULL)
  248. snprintf(serv_p->description, strlen(p) + 1, "%s", p);
  249. } else {
  250. printf("ldap-agent: Invalid config file\n");
  251. exit(1);
  252. }
  253. /* fourth token is the org */
  254. p = p2;
  255. if((p2 = strchr(p, ':')) != NULL) {
  256. *p2 = '\0';
  257. ++p2;
  258. if ((serv_p->org = malloc(strlen(p) + 1)) != NULL)
  259. snprintf(serv_p->org, strlen(p) + 1, "%s", p);
  260. } else {
  261. printf("ldap-agent: Invalid config file\n");
  262. exit(1);
  263. }
  264. /* fifth token is the location */
  265. p = p2;
  266. if((p2 = strchr(p, ':')) != NULL) {
  267. *p2 = '\0';
  268. ++p2;
  269. if ((serv_p->location = malloc(strlen(p) + 1)) != NULL)
  270. snprintf(serv_p->location, strlen(p) + 1, "%s", p);
  271. } else {
  272. printf("ldap-agent: Invalid config file\n");
  273. exit(1);
  274. }
  275. /* sixth token is the contact */
  276. p = p2;
  277. if((p2 = strchr(p, '\n')) != NULL) {
  278. *p2 = '\0';
  279. if ((serv_p->contact = malloc(strlen(p) + 1)) != NULL)
  280. snprintf(serv_p->contact, strlen(p) + 1, "%s", p);
  281. } else {
  282. printf("ldap-agent: Invalid config file\n");
  283. exit(1);
  284. }
  285. }
  286. /* Open dse.ldif */
  287. if ((dse_fp = fopen(serv_p->dse_ldif, "r")) == NULL) {
  288. printf("ldap-agent: Error opening server config file: %s\n",
  289. serv_p->dse_ldif);
  290. exit(1);
  291. }
  292. /* Get port value */
  293. while (fgets(line, MAXLINE, dse_fp) != NULL) {
  294. if ((p = strstr(line, "nsslapd-port: ")) != NULL) {
  295. p = p + 14;
  296. if ((p = strtok(p, ": \t\n")) != NULL)
  297. serv_p->port = atol(p);
  298. }
  299. }
  300. /* Close dse.ldif */
  301. fclose(dse_fp);
  302. /* Insert server instance into linked list */
  303. serv_p->next = server_head;
  304. server_head = serv_p;
  305. }
  306. }
  307. /* Close config file */
  308. fclose(conf_file);
  309. /* check for at least one directory server instance */
  310. if (server_head == NULL) {
  311. printf("ldap-agent: No server instances defined in config file\n");
  312. exit(1);
  313. }
  314. }
  315. /************************************************************************
  316. * exit_usage
  317. *
  318. * Prints usage message and exits program.
  319. */
  320. void
  321. exit_usage()
  322. {
  323. printf("Usage: ldap-agent [-D] configfile\n");
  324. printf(" -D Enable debug logging\n");
  325. exit(1);
  326. }