17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright (c) 2001 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate * All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <poll.h>
287c478bd9Sstevel@tonic-gate #include <sys/time.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include "nis_ldap.h"
317c478bd9Sstevel@tonic-gate #include "nis_hashitem.h"
327c478bd9Sstevel@tonic-gate #include "ldap_map.h"
337c478bd9Sstevel@tonic-gate #include "ldap_parse.h"
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate * Global structure keeping config state. Since it's created and modified
387c478bd9Sstevel@tonic-gate * while the rpc.nisd still is single-threaded, and only read in MT mode,
397c478bd9Sstevel@tonic-gate * no locking is needed.
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate __nis_config_t ldapConfig = {
427c478bd9Sstevel@tonic-gate ini_none, /* nisplusLDAPinitialUpdate */
437c478bd9Sstevel@tonic-gate pass_error, /* nisplusLDAPthreadCreationError */
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate -1, /* Try forever */
467c478bd9Sstevel@tonic-gate 15 /* 15 second timeout */
477c478bd9Sstevel@tonic-gate },
487c478bd9Sstevel@tonic-gate de_retry, /* nisplusLDAPdumpError */
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate -1, /* Try forever */
517c478bd9Sstevel@tonic-gate 200 /* 200 second timeout */
527c478bd9Sstevel@tonic-gate },
537c478bd9Sstevel@tonic-gate directory_locked, /* nisplusLDAPresyncService */
547c478bd9Sstevel@tonic-gate accumulate, /* nisplusLDAPupdateBatching */
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate -1, /* Not used */
577c478bd9Sstevel@tonic-gate 120 /* Accumulate for 120 seconds */
587c478bd9Sstevel@tonic-gate },
597c478bd9Sstevel@tonic-gate block /* nisplusLDAPexclusiveWaitMOde */
607c478bd9Sstevel@tonic-gate };
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * Utility function that accepts a (__nisdb_retry_t *), decrements the
657c478bd9Sstevel@tonic-gate * 'attempts' counter, and sleeps for 'timeout' seconds.
667c478bd9Sstevel@tonic-gate *
677c478bd9Sstevel@tonic-gate * NOTE: Don't pass a pointer into the 'ldapConfig' structure to
687c478bd9Sstevel@tonic-gate * this function. Instead, initialize a private copy to the
697c478bd9Sstevel@tonic-gate * value from 'ldapConfig'.
707c478bd9Sstevel@tonic-gate *
717c478bd9Sstevel@tonic-gate * The value of 'attempts' upon entry determines action as follows:
727c478bd9Sstevel@tonic-gate *
737c478bd9Sstevel@tonic-gate * < 0 Don't change 'attempts', sleep as indicated, return 1
747c478bd9Sstevel@tonic-gate *
757c478bd9Sstevel@tonic-gate * 0 Don't change 'attempts', only sleep if forceSleep is set,
767c478bd9Sstevel@tonic-gate * return 0 if we didn't sleep, 1 if we slept.
777c478bd9Sstevel@tonic-gate *
787c478bd9Sstevel@tonic-gate * > 0 Decrement 'attempts', sleep as indicated, return 1
797c478bd9Sstevel@tonic-gate */
807c478bd9Sstevel@tonic-gate int
__nis_retry_sleep(__nisdb_retry_t * retry,int forceSleep)81*9c508120SToomas Soome __nis_retry_sleep(__nisdb_retry_t *retry, int forceSleep)
82*9c508120SToomas Soome {
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate if (retry == NULL)
857c478bd9Sstevel@tonic-gate return (0);
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate if (retry->attempts > 0) {
887c478bd9Sstevel@tonic-gate retry->attempts -= 1;
897c478bd9Sstevel@tonic-gate } else if (retry->attempts == 0 && !forceSleep) {
907c478bd9Sstevel@tonic-gate return (0);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, retry->timeout*1000);
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate return (1);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * The root directory is special in NIS+; it's the only directory that
1007c478bd9Sstevel@tonic-gate * doesn't appear as an entry in another directory. Hence, our method
1017c478bd9Sstevel@tonic-gate * of keeping the directory/table entry expiration time in the
1027c478bd9Sstevel@tonic-gate * directory/table doesn't work, and we instead implement the following
1037c478bd9Sstevel@tonic-gate * interface.
1047c478bd9Sstevel@tonic-gate */
1057c478bd9Sstevel@tonic-gate static time_t rootDirExpire = 0;
1067c478bd9Sstevel@tonic-gate static int rootDirTtl = 0;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate * Return 1 if the root dir has expired, 0 otherwise.
1107c478bd9Sstevel@tonic-gate */
1117c478bd9Sstevel@tonic-gate int
rootDirExpired(void)112*9c508120SToomas Soome rootDirExpired(void)
113*9c508120SToomas Soome {
1147c478bd9Sstevel@tonic-gate struct timeval now;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, 0);
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate if (rootDirExpire >= now.tv_sec)
1197c478bd9Sstevel@tonic-gate return (1);
1207c478bd9Sstevel@tonic-gate else
1217c478bd9Sstevel@tonic-gate return (0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate * Update the expiration time of the root dir to be now plus the TTL.
1267c478bd9Sstevel@tonic-gate * Also establishes the TTL if not set.
1277c478bd9Sstevel@tonic-gate */
1287c478bd9Sstevel@tonic-gate int
touchRootDir(void)129*9c508120SToomas Soome touchRootDir(void)
130*9c508120SToomas Soome {
1317c478bd9Sstevel@tonic-gate struct timeval now;
1327c478bd9Sstevel@tonic-gate int ttl;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, 0);
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate /* Do we need to initialize the TTL ? */
1377c478bd9Sstevel@tonic-gate if (rootDirTtl == 0) {
1387c478bd9Sstevel@tonic-gate __nis_table_mapping_t *t;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate t = __nis_find_item_mt(ROOTDIRFILE, &ldapMappingList, 0, 0);
1417c478bd9Sstevel@tonic-gate if (t != 0) {
1427c478bd9Sstevel@tonic-gate int interval;
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate interval = t->initTtlHi - t->initTtlLo + 1;
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate if (interval > 1) {
1477c478bd9Sstevel@tonic-gate srand48(now.tv_sec);
1487c478bd9Sstevel@tonic-gate ttl = (lrand48() % interval);
1497c478bd9Sstevel@tonic-gate } else {
1507c478bd9Sstevel@tonic-gate ttl = t->initTtlLo;
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate rootDirTtl = t->ttl;
1547c478bd9Sstevel@tonic-gate } else {
1557c478bd9Sstevel@tonic-gate ttl = rootDirTtl = 3600;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate } else {
1587c478bd9Sstevel@tonic-gate ttl = rootDirTtl;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate rootDirExpire = now.tv_sec + ttl;
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate return (0);
1647c478bd9Sstevel@tonic-gate }
165