check_smtp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 = NULL;
  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. /* initialize the HELO command with the localhostname */
  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. } else {
  175. helocmd = localhostname;
  176. }
  177. if(use_ehlo)
  178. asprintf (&helocmd, "%s%s%s", SMTP_EHLO, helocmd, "\r\n");
  179. else
  180. asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
  181. if (verbose)
  182. printf("HELOCMD: %s", helocmd);
  183. /* initialize the MAIL command with optional FROM command */
  184. asprintf (&cmd_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
  185. if (verbose && smtp_use_dummycmd)
  186. printf ("FROM CMD: %s", cmd_str);
  187. /* initialize alarm signal handling */
  188. (void) signal (SIGALRM, socket_timeout_alarm_handler);
  189. /* set socket timeout */
  190. (void) alarm (socket_timeout);
  191. /* start timer */
  192. gettimeofday (&tv, NULL);
  193. /* try to connect to the host at the given port number */
  194. result = my_tcp_connect (server_address, server_port, &sd);
  195. if (result == STATE_OK) { /* we connected */
  196. /* watch for the SMTP connection string and */
  197. /* return a WARNING status if we couldn't read any data */
  198. if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
  199. printf (_("recv() failed\n"));
  200. result = STATE_WARNING;
  201. }
  202. else {
  203. if (verbose)
  204. printf ("%s", buffer);
  205. /* strip the buffer of carriage returns */
  206. strip (buffer);
  207. /* make sure we find the response we are looking for */
  208. if (!strstr (buffer, server_expect)) {
  209. if (server_port == SMTP_PORT)
  210. printf (_("Invalid SMTP response received from host\n"));
  211. else
  212. printf (_("Invalid SMTP response received from host on port %d\n"),
  213. server_port);
  214. result = STATE_WARNING;
  215. }
  216. }
  217. /* send the HELO/EHLO command */
  218. send(sd, helocmd, strlen(helocmd), 0);
  219. /* allow for response to helo command to reach us */
  220. if(read (sd, buffer, MAXBUF - 1) < 0){
  221. printf (_("recv() failed\n"));
  222. return STATE_WARNING;
  223. } else if(use_ehlo){
  224. buffer[MAXBUF-1]='\0';
  225. if(strstr(buffer, "250 STARTTLS") != NULL ||
  226. strstr(buffer, "250-STARTTLS") != NULL){
  227. supports_tls=TRUE;
  228. }
  229. }
  230. if(use_ssl && ! supports_tls){
  231. printf(_("WARNING - TLS not supported by server\n"));
  232. send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
  233. return STATE_WARNING;
  234. }
  235. #ifdef HAVE_SSL
  236. if(use_ssl) {
  237. /* send the STARTTLS command */
  238. send(sd, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0);
  239. recv(sd,buffer, MAX_INPUT_BUFFER-1, 0); /* wait for it */
  240. if (!strstr (buffer, server_expect)) {
  241. printf (_("Server does not support STARTTLS\n"));
  242. send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
  243. return STATE_UNKNOWN;
  244. }
  245. result = np_net_ssl_init(sd);
  246. if(result != STATE_OK) {
  247. printf (_("CRITICAL - Cannot create SSL context.\n"));
  248. np_net_ssl_cleanup();
  249. close(sd);
  250. return STATE_CRITICAL;
  251. } else {
  252. ssl_established = 1;
  253. }
  254. /*
  255. * Resend the EHLO command.
  256. *
  257. * RFC 3207 (4.2) says: ``The client MUST discard any knowledge
  258. * obtained from the server, such as the list of SMTP service
  259. * extensions, which was not obtained from the TLS negotiation
  260. * itself. The client SHOULD send an EHLO command as the first
  261. * command after a successful TLS negotiation.'' For this
  262. * reason, some MTAs will not allow an AUTH LOGIN command before
  263. * we resent EHLO via TLS.
  264. */
  265. if (my_send(helocmd, strlen(helocmd)) <= 0) {
  266. printf(_("SMTP UNKNOWN - Cannot send EHLO command via TLS.\n"));
  267. my_close();
  268. return STATE_UNKNOWN;
  269. }
  270. if (verbose)
  271. printf(_("sent %s"), helocmd);
  272. if ((n = my_recv(buffer, MAX_INPUT_BUFFER - 1)) <= 0) {
  273. printf(_("SMTP UNKNOWN - Cannot read EHLO response via TLS.\n"));
  274. my_close();
  275. return STATE_UNKNOWN;
  276. }
  277. if (verbose) {
  278. buffer[n] = '\0';
  279. printf("%s", buffer);
  280. }
  281. # ifdef USE_OPENSSL
  282. if ( check_cert ) {
  283. result = np_net_ssl_check_cert(days_till_exp);
  284. if(result != STATE_OK){
  285. printf (_("CRITICAL - Cannot retrieve server certificate.\n"));
  286. }
  287. my_close();
  288. return result;
  289. }
  290. # endif /* USE_OPENSSL */
  291. }
  292. #endif
  293. /* sendmail will syslog a "NOQUEUE" error if session does not attempt
  294. * to do something useful. This can be prevented by giving a command
  295. * even if syntax is illegal (MAIL requires a FROM:<...> argument)
  296. *
  297. * According to rfc821 you can include a null reversepath in the from command
  298. * - but a log message is generated on the smtp server.
  299. *
  300. * You can disable sending mail_command with '--nocommand'
  301. * Use the -f option to provide a FROM address
  302. */
  303. if (smtp_use_dummycmd) {
  304. my_send(cmd_str, strlen(cmd_str));
  305. my_recv(buffer, MAX_INPUT_BUFFER-1);
  306. if (verbose)
  307. printf("%s", buffer);
  308. }
  309. while (n < ncommands) {
  310. asprintf (&cmd_str, "%s%s", commands[n], "\r\n");
  311. my_send(cmd_str, strlen(cmd_str));
  312. my_recv(buffer, MAX_INPUT_BUFFER-1);
  313. if (verbose)
  314. printf("%s", buffer);
  315. strip (buffer);
  316. if (n < nresponses) {
  317. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  318. errcode = regcomp (&preg, responses[n], cflags);
  319. if (errcode != 0) {
  320. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  321. printf (_("Could Not Compile Regular Expression"));
  322. return ERROR;
  323. }
  324. excode = regexec (&preg, buffer, 10, pmatch, eflags);
  325. if (excode == 0) {
  326. result = STATE_OK;
  327. }
  328. else if (excode == REG_NOMATCH) {
  329. result = STATE_WARNING;
  330. printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
  331. }
  332. else {
  333. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  334. printf (_("Execute Error: %s\n"), errbuf);
  335. result = STATE_UNKNOWN;
  336. }
  337. }
  338. n++;
  339. }
  340. if (authtype != NULL) {
  341. if (strcmp (authtype, "LOGIN") == 0) {
  342. char *abuf;
  343. int ret;
  344. do {
  345. if (authuser == NULL) {
  346. result = STATE_CRITICAL;
  347. error_msg = _("no authuser specified, ");
  348. break;
  349. }
  350. if (authpass == NULL) {
  351. result = STATE_CRITICAL;
  352. error_msg = _("no authpass specified, ");
  353. break;
  354. }
  355. /* send AUTH LOGIN */
  356. my_send(SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN));
  357. if (verbose)
  358. printf (_("sent %s\n"), "AUTH LOGIN");
  359. if((ret = my_recv(buffer, MAXBUF - 1)) < 0){
  360. error_msg = _("recv() failed after AUTH LOGIN, \n");
  361. result = STATE_WARNING;
  362. break;
  363. }
  364. buffer[ret] = 0;
  365. if (verbose)
  366. printf (_("received %s\n"), buffer);
  367. if (strncmp (buffer, "334", 3) != 0) {
  368. result = STATE_CRITICAL;
  369. error_msg = _("invalid response received after AUTH LOGIN, ");
  370. break;
  371. }
  372. /* encode authuser with base64 */
  373. abuf = base64 (authuser, strlen(authuser));
  374. strcat (abuf, "\r\n");
  375. my_send(abuf, strlen(abuf));
  376. if (verbose)
  377. printf (_("sent %s\n"), abuf);
  378. if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
  379. result = STATE_CRITICAL;
  380. error_msg = _("recv() failed after sending authuser, ");
  381. break;
  382. }
  383. buffer[ret] = 0;
  384. if (verbose) {
  385. printf (_("received %s\n"), buffer);
  386. }
  387. if (strncmp (buffer, "334", 3) != 0) {
  388. result = STATE_CRITICAL;
  389. error_msg = _("invalid response received after authuser, ");
  390. break;
  391. }
  392. /* encode authpass with base64 */
  393. abuf = base64 (authpass, strlen(authpass));
  394. strcat (abuf, "\r\n");
  395. my_send(abuf, strlen(abuf));
  396. if (verbose) {
  397. printf (_("sent %s\n"), abuf);
  398. }
  399. if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
  400. result = STATE_CRITICAL;
  401. error_msg = _("recv() failed after sending authpass, ");
  402. break;
  403. }
  404. buffer[ret] = 0;
  405. if (verbose) {
  406. printf (_("received %s\n"), buffer);
  407. }
  408. if (strncmp (buffer, "235", 3) != 0) {
  409. result = STATE_CRITICAL;
  410. error_msg = _("invalid response received after authpass, ");
  411. break;
  412. }
  413. break;
  414. } while (0);
  415. } else {
  416. result = STATE_CRITICAL;
  417. error_msg = _("only authtype LOGIN is supported, ");
  418. }
  419. }
  420. /* tell the server we're done */
  421. my_send (SMTP_QUIT, strlen (SMTP_QUIT));
  422. /* finally close the connection */
  423. close (sd);
  424. }
  425. /* reset the alarm */
  426. alarm (0);
  427. microsec = deltime (tv);
  428. elapsed_time = (double)microsec / 1.0e6;
  429. if (result == STATE_OK) {
  430. if (check_critical_time && elapsed_time > (double) critical_time)
  431. result = STATE_CRITICAL;
  432. else if (check_warning_time && elapsed_time > (double) warning_time)
  433. result = STATE_WARNING;
  434. }
  435. printf (_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"),
  436. state_text (result),
  437. (error_msg == NULL ? "" : error_msg),
  438. elapsed_time,
  439. verbose?", ":"", verbose?buffer:"",
  440. fperfdata ("time", elapsed_time, "s",
  441. (int)check_warning_time, warning_time,
  442. (int)check_critical_time, critical_time,
  443. TRUE, 0, FALSE, 0));
  444. return result;
  445. }
  446. /* process command-line arguments */
  447. int
  448. process_arguments (int argc, char **argv)
  449. {
  450. int c;
  451. int option = 0;
  452. static struct option longopts[] = {
  453. {"hostname", required_argument, 0, 'H'},
  454. {"expect", required_argument, 0, 'e'},
  455. {"critical", required_argument, 0, 'c'},
  456. {"warning", required_argument, 0, 'w'},
  457. {"timeout", required_argument, 0, 't'},
  458. {"port", required_argument, 0, 'p'},
  459. {"from", required_argument, 0, 'f'},
  460. {"fqdn", required_argument, 0, 'F'},
  461. {"authtype", required_argument, 0, 'A'},
  462. {"authuser", required_argument, 0, 'U'},
  463. {"authpass", required_argument, 0, 'P'},
  464. {"command", required_argument, 0, 'C'},
  465. {"response", required_argument, 0, 'R'},
  466. {"nocommand", required_argument, 0, 'n'},
  467. {"verbose", no_argument, 0, 'v'},
  468. {"version", no_argument, 0, 'V'},
  469. {"use-ipv4", no_argument, 0, '4'},
  470. {"use-ipv6", no_argument, 0, '6'},
  471. {"help", no_argument, 0, 'h'},
  472. {"starttls",no_argument,0,'S'},
  473. {"certificate",required_argument,0,'D'},
  474. {0, 0, 0, 0}
  475. };
  476. if (argc < 2)
  477. return ERROR;
  478. for (c = 1; c < argc; c++) {
  479. if (strcmp ("-to", argv[c]) == 0)
  480. strcpy (argv[c], "-t");
  481. else if (strcmp ("-wt", argv[c]) == 0)
  482. strcpy (argv[c], "-w");
  483. else if (strcmp ("-ct", argv[c]) == 0)
  484. strcpy (argv[c], "-c");
  485. }
  486. while (1) {
  487. c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:SD:F:A:U:P:",
  488. longopts, &option);
  489. if (c == -1 || c == EOF)
  490. break;
  491. switch (c) {
  492. case 'H': /* hostname */
  493. if (is_host (optarg)) {
  494. server_address = optarg;
  495. }
  496. else {
  497. usage2 (_("Invalid hostname/address"), optarg);
  498. }
  499. break;
  500. case 'p': /* port */
  501. if (is_intpos (optarg))
  502. server_port = atoi (optarg);
  503. else
  504. usage4 (_("Port must be a positive integer"));
  505. break;
  506. case 'F':
  507. /* localhostname */
  508. localhostname = strdup(optarg);
  509. break;
  510. case 'f': /* from argument */
  511. from_arg = optarg;
  512. smtp_use_dummycmd = 1;
  513. break;
  514. case 'A':
  515. authtype = optarg;
  516. break;
  517. case 'U':
  518. authuser = optarg;
  519. break;
  520. case 'P':
  521. authpass = optarg;
  522. break;
  523. case 'e': /* server expect string on 220 */
  524. server_expect = optarg;
  525. break;
  526. case 'C': /* commands */
  527. if (ncommands >= command_size) {
  528. commands = realloc (commands, command_size+8);
  529. if (commands == NULL)
  530. die (STATE_UNKNOWN,
  531. _("Could not realloc() units [%d]\n"), ncommands);
  532. }
  533. commands[ncommands] = optarg;
  534. ncommands++;
  535. break;
  536. case 'R': /* server responses */
  537. if (nresponses >= response_size) {
  538. responses = realloc (responses, response_size+8);
  539. if (responses == NULL)
  540. die (STATE_UNKNOWN,
  541. _("Could not realloc() units [%d]\n"), nresponses);
  542. }
  543. responses[nresponses] = optarg;
  544. nresponses++;
  545. break;
  546. case 'c': /* critical time threshold */
  547. if (is_intnonneg (optarg)) {
  548. critical_time = atoi (optarg);
  549. check_critical_time = TRUE;
  550. }
  551. else {
  552. usage4 (_("Critical time must be a positive integer"));
  553. }
  554. break;
  555. case 'w': /* warning time threshold */
  556. if (is_intnonneg (optarg)) {
  557. warning_time = atoi (optarg);
  558. check_warning_time = TRUE;
  559. }
  560. else {
  561. usage4 (_("Warning time must be a positive integer"));
  562. }
  563. break;
  564. case 'v': /* verbose */
  565. verbose++;
  566. break;
  567. case 't': /* timeout */
  568. if (is_intnonneg (optarg)) {
  569. socket_timeout = atoi (optarg);
  570. }
  571. else {
  572. usage4 (_("Timeout interval must be a positive integer"));
  573. }
  574. break;
  575. case 'S':
  576. /* starttls */
  577. use_ssl = TRUE;
  578. use_ehlo = TRUE;
  579. break;
  580. case 'D':
  581. /* Check SSL cert validity */
  582. #ifdef USE_OPENSSL
  583. if (!is_intnonneg (optarg))
  584. usage2 ("Invalid certificate expiration period",optarg);
  585. days_till_exp = atoi (optarg);
  586. check_cert = TRUE;
  587. #else
  588. usage (_("SSL support not available - install OpenSSL and recompile"));
  589. #endif
  590. break;
  591. case '4':
  592. address_family = AF_INET;
  593. break;
  594. case '6':
  595. #ifdef USE_IPV6
  596. address_family = AF_INET6;
  597. #else
  598. usage4 (_("IPv6 support not available"));
  599. #endif
  600. break;
  601. case 'V': /* version */
  602. print_revision (progname, revision);
  603. exit (STATE_OK);
  604. case 'h': /* help */
  605. print_help ();
  606. exit (STATE_OK);
  607. case '?': /* help */
  608. usage2 (_("Unknown argument"), optarg);
  609. }
  610. }
  611. c = optind;
  612. if (server_address == NULL) {
  613. if (argv[c]) {
  614. if (is_host (argv[c]))
  615. server_address = argv[c];
  616. else
  617. usage2 (_("Invalid hostname/address"), argv[c]);
  618. }
  619. else {
  620. asprintf (&server_address, "127.0.0.1");
  621. }
  622. }
  623. if (server_expect == NULL)
  624. server_expect = strdup (SMTP_EXPECT);
  625. if (mail_command == NULL)
  626. mail_command = strdup("MAIL ");
  627. if (from_arg==NULL)
  628. from_arg = strdup(" ");
  629. return validate_arguments ();
  630. }
  631. int
  632. validate_arguments (void)
  633. {
  634. return OK;
  635. }
  636. int
  637. my_close (void)
  638. {
  639. #ifdef HAVE_SSL
  640. np_net_ssl_cleanup();
  641. #endif
  642. return close(sd);
  643. }
  644. void
  645. print_help (void)
  646. {
  647. char *myport;
  648. asprintf (&myport, "%d", SMTP_PORT);
  649. print_revision (progname, revision);
  650. printf ("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n");
  651. printf (COPYRIGHT, copyright, email);
  652. printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host."));
  653. printf ("\n\n");
  654. print_usage ();
  655. printf (_(UT_HELP_VRSN));
  656. printf (_(UT_HOST_PORT), 'p', myport);
  657. printf (_(UT_IPv46));
  658. printf (" %s\n", "-e, --expect=STRING");
  659. printf (_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT);
  660. printf (" %s\n", "-n, nocommand");
  661. printf (" %s\n", _("Suppress SMTP command"));
  662. printf (" %s\n", "-C, --command=STRING");
  663. printf (" %s\n", _("SMTP command (may be used repeatedly)"));
  664. printf (" %s\n", "-R, --command=STRING");
  665. printf (" %s\n", _("Expected response to command (may be used repeatedly)"));
  666. printf (" %s\n", "-f, --from=STRING");
  667. printf (" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")),
  668. #ifdef HAVE_SSL
  669. printf (" %s\n", "-D, --certificate=INTEGER");
  670. printf (" %s\n", _("Minimum number of days a certificate has to be valid."));
  671. printf (" %s\n", "-S, --starttls");
  672. printf (" %s\n", _("Use STARTTLS for the connection."));
  673. #endif
  674. printf (" %s\n", "-A, --authtype=STRING");
  675. printf (" %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)"));
  676. printf (" %s\n", "-U, --authuser=STRING");
  677. printf (" %s\n", _("SMTP AUTH username"));
  678. printf (" %s\n", "-P, --authpass=STRING");
  679. printf (" %s\n", _("SMTP AUTH password"));
  680. printf (_(UT_WARN_CRIT));
  681. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  682. printf (_(UT_VERBOSE));
  683. printf("\n");
  684. printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
  685. printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful"));
  686. printf ("%s\n", _("connects, but incorrect reponse messages from the host result in"));
  687. printf ("%s\n", _("STATE_WARNING return values."));
  688. printf (_(UT_SUPPORT));
  689. }
  690. void
  691. print_usage (void)
  692. {
  693. printf (_("Usage:"));
  694. printf ("%s -H host [-p port] [-e expect] [-C command] [-f from addr]", progname);
  695. printf ("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout]\n");
  696. printf ("[-S] [-D days] [-n] [-v] [-4|-6]\n");
  697. }