1 /******************************************************************************
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 ******************************************************************************/
19 const char *progname = "check_ldap";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "netutils.h"
26 #include "utils.h"
28 #include <lber.h>
29 #include <ldap.h>
31 enum {
32 UNDEFINED = -1,
33 #ifdef HAVE_LDAP_SET_OPTION
34 DEFAULT_PROTOCOL = 2,
35 #endif
36 DEFAULT_PORT = 389
37 };
39 void
40 print_usage ()
41 {
42 printf (_("\
43 Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n\
44 [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n\
45 (Note: all times are in seconds.)\n"),
46 progname, (HAVE_LDAP_SET_OPTION ? "[-2|-3] [-4|-6]" : ""));
47 printf (_(UT_HLP_VRS), progname, progname);
48 }
50 void
51 print_help ()
52 {
53 char *myport;
54 asprintf (&myport, "%d", DEFAULT_PORT);
56 print_revision (progname, revision);
58 printf (_("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"));
59 printf (_(COPYRIGHT), copyright, email);
61 print_usage ();
63 printf (_(UT_HELP_VRSN));
65 printf (_(UT_HOST_PORT), 'p', myport);
67 printf (_(UT_IPv46));
69 printf (_("\
70 -a [--attr]\n\
71 ldap attribute to search (default: \"(objectclass=*)\"\n\
72 -b [--base]\n\
73 ldap base (eg. ou=my unit, o=my org, c=at)\n\
74 -D [--bind]\n\
75 ldap bind DN (if required)\n\
76 -P [--pass]\n\
77 ldap password (if required)\n"));
79 #ifdef HAVE_LDAP_SET_OPTION
80 printf (_("\
81 -2 [--ver2]\n\
82 use ldap protocol version 2\n\
83 -3 [--ver3]\n\
84 use ldap protocol version 3\n\
85 (default protocol version: %d)\n"),
86 DEFAULT_PROTOCOL);
87 #endif
89 printf (_(UT_WARN_CRIT));
91 printf (_(UT_SUPPORT));
92 }
93 \f
94 int process_arguments (int, char **);
95 int validate_arguments (void);
97 char ld_defattr[] = "(objectclass=*)";
98 char *ld_attr = ld_defattr;
99 char *ld_host = "";
100 char *ld_base = "";
101 char *ld_passwd = NULL;
102 char *ld_binddn = NULL;
103 unsigned int ld_port = DEFAULT_PORT;
104 #ifdef HAVE_LDAP_SET_OPTION
105 int ld_protocol = DEFAULT_PROTOCOL;
106 #endif
107 int warn_time = UNDEFINED;
108 int crit_time = UNDEFINED;
110 int
111 main (int argc, char *argv[])
112 {
114 LDAP *ld;
115 LDAPMessage *result;
117 int t_diff;
118 time_t time0, time1;
120 if (process_arguments (argc, argv) == ERROR)
121 usage (_("check_ldap: could not parse arguments\n"));
123 /* initialize alarm signal handling */
124 signal (SIGALRM, socket_timeout_alarm_handler);
126 /* set socket timeout */
127 alarm (socket_timeout);
129 /* get the start time */
130 time (&time0);
132 /* initialize ldap */
133 if (!(ld = ldap_open (ld_host, ld_port))) {
134 /*ldap_perror(ld, "ldap_open"); */
135 printf (_("Could not connect to the server at port %i\n"), ld_port);
136 return STATE_CRITICAL;
137 }
139 #ifdef HAVE_LDAP_SET_OPTION
140 /* set ldap options */
141 if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
142 LDAP_OPT_SUCCESS ) {
143 printf(_("Could not set protocol version %d\n"), ld_protocol);
144 return STATE_CRITICAL;
145 }
146 #endif
147 /* bind to the ldap server */
148 if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
149 LDAP_SUCCESS) {
150 /*ldap_perror(ld, "ldap_bind"); */
151 printf (_("Could not bind to the ldap-server\n"));
152 return STATE_CRITICAL;
153 }
155 /* do a search of all objectclasses in the base dn */
156 if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
157 != LDAP_SUCCESS) {
158 /*ldap_perror(ld, "ldap_search"); */
159 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
160 return STATE_CRITICAL;
161 }
163 /* unbind from the ldap server */
164 ldap_unbind (ld);
166 /* reset the alarm handler */
167 alarm (0);
169 /* get the finish time */
170 time (&time1);
172 /* calcutate the elapsed time and compare to thresholds */
173 t_diff = time1 - time0;
175 if (crit_time!=UNDEFINED && t_diff>=crit_time) {
176 printf (_("LDAP CRITICAL - %i seconds response time\n"), t_diff);
177 return STATE_CRITICAL;
178 }
180 if (warn_time!=UNDEFINED && t_diff>=warn_time) {
181 printf (_("LDAP WARNING - %i seconds response time\n"), t_diff);
182 return STATE_WARNING;
183 }
185 /* print out the result */
186 printf (_("LDAP OK - %i seconds response time\n"), t_diff);
188 return STATE_OK;
189 }
191 /* process command-line arguments */
192 int
193 process_arguments (int argc, char **argv)
194 {
195 int c;
197 int option_index = 0;
198 /* initialize the long option struct */
199 static struct option longopts[] = {
200 {"help", no_argument, 0, 'h'},
201 {"version", no_argument, 0, 'V'},
202 {"timeout", required_argument, 0, 't'},
203 {"host", required_argument, 0, 'H'},
204 {"base", required_argument, 0, 'b'},
205 {"attr", required_argument, 0, 'a'},
206 {"bind", required_argument, 0, 'D'},
207 {"pass", required_argument, 0, 'P'},
208 #ifdef HAVE_LDAP_SET_OPTION
209 {"ver2", no_argument, 0, '2'},
210 {"ver3", no_argument, 0, '3'},
211 #endif
212 {"use-ipv4", no_argument, 0, '4'},
213 {"use-ipv6", no_argument, 0, '6'},
214 {"port", required_argument, 0, 'p'},
215 {"warn", required_argument, 0, 'w'},
216 {"crit", required_argument, 0, 'c'},
217 {0, 0, 0, 0}
218 };
220 if (argc < 2)
221 return ERROR;
223 for (c = 1; c < argc; c++) {
224 if (strcmp ("-to", argv[c]) == 0)
225 strcpy (argv[c], "-t");
226 }
228 while (1) {
229 c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option_index);
231 if (c == -1 || c == EOF)
232 break;
234 switch (c) {
235 case 'h': /* help */
236 print_help ();
237 exit (STATE_OK);
238 case 'V': /* version */
239 print_revision (progname, revision);
240 exit (STATE_OK);
241 case 't': /* timeout period */
242 if (!is_intnonneg (optarg))
243 usage2 (_("timeout interval must be a positive integer"), optarg);
244 socket_timeout = atoi (optarg);
245 break;
246 case 'H':
247 ld_host = optarg;
248 break;
249 case 'b':
250 ld_base = optarg;
251 break;
252 case 'p':
253 ld_port = atoi (optarg);
254 break;
255 case 'a':
256 ld_attr = optarg;
257 break;
258 case 'D':
259 ld_binddn = optarg;
260 break;
261 case 'P':
262 ld_passwd = optarg;
263 break;
264 case 'w':
265 warn_time = atoi (optarg);
266 break;
267 case 'c':
268 crit_time = atoi (optarg);
269 break;
270 #ifdef HAVE_LDAP_SET_OPTION
271 case '2':
272 ld_protocol = 2;
273 break;
274 case '3':
275 ld_protocol = 3;
276 break;
277 #endif
278 case '4':
279 address_family = AF_INET;
280 break;
281 case '6':
282 #ifdef USE_IPV6
283 address_family = AF_INET6;
284 #else
285 usage (_("IPv6 support not available\n"));
286 #endif
287 break;
288 default:
289 usage (_("check_ldap: could not parse unknown arguments\n"));
290 break;
291 }
292 }
294 c = optind;
295 if (strlen(ld_host) == 0 && is_host(argv[c])) {
296 asprintf (&ld_host, "%s", argv[c++]);
297 }
298 if (strlen(ld_base) == 0 && argv[c]) {
299 asprintf (&ld_base, "%s", argv[c++]);
300 }
302 return validate_arguments ();
303 }
305 int
306 validate_arguments ()
307 {
308 if (strlen(ld_host) == 0)
309 usage (_("please specify the host name\n"));
311 if (strlen(ld_base) == 0)
312 usage (_("please specify the LDAP base\n"));
314 return OK;
316 }