check_smtp.c 20 KB

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