4
0

check_smtp.c 23 KB

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