check_smtp.c 19 KB

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