check_smtp.c 20 KB

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