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_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
93 printf (_(UT_VERBOSE));
95 printf (_(UT_SUPPORT));
96 }
97 \f
98 int process_arguments (int, char **);
99 int validate_arguments (void);
101 char ld_defattr[] = "(objectclass=*)";
102 char *ld_attr = ld_defattr;
103 char *ld_host = "";
104 char *ld_base = "";
105 char *ld_passwd = NULL;
106 char *ld_binddn = NULL;
107 unsigned int ld_port = DEFAULT_PORT;
108 #ifdef HAVE_LDAP_SET_OPTION
109 int ld_protocol = DEFAULT_PROTOCOL;
110 #endif
111 int warn_time = UNDEFINED;
112 int crit_time = UNDEFINED;
114 int
115 main (int argc, char *argv[])
116 {
118 LDAP *ld;
119 LDAPMessage *result;
121 int t_diff;
122 time_t time0, time1;
124 if (process_arguments (argc, argv) == ERROR)
125 usage (_("check_ldap: could not parse arguments\n"));
127 /* initialize alarm signal handling */
128 signal (SIGALRM, socket_timeout_alarm_handler);
130 /* set socket timeout */
131 alarm (socket_timeout);
133 /* get the start time */
134 time (&time0);
136 /* initialize ldap */
137 if (!(ld = ldap_open (ld_host, ld_port))) {
138 /*ldap_perror(ld, "ldap_open"); */
139 printf (_("Could not connect to the server at port %i\n"), ld_port);
140 return STATE_CRITICAL;
141 }
143 #ifdef HAVE_LDAP_SET_OPTION
144 /* set ldap options */
145 if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
146 LDAP_OPT_SUCCESS ) {
147 printf(_("Could not set protocol version %d\n"), ld_protocol);
148 return STATE_CRITICAL;
149 }
150 #endif
151 /* bind to the ldap server */
152 if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
153 LDAP_SUCCESS) {
154 /*ldap_perror(ld, "ldap_bind"); */
155 printf (_("Could not bind to the ldap-server\n"));
156 return STATE_CRITICAL;
157 }
159 /* do a search of all objectclasses in the base dn */
160 if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
161 != LDAP_SUCCESS) {
162 /*ldap_perror(ld, "ldap_search"); */
163 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
164 return STATE_CRITICAL;
165 }
167 /* unbind from the ldap server */
168 ldap_unbind (ld);
170 /* reset the alarm handler */
171 alarm (0);
173 /* get the finish time */
174 time (&time1);
176 /* calcutate the elapsed time and compare to thresholds */
177 t_diff = time1 - time0;
179 if (crit_time!=UNDEFINED && t_diff>=crit_time) {
180 printf (_("LDAP CRITICAL - %i seconds response time\n"), t_diff);
181 return STATE_CRITICAL;
182 }
184 if (warn_time!=UNDEFINED && t_diff>=warn_time) {
185 printf (_("LDAP WARNING - %i seconds response time\n"), t_diff);
186 return STATE_WARNING;
187 }
189 /* print out the result */
190 printf (_("LDAP OK - %i seconds response time\n"), t_diff);
192 return STATE_OK;
193 }
195 /* process command-line arguments */
196 int
197 process_arguments (int argc, char **argv)
198 {
199 int c;
201 int option_index = 0;
202 /* initialize the long option struct */
203 static struct option longopts[] = {
204 {"help", no_argument, 0, 'h'},
205 {"version", no_argument, 0, 'V'},
206 {"timeout", required_argument, 0, 't'},
207 {"host", required_argument, 0, 'H'},
208 {"base", required_argument, 0, 'b'},
209 {"attr", required_argument, 0, 'a'},
210 {"bind", required_argument, 0, 'D'},
211 {"pass", required_argument, 0, 'P'},
212 #ifdef HAVE_LDAP_SET_OPTION
213 {"ver2", no_argument, 0, '2'},
214 {"ver3", no_argument, 0, '3'},
215 #endif
216 {"use-ipv4", no_argument, 0, '4'},
217 {"use-ipv6", no_argument, 0, '6'},
218 {"port", required_argument, 0, 'p'},
219 {"warn", required_argument, 0, 'w'},
220 {"crit", required_argument, 0, 'c'},
221 {0, 0, 0, 0}
222 };
224 if (argc < 2)
225 return ERROR;
227 for (c = 1; c < argc; c++) {
228 if (strcmp ("-to", argv[c]) == 0)
229 strcpy (argv[c], "-t");
230 }
232 while (1) {
233 c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option_index);
235 if (c == -1 || c == EOF)
236 break;
238 switch (c) {
239 case 'h': /* help */
240 print_help ();
241 exit (STATE_OK);
242 case 'V': /* version */
243 print_revision (progname, revision);
244 exit (STATE_OK);
245 case 't': /* timeout period */
246 if (!is_intnonneg (optarg))
247 usage2 (_("timeout interval must be a positive integer"), optarg);
248 socket_timeout = atoi (optarg);
249 break;
250 case 'H':
251 ld_host = optarg;
252 break;
253 case 'b':
254 ld_base = optarg;
255 break;
256 case 'p':
257 ld_port = atoi (optarg);
258 break;
259 case 'a':
260 ld_attr = optarg;
261 break;
262 case 'D':
263 ld_binddn = optarg;
264 break;
265 case 'P':
266 ld_passwd = optarg;
267 break;
268 case 'w':
269 warn_time = atoi (optarg);
270 break;
271 case 'c':
272 crit_time = atoi (optarg);
273 break;
274 #ifdef HAVE_LDAP_SET_OPTION
275 case '2':
276 ld_protocol = 2;
277 break;
278 case '3':
279 ld_protocol = 3;
280 break;
281 #endif
282 case '4':
283 address_family = AF_INET;
284 break;
285 case '6':
286 #ifdef USE_IPV6
287 address_family = AF_INET6;
288 #else
289 usage (_("IPv6 support not available\n"));
290 #endif
291 break;
292 default:
293 usage (_("check_ldap: could not parse unknown arguments\n"));
294 break;
295 }
296 }
298 c = optind;
299 if (strlen(ld_host) == 0 && is_host(argv[c])) {
300 asprintf (&ld_host, "%s", argv[c++]);
301 }
302 if (strlen(ld_base) == 0 && argv[c]) {
303 asprintf (&ld_base, "%s", argv[c++]);
304 }
306 return validate_arguments ();
307 }
309 int
310 validate_arguments ()
311 {
312 if (strlen(ld_host) == 0)
313 usage (_("please specify the host name\n"));
315 if (strlen(ld_base) == 0)
316 usage (_("please specify the LDAP base\n"));
318 return OK;
320 }