main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. ((char*)log_hdl->token)[(strlen(agent_logdir) + strlen(LDAP_AGENT_LOGFILE) + 1)] = (char)0;
  100. }
  101. } else {
  102. /* agent-logdir not set */
  103. printf("ldap-agent: Error determining log directory.\n");
  104. exit(1);
  105. }
  106. netsnmp_enable_filelog(log_hdl, 1);
  107. } else {
  108. printf("Error starting logging.");
  109. exit(1);
  110. }
  111. snmp_log(LOG_INFO, "Starting ldap-agent...\n");
  112. /* setup agentx master */
  113. netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
  114. NETSNMP_DS_AGENT_ROLE, 1);
  115. if (agentx_master)
  116. netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
  117. NETSNMP_DS_AGENT_X_SOCKET, agentx_master);
  118. /* run as a daemon */
  119. if (netsnmp_daemonize(0, 0)) {
  120. /* sleep to allow pidfile to be created by child */
  121. sleep(5);
  122. if((pid_fp = fopen(pidfile,"r")) == NULL) {
  123. printf("ldap-agent: Not started! Check log file for details.\n");
  124. exit(1);
  125. } else {
  126. fscanf(pid_fp, "%d", &child_pid);
  127. fclose(pid_fp);
  128. }
  129. printf("ldap-agent: Started as pid %d\n", child_pid);
  130. exit(1);
  131. }
  132. /* initialize the agent */
  133. init_agent("ldap-agent");
  134. init_ldap_agent();
  135. init_snmp("ldap-agent");
  136. /* listen for signals */
  137. keep_running = 1;
  138. signal(SIGUSR1, stop_server);
  139. signal(SIGTERM, stop_server);
  140. signal(SIGINT, stop_server);
  141. /* create pidfile in config file dir */
  142. child_pid = getpid();
  143. if ((pid_fp = fopen(pidfile, "w")) == NULL) {
  144. snmp_log(LOG_ERR, "Error creating pid file: %s\n", pidfile);
  145. exit(1);
  146. } else {
  147. if (fprintf(pid_fp, "%d", child_pid) < 0) {
  148. snmp_log(LOG_ERR, "Error writing pid file: %s\n", pidfile);
  149. exit(1);
  150. }
  151. fclose(pid_fp);
  152. }
  153. /* we're up and running! */
  154. snmp_log(LOG_INFO, "Started ldap-agent as pid %d\n", child_pid);
  155. /* loop here until asked to stop */
  156. while(keep_running) {
  157. agent_check_and_process(1);
  158. }
  159. /* say goodbye */
  160. snmp_shutdown("ldap-agent");
  161. snmp_log(LOG_INFO, "ldap-agent stopped.\n");
  162. /* remove pidfile */
  163. remove(pidfile);
  164. return 0;
  165. }
  166. /************************************************************************
  167. * load_config
  168. *
  169. * Loads subagent config file and reads directory server config files.
  170. */
  171. void
  172. load_config(char *conf_path)
  173. {
  174. server_instance *serv_p = NULL;
  175. FILE *conf_file = NULL;
  176. FILE *dse_fp = NULL;
  177. char line[MAXLINE];
  178. char *p = NULL;
  179. char *p2 = NULL;
  180. /* Make sure we are getting an absolute path */
  181. if (*conf_path != '/') {
  182. printf("ldap-agent: Error opening config file: %s\n", conf_path);
  183. printf("ldap-agent: You must specify the absolute path to your config file\n");
  184. exit(1);
  185. }
  186. /* Open config file */
  187. if ((conf_file = fopen(conf_path, "r")) == NULL) {
  188. printf("ldap-agent: Error opening config file: %s\n", conf_path);
  189. exit(1);
  190. }
  191. /* set pidfile path */
  192. for (p = (conf_path + strlen(conf_path) - 1); p >= conf_path; p--) {
  193. if (*p == '/') {
  194. /* set pidfile path */
  195. if ((pidfile = malloc((p - conf_path) +
  196. strlen(LDAP_AGENT_PIDFILE) + 2)) != NULL) {
  197. strncpy(pidfile, conf_path, (p - conf_path + 1));
  198. strcat(pidfile, LDAP_AGENT_PIDFILE);
  199. pidfile[((p - conf_path) + strlen(LDAP_AGENT_PIDFILE) + 1)] = (char)0;
  200. } else {
  201. printf("ldap-agent: malloc error processing config file\n");
  202. exit(1);
  203. }
  204. /* set default logdir to location of config file */
  205. if ((agent_logdir = malloc((p - conf_path) + 1)) != NULL) {
  206. strncpy(agent_logdir, conf_path, (p - conf_path));
  207. agent_logdir[(p - conf_path)] = (char)0;
  208. break;
  209. } else {
  210. printf("ldap-agent: malloc error processing config file\n");
  211. exit(1);
  212. }
  213. }
  214. }
  215. while (fgets(line, MAXLINE, conf_file) != NULL) {
  216. /* Ignore comment lines in config file */
  217. if (line[0] == '#')
  218. continue;
  219. if ((p = strstr(line, "agentx-master")) != NULL) {
  220. /* load agentx-master setting */
  221. p = p + 13;
  222. if ((p = strtok(p, " \t\n")) != NULL) {
  223. if ((agentx_master = (char *) malloc(strlen(p) + 1)) != NULL)
  224. strcpy(agentx_master, p);
  225. }
  226. } else if ((p = strstr(line, "agent-logdir")) != NULL) {
  227. /* free the default logdir setting */
  228. if (agent_logdir != NULL) {
  229. free(agent_logdir);
  230. }
  231. /* load agent-logdir setting */
  232. p = p + 12;
  233. if ((p = strtok(p, " \t\n")) != NULL) {
  234. if ((agent_logdir = (char *) malloc(strlen(p) + 1)) != NULL)
  235. strcpy(agent_logdir, p);
  236. }
  237. } else if ((p = strstr(line, "server")) != NULL) {
  238. /* Allocate a server_instance */
  239. if ((serv_p = malloc(sizeof(server_instance))) == NULL) {
  240. printf("ldap-agent: malloc error processing config file\n");
  241. exit(1);
  242. }
  243. /* load server setting */
  244. p = p + 6;
  245. if ((p = strtok_r(p, " :\t\n", &p2)) != NULL) {
  246. /* first token is the instance root */
  247. if ((serv_p->stats_file = malloc(strlen(p) + 18)) != NULL)
  248. snprintf(serv_p->stats_file, strlen(p) + 18,
  249. "%s/logs/slapd.stats", p);
  250. serv_p->stats_file[(strlen(p) + 17)] = (char)0;
  251. if ((serv_p->dse_ldif = malloc(strlen(p) + 17)) != NULL) {
  252. snprintf(serv_p->dse_ldif, strlen(p) + 17, "%s/config/dse.ldif", p);
  253. serv_p->dse_ldif[(strlen(p) + 16)] = (char)0;
  254. }
  255. }
  256. /* Open dse.ldif */
  257. if ((dse_fp = fopen(serv_p->dse_ldif, "r")) == NULL) {
  258. printf("ldap-agent: Error opening server config file: %s\n",
  259. serv_p->dse_ldif);
  260. exit(1);
  261. }
  262. /* Get port value */
  263. while (fgets(line, MAXLINE, dse_fp) != NULL) {
  264. if ((p = strstr(line, "nsslapd-port: ")) != NULL) {
  265. p = p + 14;
  266. if ((p = strtok(p, ": \t\n")) != NULL)
  267. serv_p->port = atol(p);
  268. }
  269. }
  270. /* Close dse.ldif */
  271. fclose(dse_fp);
  272. /* Insert server instance into linked list */
  273. serv_p->next = server_head;
  274. server_head = serv_p;
  275. }
  276. }
  277. /* Close config file */
  278. fclose(conf_file);
  279. /* check for at least one directory server instance */
  280. if (server_head == NULL) {
  281. printf("ldap-agent: No server instances defined in config file\n");
  282. exit(1);
  283. }
  284. }
  285. /************************************************************************
  286. * exit_usage
  287. *
  288. * Prints usage message and exits program.
  289. */
  290. void
  291. exit_usage()
  292. {
  293. printf("Usage: ldap-agent [-D] configfile\n");
  294. printf(" -D Enable debug logging\n");
  295. exit(1);
  296. }