check_smtp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. # ifdef USE_OPENSSL
  254. if ( check_cert ) {
  255. result = np_net_ssl_check_cert(days_till_exp);
  256. if(result != STATE_OK){
  257. printf (_("CRITICAL - Cannot retrieve server certificate.\n"));
  258. }
  259. my_close();
  260. return result;
  261. }
  262. # endif /* USE_OPENSSL */
  263. }
  264. #endif
  265. /* sendmail will syslog a "NOQUEUE" error if session does not attempt
  266. * to do something useful. This can be prevented by giving a command
  267. * even if syntax is illegal (MAIL requires a FROM:<...> argument)
  268. *
  269. * According to rfc821 you can include a null reversepath in the from command
  270. * - but a log message is generated on the smtp server.
  271. *
  272. * You can disable sending mail_command with '--nocommand'
  273. * Use the -f option to provide a FROM address
  274. */
  275. if (smtp_use_dummycmd) {
  276. my_send(cmd_str, strlen(cmd_str));
  277. my_recv(buffer, MAX_INPUT_BUFFER-1);
  278. if (verbose)
  279. printf("%s", buffer);
  280. }
  281. while (n < ncommands) {
  282. asprintf (&cmd_str, "%s%s", commands[n], "\r\n");
  283. my_send(cmd_str, strlen(cmd_str));
  284. my_recv(buffer, MAX_INPUT_BUFFER-1);
  285. if (verbose)
  286. printf("%s", buffer);
  287. strip (buffer);
  288. if (n < nresponses) {
  289. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  290. errcode = regcomp (&preg, responses[n], cflags);
  291. if (errcode != 0) {
  292. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  293. printf (_("Could Not Compile Regular Expression"));
  294. return ERROR;
  295. }
  296. excode = regexec (&preg, buffer, 10, pmatch, eflags);
  297. if (excode == 0) {
  298. result = STATE_OK;
  299. }
  300. else if (excode == REG_NOMATCH) {
  301. result = STATE_WARNING;
  302. printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
  303. }
  304. else {
  305. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  306. printf (_("Execute Error: %s\n"), errbuf);
  307. result = STATE_UNKNOWN;
  308. }
  309. }
  310. n++;
  311. }
  312. if (authtype != NULL) {
  313. if (strcmp (authtype, "LOGIN") == 0) {
  314. char *abuf;
  315. int ret;
  316. do {
  317. if (authuser == NULL) {
  318. result = STATE_CRITICAL;
  319. error_msg = _("no authuser specified, ");
  320. break;
  321. }
  322. if (authpass == NULL) {
  323. result = STATE_CRITICAL;
  324. error_msg = _("no authpass specified, ");
  325. break;
  326. }
  327. /* send AUTH LOGIN */
  328. my_send(SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN));
  329. if (verbose)
  330. printf (_("sent %s\n"), "AUTH LOGIN");
  331. if((ret = my_recv(buffer, MAXBUF - 1)) < 0){
  332. error_msg = _("recv() failed after AUTH LOGIN, \n");
  333. result = STATE_WARNING;
  334. break;
  335. }
  336. buffer[ret] = 0;
  337. if (verbose)
  338. printf (_("received %s\n"), buffer);
  339. if (strncmp (buffer, "334", 3) != 0) {
  340. result = STATE_CRITICAL;
  341. error_msg = _("invalid response received after AUTH LOGIN, ");
  342. break;
  343. }
  344. /* encode authuser with base64 */
  345. abuf = base64 (authuser, strlen(authuser));
  346. strcat (abuf, "\r\n");
  347. my_send(abuf, strlen(abuf));
  348. if (verbose)
  349. printf (_("sent %s\n"), abuf);
  350. if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
  351. result = STATE_CRITICAL;
  352. error_msg = _("recv() failed after sending authuser, ");
  353. break;
  354. }
  355. buffer[ret] = 0;
  356. if (verbose) {
  357. printf (_("received %s\n"), buffer);
  358. }
  359. if (strncmp (buffer, "334", 3) != 0) {
  360. result = STATE_CRITICAL;
  361. error_msg = _("invalid response received after authuser, ");
  362. break;
  363. }
  364. /* encode authpass with base64 */
  365. abuf = base64 (authpass, strlen(authpass));
  366. strcat (abuf, "\r\n");
  367. my_send(abuf, strlen(abuf));
  368. if (verbose) {
  369. printf (_("sent %s\n"), abuf);
  370. }
  371. if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
  372. result = STATE_CRITICAL;
  373. error_msg = _("recv() failed after sending authpass, ");
  374. break;
  375. }
  376. buffer[ret] = 0;
  377. if (verbose) {
  378. printf (_("received %s\n"), buffer);
  379. }
  380. if (strncmp (buffer, "235", 3) != 0) {
  381. result = STATE_CRITICAL;
  382. error_msg = _("invalid response received after authpass, ");
  383. break;
  384. }
  385. break;
  386. } while (0);
  387. } else {
  388. result = STATE_CRITICAL;
  389. error_msg = _("only authtype LOGIN is supported, ");
  390. }
  391. }
  392. /* tell the server we're done */
  393. my_send (SMTP_QUIT, strlen (SMTP_QUIT));
  394. /* finally close the connection */
  395. close (sd);
  396. }
  397. /* reset the alarm */
  398. alarm (0);
  399. microsec = deltime (tv);
  400. elapsed_time = (double)microsec / 1.0e6;
  401. if (result == STATE_OK) {
  402. if (check_critical_time && elapsed_time > (double) critical_time)
  403. result = STATE_CRITICAL;
  404. else if (check_warning_time && elapsed_time > (double) warning_time)
  405. result = STATE_WARNING;
  406. }
  407. printf (_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"),
  408. state_text (result),
  409. (error_msg == NULL ? "" : error_msg),
  410. elapsed_time,
  411. verbose?", ":"", verbose?buffer:"",
  412. fperfdata ("time", elapsed_time, "s",
  413. (int)check_warning_time, warning_time,
  414. (int)check_critical_time, critical_time,
  415. TRUE, 0, FALSE, 0));
  416. return result;
  417. }
  418. /* process command-line arguments */
  419. int
  420. process_arguments (int argc, char **argv)
  421. {
  422. int c;
  423. int option = 0;
  424. static struct option longopts[] = {
  425. {"hostname", required_argument, 0, 'H'},
  426. {"expect", required_argument, 0, 'e'},
  427. {"critical", required_argument, 0, 'c'},
  428. {"warning", required_argument, 0, 'w'},
  429. {"timeout", required_argument, 0, 't'},
  430. {"port", required_argument, 0, 'p'},
  431. {"from", required_argument, 0, 'f'},
  432. {"fqdn", required_argument, 0, 'F'},
  433. {"authtype", required_argument, 0, 'A'},
  434. {"authuser", required_argument, 0, 'U'},
  435. {"authpass", required_argument, 0, 'P'},
  436. {"command", required_argument, 0, 'C'},
  437. {"response", required_argument, 0, 'R'},
  438. {"nocommand", required_argument, 0, 'n'},
  439. {"verbose", no_argument, 0, 'v'},
  440. {"version", no_argument, 0, 'V'},
  441. {"use-ipv4", no_argument, 0, '4'},
  442. {"use-ipv6", no_argument, 0, '6'},
  443. {"help", no_argument, 0, 'h'},
  444. {"starttls",no_argument,0,'S'},
  445. {"certificate",required_argument,0,'D'},
  446. {0, 0, 0, 0}
  447. };
  448. if (argc < 2)
  449. return ERROR;
  450. for (c = 1; c < argc; c++) {
  451. if (strcmp ("-to", argv[c]) == 0)
  452. strcpy (argv[c], "-t");
  453. else if (strcmp ("-wt", argv[c]) == 0)
  454. strcpy (argv[c], "-w");
  455. else if (strcmp ("-ct", argv[c]) == 0)
  456. strcpy (argv[c], "-c");
  457. }
  458. while (1) {
  459. c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:SD:F:A:U:P:",
  460. longopts, &option);
  461. if (c == -1 || c == EOF)
  462. break;
  463. switch (c) {
  464. case 'H': /* hostname */
  465. if (is_host (optarg)) {
  466. server_address = optarg;
  467. }
  468. else {
  469. usage2 (_("Invalid hostname/address"), optarg);
  470. }
  471. break;
  472. case 'p': /* port */
  473. if (is_intpos (optarg))
  474. server_port = atoi (optarg);
  475. else
  476. usage4 (_("Port must be a positive integer"));
  477. break;
  478. case 'F':
  479. /* localhostname */
  480. localhostname = strdup(optarg);
  481. break;
  482. case 'f': /* from argument */
  483. from_arg = optarg;
  484. smtp_use_dummycmd = 1;
  485. break;
  486. case 'A':
  487. authtype = optarg;
  488. break;
  489. case 'U':
  490. authuser = optarg;
  491. break;
  492. case 'P':
  493. authpass = optarg;
  494. break;
  495. case 'e': /* server expect string on 220 */
  496. server_expect = optarg;
  497. break;
  498. case 'C': /* commands */
  499. if (ncommands >= command_size) {
  500. commands = realloc (commands, command_size+8);
  501. if (commands == NULL)
  502. die (STATE_UNKNOWN,
  503. _("Could not realloc() units [%d]\n"), ncommands);
  504. }
  505. commands[ncommands] = optarg;
  506. ncommands++;
  507. break;
  508. case 'R': /* server responses */
  509. if (nresponses >= response_size) {
  510. responses = realloc (responses, response_size+8);
  511. if (responses == NULL)
  512. die (STATE_UNKNOWN,
  513. _("Could not realloc() units [%d]\n"), nresponses);
  514. }
  515. responses[nresponses] = optarg;
  516. nresponses++;
  517. break;
  518. case 'c': /* critical time threshold */
  519. if (is_intnonneg (optarg)) {
  520. critical_time = atoi (optarg);
  521. check_critical_time = TRUE;
  522. }
  523. else {
  524. usage4 (_("Critical time must be a positive integer"));
  525. }
  526. break;
  527. case 'w': /* warning time threshold */
  528. if (is_intnonneg (optarg)) {
  529. warning_time = atoi (optarg);
  530. check_warning_time = TRUE;
  531. }
  532. else {
  533. usage4 (_("Warning time must be a positive integer"));
  534. }
  535. break;
  536. case 'v': /* verbose */
  537. verbose++;
  538. break;
  539. case 't': /* timeout */
  540. if (is_intnonneg (optarg)) {
  541. socket_timeout = atoi (optarg);
  542. }
  543. else {
  544. usage4 (_("Timeout interval must be a positive integer"));
  545. }
  546. break;
  547. case 'S':
  548. /* starttls */
  549. use_ssl = TRUE;
  550. use_ehlo = TRUE;
  551. break;
  552. case 'D':
  553. /* Check SSL cert validity */
  554. #ifdef USE_OPENSSL
  555. if (!is_intnonneg (optarg))
  556. usage2 ("Invalid certificate expiration period",optarg);
  557. days_till_exp = atoi (optarg);
  558. check_cert = TRUE;
  559. #else
  560. usage (_("SSL support not available - install OpenSSL and recompile"));
  561. #endif
  562. break;
  563. case '4':
  564. address_family = AF_INET;
  565. break;
  566. case '6':
  567. #ifdef USE_IPV6
  568. address_family = AF_INET6;
  569. #else
  570. usage4 (_("IPv6 support not available"));
  571. #endif
  572. break;
  573. case 'V': /* version */
  574. print_revision (progname, revision);
  575. exit (STATE_OK);
  576. case 'h': /* help */
  577. print_help ();
  578. exit (STATE_OK);
  579. case '?': /* help */
  580. usage2 (_("Unknown argument"), optarg);
  581. }
  582. }
  583. c = optind;
  584. if (server_address == NULL) {
  585. if (argv[c]) {
  586. if (is_host (argv[c]))
  587. server_address = argv[c];
  588. else
  589. usage2 (_("Invalid hostname/address"), argv[c]);
  590. }
  591. else {
  592. asprintf (&server_address, "127.0.0.1");
  593. }
  594. }
  595. if (server_expect == NULL)
  596. server_expect = strdup (SMTP_EXPECT);
  597. if (mail_command == NULL)
  598. mail_command = strdup("MAIL ");
  599. if (from_arg==NULL)
  600. from_arg = strdup(" ");
  601. return validate_arguments ();
  602. }
  603. int
  604. validate_arguments (void)
  605. {
  606. return OK;
  607. }
  608. int
  609. my_close (void)
  610. {
  611. #ifdef HAVE_SSL
  612. np_net_ssl_cleanup();
  613. #endif
  614. return close(sd);
  615. }
  616. void
  617. print_help (void)
  618. {
  619. char *myport;
  620. asprintf (&myport, "%d", SMTP_PORT);
  621. print_revision (progname, revision);
  622. printf ("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n");
  623. printf (COPYRIGHT, copyright, email);
  624. printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host."));
  625. printf ("\n\n");
  626. print_usage ();
  627. printf (_(UT_HELP_VRSN));
  628. printf (_(UT_HOST_PORT), 'p', myport);
  629. printf (_(UT_IPv46));
  630. printf (" %s\n", "-e, --expect=STRING");
  631. printf (_("String to expect in first line of server response (default: '%s')"),SMTP_EXPECT);
  632. printf (" %s\n\n", "-n, nocommand\n");
  633. printf (" %s\n", _("Suppress SMTP command"));
  634. printf (" %s\n", "-C, --command=STRING");
  635. printf (" %s\n", _("SMTP command (may be used repeatedly)"));
  636. printf (" %s\n", "-R, --command=STRING");
  637. printf (" %s\n", _("Expected response to command (may be used repeatedly)"));
  638. printf (" %s\n", "-f, --from=STRING");
  639. printf (" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")),
  640. #ifdef HAVE_SSL
  641. printf (" %s\n", "-D, --certificate=INTEGER");
  642. printf (" %s\n", _("Minimum number of days a certificate has to be valid."));
  643. printf (" %s\n", "-S, --starttls");
  644. printf (" %s\n", _("Use STARTTLS for the connection."));
  645. #endif
  646. printf (" %s\n", "-A, --authtype=STRING");
  647. printf (" %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)"));
  648. printf (" %s\n", "-U, --authuser=STRING");
  649. printf (" %s\n", _("SMTP AUTH username"));
  650. printf (" %s\n", "-P, --authpass=STRING");
  651. printf (" %s\n", _("SMTP AUTH password"));
  652. printf (_(UT_WARN_CRIT));
  653. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  654. printf (_(UT_VERBOSE));
  655. printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
  656. printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful"));
  657. printf ("%s\n", _("connects, but incorrect reponse messages from the host result in"));
  658. printf ("%s\n", _("STATE_WARNING return values."));
  659. printf (_(UT_SUPPORT));
  660. }
  661. void
  662. print_usage (void)
  663. {
  664. printf (_("Usage:"));
  665. printf ("%s -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
  666. [-A authtype -U authuser -P authpass]\n\
  667. [-w warn] [-c crit] [-t timeout] [-S] [-D days] [-n] [-v] [-4|-6]\n", progname);
  668. }