check_smtp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*****************************************************************************
  2. *
  3. * Nagios check_smtp plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2007 Nagios Plugins Development 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. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * $Id$
  31. *
  32. *****************************************************************************/
  33. const char *progname = "check_smtp";
  34. const char *revision = "$Revision$";
  35. const char *copyright = "2000-2007";
  36. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  37. #include "common.h"
  38. #include "netutils.h"
  39. #include "utils.h"
  40. #include "base64.h"
  41. #include <ctype.h>
  42. #ifdef HAVE_SSL
  43. int check_cert = FALSE;
  44. int days_till_exp;
  45. # define my_recv(buf, len) ((use_ssl && ssl_established) ? np_net_ssl_read(buf, len) : read(sd, buf, len))
  46. # define my_send(buf, len) ((use_ssl && ssl_established) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0))
  47. #else /* ifndef HAVE_SSL */
  48. # define my_recv(buf, len) read(sd, buf, len)
  49. # define my_send(buf, len) send(sd, buf, len, 0)
  50. #endif
  51. enum {
  52. SMTP_PORT = 25
  53. };
  54. #define SMTP_EXPECT "220"
  55. #define SMTP_HELO "HELO "
  56. #define SMTP_EHLO "EHLO "
  57. #define SMTP_QUIT "QUIT\r\n"
  58. #define SMTP_STARTTLS "STARTTLS\r\n"
  59. #define SMTP_AUTH_LOGIN "AUTH LOGIN\r\n"
  60. #ifndef HOST_MAX_BYTES
  61. #define HOST_MAX_BYTES 255
  62. #endif
  63. #define EHLO_SUPPORTS_STARTTLS 1
  64. int process_arguments (int, char **);
  65. int validate_arguments (void);
  66. void print_help (void);
  67. void print_usage (void);
  68. void smtp_quit(void);
  69. int recvline(char *, size_t);
  70. int recvlines(char *, size_t);
  71. int my_close(void);
  72. #include "regex.h"
  73. char regex_expect[MAX_INPUT_BUFFER] = "";
  74. regex_t preg;
  75. regmatch_t pmatch[10];
  76. char timestamp[20] = "";
  77. char errbuf[MAX_INPUT_BUFFER];
  78. int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  79. int eflags = 0;
  80. int errcode, excode;
  81. int server_port = SMTP_PORT;
  82. char *server_address = NULL;
  83. char *server_expect = NULL;
  84. int smtp_use_dummycmd = 0;
  85. char *mail_command = NULL;
  86. char *from_arg = NULL;
  87. int ncommands=0;
  88. int command_size=0;
  89. int nresponses=0;
  90. int response_size=0;
  91. char **commands = NULL;
  92. char **responses = NULL;
  93. char *authtype = NULL;
  94. char *authuser = NULL;
  95. char *authpass = NULL;
  96. int warning_time = 0;
  97. int check_warning_time = FALSE;
  98. int critical_time = 0;
  99. int check_critical_time = FALSE;
  100. int verbose = 0;
  101. int use_ssl = FALSE;
  102. short use_ehlo = FALSE;
  103. short ssl_established = 0;
  104. char *localhostname = NULL;
  105. int sd;
  106. char buffer[MAX_INPUT_BUFFER];
  107. enum {
  108. TCP_PROTOCOL = 1,
  109. UDP_PROTOCOL = 2,
  110. };
  111. int
  112. main (int argc, char **argv)
  113. {
  114. short supports_tls=FALSE;
  115. int n = 0;
  116. double elapsed_time;
  117. long microsec;
  118. int result = STATE_UNKNOWN;
  119. char *cmd_str = NULL;
  120. char *helocmd = NULL;
  121. char *error_msg = "";
  122. struct timeval tv;
  123. setlocale (LC_ALL, "");
  124. bindtextdomain (PACKAGE, LOCALEDIR);
  125. textdomain (PACKAGE);
  126. /* Parse extra opts if any */
  127. argv=np_extra_opts (&argc, argv, progname);
  128. if (process_arguments (argc, argv) == ERROR)
  129. usage4 (_("Could not parse arguments"));
  130. /* If localhostname not set on command line, use gethostname to set */
  131. if(! localhostname){
  132. localhostname = malloc (HOST_MAX_BYTES);
  133. if(!localhostname){
  134. printf(_("malloc() failed!\n"));
  135. return STATE_CRITICAL;
  136. }
  137. if(gethostname(localhostname, HOST_MAX_BYTES)){
  138. printf(_("gethostname() failed!\n"));
  139. return STATE_CRITICAL;
  140. }
  141. }
  142. if(use_ehlo)
  143. asprintf (&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n");
  144. else
  145. asprintf (&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n");
  146. if (verbose)
  147. printf("HELOCMD: %s", helocmd);
  148. /* initialize the MAIL command with optional FROM command */
  149. asprintf (&cmd_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
  150. if (verbose && smtp_use_dummycmd)
  151. printf ("FROM CMD: %s", cmd_str);
  152. /* initialize alarm signal handling */
  153. (void) signal (SIGALRM, socket_timeout_alarm_handler);
  154. /* set socket timeout */
  155. (void) alarm (socket_timeout);
  156. /* start timer */
  157. gettimeofday (&tv, NULL);
  158. /* try to connect to the host at the given port number */
  159. result = my_tcp_connect (server_address, server_port, &sd);
  160. if (result == STATE_OK) { /* we connected */
  161. /* watch for the SMTP connection string and */
  162. /* return a WARNING status if we couldn't read any data */
  163. if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) {
  164. printf (_("recv() failed\n"));
  165. result = STATE_WARNING;
  166. }
  167. else {
  168. if (verbose)
  169. printf ("%s", buffer);
  170. /* strip the buffer of carriage returns */
  171. strip (buffer);
  172. /* make sure we find the response we are looking for */
  173. if (!strstr (buffer, server_expect)) {
  174. if (server_port == SMTP_PORT)
  175. printf (_("Invalid SMTP response received from host: %s\n"), buffer);
  176. else
  177. printf (_("Invalid SMTP response received from host on port %d: %s\n"),
  178. server_port, buffer);
  179. result = STATE_WARNING;
  180. }
  181. }
  182. /* send the HELO/EHLO command */
  183. send(sd, helocmd, strlen(helocmd), 0);
  184. /* allow for response to helo command to reach us */
  185. if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) {
  186. printf (_("recv() failed\n"));
  187. return STATE_WARNING;
  188. } else if(use_ehlo){
  189. if(strstr(buffer, "250 STARTTLS") != NULL ||
  190. strstr(buffer, "250-STARTTLS") != NULL){
  191. supports_tls=TRUE;
  192. }
  193. }
  194. if(use_ssl && ! supports_tls){
  195. printf(_("WARNING - TLS not supported by server\n"));
  196. smtp_quit();
  197. return STATE_WARNING;
  198. }
  199. #ifdef HAVE_SSL
  200. if(use_ssl) {
  201. /* send the STARTTLS command */
  202. send(sd, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0);
  203. recvlines(buffer, MAX_INPUT_BUFFER); /* wait for it */
  204. if (!strstr (buffer, server_expect)) {
  205. printf (_("Server does not support STARTTLS\n"));
  206. smtp_quit();
  207. return STATE_UNKNOWN;
  208. }
  209. result = np_net_ssl_init(sd);
  210. if(result != STATE_OK) {
  211. printf (_("CRITICAL - Cannot create SSL context.\n"));
  212. np_net_ssl_cleanup();
  213. close(sd);
  214. return STATE_CRITICAL;
  215. } else {
  216. ssl_established = 1;
  217. }
  218. /*
  219. * Resend the EHLO command.
  220. *
  221. * RFC 3207 (4.2) says: ``The client MUST discard any knowledge
  222. * obtained from the server, such as the list of SMTP service
  223. * extensions, which was not obtained from the TLS negotiation
  224. * itself. The client SHOULD send an EHLO command as the first
  225. * command after a successful TLS negotiation.'' For this
  226. * reason, some MTAs will not allow an AUTH LOGIN command before
  227. * we resent EHLO via TLS.
  228. */
  229. if (my_send(helocmd, strlen(helocmd)) <= 0) {
  230. printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS."));
  231. my_close();
  232. return STATE_UNKNOWN;
  233. }
  234. if (verbose)
  235. printf(_("sent %s"), helocmd);
  236. if ((n = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
  237. printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS."));
  238. my_close();
  239. return STATE_UNKNOWN;
  240. }
  241. if (verbose) {
  242. printf("%s", buffer);
  243. }
  244. # ifdef USE_OPENSSL
  245. if ( check_cert ) {
  246. result = np_net_ssl_check_cert(days_till_exp);
  247. if(result != STATE_OK){
  248. printf ("%s\n", _("CRITICAL - Cannot retrieve server certificate."));
  249. }
  250. my_close();
  251. return result;
  252. }
  253. # endif /* USE_OPENSSL */
  254. }
  255. #endif
  256. /* sendmail will syslog a "NOQUEUE" error if session does not attempt
  257. * to do something useful. This can be prevented by giving a command
  258. * even if syntax is illegal (MAIL requires a FROM:<...> argument)
  259. *
  260. * According to rfc821 you can include a null reversepath in the from command
  261. * - but a log message is generated on the smtp server.
  262. *
  263. * You can disable sending mail_command with '--nocommand'
  264. * Use the -f option to provide a FROM address
  265. */
  266. if (smtp_use_dummycmd) {
  267. my_send(cmd_str, strlen(cmd_str));
  268. if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose)
  269. printf("%s", buffer);
  270. }
  271. while (n < ncommands) {
  272. asprintf (&cmd_str, "%s%s", commands[n], "\r\n");
  273. my_send(cmd_str, strlen(cmd_str));
  274. if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose)
  275. printf("%s", buffer);
  276. strip (buffer);
  277. if (n < nresponses) {
  278. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  279. errcode = regcomp (&preg, responses[n], cflags);
  280. if (errcode != 0) {
  281. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  282. printf (_("Could Not Compile Regular Expression"));
  283. return ERROR;
  284. }
  285. excode = regexec (&preg, buffer, 10, pmatch, eflags);
  286. if (excode == 0) {
  287. result = STATE_OK;
  288. }
  289. else if (excode == REG_NOMATCH) {
  290. result = STATE_WARNING;
  291. printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
  292. }
  293. else {
  294. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  295. printf (_("Execute Error: %s\n"), errbuf);
  296. result = STATE_UNKNOWN;
  297. }
  298. }
  299. n++;
  300. }
  301. if (authtype != NULL) {
  302. if (strcmp (authtype, "LOGIN") == 0) {
  303. char *abuf;
  304. int ret;
  305. do {
  306. if (authuser == NULL) {
  307. result = STATE_CRITICAL;
  308. asprintf(&error_msg, _("no authuser specified, "));
  309. break;
  310. }
  311. if (authpass == NULL) {
  312. result = STATE_CRITICAL;
  313. asprintf(&error_msg, _("no authpass specified, "));
  314. break;
  315. }
  316. /* send AUTH LOGIN */
  317. my_send(SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN));
  318. if (verbose)
  319. printf (_("sent %s\n"), "AUTH LOGIN");
  320. if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
  321. asprintf(&error_msg, _("recv() failed after AUTH LOGIN, "));
  322. result = STATE_WARNING;
  323. break;
  324. }
  325. if (verbose)
  326. printf (_("received %s\n"), buffer);
  327. if (strncmp (buffer, "334", 3) != 0) {
  328. result = STATE_CRITICAL;
  329. asprintf(&error_msg, _("invalid response received after AUTH LOGIN, "));
  330. break;
  331. }
  332. /* encode authuser with base64 */
  333. base64_encode_alloc (authuser, strlen(authuser), &abuf);
  334. /* FIXME: abuf shouldn't have enough space to strcat a '\r\n' into it. */
  335. strcat (abuf, "\r\n");
  336. my_send(abuf, strlen(abuf));
  337. if (verbose)
  338. printf (_("sent %s\n"), abuf);
  339. if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
  340. result = STATE_CRITICAL;
  341. asprintf(&error_msg, _("recv() failed after sending authuser, "));
  342. break;
  343. }
  344. if (verbose) {
  345. printf (_("received %s\n"), buffer);
  346. }
  347. if (strncmp (buffer, "334", 3) != 0) {
  348. result = STATE_CRITICAL;
  349. asprintf(&error_msg, _("invalid response received after authuser, "));
  350. break;
  351. }
  352. /* encode authpass with base64 */
  353. base64_encode_alloc (authpass, strlen(authpass), &abuf);
  354. /* FIXME: abuf shouldn't have enough space to strcat a '\r\n' into it. */
  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_EXTRA_OPTS));
  693. printf (_(UT_HOST_PORT), 'p', myport);
  694. printf (_(UT_IPv46));
  695. printf (" %s\n", "-e, --expect=STRING");
  696. printf (_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT);
  697. printf (" %s\n", "-n, nocommand");
  698. printf (" %s\n", _("Suppress SMTP command"));
  699. printf (" %s\n", "-C, --command=STRING");
  700. printf (" %s\n", _("SMTP command (may be used repeatedly)"));
  701. printf (" %s\n", "-R, --command=STRING");
  702. printf (" %s\n", _("Expected response to command (may be used repeatedly)"));
  703. printf (" %s\n", "-f, --from=STRING");
  704. printf (" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")),
  705. #ifdef HAVE_SSL
  706. printf (" %s\n", "-D, --certificate=INTEGER");
  707. printf (" %s\n", _("Minimum number of days a certificate has to be valid."));
  708. printf (" %s\n", "-S, --starttls");
  709. printf (" %s\n", _("Use STARTTLS for the connection."));
  710. #endif
  711. printf (" %s\n", "-A, --authtype=STRING");
  712. printf (" %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)"));
  713. printf (" %s\n", "-U, --authuser=STRING");
  714. printf (" %s\n", _("SMTP AUTH username"));
  715. printf (" %s\n", "-P, --authpass=STRING");
  716. printf (" %s\n", _("SMTP AUTH password"));
  717. printf (_(UT_WARN_CRIT));
  718. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  719. printf (_(UT_VERBOSE));
  720. printf("\n");
  721. printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
  722. printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful"));
  723. printf ("%s\n", _("connects, but incorrect reponse messages from the host result in"));
  724. printf ("%s\n", _("STATE_WARNING return values."));
  725. #ifdef NP_EXTRA_OPTS
  726. printf ("\n");
  727. printf ("%s\n", _("Notes:"));
  728. printf (_(UT_EXTRA_OPTS_NOTES));
  729. #endif
  730. printf (_(UT_SUPPORT));
  731. }
  732. void
  733. print_usage (void)
  734. {
  735. printf (_("Usage:"));
  736. printf ("%s -H host [-p port] [-e expect] [-C command] [-f from addr]", progname);
  737. printf ("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout]\n");
  738. printf ("[-S] [-D days] [-n] [-v] [-4|-6]\n");
  739. }