check_smtp.c 22 KB

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