This file is indexed.

/usr/lib/ocf/resource.d/heartbeat/slapd is in resource-agents 1:4.1.0~rc1-1ubuntu1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/bin/bash
#
# Stand-alone LDAP Daemon (slapd)
#
# Description:  Manages Stand-alone LDAP Daemon (slapd) as an OCF resource in
#               an high-availability setup.
#
# Authors:      Jeroen Koekkoek
#               nozawat@gmail.com
#               John Keith Hohm
#
# License:      GNU General Public License (GPL)
# Copyright:    (C) 2011 Pagelink B.V.
#
#       The OCF code was inspired by the Postfix resource script written by
#       Raoul Bhatia <r.bhatia@ipax.at>.
#
#       The code for managing the slapd instance is based on the the slapd init
#       script found in Debian GNU/Linux 6.0.
#
# OCF parameters:
#   OCF_RESKEY_slapd
#   OCF_RESKEY_ldapsearch
#   OCF_RESKEY_config
#   OCF_RESKEY_pidfile
#   OCF_RESKEY_user
#   OCF_RESKEY_group
#   OCF_RESKEY_services
#   OCF_RESKEY_watch_suffix
#   OCF_RESKEY_ignore_suffix
#   OCF_RESKEY_bind_dn
#   OCF_RESKEY_password
#   OCF_RESKEY_parameters
#   OCF_RESKEY_stop_escalate
#   OCF_RESKEY_maxfiles
#
################################################################################

# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

: ${OCF_RESKEY_slapd="/usr/sbin/slapd"}
: ${OCF_RESKEY_ldapsearch="ldapsearch"}
: ${OCF_RESKEY_config=""}
: ${OCF_RESKEY_pidfile=""}
: ${OCF_RESKEY_user=""}
: ${OCF_RESKEY_group=""}
: ${OCF_RESKEY_services="ldap:///"}
: ${OCF_RESKEY_watch_suffix=""}
: ${OCF_RESKEY_ignore_suffix=""}
: ${OCF_RESKEY_bind_dn=""}
: ${OCF_RESKEY_password=""}
: ${OCF_RESKEY_parameters=""}
: ${OCF_RESKEY_stop_escalate=15}
: ${OCF_RESKEY_maxfiles=""}

USAGE="Usage: $0 {start|stop|status|monitor|validate-all|meta-data}"
ORIG_IFS=$IFS
NEWLINE='
'

################################################################################

usage() {
    echo $USAGE >&2
}

meta_data()
{
  cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="slapd">
<version>0.1</version>

<longdesc lang="en">
Resource script for Stand-alone LDAP Daemon (slapd). It manages a slapd instance as an OCF resource.
</longdesc>
<shortdesc lang="en">Manages a Stand-alone LDAP Daemon (slapd) instance</shortdesc>

<parameters>

<parameter name="slapd" unique="0" required="0">
<longdesc lang="en">
Full path to the slapd binary.
For example, "/usr/sbin/slapd".
</longdesc>
<shortdesc lang="en">Full path to slapd binary</shortdesc>
<content type="string" default="/usr/sbin/slapd" />
</parameter>

<parameter name="ldapsearch" unique="0" required="0">
<longdesc lang="en">
Full path to the ldapsearch binary.
For example, "/usr/bin/ldapsearch".
</longdesc>
<shortdesc lang="en">Full path to ldapsearch binary</shortdesc>
<content type="string" default="ldapsearch" />
</parameter>

<parameter name="config" required="0" unique="1">
<longdesc lang="en">
Full path to a slapd configuration directory or a slapd configuration file.
For example, "/etc/ldap/slapd.d" or "/etc/ldap/slapd.conf".
</longdesc>
<shortdesc lang="en">Full path to configuration directory or file</shortdesc>
<content type="string" default=""/>
</parameter>

<parameter name="pidfile" required="0" unique="0">
<longdesc lang="en">
File to read the PID from; read from olcPidFile/pidfile in config if not set.
</longdesc>
<shortdesc lang="en">File to read PID from</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="user" unique="0" required="0">
<longdesc lang="en">
User name or id slapd will run with. The group id is also changed to this
user's gid, unless the group parameter is used to override.
</longdesc>
<shortdesc lang="en">User name or id slapd will run with</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="group" unique="0" required="0">
<longdesc lang="en">
Group name or id slapd will run with.
</longdesc>
<shortdesc lang="en">Group name or id slapd will run with</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="services" required="0" unique="1">
<longdesc lang="en">
LDAP (and other scheme) URLs slapd will serve.
For example, "ldap://127.0.0.1:389 ldaps:/// ldapi:///"
</longdesc>
<shortdesc lang="en">LDAP (and other scheme) URLs to serve</shortdesc>
<content type="string" default="ldap:///"/>
</parameter>

<parameter name="watch_suffix" required="0" unique="0">
<longdesc lang="en">
Suffix (database backend) that will be monitored for availability. Multiple
suffixes can be specified by providing a space seperated list. By providing one
or more suffixes here, the ignore_suffix parameter is discarded. All suffixes
will be monitored if left blank.
</longdesc>
<shortdesc lang="en">Suffix that will be monitored for availability.</shortdesc>
<content type="string" default=""/>
</parameter>

<parameter name="ignore_suffix" required="0" unique="0">
<longdesc lang="en">
Suffix (database backend) that will not be monitored for availability. Multiple
suffixes can be specified by providing a space seperated list. No suffix will
be excluded if left blank.
</longdesc>
<shortdesc lang="en">Suffix that will not be monitored for availability.</shortdesc>
<content type="string" default=""/>
</parameter>

<parameter name="bind_dn" required="0" unique="0">
<longdesc lang="en">
Distinguished Name used to bind to the LDAP directory for testing. Leave blank
to bind to the LDAP directory anonymously.
</longdesc>
<shortdesc lang="en">Distinguished Name used to bind to the LDAP directory for testing.</shortdesc>
<content type="string" default=""/>
</parameter>

<parameter name="password" required="0" unique="0">
<longdesc lang="en">
Password used to bind to the LDAP directory for testing.
</longdesc>
<shortdesc lang="en">Password used to bind to the LDAP directory for testing.</shortdesc>
<content type="string" default=""/>
</parameter>

<parameter name="parameters" unique="0" required="0">
<longdesc lang="en">
slapd may be called with additional parameters.
Specify any of them here.
</longdesc>
<shortdesc lang="en">Any additional parameters to slapd.</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="stop_escalate" unique="0" required="0">
<longdesc lang="en">
Number of seconds to wait for shutdown (using SIGTERM) before resorting to
SIGKILL
</longdesc>
<shortdesc lang="en">Seconds before stop escalation to KILL</shortdesc>
<content type="integer" default="15" />
</parameter>

<parameter name="maxfiles">
<longdesc lang="en">
Maximum number of open files (for ulimit -n)
</longdesc>
<shortdesc lang="en">Max open files</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>

<actions>
<action name="start"   timeout="20s" />
<action name="stop"    timeout="20s" />
<action name="monitor" depth="0"  timeout="20s" interval="60s" />
<action name="validate-all"  timeout="20s" />
<action name="meta-data"  timeout="5s" />
</actions>
</resource-agent>
END
}

watch_suffix()
{
  local rc

  if [ -n "$OCF_RESKEY_watch_suffix" ]; then
    if echo "'$OCF_RESKEY_watch_suffix'" | grep "'$1'" >/dev/null 2>&1; then
      rc=0
    else
      rc=1
    fi
  else
    if echo "'$OCF_RESKEY_ignore_suffix'" | grep "'$1'" >/dev/null 2>&1; then
      rc=1
    else
      rc=0
    fi
  fi

  return $rc
}

slapd_pid()
{
  local pid

  if [ -f "$pid_file" ]; then
    pid=`head -n 1 "$pid_file" 2>/dev/null`

    if [ "X$pid" != "X" ]; then
      echo "$pid"
      return $OCF_SUCCESS
    fi

    ocf_exit_reason "slapd pid file '$pid_file' empty."
    return $OCF_ERR_GENERIC
  fi

  ocf_log info "slapd pid file '$pid_file' does not exist."
  return $OCF_NOT_RUNNING
}

slapd_status()
{
  local pid=$1

  if ! kill -0 $pid >/dev/null 2>&1; then
    return $OCF_NOT_RUNNING
  else
    return $OCF_SUCCESS
  fi
}

slapd_start()
{
  local options
  local reason
  local rc
  local state

  slapd_status `slapd_pid`; state=$?

  if [ $state -eq $OCF_SUCCESS ]; then
    ocf_log info "slapd already running."
    return $state
  elif [ $state -eq $OCF_ERR_GENERIC ]; then
    return $state
  fi

  options="-u $user -g $group"

  if [ -d "$config" ]; then
    options="$options -F $config"
  elif [ -f "$config" ]; then
    options="$options -f $config"
  else
    ocf_exit_reason "slapd configuration '$config' does not exist."
    return $OCF_ERR_INSTALLED
  fi

  if [ -n "$parameters" ]; then
    options="$options $parameters"
  fi

  if [ -n "$OCF_RESKEY_maxfiles" ]; then
    ulimit -n $OCF_RESKEY_maxfiles
    u_rc=$?
      if [ "$u_rc" -ne 0 ]; then
        ocf_log warn "Could not set ulimit for open files for slapd to '$OCF_RESKEY_maxfiles'"
      fi
  fi

  if [ -n "$services" ]; then
    $slapd -h "$services" $options 2>&1; rc=$?
  else
    $slapd $options 2>&1; rc=$?
  fi

  if [ $rc -ne 0 ]; then
    ocf_exit_reason "slapd returned error."

    return $OCF_ERR_GENERIC
  fi

  while true; do
    slapd_monitor start
    if [ $? = "$OCF_SUCCESS" ]; then
      break
    fi
    sleep 1
  done

  ocf_log info "slapd started."

  return $OCF_SUCCESS
}

slapd_stop()
{
  local pid
  local rc
  local state

  pid=`slapd_pid`; slapd_status $pid; state=$?

  if [ $state -eq $OCF_NOT_RUNNING ]; then
    ocf_log info "slapd already stopped."
    return $OCF_SUCCESS
  elif [ $state -eq $OCF_ERR_GENERIC ]; then
    return $state
  fi

  ocf_stop_processes TERM $OCF_RESKEY_stop_escalate $pid; rc=$?
  if [ $rc -eq 1 ]; then
    ocf_log err "cannot stop slapd."
    return $OCF_ERR_GENERIC
  fi

  if [ -f "$pid_file" ]; then
    rm -f "$pid_file" >/dev/null 2>&1
  fi

  ocf_log info "slapd stopped."
  return $OCF_SUCCESS
}

slapd_monitor()
{
  local options
  local rc
  local state
  local suffix
  local suffixes
  local err_option="-info"

  slapd_status `slapd_pid`; state=$?
  if [ $state -eq $OCF_NOT_RUNNING ]; then
    if [ -z "$1" ];then
      if ! ocf_is_probe; then
        ocf_exit_reason "slapd process not found."
      fi
    fi
    return $state
  elif [ $state -ne $OCF_SUCCESS ]; then
    ocf_exit_reason "slapd returned error."
    return $state
  fi

  if [ -d "$config" ]; then
    for suffix in `find "$config"/'cn=config' -type f -name olcDatabase* -exec \
                   sed -ne 's/^[[:space:]]*olcSuffix:[[:space:]]\+\(.\+\)/\1/p' {} \;`
    do
      suffix=${suffix#\"*}
      suffix=${suffix%\"*}

      if watch_suffix $suffix; then
        suffixes="$suffixes $suffix"
      fi
    done

  elif [ -f "$config" ]; then
    for suffix in `sed -ne 's/^[[:space:]]*suffix[[:space:]]\+\(.\+\)/\1/p' "$config"`
    do
      suffix=${suffix#\"*}
      suffix=${suffix%\"*}

      if watch_suffix $suffix; then
        suffixes="$suffixes $suffix"
      fi
    done

  else
    if ocf_is_probe; then
      ocf_log info "slapd configuration '$config' does not exist during probe."
    else
      ocf_exit_reason "slapd configuration '$config' does not exist."
      return $OCF_ERR_INSTALLED
    fi
  fi

  options="-LLL -s base -x"

  if [ -n "$bind_dn" ]; then
    options="$options -D $bind_dn -w $password"
  fi

  [ -z "$1" ] && err_option=""
  for suffix in $suffixes; do
    ocf_run -q $err_option "$ldapsearch" -H "$services" -b "$suffix" $options >/dev/null 2>&1; rc=$?

    case "$rc" in
      "0")
        ocf_log debug "slapd database with suffix '$suffix' reachable"
        ;;
      "49")
        ocf_exit_reason "slapd database with suffix '$suffix' unreachable. Invalid credentials."
        return $OCF_ERR_CONFIGURED
        ;;
      *)
        if [ -z "$1" ] || [ -n "$1" -a $rc -ne 1 ]; then
          ocf_exit_reason "slapd database with suffix '$suffix' unreachable. exit code ($rc)"
        fi
        state=$OCF_ERR_GENERIC
        ;;
    esac
  done

  return $state
}

slapd_validate_all()
{
  check_binary "$slapd"
  check_binary "$ldapsearch"

  if [ -z "$pid_file" ]; then
    if [ -d "$config" ]; then
      pid_file=`sed -ne \
	       's/^olcPidFile:[[:space:]]\+\(.\+\)[[:space:]]*/\1/p' \
	       "$config"/'cn=config.ldif' 2>/dev/null`
    elif [ -f "$config" ]; then
      pid_file=`sed -ne \
		's/^pidfile[[:space:]]\+\(.\+\)/\1/p' \
		"$config" 2>/dev/null`
    else
      if ocf_is_probe; then
        ocf_log info "slapd configuration '$config' does not exist during probe."
      else
        ocf_exit_reason "slapd configuration '$config' does not exist."
        return $OCF_ERR_INSTALLED
      fi
    fi
  fi

  if [ -z "$user" ]; then
    user=`id -nu 2>/dev/null`
  elif ! id "$user" >/dev/null 2>&1; then
    ocf_exit_reason "slapd user '$user' does not exist"
    return $OCF_ERR_INSTALLED
  fi

  if [ -z "$group" ]; then
    group=`id -ng 2>/dev/null`
  elif ! grep "^$group:" /etc/group >/dev/null 2>&1; then
    ocf_exit_reason "slapd group '$group' does not exist"
    return $OCF_ERR_INSTALLED
  fi

  pid_dir=`dirname "$pid_file"`
  if [ ! -d "$pid_dir" ]; then
    mkdir -p "$pid_dir"
    chown -R "$user" "$pid_dir"
    chgrp -R "$group" "$pid_dir"
  fi

  return $OCF_SUCCESS
}

#
# Main
#

slapd=$OCF_RESKEY_slapd
ldapsearch=$OCF_RESKEY_ldapsearch
config=$OCF_RESKEY_config
user=$OCF_RESKEY_user
group=$OCF_RESKEY_group
services=$OCF_RESKEY_services
bind_dn=$OCF_RESKEY_bind_dn
password=$OCF_RESKEY_password
parameters=$OCF_RESKEY_parameters
pid_file=$OCF_RESKEY_pidfile

if [ -z "$config" ]; then
  config_dirname="/etc/ldap"
  if [ -e "/etc/openldap" ]; then
    config_dirname="/etc/openldap"
  fi

  config="$config_dirname/slapd.conf"
  if [ -e "$config_dirname/slapd.d" ]; then
    config="$config_dirname/slapd.d"
  fi
fi

if [ $# -ne 1 ]; then
  usage
  exit $OCF_ERR_ARGS
fi

case $1 in
  meta-data)
    meta_data
    exit $OCF_SUCCESS
    ;;
  usage|help)
    usage
    exit $OCF_SUCCESS
    ;;
esac

slapd_validate_all
rc=$?
[ $rc -eq $OCF_SUCCESS ] || exit $rc

case $1 in
  status)
    slapd_status `slapd_pid`; state=$?

    if [ $state -eq $OCF_SUCCESS ]; then
      ocf_log debug "slapd is running."
    elif [ $state -eq $OCF_NOT_RUNNING ]; then
      ocf_log debug "slapd is stopped."
    fi

    exit $state
    ;;
  start)
    slapd_start
    exit $?
    ;;
  stop)
    slapd_stop
    exit $?
    ;;
  monitor)
    slapd_monitor; state=$?
    exit $state
    ;;
  validate-all)
    exit $OCF_SUCCESS
    ;;
  *)
    usage
    exit $OCF_ERR_UNIMPLEMENTED
    ;;
esac