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 int process_arguments (int, char **);
40 int validate_arguments (void);
41 void print_help (void);
42 void print_usage (void);
44 char ld_defattr[] = "(objectclass=*)";
45 char *ld_attr = ld_defattr;
46 char *ld_host = NULL;
47 char *ld_base = NULL;
48 char *ld_passwd = NULL;
49 char *ld_binddn = NULL;
50 int ld_port = DEFAULT_PORT;
51 #ifdef HAVE_LDAP_SET_OPTION
52 int ld_protocol = DEFAULT_PROTOCOL;
53 #endif
54 int warn_time = UNDEFINED;
55 int crit_time = UNDEFINED;
57 int
58 main (int argc, char *argv[])
59 {
61 LDAP *ld;
62 LDAPMessage *result;
64 int t_diff;
65 time_t time0, time1;
67 setlocale (LC_ALL, "");
68 bindtextdomain (PACKAGE, LOCALEDIR);
69 textdomain (PACKAGE);
71 if (process_arguments (argc, argv) == ERROR)
72 usage (_("check_ldap: could not parse arguments\n"));
74 /* initialize alarm signal handling */
75 signal (SIGALRM, socket_timeout_alarm_handler);
77 /* set socket timeout */
78 alarm (socket_timeout);
80 /* get the start time */
81 time (&time0);
83 /* initialize ldap */
84 if (!(ld = ldap_open (ld_host, ld_port))) {
85 /*ldap_perror(ld, "ldap_open"); */
86 printf (_("Could not connect to the server at port %i\n"), ld_port);
87 return STATE_CRITICAL;
88 }
90 #ifdef HAVE_LDAP_SET_OPTION
91 /* set ldap options */
92 if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
93 LDAP_OPT_SUCCESS ) {
94 printf(_("Could not set protocol version %d\n"), ld_protocol);
95 return STATE_CRITICAL;
96 }
97 #endif
98 /* bind to the ldap server */
99 if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
100 LDAP_SUCCESS) {
101 /*ldap_perror(ld, "ldap_bind"); */
102 printf (_("Could not bind to the ldap-server\n"));
103 return STATE_CRITICAL;
104 }
106 /* do a search of all objectclasses in the base dn */
107 if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
108 != LDAP_SUCCESS) {
109 /*ldap_perror(ld, "ldap_search"); */
110 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
111 return STATE_CRITICAL;
112 }
114 /* unbind from the ldap server */
115 ldap_unbind (ld);
117 /* reset the alarm handler */
118 alarm (0);
120 /* get the finish time */
121 time (&time1);
123 /* calcutate the elapsed time and compare to thresholds */
124 t_diff = time1 - time0;
126 if (crit_time!=UNDEFINED && t_diff>=crit_time) {
127 printf (_("LDAP CRITICAL - %i seconds response time\n"), t_diff);
128 return STATE_CRITICAL;
129 }
131 if (warn_time!=UNDEFINED && t_diff>=warn_time) {
132 printf (_("LDAP WARNING - %i seconds response time\n"), t_diff);
133 return STATE_WARNING;
134 }
136 /* print out the result */
137 printf (_("LDAP OK - %i seconds response time\n"), t_diff);
139 return STATE_OK;
140 }
142 /* process command-line arguments */
143 int
144 process_arguments (int argc, char **argv)
145 {
146 int c;
148 int option = 0;
149 /* initialize the long option struct */
150 static struct option longopts[] = {
151 {"help", no_argument, 0, 'h'},
152 {"version", no_argument, 0, 'V'},
153 {"timeout", required_argument, 0, 't'},
154 {"host", required_argument, 0, 'H'},
155 {"base", required_argument, 0, 'b'},
156 {"attr", required_argument, 0, 'a'},
157 {"bind", required_argument, 0, 'D'},
158 {"pass", required_argument, 0, 'P'},
159 #ifdef HAVE_LDAP_SET_OPTION
160 {"ver2", no_argument, 0, '2'},
161 {"ver3", no_argument, 0, '3'},
162 #endif
163 {"use-ipv4", no_argument, 0, '4'},
164 {"use-ipv6", no_argument, 0, '6'},
165 {"port", required_argument, 0, 'p'},
166 {"warn", required_argument, 0, 'w'},
167 {"crit", required_argument, 0, 'c'},
168 {0, 0, 0, 0}
169 };
171 if (argc < 2)
172 return ERROR;
174 for (c = 1; c < argc; c++) {
175 if (strcmp ("-to", argv[c]) == 0)
176 strcpy (argv[c], "-t");
177 }
179 while (1) {
180 c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option);
182 if (c == -1 || c == EOF)
183 break;
185 switch (c) {
186 case 'h': /* help */
187 print_help ();
188 exit (STATE_OK);
189 case 'V': /* version */
190 print_revision (progname, revision);
191 exit (STATE_OK);
192 case 't': /* timeout period */
193 if (!is_intnonneg (optarg))
194 usage2 (_("timeout interval must be a positive integer"), optarg);
195 else
196 socket_timeout = atoi (optarg);
197 break;
198 case 'H':
199 ld_host = optarg;
200 break;
201 case 'b':
202 ld_base = optarg;
203 break;
204 case 'p':
205 ld_port = atoi (optarg);
206 break;
207 case 'a':
208 ld_attr = optarg;
209 break;
210 case 'D':
211 ld_binddn = optarg;
212 break;
213 case 'P':
214 ld_passwd = optarg;
215 break;
216 case 'w':
217 warn_time = atoi (optarg);
218 break;
219 case 'c':
220 crit_time = atoi (optarg);
221 break;
222 #ifdef HAVE_LDAP_SET_OPTION
223 case '2':
224 ld_protocol = 2;
225 break;
226 case '3':
227 ld_protocol = 3;
228 break;
229 #endif
230 case '4':
231 address_family = AF_INET;
232 break;
233 case '6':
234 #ifdef USE_IPV6
235 address_family = AF_INET6;
236 #else
237 usage (_("IPv6 support not available\n"));
238 #endif
239 break;
240 default:
241 usage (_("check_ldap: could not parse unknown arguments\n"));
242 break;
243 }
244 }
246 c = optind;
247 if (ld_host == NULL && is_host(argv[c]))
248 ld_host = strdup (argv[c++]);
250 if (ld_base == NULL && argv[c])
251 ld_base = strdup (argv[c++]);
253 return validate_arguments ();
254 }
256 int
257 validate_arguments ()
258 {
259 if (strlen(ld_host) == 0)
260 usage (_("please specify the host name\n"));
262 if (strlen(ld_base) == 0)
263 usage (_("please specify the LDAP base\n"));
265 return OK;
267 }
273 \f
274 void
275 print_help (void)
276 {
277 char *myport;
278 asprintf (&myport, "%d", DEFAULT_PORT);
280 print_revision (progname, revision);
282 printf (_("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"));
283 printf (_(COPYRIGHT), copyright, email);
285 print_usage ();
287 printf (_(UT_HELP_VRSN));
289 printf (_(UT_HOST_PORT), 'p', myport);
291 printf (_(UT_IPv46));
293 printf (_("\
294 -a [--attr]\n\
295 ldap attribute to search (default: \"(objectclass=*)\"\n\
296 -b [--base]\n\
297 ldap base (eg. ou=my unit, o=my org, c=at)\n\
298 -D [--bind]\n\
299 ldap bind DN (if required)\n\
300 -P [--pass]\n\
301 ldap password (if required)\n"));
303 #ifdef HAVE_LDAP_SET_OPTION
304 printf (_("\
305 -2 [--ver2]\n\
306 use ldap protocol version 2\n\
307 -3 [--ver3]\n\
308 use ldap protocol version 3\n\
309 (default protocol version: %d)\n"),
310 DEFAULT_PROTOCOL);
311 #endif
313 printf (_(UT_WARN_CRIT));
315 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
317 printf (_(UT_VERBOSE));
319 printf (_(UT_SUPPORT));
320 }
325 void
326 print_usage (void)
327 {
328 printf (_("\
329 Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n\
330 [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n\
331 (Note: all times are in seconds.)\n"),
332 progname, (HAVE_LDAP_SET_OPTION ? "[-2|-3] [-4|-6]" : ""));
333 printf (_(UT_HLP_VRS), progname, progname);
334 }