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 if (process_arguments (argc, argv) == ERROR)
68 usage (_("check_ldap: could not parse arguments\n"));
70 /* initialize alarm signal handling */
71 signal (SIGALRM, socket_timeout_alarm_handler);
73 /* set socket timeout */
74 alarm (socket_timeout);
76 /* get the start time */
77 time (&time0);
79 /* initialize ldap */
80 if (!(ld = ldap_open (ld_host, ld_port))) {
81 /*ldap_perror(ld, "ldap_open"); */
82 printf (_("Could not connect to the server at port %i\n"), ld_port);
83 return STATE_CRITICAL;
84 }
86 #ifdef HAVE_LDAP_SET_OPTION
87 /* set ldap options */
88 if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
89 LDAP_OPT_SUCCESS ) {
90 printf(_("Could not set protocol version %d\n"), ld_protocol);
91 return STATE_CRITICAL;
92 }
93 #endif
94 /* bind to the ldap server */
95 if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
96 LDAP_SUCCESS) {
97 /*ldap_perror(ld, "ldap_bind"); */
98 printf (_("Could not bind to the ldap-server\n"));
99 return STATE_CRITICAL;
100 }
102 /* do a search of all objectclasses in the base dn */
103 if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
104 != LDAP_SUCCESS) {
105 /*ldap_perror(ld, "ldap_search"); */
106 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
107 return STATE_CRITICAL;
108 }
110 /* unbind from the ldap server */
111 ldap_unbind (ld);
113 /* reset the alarm handler */
114 alarm (0);
116 /* get the finish time */
117 time (&time1);
119 /* calcutate the elapsed time and compare to thresholds */
120 t_diff = time1 - time0;
122 if (crit_time!=UNDEFINED && t_diff>=crit_time) {
123 printf (_("LDAP CRITICAL - %i seconds response time\n"), t_diff);
124 return STATE_CRITICAL;
125 }
127 if (warn_time!=UNDEFINED && t_diff>=warn_time) {
128 printf (_("LDAP WARNING - %i seconds response time\n"), t_diff);
129 return STATE_WARNING;
130 }
132 /* print out the result */
133 printf (_("LDAP OK - %i seconds response time\n"), t_diff);
135 return STATE_OK;
136 }
138 /* process command-line arguments */
139 int
140 process_arguments (int argc, char **argv)
141 {
142 int c;
144 int option = 0;
145 /* initialize the long option struct */
146 static struct option longopts[] = {
147 {"help", no_argument, 0, 'h'},
148 {"version", no_argument, 0, 'V'},
149 {"timeout", required_argument, 0, 't'},
150 {"host", required_argument, 0, 'H'},
151 {"base", required_argument, 0, 'b'},
152 {"attr", required_argument, 0, 'a'},
153 {"bind", required_argument, 0, 'D'},
154 {"pass", required_argument, 0, 'P'},
155 #ifdef HAVE_LDAP_SET_OPTION
156 {"ver2", no_argument, 0, '2'},
157 {"ver3", no_argument, 0, '3'},
158 #endif
159 {"use-ipv4", no_argument, 0, '4'},
160 {"use-ipv6", no_argument, 0, '6'},
161 {"port", required_argument, 0, 'p'},
162 {"warn", required_argument, 0, 'w'},
163 {"crit", required_argument, 0, 'c'},
164 {0, 0, 0, 0}
165 };
167 if (argc < 2)
168 return ERROR;
170 for (c = 1; c < argc; c++) {
171 if (strcmp ("-to", argv[c]) == 0)
172 strcpy (argv[c], "-t");
173 }
175 while (1) {
176 c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option);
178 if (c == -1 || c == EOF)
179 break;
181 switch (c) {
182 case 'h': /* help */
183 print_help ();
184 exit (STATE_OK);
185 case 'V': /* version */
186 print_revision (progname, revision);
187 exit (STATE_OK);
188 case 't': /* timeout period */
189 if (!is_intnonneg (optarg))
190 usage2 (_("timeout interval must be a positive integer"), optarg);
191 else
192 socket_timeout = atoi (optarg);
193 break;
194 case 'H':
195 ld_host = optarg;
196 break;
197 case 'b':
198 ld_base = optarg;
199 break;
200 case 'p':
201 ld_port = atoi (optarg);
202 break;
203 case 'a':
204 ld_attr = optarg;
205 break;
206 case 'D':
207 ld_binddn = optarg;
208 break;
209 case 'P':
210 ld_passwd = optarg;
211 break;
212 case 'w':
213 warn_time = atoi (optarg);
214 break;
215 case 'c':
216 crit_time = atoi (optarg);
217 break;
218 #ifdef HAVE_LDAP_SET_OPTION
219 case '2':
220 ld_protocol = 2;
221 break;
222 case '3':
223 ld_protocol = 3;
224 break;
225 #endif
226 case '4':
227 address_family = AF_INET;
228 break;
229 case '6':
230 #ifdef USE_IPV6
231 address_family = AF_INET6;
232 #else
233 usage (_("IPv6 support not available\n"));
234 #endif
235 break;
236 default:
237 usage (_("check_ldap: could not parse unknown arguments\n"));
238 break;
239 }
240 }
242 c = optind;
243 if (ld_host == NULL && is_host(argv[c]))
244 ld_host = strdup (argv[c++]);
246 if (ld_base == NULL && argv[c])
247 ld_base = strdup (argv[c++]);
249 return validate_arguments ();
250 }
252 int
253 validate_arguments ()
254 {
255 if (strlen(ld_host) == 0)
256 usage (_("please specify the host name\n"));
258 if (strlen(ld_base) == 0)
259 usage (_("please specify the LDAP base\n"));
261 return OK;
263 }
269 \f
270 void
271 print_help (void)
272 {
273 char *myport;
274 asprintf (&myport, "%d", DEFAULT_PORT);
276 print_revision (progname, revision);
278 printf (_("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"));
279 printf (_(COPYRIGHT), copyright, email);
281 print_usage ();
283 printf (_(UT_HELP_VRSN));
285 printf (_(UT_HOST_PORT), 'p', myport);
287 printf (_(UT_IPv46));
289 printf (_("\
290 -a [--attr]\n\
291 ldap attribute to search (default: \"(objectclass=*)\"\n\
292 -b [--base]\n\
293 ldap base (eg. ou=my unit, o=my org, c=at)\n\
294 -D [--bind]\n\
295 ldap bind DN (if required)\n\
296 -P [--pass]\n\
297 ldap password (if required)\n"));
299 #ifdef HAVE_LDAP_SET_OPTION
300 printf (_("\
301 -2 [--ver2]\n\
302 use ldap protocol version 2\n\
303 -3 [--ver3]\n\
304 use ldap protocol version 3\n\
305 (default protocol version: %d)\n"),
306 DEFAULT_PROTOCOL);
307 #endif
309 printf (_(UT_WARN_CRIT));
311 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
313 printf (_(UT_VERBOSE));
315 printf (_(UT_SUPPORT));
316 }
321 void
322 print_usage (void)
323 {
324 printf (_("\
325 Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n\
326 [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n\
327 (Note: all times are in seconds.)\n"),
328 progname, (HAVE_LDAP_SET_OPTION ? "[-2|-3] [-4|-6]" : ""));
329 printf (_(UT_HLP_VRS), progname, progname);
330 }