check_smtp.c 22 KB

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