check_smtp.c 21 KB

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