check_smtp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /******************************************************************************
  2. *
  3. * Nagios check_smtp plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2006 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_smtp plugin
  13. *
  14. * This plugin will attempt to open an SMTP connection with the host.
  15. *
  16. *
  17. * License Information:
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. *
  33. *
  34. * $Id$
  35. *
  36. ******************************************************************************/
  37. const char *progname = "check_smtp";
  38. const char *revision = "$Revision$";
  39. const char *copyright = "2000-2006";
  40. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  41. #include "common.h"
  42. #include "netutils.h"
  43. #include "utils.h"
  44. #ifdef HAVE_SSL
  45. int check_cert = FALSE;
  46. int days_till_exp;
  47. # define my_recv(buf, len) ((use_ssl && ssl_established) ? np_net_ssl_read(buf, len) : read(sd, buf, len))
  48. # define my_send(buf, len) ((use_ssl && ssl_established) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0))
  49. #else /* ifndef HAVE_SSL */
  50. # define my_recv(buf, len) read(sd, buf, len)
  51. # define my_send(buf, len) send(sd, buf, len, 0)
  52. #endif
  53. enum {
  54. SMTP_PORT = 25
  55. };
  56. #define SMTP_EXPECT "220"
  57. #define SMTP_HELO "HELO "
  58. #define SMTP_EHLO "EHLO "
  59. #define SMTP_QUIT "QUIT\r\n"
  60. #define SMTP_STARTTLS "STARTTLS\r\n"
  61. #define SMTP_AUTH_LOGIN "AUTH LOGIN\r\n"
  62. #ifndef HOST_MAX_BYTES
  63. #define HOST_MAX_BYTES 255
  64. #endif
  65. #define EHLO_SUPPORTS_STARTTLS 1
  66. int process_arguments (int, char **);
  67. int validate_arguments (void);
  68. void print_help (void);
  69. void print_usage (void);
  70. int my_close(void);
  71. #include "regex.h"
  72. char regex_expect[MAX_INPUT_BUFFER] = "";
  73. regex_t preg;
  74. regmatch_t pmatch[10];
  75. char timestamp[20] = "";
  76. char errbuf[MAX_INPUT_BUFFER];
  77. int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  78. int eflags = 0;
  79. int errcode, excode;
  80. int server_port = SMTP_PORT;
  81. char *server_address = NULL;
  82. char *server_expect = NULL;
  83. int smtp_use_dummycmd = 0;
  84. char *mail_command = NULL;
  85. char *from_arg = NULL;
  86. int ncommands=0;
  87. int command_size=0;
  88. int nresponses=0;
  89. int response_size=0;
  90. char **commands = NULL;
  91. char **responses = NULL;
  92. char *authtype = NULL;
  93. char *authuser = NULL;
  94. char *authpass = NULL;
  95. int warning_time = 0;
  96. int check_warning_time = FALSE;
  97. int critical_time = 0;
  98. int check_critical_time = FALSE;
  99. int verbose = 0;
  100. int use_ssl = FALSE;
  101. short use_ehlo = FALSE;
  102. short ssl_established = 0;
  103. char *localhostname = NULL;
  104. int sd;
  105. char buffer[MAX_INPUT_BUFFER];
  106. enum {
  107. TCP_PROTOCOL = 1,
  108. UDP_PROTOCOL = 2,
  109. MAXBUF = 1024
  110. };
  111. /* written by lauri alanko */
  112. static char *
  113. base64 (const char *bin, size_t len)
  114. {
  115. char *buf = (char *) malloc ((len + 2) / 3 * 4 + 1);
  116. size_t i = 0, j = 0;
  117. char BASE64_END = '=';
  118. char base64_table[64];
  119. strncpy (base64_table, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", 64);
  120. while (j < len - 2) {
  121. buf[i++] = base64_table[bin[j] >> 2];
  122. buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
  123. buf[i++] = base64_table[((bin[j + 1] & 15) << 2) | (bin[j + 2] >> 6)];
  124. buf[i++] = base64_table[bin[j + 2] & 63];
  125. j += 3;
  126. }
  127. switch (len - j) {
  128. case 1:
  129. buf[i++] = base64_table[bin[j] >> 2];
  130. buf[i++] = base64_table[(bin[j] & 3) << 4];
  131. buf[i++] = BASE64_END;
  132. buf[i++] = BASE64_END;
  133. break;
  134. case 2:
  135. buf[i++] = base64_table[bin[j] >> 2];
  136. buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
  137. buf[i++] = base64_table[(bin[j + 1] & 15) << 2];
  138. buf[i++] = BASE64_END;
  139. break;
  140. case 0:
  141. break;
  142. }
  143. buf[i] = '\0';
  144. return buf;
  145. }
  146. int
  147. main (int argc, char **argv)
  148. {
  149. short supports_tls=FALSE;
  150. int n = 0;
  151. double elapsed_time;
  152. long microsec;
  153. int result = STATE_UNKNOWN;
  154. char *cmd_str = NULL;
  155. char *helocmd = NULL;
  156. char *error_msg = "";
  157. struct timeval tv;
  158. setlocale (LC_ALL, "");
  159. bindtextdomain (PACKAGE, LOCALEDIR);
  160. textdomain (PACKAGE);
  161. if (process_arguments (argc, argv) == ERROR)
  162. usage4 (_("Could not parse arguments"));
  163. /* If localhostname not set on command line, use gethostname to set */
  164. if(! localhostname){
  165. localhostname = malloc (HOST_MAX_BYTES);
  166. if(!localhostname){
  167. printf(_("malloc() failed!\n"));
  168. return STATE_CRITICAL;
  169. }
  170. if(gethostname(localhostname, HOST_MAX_BYTES)){
  171. printf(_("gethostname() failed!\n"));
  172. return STATE_CRITICAL;
  173. }
  174. }
  175. if(use_ehlo)
  176. asprintf (&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n");
  177. else
  178. asprintf (&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n");
  179. if (verbose)
  180. printf("HELOCMD: %s", helocmd);
  181. /* initialize the MAIL command with optional FROM command */
  182. asprintf (&cmd_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
  183. if (verbose && smtp_use_dummycmd)
  184. printf ("FROM CMD: %s", cmd_str);
  185. /* initialize alarm signal handling */
  186. (void) signal (SIGALRM, socket_timeout_alarm_handler);
  187. /* set socket timeout */
  188. (void) alarm (socket_timeout);
  189. /* start timer */
  190. gettimeofday (&tv, NULL);
  191. /* try to connect to the host at the given port number */
  192. result = my_tcp_connect (server_address, server_port, &sd);
  193. if (result == STATE_OK) { /* we connected */
  194. /* watch for the SMTP connection string and */
  195. /* return a WARNING status if we couldn't read any data */
  196. if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
  197. printf (_("recv() failed\n"));
  198. result = STATE_WARNING;
  199. }
  200. else {
  201. if (verbose)
  202. printf ("%s", buffer);
  203. /* strip the buffer of carriage returns */
  204. strip (buffer);
  205. /* make sure we find the response we are looking for */
  206. if (!strstr (buffer, server_expect)) {
  207. if (server_port == SMTP_PORT)
  208. printf (_("Invalid SMTP response received from host\n"));
  209. else
  210. printf (_("Invalid SMTP response received from host on port %d\n"),
  211. server_port);
  212. result = STATE_WARNING;
  213. }
  214. }
  215. /* send the HELO/EHLO command */
  216. send(sd, helocmd, strlen(helocmd), 0);
  217. /* allow for response to helo command to reach us */
  218. if(read (sd, buffer, MAXBUF - 1) < 0){
  219. printf (_("recv() failed\n"));
  220. return STATE_WARNING;
  221. } else if(use_ehlo){
  222. buffer[MAXBUF-1]='\0';
  223. if(strstr(buffer, "250 STARTTLS") != NULL ||
  224. strstr(buffer, "250-STARTTLS") != NULL){
  225. supports_tls=TRUE;
  226. }
  227. }
  228. if(use_ssl && ! supports_tls){
  229. printf(_("WARNING - TLS not supported by server\n"));
  230. send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
  231. return STATE_WARNING;
  232. }
  233. #ifdef HAVE_SSL
  234. if(use_ssl) {
  235. /* send the STARTTLS command */
  236. send(sd, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0);
  237. recv(sd,buffer, MAX_INPUT_BUFFER-1, 0); /* wait for it */
  238. if (!strstr (buffer, server_expect)) {
  239. printf (_("Server does not support STARTTLS\n"));
  240. send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
  241. return STATE_UNKNOWN;
  242. }
  243. result = np_net_ssl_init(sd);
  244. if(result != STATE_OK) {
  245. printf (_("CRITICAL - Cannot create SSL context.\n"));
  246. np_net_ssl_cleanup();
  247. close(sd);
  248. return STATE_CRITICAL;
  249. } else {
  250. ssl_established = 1;
  251. }
  252. /*
  253. * Resend the EHLO command.
  254. *
  255. * RFC 3207 (4.2) says: ``The client MUST discard any knowledge
  256. * obtained from the server, such as the list of SMTP service
  257. * extensions, which was not obtained from the TLS negotiation
  258. * itself. The client SHOULD send an EHLO command as the first
  259. * command after a successful TLS negotiation.'' For this
  260. * reason, some MTAs will not allow an AUTH LOGIN command before
  261. * we resent EHLO via TLS.
  262. */
  263. if (my_send(helocmd, strlen(helocmd)) <= 0) {
  264. printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS."));
  265. my_close();
  266. return STATE_UNKNOWN;
  267. }
  268. if (verbose)
  269. printf(_("sent %s"), helocmd);
  270. if ((n = my_recv(buffer, MAX_INPUT_BUFFER - 1)) <= 0) {
  271. printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS."));
  272. my_close();
  273. return STATE_UNKNOWN;
  274. }
  275. if (verbose) {
  276. buffer[n] = '\0';
  277. printf("%s", buffer);
  278. }
  279. # ifdef USE_OPENSSL
  280. if ( check_cert ) {
  281. result = np_net_ssl_check_cert(days_till_exp);
  282. if(result != STATE_OK){
  283. printf ("%s\n", _("CRITICAL - Cannot retrieve server certificate."));
  284. }
  285. my_close();
  286. return result;
  287. }
  288. # endif /* USE_OPENSSL */
  289. }
  290. #endif
  291. /* sendmail will syslog a "NOQUEUE" error if session does not attempt
  292. * to do something useful. This can be prevented by giving a command
  293. * even if syntax is illegal (MAIL requires a FROM:<...> argument)
  294. *
  295. * According to rfc821 you can include a null reversepath in the from command
  296. * - but a log message is generated on the smtp server.
  297. *
  298. * You can disable sending mail_command with '--nocommand'
  299. * Use the -f option to provide a FROM address
  300. */
  301. if (smtp_use_dummycmd) {
  302. my_send(cmd_str, strlen(cmd_str));
  303. my_recv(buffer, MAX_INPUT_BUFFER-1);
  304. if (verbose)
  305. printf("%s", buffer);
  306. }
  307. while (n < ncommands) {
  308. asprintf (&cmd_str, "%s%s", commands[n], "\r\n");
  309. my_send(cmd_str, strlen(cmd_str));
  310. my_recv(buffer, MAX_INPUT_BUFFER-1);
  311. if (verbose)
  312. printf("%s", buffer);
  313. strip (buffer);
  314. if (n < nresponses) {
  315. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  316. errcode = regcomp (&preg, responses[n], cflags);
  317. if (errcode != 0) {
  318. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  319. printf (_("Could Not Compile Regular Expression"));
  320. return ERROR;
  321. }
  322. excode = regexec (&preg, buffer, 10, pmatch, eflags);
  323. if (excode == 0) {
  324. result = STATE_OK;
  325. }
  326. else if (excode == REG_NOMATCH) {
  327. result = STATE_WARNING;
  328. printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
  329. }
  330. else {
  331. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  332. printf (_("Execute Error: %s\n"), errbuf);
  333. result = STATE_UNKNOWN;
  334. }
  335. }
  336. n++;
  337. }
  338. if (authtype != NULL) {
  339. if (strcmp (authtype, "LOGIN") == 0) {
  340. char *abuf;
  341. int ret;
  342. do {
  343. if (authuser == NULL) {
  344. result = STATE_CRITICAL;
  345. asprintf(&error_msg, _("no authuser specified, "));
  346. break;
  347. }
  348. if (authpass == NULL) {
  349. result = STATE_CRITICAL;
  350. asprintf(&error_msg, _("no authpass specified, "));
  351. break;
  352. }
  353. /* send AUTH LOGIN */
  354. my_send(SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN));
  355. if (verbose)
  356. printf (_("sent %s\n"), "AUTH LOGIN");
  357. if((ret = my_recv(buffer, MAXBUF - 1)) < 0){
  358. asprintf(&error_msg, _("recv() failed after AUTH LOGIN, "));
  359. result = STATE_WARNING;
  360. break;
  361. }
  362. buffer[ret] = 0;
  363. if (verbose)
  364. printf (_("received %s\n"), buffer);
  365. if (strncmp (buffer, "334", 3) != 0) {
  366. result = STATE_CRITICAL;
  367. asprintf(&error_msg, _("invalid response received after AUTH LOGIN, "));
  368. break;
  369. }
  370. /* encode authuser with base64 */
  371. abuf = base64 (authuser, strlen(authuser));
  372. strcat (abuf, "\r\n");
  373. my_send(abuf, strlen(abuf));
  374. if (verbose)
  375. printf (_("sent %s\n"), abuf);
  376. if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
  377. result = STATE_CRITICAL;
  378. asprintf(&error_msg, _("recv() failed after sending authuser, "));
  379. break;
  380. }
  381. buffer[ret] = 0;
  382. if (verbose) {
  383. printf (_("received %s\n"), buffer);
  384. }
  385. if (strncmp (buffer, "334", 3) != 0) {
  386. result = STATE_CRITICAL;
  387. asprintf(&error_msg, _("invalid response received after authuser, "));
  388. break;
  389. }
  390. /* encode authpass with base64 */
  391. abuf = base64 (authpass, strlen(authpass));
  392. strcat (abuf, "\r\n");
  393. my_send(abuf, strlen(abuf));
  394. if (verbose) {
  395. printf (_("sent %s\n"), abuf);
  396. }
  397. if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
  398. result = STATE_CRITICAL;
  399. asprintf(&error_msg, _("recv() failed after sending authpass, "));
  400. break;
  401. }
  402. buffer[ret] = 0;
  403. if (verbose) {
  404. printf (_("received %s\n"), buffer);
  405. }
  406. if (strncmp (buffer, "235", 3) != 0) {
  407. result = STATE_CRITICAL;
  408. asprintf(&error_msg, _("invalid response received after authpass, "));
  409. break;
  410. }
  411. break;
  412. } while (0);
  413. } else {
  414. result = STATE_CRITICAL;
  415. asprintf(&error_msg, _("only authtype LOGIN is supported, "));
  416. }
  417. }
  418. /* tell the server we're done */
  419. my_send (SMTP_QUIT, strlen (SMTP_QUIT));
  420. /* finally close the connection */
  421. close (sd);
  422. }
  423. /* reset the alarm */
  424. alarm (0);
  425. microsec = deltime (tv);
  426. elapsed_time = (double)microsec / 1.0e6;
  427. if (result == STATE_OK) {
  428. if (check_critical_time && elapsed_time > (double) critical_time)
  429. result = STATE_CRITICAL;
  430. else if (check_warning_time && elapsed_time > (double) warning_time)
  431. result = STATE_WARNING;
  432. }
  433. printf (_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"),
  434. state_text (result),
  435. error_msg,
  436. elapsed_time,
  437. verbose?", ":"", verbose?buffer:"",
  438. fperfdata ("time", elapsed_time, "s",
  439. (int)check_warning_time, warning_time,
  440. (int)check_critical_time, critical_time,
  441. TRUE, 0, FALSE, 0));
  442. return result;
  443. }
  444. /* process command-line arguments */
  445. int
  446. process_arguments (int argc, char **argv)
  447. {
  448. int c;
  449. int option = 0;
  450. static struct option longopts[] = {
  451. {"hostname", required_argument, 0, 'H'},
  452. {"expect", required_argument, 0, 'e'},
  453. {"critical", required_argument, 0, 'c'},
  454. {"warning", required_argument, 0, 'w'},
  455. {"timeout", required_argument, 0, 't'},
  456. {"port", required_argument, 0, 'p'},
  457. {"from", required_argument, 0, 'f'},
  458. {"fqdn", required_argument, 0, 'F'},
  459. {"authtype", required_argument, 0, 'A'},
  460. {"authuser", required_argument, 0, 'U'},
  461. {"authpass", required_argument, 0, 'P'},
  462. {"command", required_argument, 0, 'C'},
  463. {"response", required_argument, 0, 'R'},
  464. {"nocommand", required_argument, 0, 'n'},
  465. {"verbose", no_argument, 0, 'v'},
  466. {"version", no_argument, 0, 'V'},
  467. {"use-ipv4", no_argument, 0, '4'},
  468. {"use-ipv6", no_argument, 0, '6'},
  469. {"help", no_argument, 0, 'h'},
  470. {"starttls",no_argument,0,'S'},
  471. {"certificate",required_argument,0,'D'},
  472. {0, 0, 0, 0}
  473. };
  474. if (argc < 2)
  475. return ERROR;
  476. for (c = 1; c < argc; c++) {
  477. if (strcmp ("-to", argv[c]) == 0)
  478. strcpy (argv[c], "-t");
  479. else if (strcmp ("-wt", argv[c]) == 0)
  480. strcpy (argv[c], "-w");
  481. else if (strcmp ("-ct", argv[c]) == 0)
  482. strcpy (argv[c], "-c");
  483. }
  484. while (1) {
  485. c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:SD:F:A:U:P:",
  486. longopts, &option);
  487. if (c == -1 || c == EOF)
  488. break;
  489. switch (c) {
  490. case 'H': /* hostname */
  491. if (is_host (optarg)) {
  492. server_address = optarg;
  493. }
  494. else {
  495. usage2 (_("Invalid hostname/address"), optarg);
  496. }
  497. break;
  498. case 'p': /* port */
  499. if (is_intpos (optarg))
  500. server_port = atoi (optarg);
  501. else
  502. usage4 (_("Port must be a positive integer"));
  503. break;
  504. case 'F':
  505. /* localhostname */
  506. localhostname = strdup(optarg);
  507. break;
  508. case 'f': /* from argument */
  509. from_arg = optarg;
  510. smtp_use_dummycmd = 1;
  511. break;
  512. case 'A':
  513. authtype = optarg;
  514. break;
  515. case 'U':
  516. authuser = optarg;
  517. break;
  518. case 'P':
  519. authpass = optarg;
  520. break;
  521. case 'e': /* server expect string on 220 */
  522. server_expect = optarg;
  523. break;
  524. case 'C': /* commands */
  525. if (ncommands >= command_size) {
  526. commands = realloc (commands, command_size+8);
  527. if (commands == NULL)
  528. die (STATE_UNKNOWN,
  529. _("Could not realloc() units [%d]\n"), ncommands);
  530. }
  531. commands[ncommands] = optarg;
  532. ncommands++;
  533. break;
  534. case 'R': /* server responses */
  535. if (nresponses >= response_size) {
  536. responses = realloc (responses, response_size+8);
  537. if (responses == NULL)
  538. die (STATE_UNKNOWN,
  539. _("Could not realloc() units [%d]\n"), nresponses);
  540. }
  541. responses[nresponses] = optarg;
  542. nresponses++;
  543. break;
  544. case 'c': /* critical time threshold */
  545. if (is_intnonneg (optarg)) {
  546. critical_time = atoi (optarg);
  547. check_critical_time = TRUE;
  548. }
  549. else {
  550. usage4 (_("Critical time must be a positive integer"));
  551. }
  552. break;
  553. case 'w': /* warning time threshold */
  554. if (is_intnonneg (optarg)) {
  555. warning_time = atoi (optarg);
  556. check_warning_time = TRUE;
  557. }
  558. else {
  559. usage4 (_("Warning time must be a positive integer"));
  560. }
  561. break;
  562. case 'v': /* verbose */
  563. verbose++;
  564. break;
  565. case 't': /* timeout */
  566. if (is_intnonneg (optarg)) {
  567. socket_timeout = atoi (optarg);
  568. }
  569. else {
  570. usage4 (_("Timeout interval must be a positive integer"));
  571. }
  572. break;
  573. case 'S':
  574. /* starttls */
  575. use_ssl = TRUE;
  576. use_ehlo = TRUE;
  577. break;
  578. case 'D':
  579. /* Check SSL cert validity */
  580. #ifdef USE_OPENSSL
  581. if (!is_intnonneg (optarg))
  582. usage2 ("Invalid certificate expiration period",optarg);
  583. days_till_exp = atoi (optarg);
  584. check_cert = TRUE;
  585. #else
  586. usage (_("SSL support not available - install OpenSSL and recompile"));
  587. #endif
  588. break;
  589. case '4':
  590. address_family = AF_INET;
  591. break;
  592. case '6':
  593. #ifdef USE_IPV6
  594. address_family = AF_INET6;
  595. #else
  596. usage4 (_("IPv6 support not available"));
  597. #endif
  598. break;
  599. case 'V': /* version */
  600. print_revision (progname, revision);
  601. exit (STATE_OK);
  602. case 'h': /* help */
  603. print_help ();
  604. exit (STATE_OK);
  605. case '?': /* help */
  606. usage5 ();
  607. }
  608. }
  609. c = optind;
  610. if (server_address == NULL) {
  611. if (argv[c]) {
  612. if (is_host (argv[c]))
  613. server_address = argv[c];
  614. else
  615. usage2 (_("Invalid hostname/address"), argv[c]);
  616. }
  617. else {
  618. asprintf (&server_address, "127.0.0.1");
  619. }
  620. }
  621. if (server_expect == NULL)
  622. server_expect = strdup (SMTP_EXPECT);
  623. if (mail_command == NULL)
  624. mail_command = strdup("MAIL ");
  625. if (from_arg==NULL)
  626. from_arg = strdup(" ");
  627. return validate_arguments ();
  628. }
  629. int
  630. validate_arguments (void)
  631. {
  632. return OK;
  633. }
  634. int
  635. my_close (void)
  636. {
  637. #ifdef HAVE_SSL
  638. np_net_ssl_cleanup();
  639. #endif
  640. return close(sd);
  641. }
  642. void
  643. print_help (void)
  644. {
  645. char *myport;
  646. asprintf (&myport, "%d", SMTP_PORT);
  647. print_revision (progname, revision);
  648. printf ("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n");
  649. printf (COPYRIGHT, copyright, email);
  650. printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host."));
  651. printf ("\n\n");
  652. print_usage ();
  653. printf (_(UT_HELP_VRSN));
  654. printf (_(UT_HOST_PORT), 'p', myport);
  655. printf (_(UT_IPv46));
  656. printf (" %s\n", "-e, --expect=STRING");
  657. printf (_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT);
  658. printf (" %s\n", "-n, nocommand");
  659. printf (" %s\n", _("Suppress SMTP command"));
  660. printf (" %s\n", "-C, --command=STRING");
  661. printf (" %s\n", _("SMTP command (may be used repeatedly)"));
  662. printf (" %s\n", "-R, --command=STRING");
  663. printf (" %s\n", _("Expected response to command (may be used repeatedly)"));
  664. printf (" %s\n", "-f, --from=STRING");
  665. printf (" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")),
  666. #ifdef HAVE_SSL
  667. printf (" %s\n", "-D, --certificate=INTEGER");
  668. printf (" %s\n", _("Minimum number of days a certificate has to be valid."));
  669. printf (" %s\n", "-S, --starttls");
  670. printf (" %s\n", _("Use STARTTLS for the connection."));
  671. #endif
  672. printf (" %s\n", "-A, --authtype=STRING");
  673. printf (" %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)"));
  674. printf (" %s\n", "-U, --authuser=STRING");
  675. printf (" %s\n", _("SMTP AUTH username"));
  676. printf (" %s\n", "-P, --authpass=STRING");
  677. printf (" %s\n", _("SMTP AUTH password"));
  678. printf (_(UT_WARN_CRIT));
  679. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  680. printf (_(UT_VERBOSE));
  681. printf("\n");
  682. printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
  683. printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful"));
  684. printf ("%s\n", _("connects, but incorrect reponse messages from the host result in"));
  685. printf ("%s\n", _("STATE_WARNING return values."));
  686. printf (_(UT_SUPPORT));
  687. }
  688. void
  689. print_usage (void)
  690. {
  691. printf (_("Usage:"));
  692. printf ("%s -H host [-p port] [-e expect] [-C command] [-f from addr]", progname);
  693. printf ("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout]\n");
  694. printf ("[-S] [-D days] [-n] [-v] [-4|-6]\n");
  695. }