check_smtp.c 23 KB

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