1 /******************************************************************************
2 *
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.
7 *
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.
12 *
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.
16 *
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 "config.h"
25 #include "common.h"
26 #include "netutils.h"
27 #include "utils.h"
29 #include <lber.h>
30 #include <ldap.h>
32 enum {
33 UNDEFINED = -1,
34 #ifdef HAVE_LDAP_SET_OPTION
35 DEFAULT_PROTOCOL = 2,
36 #endif
37 DEFAULT_PORT = 389
38 };
40 void
41 print_usage ()
42 {
43 printf (_("\
44 Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n\
45 [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n\
46 (Note: all times are in seconds.)\n"),
47 progname, (HAVE_LDAP_SET_OPTION ? "[-2|-3] [-4|-6]" : ""));
48 printf (_(UT_HLP_VRS), progname, progname);
49 }
51 void
52 print_help ()
53 {
54 char *myport;
55 asprintf (&myport, "%d", DEFAULT_PORT);
57 print_revision (progname, revision);
59 printf (_("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"));
60 printf (_(COPYRIGHT), copyright, email);
62 print_usage ();
64 printf (_(UT_HELP_VRSN));
66 printf (_(UT_HOST_PORT), 'p', myport);
68 printf (_(UT_IPv46));
70 printf (_("\
71 -a [--attr]\n\
72 ldap attribute to search (default: \"(objectclass=*)\"\n\
73 -b [--base]\n\
74 ldap base (eg. ou=my unit, o=my org, c=at)\n\
75 -D [--bind]\n\
76 ldap bind DN (if required)\n\
77 -P [--pass]\n\
78 ldap password (if required)\n"));
80 #ifdef HAVE_LDAP_SET_OPTION
81 printf (_("\
82 -2 [--ver2]\n\
83 use ldap protocol version 2\n\
84 -3 [--ver3]\n\
85 use ldap protocol version 3\n\
86 (default protocol version: %d)\n"),
87 DEFAULT_PROTOCOL);
88 #endif
90 printf (_(UT_WARN_CRIT));
91 }
92 \f
93 int process_arguments (int, char **);
94 int validate_arguments (void);
96 char ld_defattr[] = "(objectclass=*)";
97 char *ld_attr = ld_defattr;
98 char *ld_host = "";
99 char *ld_base = "";
100 char *ld_passwd = NULL;
101 char *ld_binddn = NULL;
102 unsigned int ld_port = DEFAULT_PORT;
103 #ifdef HAVE_LDAP_SET_OPTION
104 int ld_protocol = DEFAULT_PROTOCOL;
105 #endif
106 int warn_time = UNDEFINED;
107 int crit_time = UNDEFINED;
109 int
110 main (int argc, char *argv[])
111 {
113 LDAP *ld;
114 LDAPMessage *result;
116 int t_diff;
117 time_t time0, time1;
119 if (process_arguments (argc, argv) == ERROR)
120 usage (_("check_ldap: could not parse arguments\n"));
122 /* initialize alarm signal handling */
123 signal (SIGALRM, socket_timeout_alarm_handler);
125 /* set socket timeout */
126 alarm (socket_timeout);
128 /* get the start time */
129 time (&time0);
131 /* initialize ldap */
132 if (!(ld = ldap_open (ld_host, ld_port))) {
133 /*ldap_perror(ld, "ldap_open"); */
134 printf (_("Could not connect to the server at port %i\n"), ld_port);
135 return STATE_CRITICAL;
136 }
138 #ifdef HAVE_LDAP_SET_OPTION
139 /* set ldap options */
140 if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
141 LDAP_OPT_SUCCESS ) {
142 printf(_("Could not set protocol version %d\n"), ld_protocol);
143 return STATE_CRITICAL;
144 }
145 #endif
146 /* bind to the ldap server */
147 if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
148 LDAP_SUCCESS) {
149 /*ldap_perror(ld, "ldap_bind"); */
150 printf (_("Could not bind to the ldap-server\n"));
151 return STATE_CRITICAL;
152 }
154 /* do a search of all objectclasses in the base dn */
155 if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
156 != LDAP_SUCCESS) {
157 /*ldap_perror(ld, "ldap_search"); */
158 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
159 return STATE_CRITICAL;
160 }
162 /* unbind from the ldap server */
163 ldap_unbind (ld);
165 /* reset the alarm handler */
166 alarm (0);
168 /* get the finish time */
169 time (&time1);
171 /* calcutate the elapsed time and compare to thresholds */
172 t_diff = time1 - time0;
174 if (crit_time!=UNDEFINED && t_diff>=crit_time) {
175 printf (_("LDAP CRITICAL - %i seconds response time\n"), t_diff);
176 return STATE_CRITICAL;
177 }
179 if (warn_time!=UNDEFINED && t_diff>=warn_time) {
180 printf (_("LDAP WARNING - %i seconds response time\n"), t_diff);
181 return STATE_WARNING;
182 }
184 /* print out the result */
185 printf (_("LDAP OK - %i seconds response time\n"), t_diff);
187 return STATE_OK;
188 }
190 /* process command-line arguments */
191 int
192 process_arguments (int argc, char **argv)
193 {
194 int c;
196 int option_index = 0;
197 /* initialize the long option struct */
198 static struct option longopts[] = {
199 {"help", no_argument, 0, 'h'},
200 {"version", no_argument, 0, 'V'},
201 {"timeout", required_argument, 0, 't'},
202 {"host", required_argument, 0, 'H'},
203 {"base", required_argument, 0, 'b'},
204 {"attr", required_argument, 0, 'a'},
205 {"bind", required_argument, 0, 'D'},
206 {"pass", required_argument, 0, 'P'},
207 #ifdef HAVE_LDAP_SET_OPTION
208 {"ver2", no_argument, 0, '2'},
209 {"ver3", no_argument, 0, '3'},
210 #endif
211 {"use-ipv4", no_argument, 0, '4'},
212 {"use-ipv6", no_argument, 0, '6'},
213 {"port", required_argument, 0, 'p'},
214 {"warn", required_argument, 0, 'w'},
215 {"crit", required_argument, 0, 'c'},
216 {0, 0, 0, 0}
217 };
219 if (argc < 2)
220 return ERROR;
222 for (c = 1; c < argc; c++) {
223 if (strcmp ("-to", argv[c]) == 0)
224 strcpy (argv[c], "-t");
225 }
227 while (1) {
228 c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option_index);
230 if (c == -1 || c == EOF)
231 break;
233 switch (c) {
234 case 'h': /* help */
235 print_help ();
236 exit (STATE_OK);
237 case 'V': /* version */
238 print_revision (progname, revision);
239 exit (STATE_OK);
240 case 't': /* timeout period */
241 if (!is_intnonneg (optarg))
242 usage2 (_("timeout interval must be a positive integer"), optarg);
243 socket_timeout = atoi (optarg);
244 break;
245 case 'H':
246 ld_host = optarg;
247 break;
248 case 'b':
249 ld_base = optarg;
250 break;
251 case 'p':
252 ld_port = atoi (optarg);
253 break;
254 case 'a':
255 ld_attr = optarg;
256 break;
257 case 'D':
258 ld_binddn = optarg;
259 break;
260 case 'P':
261 ld_passwd = optarg;
262 break;
263 case 'w':
264 warn_time = atoi (optarg);
265 break;
266 case 'c':
267 crit_time = atoi (optarg);
268 break;
269 #ifdef HAVE_LDAP_SET_OPTION
270 case '2':
271 ld_protocol = 2;
272 break;
273 case '3':
274 ld_protocol = 3;
275 break;
276 #endif
277 case '4':
278 address_family = AF_INET;
279 break;
280 case '6':
281 #ifdef USE_IPV6
282 address_family = AF_INET6;
283 #else
284 usage (_("IPv6 support not available\n"));
285 #endif
286 break;
287 default:
288 usage (_("check_ldap: could not parse unknown arguments\n"));
289 break;
290 }
291 }
293 c = optind;
294 if (strlen(ld_host) == 0 && is_host(argv[c])) {
295 asprintf (&ld_host, "%s", argv[c++]);
296 }
297 if (strlen(ld_base) == 0 && argv[c]) {
298 asprintf (&ld_base, "%s", argv[c++]);
299 }
301 return validate_arguments ();
302 }
304 int
305 validate_arguments ()
306 {
307 if (strlen(ld_host) == 0)
308 usage (_("please specify the host name\n"));
310 if (strlen(ld_base) == 0)
311 usage (_("please specify the LDAP base\n"));
313 return OK;
315 }