-
Notifications
You must be signed in to change notification settings - Fork 80
/
daemon.c
2867 lines (2577 loc) · 101 KB
/
daemon.c
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <pthread.h>
#include <stdint.h>
#if defined(HAVE_MNTENT_H)
#include <mntent.h>
#endif
#include <time.h>
#include <signal.h>
#include <fcntl.h>
#define TCPLEN_T int
#ifdef NEED_FILIO
#include <sys/filio.h>
#else /* NEED_FILIO */
#include <sys/ioctl.h>
#endif /* NEED_FILIO */
/* for some reason, linux tty stuff defines CTIME */
#include <stdio.h>
#if defined(LINUX) || defined(__FreeBSD__)
#ifdef LINUX
#undef CTIME
#endif /* linux*/
#include <sys/param.h>
#include <sys/mount.h>
#else /* Linux or fbsd */
#include <sys/mnttab.h>
#endif
#include <sys/statvfs.h>
#include "slap.h"
#include "slapi-plugin.h"
#include "snmp_collator.h"
#include <private/pprio.h>
#include <ssl.h>
#include "fe.h"
#if defined(ENABLE_LDAPI)
#include "getsocketpeer.h"
#endif /* ENABLE_LDAPI */
#if defined(LDAP_IOCP)
#define SLAPD_WAKEUP_TIMER 250
#define SLAPD_ACCEPT_WAKEUP_TIMER 250
#else
#define SLAPD_WAKEUP_TIMER 250
#define SLAPD_ACCEPT_WAKEUP_TIMER 250
#endif
int slapd_wakeup_timer = SLAPD_WAKEUP_TIMER; /* time in ms to wakeup */
int slapd_accept_wakeup_timer = SLAPD_ACCEPT_WAKEUP_TIMER; /* time in ms to wakeup */
#ifdef notdef /* GGOODREPL */
/*
* time in secs to do housekeeping:
* this must be greater than slapd_wakeup_timer
*/
short slapd_housekeeping_timer = 10;
#endif /* notdef GGOODREPL */
PRFileDesc *signalpipe[2];
static int writesignalpipe = SLAPD_INVALID_SOCKET;
static int readsignalpipe = SLAPD_INVALID_SOCKET;
#define FDS_SIGNAL_PIPE 0
static PRThread *accept_thread_p = NULL;
static PRThread *disk_thread_p = NULL;
static PRCondVar *diskmon_cvar = NULL;
static PRLock *diskmon_mutex = NULL;
void disk_monitoring_stop(void);
typedef struct listener_info
{
PRStackElem stackelem; /* must be first in struct for PRStack to work */
int idx; /* index of this listener in the ct->fd array */
PRFileDesc *listenfd; /* the listener fd */
int secure;
int local;
Connection_Table *ct; /* for listen job callback */
struct ns_job_t *ns_job; /* the ns accept job */
} listener_info;
static size_t listeners = 0; /* number of listener sockets */
static listener_info *listener_idxs = NULL; /* array of indexes of listener sockets in the ct->fd array */
static PRFileDesc *tls_listener = NULL; /* Stashed tls listener for get_ssl_listener_fd */
#define SLAPD_POLL_LISTEN_READY(xxflagsxx) (xxflagsxx & PR_POLL_READ)
static int get_configured_connection_table_size(void);
#ifdef RESOLVER_NEEDS_LOW_FILE_DESCRIPTORS
static void get_loopback_by_addr(void);
#endif
static PRFileDesc **createprlistensockets(unsigned short port,
PRNetAddr **listenaddr,
int secure,
int local);
static const char *netaddr2string(const PRNetAddr *addr, char *addrbuf, size_t addrbuflen);
static void set_shutdown(int);
static void setup_pr_ct_firsttime_pds(Connection_Table *ct);
static PRIntn setup_pr_accept_pds(PRFileDesc **n_tcps, PRFileDesc **s_tcps, PRFileDesc **i_unix, struct POLL_STRUCT **fds);
static PRIntn setup_pr_read_pds(Connection_Table *ct);
#ifdef HPUX10
static void *catch_signals();
#endif
static int createsignalpipe(void);
static char *
get_pid_file(void)
{
return (pid_file);
}
static int
accept_and_configure(int s __attribute__((unused)), PRFileDesc *pr_acceptfd, PRNetAddr *pr_netaddr, int addrlen __attribute__((unused)), int secure, int local, PRFileDesc **pr_clonefd)
{
int ns = 0;
PRIntervalTime pr_timeout = PR_MillisecondsToInterval(slapd_accept_wakeup_timer);
(*pr_clonefd) = PR_Accept(pr_acceptfd, pr_netaddr, pr_timeout);
if (!(*pr_clonefd)) {
PRErrorCode prerr = PR_GetError();
slapi_log_err(SLAPI_LOG_ERR, "accept_and_configure", "PR_Accept() failed, " SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
prerr, slapd_pr_strerror(prerr));
return (SLAPD_INVALID_SOCKET);
}
ns = configure_pr_socket(pr_clonefd, secure, local);
return ns;
}
/*
* This is the shiny new re-born daemon function, without all the hair
*/
static int handle_new_connection(Connection_Table *ct, int tcps, PRFileDesc *pr_acceptfd, int secure, int local, Connection **newconn);
static void handle_pr_read_ready(Connection_Table *ct, PRIntn num_poll);
static int clear_signal(struct POLL_STRUCT *fds);
static void unfurl_banners(Connection_Table *ct, daemon_ports_t *ports, PRFileDesc **n_tcps, PRFileDesc **s_tcps, PRFileDesc **i_unix);
static int write_pid_file(void);
static int init_shutdown_detect(void);
/* Globals which are used to store the sockets between
* calls to daemon_pre_setuid_init() and the daemon thread
* creation. */
int
daemon_pre_setuid_init(daemon_ports_t *ports)
{
int rc = 0;
if (0 != ports->n_port) {
ports->n_socket = createprlistensockets(ports->n_port,
ports->n_listenaddr, 0, 0);
}
if (config_get_security() && (0 != ports->s_port)) {
ports->s_socket = createprlistensockets((unsigned short)ports->s_port,
ports->s_listenaddr, 1, 0);
} else {
ports->s_socket = SLAPD_INVALID_SOCKET;
}
#if defined(ENABLE_LDAPI)
/* ldapi */
if (0 != ports->i_port) {
ports->i_socket = createprlistensockets(1, ports->i_listenaddr, 0, 1);
}
#endif /* ENABLE_LDAPI */
return (rc);
}
/*
* The time_shutdown static variable is used to signal the time thread
* to shutdown. We used to shut down the time thread when g_get_shutdown()
* returned a non-zero value, but that caused the clock to stop, so to speak,
* and all error log entries to have the same timestamp once the shutdown
* process began.
*/
static int time_shutdown = 0;
/*
* Return a copy of the mount point for the specified directory
*/
#if LINUX
char *
disk_mon_get_mount_point(char *dir)
{
struct mntent *mnt;
struct stat s;
dev_t dev_id;
FILE *fp;
if (stat(dir, &s) != 0) {
return NULL;
}
dev_id = s.st_dev;
if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
return NULL;
}
while ((mnt = getmntent(fp))) {
if (stat(mnt->mnt_dir, &s) != 0) {
continue;
}
if (s.st_dev == dev_id) {
endmntent(fp);
return (slapi_ch_strdup(mnt->mnt_dir));
}
}
endmntent(fp);
return NULL;
}
#elif __FreeBSD__
char *
disk_mon_get_mount_point(char *dir)
{
struct statvfs sb;
if (statvfs(dir, &sb) != 0) {
return NULL;
}
return slapi_ch_strdup(sb.f_mntonname);
}
#endif
/*
* Get the mount point of the directory, and add it to the
* list. Skip duplicate mount points.
*/
void
disk_mon_add_dir(char ***list, char *directory)
{
char *dir = disk_mon_get_mount_point(directory);
if (dir == NULL) {
return;
}
if (!charray_inlist(*list, dir)) {
slapi_ch_array_add(list, dir);
} else {
slapi_ch_free((void **)&dir);
}
}
/*
* We gather all the log, txn log, config, and db directories
*/
void
disk_mon_get_dirs(char ***list)
{
slapdFrontendConfig_t *config = getFrontendConfig();
Slapi_Backend *be = NULL;
char *cookie = NULL;
char *dir = NULL;
/* Add /var just to be safe */
#ifdef LOCALSTATEDIR
disk_mon_add_dir(list, LOCALSTATEDIR);
#else
disk_mon_add_dir(list, "/var");
#endif
/* config and backend directories */
CFG_LOCK_READ(config);
disk_mon_add_dir(list, config->configdir);
disk_mon_add_dir(list, config->accesslog);
disk_mon_add_dir(list, config->errorlog);
disk_mon_add_dir(list, config->auditlog);
disk_mon_add_dir(list, config->auditfaillog);
CFG_UNLOCK_READ(config);
be = slapi_get_first_backend(&cookie);
while (be) {
if (slapi_back_get_info(be, BACK_INFO_DIRECTORY, (void **)&dir) == LDAP_SUCCESS) {
/* db directory */
disk_mon_add_dir(list, dir);
}
if (slapi_back_get_info(be, BACK_INFO_LOG_DIRECTORY, (void **)&dir) == LDAP_SUCCESS) {
/* txn log dir */
disk_mon_add_dir(list, dir);
}
be = (backend *)slapi_get_next_backend(cookie);
}
slapi_ch_free((void **)&cookie);
}
/*
* This function gets the stats of the directory and returns total space,
* available space, and used space of the directory.
*/
int32_t
disk_get_info(char *dir, uint64_t *total_space, uint64_t *avail_space, uint64_t *used_space)
{
int32_t rc = LDAP_SUCCESS;
struct statvfs buf;
uint64_t freeBytes = 0;
uint64_t blockSize = 0;
uint64_t blocks = 0;
if (statvfs(dir, &buf) != -1) {
LL_UI2L(freeBytes, buf.f_bavail);
LL_UI2L(blockSize, buf.f_bsize);
LL_UI2L(blocks, buf.f_blocks);
LL_MUL(*total_space, blocks, blockSize);
LL_MUL(*avail_space, freeBytes, blockSize);
*used_space = *total_space - *avail_space;
} else {
*total_space = 0;
*avail_space = 0;
*used_space = 0;
rc = -1;
}
return rc;
}
/*
* This function checks the list of directories to see if any are below the
* threshold. We return the directory/free disk space of the most critical
* directory.
*/
char *
disk_mon_check_diskspace(char **dirs, uint64_t threshold, uint64_t *disk_space)
{
struct statvfs buf;
uint64_t worst_disk_space = threshold;
uint64_t freeBytes = 0;
uint64_t blockSize = 0;
char *worst_dir = NULL;
int32_t hit_threshold = 0;
int32_t i = 0;
for (i = 0; dirs && dirs[i]; i++) {
if (statvfs(dirs[i], &buf) != -1) {
LL_UI2L(freeBytes, buf.f_bavail);
LL_UI2L(blockSize, buf.f_bsize);
LL_MUL(freeBytes, freeBytes, blockSize);
if (LL_UCMP(freeBytes, <, threshold)) {
hit_threshold = 1;
if (LL_UCMP(freeBytes, <, worst_disk_space)) {
worst_disk_space = freeBytes;
worst_dir = dirs[i];
}
}
}
}
if (hit_threshold) {
*disk_space = worst_disk_space;
return worst_dir;
} else {
*disk_space = 0;
return NULL;
}
}
#define LOGGING_OFF 0
#define LOGGING_ON 1
/*
* Disk Space Monitoring Thread
*
* We need to monitor the free disk space of critical disks.
*
* If we get below the free disk space threshold, start taking measures
* to avoid additional disk space consumption by stopping verbose logging,
* access/audit logging, and deleting rotated logs.
*
* If this is not enough, then we need to shut slapd down to avoid
* possibly corrupting the db.
*
* Future - it would be nice to be able to email an alert.
*/
void
disk_monitoring_thread(void *nothing __attribute__((unused)))
{
char **dirs = NULL;
char *dirstr = NULL;
uint64_t previous_mark = 0;
uint64_t disk_space = 0;
uint64_t threshold = 0;
uint64_t halfway = 0;
time_t start = 0;
time_t now = 0;
int deleted_rotated_logs = 0;
int logging_critical = 0;
int passed_threshold = 0;
int verbose_logging = 0;
int using_accesslog = 0;
int using_auditlog = 0;
int using_auditfaillog = 0;
int logs_disabled = 0;
int grace_period = 0;
int first_pass = 1;
int ok_now = 0;
while (!g_get_shutdown()) {
if (!first_pass) {
PR_Lock(diskmon_mutex);
PR_WaitCondVar(diskmon_cvar, PR_SecondsToInterval(10));
PR_Unlock(diskmon_mutex);
/*
* We need to subtract from disk_space to account for the
* logging we just did, it doesn't hurt if we subtract a
* little more than necessary.
*/
previous_mark = disk_space - 512;
ok_now = 0;
} else {
first_pass = 0;
}
/*
* Get the config settings, as they could have changed
*/
logging_critical = config_get_disk_logging_critical();
grace_period = 60 * config_get_disk_grace_period(); /* convert it to seconds */
verbose_logging = config_get_errorlog_level();
threshold = config_get_disk_threshold();
halfway = threshold / 2;
if (config_get_auditlog_logging_enabled()) {
using_auditlog = 1;
}
if (config_get_auditfaillog_logging_enabled()) {
using_auditfaillog = 1;
}
if (config_get_accesslog_logging_enabled()) {
using_accesslog = 1;
}
/*
* Check the disk space. Always refresh the list, as backends can be added
*/
slapi_ch_array_free(dirs);
dirs = NULL;
disk_mon_get_dirs(&dirs);
dirstr = disk_mon_check_diskspace(dirs, threshold, &disk_space);
if (dirstr == NULL) {
/*
* Good, none of our disks are within the threshold,
* reset the logging if we turned it off
*/
if (passed_threshold) {
if (logs_disabled) {
slapi_log_err(SLAPI_LOG_INFO, "disk_monitoring_thread",
"Disk space is now within acceptable levels. Restoring the log settings.\n");
if (using_accesslog) {
config_set_accesslog_enabled(LOGGING_ON);
}
if (using_auditlog) {
config_set_auditlog_enabled(LOGGING_ON);
}
if (using_auditfaillog) {
config_set_auditfaillog_enabled(LOGGING_ON);
}
} else {
slapi_log_err(SLAPI_LOG_INFO, "disk_monitoring_thread", "Disk space is now within acceptable levels.\n");
}
deleted_rotated_logs = 0;
passed_threshold = 0;
previous_mark = 0;
logs_disabled = 0;
}
continue;
} else {
passed_threshold = 1;
}
/*
* Check if we are already critical
*/
if (disk_space < 4096) { /* 4 k */
slapi_log_err(SLAPI_LOG_ALERT, "disk_monitoring_thread",
"Disk space is critically low on disk (%s), remaining space: %" PRIu64 " Kb. Signaling slapd for shutdown...\n",
dirstr, (disk_space / 1024));
goto cleanup;
}
/*
* If we are low, see if we are using verbose error logging, and turn it off
* if logging is not critical
*/
if (verbose_logging != 0 &&
verbose_logging != LDAP_DEBUG_ANY &&
verbose_logging != SLAPD_DEFAULT_FE_ERRORLOG_LEVEL &&
verbose_logging != SLAPD_DEFAULT_ERRORLOG_LEVEL)
{
slapi_log_err(SLAPI_LOG_ALERT, "disk_monitoring_thread",
"Disk space is low on disk (%s), remaining space: %" PRIu64 " Kb, "
"temporarily setting error loglevel to the default level.\n",
dirstr, (disk_space / 1024));
/* Setting the log level back to zero, actually sets the value to LDAP_DEBUG_ANY */
config_set_errorlog_level(CONFIG_LOGLEVEL_ATTRIBUTE, "0", NULL, CONFIG_APPLY);
continue;
}
/*
* If we are low, there's no verbose logging, logs are not critical, then disable the
* access/audit logs, log another error, and continue.
*/
if (!logs_disabled && !logging_critical) {
slapi_log_err(SLAPI_LOG_ALERT, "disk_monitoring_thread",
"Disk space is too low on disk (%s), remaining space: %" PRIu64 " Kb, disabling access and audit logging.\n",
dirstr, (disk_space / 1024));
config_set_accesslog_enabled(LOGGING_OFF);
config_set_auditlog_enabled(LOGGING_OFF);
config_set_auditfaillog_enabled(LOGGING_OFF);
logs_disabled = 1;
continue;
}
/*
* If we are low, we turned off verbose logging, logs are not critical, and we disabled
* access/audit logging, then delete the rotated logs, log another error, and continue.
*/
if (!deleted_rotated_logs && !logging_critical) {
slapi_log_err(SLAPI_LOG_ALERT, "disk_monitoring_thread",
"Disk space is too low on disk (%s), remaining space: %" PRIu64 " Kb, deleting rotated logs.\n",
dirstr, (disk_space / 1024));
log__delete_rotated_logs();
deleted_rotated_logs = 1;
continue;
}
/*
* Ok, we've done what we can, log a message if we continue to lose available disk space
*/
if (disk_space < previous_mark) {
slapi_log_err(SLAPI_LOG_ALERT, "disk_monitoring_thread",
"Disk space is too low on disk (%s), remaining space: %" PRIu64 " Kb\n",
dirstr, (disk_space / 1024));
}
/*
* If we are below the halfway mark, and we did everything else,
* go into shutdown mode. If the disk space doesn't get critical,
* wait for the grace period before shutting down. This gives an
* admin the chance to clean things up.
*/
if (disk_space < halfway) {
slapi_log_err(SLAPI_LOG_ALERT, "disk_monitoring_thread",
"Disk space on (%s) is too far below the threshold(%" PRIu64 " bytes). "
"Waiting %d minutes for disk space to be cleaned up before shutting slapd down...\n",
dirstr, threshold, (grace_period / 60));
start = slapi_current_utc_time();
now = start;
while ((now - start) < grace_period) {
if (g_get_shutdown()) {
slapi_ch_array_free(dirs);
dirs = NULL;
return;
}
/*
* Sleep for a little bit, but we don't want to run out of disk space
* while sleeping for the entire grace period
*/
DS_Sleep(PR_SecondsToInterval(1));
/*
* Now check disk space again in hopes some space was freed up
*/
dirstr = disk_mon_check_diskspace(dirs, threshold, &disk_space);
if (!dirstr) {
/*
* Excellent, we are back to acceptable levels, reset everything...
*/
slapi_log_err(SLAPI_LOG_INFO, "disk_monitoring_thread",
"Available disk space is now acceptable (%" PRIu64 " bytes). Aborting shutdown, and restoring the log settings.\n",
disk_space);
if (logs_disabled && using_accesslog) {
config_set_accesslog_enabled(LOGGING_ON);
}
if (logs_disabled && using_auditlog) {
config_set_auditlog_enabled(LOGGING_ON);
}
if (logs_disabled && using_auditfaillog) {
config_set_auditfaillog_enabled(LOGGING_ON);
}
deleted_rotated_logs = 0;
passed_threshold = 0;
logs_disabled = 0;
previous_mark = 0;
ok_now = 1;
start = 0;
now = 0;
break;
} else if (disk_space < 4096) { /* 4 k */
/*
* Disk space is critical, log an error, and shut it down now!
*/
slapi_log_err(SLAPI_LOG_ALERT, "disk_monitoring_thread",
"Disk space is critically low on disk (%s), remaining space: %" PRIu64 " Kb. Signaling slapd for shutdown...\n",
dirstr, (disk_space / 1024));
goto cleanup;
}
now = slapi_current_utc_time();
}
if (ok_now) {
/*
* Disk space is acceptable, resume normal processing
*/
continue;
}
/*
* If disk space was freed up we would of detected in the above while loop. So shut it down.
*/
slapi_log_err(SLAPI_LOG_ALERT, "disk_monitoring_thread",
"Disk space is still too low (%" PRIu64 " Kb). Signaling slapd for shutdown...\n",
(disk_space / 1024));
goto cleanup;
}
}
cleanup:
slapi_ch_array_free(dirs);
dirs = NULL;
g_set_shutdown(SLAPI_SHUTDOWN_DISKFULL);
return;
}
static void
handle_listeners(struct POLL_STRUCT *fds)
{
Connection_Table *ct = the_connection_table;
size_t idx;
for (idx = 0; idx < listeners; ++idx) {
int fdidx = listener_idxs[idx].idx;
PRFileDesc *listenfd = listener_idxs[idx].listenfd;
int secure = listener_idxs[idx].secure;
int local = listener_idxs[idx].local;
if (listenfd) {
PR_ASSERT(fds != NULL);
PR_ASSERT(listenfd == fds[fdidx].fd);
if (SLAPD_POLL_LISTEN_READY(fds[fdidx].out_flags)) {
/* accept() the new connection, put it on the active list for handle_pr_read_ready */
int rc = handle_new_connection(ct, SLAPD_INVALID_SOCKET, listenfd, secure, local, NULL);
if (rc) {
slapi_log_err(SLAPI_LOG_CONNS, "handle_listeners", "Error accepting new connection listenfd=%d\n",
PR_FileDesc2NativeHandle(listenfd));
continue;
} else {
/* Wake up the main event loop to handle this immediately. */
signal_listner();
}
}
}
}
return;
}
/*
* Convert any pre-existing DES passwords to AES.
*
* Grab the "password" attributes and search all the backends for
* these attributes and convert them to AES if they are DES encoded.
*/
static void
convert_pbe_des_to_aes(void)
{
Slapi_Entry **entries = NULL;
struct slapdplugin *plugin = NULL;
char **attrs = NULL;
char *val = NULL;
int converted_des_passwd = 0;
int result = -1;
int have_aes = 0;
int have_des = 0;
int i = 0, ii = 0;
/*
* Check that AES plugin is enabled, and grab all the unique
* password attributes.
*/
for (plugin = get_plugin_list(PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME);
plugin != NULL;
plugin = plugin->plg_next) {
char *arg = NULL;
if (plugin->plg_started && strcasecmp(plugin->plg_name, "AES") == 0) {
/* We have the AES plugin, and its enabled */
have_aes = 1;
}
if (plugin->plg_started && strcasecmp(plugin->plg_name, "DES") == 0) {
/* We have the DES plugin, and its enabled */
have_des = 1;
}
/* Gather all the unique password attributes from all the PBE plugins */
for (i = 0, arg = plugin->plg_argv[i];
i < plugin->plg_argc;
arg = plugin->plg_argv[++i]) {
if (charray_inlist(attrs, arg)) {
continue;
}
charray_add(&attrs, slapi_ch_strdup(arg));
}
}
if (have_aes && have_des) {
/*
* Find any entries in cn=config that contain DES passwords and convert
* them to AES
*/
slapi_log_err(SLAPI_LOG_HOUSE, "convert_pbe_des_to_aes",
"Converting DES passwords to AES...\n");
for (i = 0; attrs && attrs[i]; i++) {
char *filter = PR_smprintf("%s=*", attrs[i]);
Slapi_PBlock *pb = slapi_pblock_new();
slapi_search_internal_set_pb(pb, "cn=config",
LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL,
(void *)plugin_get_default_component_id(),
SLAPI_OP_FLAG_IGNORE_UNINDEXED);
slapi_search_internal_pb(pb);
slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
for (ii = 0; entries && entries[ii]; ii++) {
if ((val = (char *)slapi_entry_attr_get_ref(entries[ii], attrs[i]))) {
if (strlen(val) >= 5 && strncmp(val, "{DES}", 5) == 0) {
/*
* We have a DES encoded password, convert it to AES
*/
Slapi_Value *sval = NULL;
LDAPMod mod_replace;
LDAPMod *mods[2];
char *replace_val[2];
char *passwd = NULL;
int rc = 0;
/* decode the DES password */
if (pw_rever_decode(val, &passwd, attrs[i]) == -1) {
slapi_log_err(SLAPI_LOG_ERR, "convert_pbe_des_to_aes",
"Failed to decode existing DES password for (%s)\n",
slapi_entry_get_dn(entries[ii]));
rc = -1;
}
/* encode the password */
if (rc == 0) {
sval = slapi_value_new_string(passwd);
if (pw_rever_encode(&sval, attrs[i]) == -1) {
slapi_log_err(SLAPI_LOG_ERR, "convert_pbe_des_to_aes",
"failed to encode AES password for (%s)\n",
slapi_entry_get_dn(entries[ii]));
rc = -1;
}
}
if (rc == 0) {
Slapi_PBlock *mod_pb = slapi_pblock_new();
/* replace the attribute in the entry */
replace_val[0] = (char *)slapi_value_get_string(sval);
replace_val[1] = NULL;
mod_replace.mod_op = LDAP_MOD_REPLACE;
mod_replace.mod_type = attrs[i];
mod_replace.mod_values = replace_val;
mods[0] = &mod_replace;
mods[1] = 0;
slapi_modify_internal_set_pb(mod_pb, slapi_entry_get_dn(entries[ii]),
mods, 0, 0, (void *)plugin_get_default_component_id(), 0);
slapi_modify_internal_pb(mod_pb);
slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
slapi_pblock_destroy(mod_pb);
if (LDAP_SUCCESS != result) {
slapi_log_err(SLAPI_LOG_ERR, "convert_pbe_des_to_aes",
"Failed to convert password for (%s) error (%d)\n",
slapi_entry_get_dn(entries[ii]), result);
} else {
slapi_log_err(SLAPI_LOG_HOUSE, "convert_pbe_des_to_aes",
"Successfully converted password for (%s)\n",
slapi_entry_get_dn(entries[ii]));
converted_des_passwd = 1;
}
}
slapi_ch_free_string(&passwd);
slapi_value_free(&sval);
}
}
}
slapi_free_search_results_internal(pb);
slapi_pblock_destroy(pb);
slapi_ch_free_string(&filter);
}
if (!converted_des_passwd) {
slapi_log_err(SLAPI_LOG_HOUSE, "convert_pbe_des_to_aes",
"No DES passwords found to convert.\n");
}
}
charray_free(attrs);
}
void
accept_thread(void *vports)
{
daemon_ports_t *ports = (daemon_ports_t *)vports;
Connection_Table *ct = the_connection_table;
PRIntn num_poll = 0;
struct POLL_STRUCT *fds = NULL;
int select_return = 0;
PRErrorCode prerr;
int last_accept_new_connections = -1;
PRIntervalTime pr_timeout = PR_MillisecondsToInterval(slapd_accept_wakeup_timer);
slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
PRFileDesc **n_tcps = NULL;
PRFileDesc **s_tcps = NULL;
PRFileDesc **i_unix = NULL;
n_tcps = ports->n_socket;
s_tcps = ports->s_socket;
#if defined(ENABLE_LDAPI)
i_unix = ports->i_socket;
#endif /* ENABLE_LDAPI */
num_poll = setup_pr_accept_pds(n_tcps, s_tcps, i_unix, &fds);
while (!g_get_shutdown()) {
/* Do we need to accept new connections? */
int accept_new_connections = ((ct->size - g_get_current_conn_count()) > slapdFrontendConfig->reservedescriptors);
last_accept_new_connections = accept_new_connections;
if (!accept_new_connections) {
if (last_accept_new_connections) {
slapi_log_err(SLAPI_LOG_ERR, "accept_thread",
"Not listening for new connections - too many fds open\n");
}
/* Need a sleep delay here. */
PR_Sleep(pr_timeout);
continue;
} else {
/* Log that we are now listening again */
if (!last_accept_new_connections && last_accept_new_connections != -1) {
slapi_log_err(SLAPI_LOG_ERR, "accept_thread",
"Listening for new connections again\n");
}
}
select_return = POLL_FN(fds, num_poll, pr_timeout);
switch (select_return) {
case 0: /* Timeout */
break;
case -1: /* Error */
prerr = PR_GetError();
slapi_log_err(SLAPI_LOG_TRACE, "accept_thread", "PR_Poll() failed, " SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
prerr, slapd_system_strerror(prerr));
break;
default: /* a new connection */
handle_listeners(fds);
break;
}
}
/* free the listener indexes */
slapi_ch_free((void **)&listener_idxs);
slapd_sockets_ports_free(ports);
slapi_ch_free((void **)&fds);
}
void
slapd_sockets_ports_free(daemon_ports_t *ports_info)
{
/* freeing PRFileDescs */
PRFileDesc **fdesp = NULL;
for (fdesp = ports_info->n_socket; fdesp && *fdesp; fdesp++) {
PR_Close(*fdesp);
}
slapi_ch_free((void **)&ports_info->n_socket);
for (fdesp = ports_info->s_socket; fdesp && *fdesp; fdesp++) {
PR_Close(*fdesp);
}
slapi_ch_free((void **)&ports_info->s_socket);
#if defined(ENABLE_LDAPI)
for (fdesp = ports_info->i_socket; fdesp && *fdesp; fdesp++) {
PR_Close(*fdesp);
}
slapi_ch_free((void **)&ports_info->i_socket);
#endif /* ENABLE_LDAPI */
/* freeing NetAddrs */
PRNetAddr **nap;
for (nap = ports_info->n_listenaddr; nap && *nap; nap++) {
slapi_ch_free((void **)nap);
}
slapi_ch_free((void **)&ports_info->n_listenaddr);
for (nap = ports_info->s_listenaddr; nap && *nap; nap++) {
slapi_ch_free((void **)nap);
}
slapi_ch_free((void **)&ports_info->s_listenaddr);
#if defined(ENABLE_LDAPI)
for (nap = ports_info->i_listenaddr; nap && *nap; nap++) {
slapi_ch_free((void **)nap);
}
slapi_ch_free((void **)&ports_info->i_listenaddr);
#endif
}
void
slapd_daemon(daemon_ports_t *ports)
{
/* We are passed some ports---one for regular connections, one
* for SSL connections, one for ldapi connections.
*/
/* Previously there was a ton of code #defined on NET_SSL.
* This looked horrible, so now I'm doing it this way:
* If you want me to do SSL, pass me something in the ssl port number.
* If you don't, pass me zero.
*/
PRFileDesc **n_tcps = NULL;
PRFileDesc **s_tcps = NULL;
PRFileDesc **i_unix = NULL;
PRFileDesc **fdesp = NULL;
PRIntn num_poll = 0;
PRIntervalTime pr_timeout = PR_MillisecondsToInterval(slapd_wakeup_timer);
uint64_t threads;
int in_referral_mode = config_check_referral_mode();
int connection_table_size = get_configured_connection_table_size();
the_connection_table = connection_table_new(connection_table_size);
/*
* Log a warning if we detect nunc-stans
*/
if (config_get_enable_nunc_stans()) {
slapi_log_err(SLAPI_LOG_WARNING, "slapd_daemon", "cn=config: nsslapd-enable-nunc-stans is on. nunc-stans has been deprecated and this flag is now ignored.\n");
slapi_log_err(SLAPI_LOG_WARNING, "slapd_daemon", "cn=config: nsslapd-enable-nunc-stans should be set to off or deleted from cn=config.\n");
}
#ifdef RESOLVER_NEEDS_LOW_FILE_DESCRIPTORS
/*
* Some DNS resolver implementations, such as the one built into
* Solaris <= 8, need to use one or more low numbered file
* descriptors internally (probably because they use a deficient
* implementation of stdio). So we make a call now that uses the
* resolver so it has an opportunity to grab whatever low file
* descriptors it needs (before we use up all of the low numbered
* ones for incoming client connections and so on).
*/
get_loopback_by_addr();
#endif
/* Retrieve the sockets from their hiding place */
n_tcps = ports->n_socket;
s_tcps = ports->s_socket;
#if defined(ENABLE_LDAPI)
i_unix = ports->i_socket;
#endif /* ENABLE_LDAPI */
createsignalpipe();
/* Setup our signal interception. */
init_shutdown_detect();
if (
(n_tcps == NULL) &&
#if defined(ENABLE_LDAPI)
(i_unix == NULL) &&
#endif /* ENABLE_LDAPI */
(s_tcps == NULL)) { /* nothing to do */
slapi_log_err(SLAPI_LOG_ERR, "slapd_daemon", "No port to listen on\n");
exit(1);
}
init_op_threads();
/*
* If we are monitoring disk space, then create the mutex, the cvar,
* and the monitoring thread.
*/
if (config_get_disk_monitoring()) {
if ((diskmon_mutex = PR_NewLock()) == NULL) {
slapi_log_err(SLAPI_LOG_ERR, "slapd_daemon",
"Cannot create new lock for disk space monitoring. " SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
PR_GetError(), slapd_pr_strerror(PR_GetError()));
g_set_shutdown(SLAPI_SHUTDOWN_EXIT);
}
if (diskmon_mutex) {
if ((diskmon_cvar = PR_NewCondVar(diskmon_mutex)) == NULL) {
slapi_log_err(SLAPI_LOG_EMERG, "slapd_daemon",
"Cannot create new condition variable for disk space monitoring. " SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
PR_GetError(), slapd_pr_strerror(PR_GetError()));
g_set_shutdown(SLAPI_SHUTDOWN_EXIT);
}
}