summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnisdb/yptol/yptol_utils.c
blob: 1b2fc776ba99722a1d59bba912349d44746e1a58 (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * DESCRIPTION: Contains helper functions for N2L
 */

/*
 * Includes. WE WANT TO USE REAL DBM FUNCTIONS SO DO NOT INCLUDE SHIM_HOOKS.H.
 */
#include <unistd.h>
#include <syslog.h>
#include <ndbm.h>
#include <sys/systeminfo.h>
#include <errno.h>
#include <strings.h>
#include "ypsym.h"
#include "ypdefs.h"
#include "shim.h"
#include "yptol.h"
#include "stdio.h"
#include "../ldap_util.h"

/* Enable standard YP code features defined in ypdefs.h */
USE_YP_PREFIX
USE_YP_MASTER_NAME
USE_YP_LAST_MODIFIED
USE_YP_INPUT_FILE
USE_YP_OUTPUT_NAME
USE_YP_DOMAIN_NAME
USE_YP_SECURE
USE_YP_INTERDOMAIN
USE_DBM

/*
 * FUNCTION :	alloc_temp_names()
 *
 * DESCRIPTION:	Creates the set of temporary names for update files. It is
 *		the caller responsibility to free these.
 *
 * GIVEN :	Name of map (fully qualified)
 *
 * RETURNS :	SUCCESS with all names allocated.
 *		FAILURE with no names allocated.
 */
suc_code
alloc_temp_names(char *name, char **temp_entries, char **temp_ttl)
{
	char *myself = "alloc_temp_names";

	*temp_entries = (char *)am(myself, strlen(name) +
						strlen(TEMP_POSTFIX) + 1);
	if (NULL == *temp_entries) {
		return (FAILURE);
	}

	*temp_ttl = (char *)am(myself, strlen(TEMP_POSTFIX) + strlen(name) +
						strlen(TTL_POSTFIX) + 1);
	if (NULL == *temp_ttl) {
		sfree(*temp_entries);
		return (FAILURE);
	}

	strcpy(*temp_entries, name);
	strcat(*temp_entries, TEMP_POSTFIX);

	strcpy(*temp_ttl, name);
	strcat(*temp_ttl, TTL_POSTFIX);
	strcat(*temp_ttl, TEMP_POSTFIX);

	return (SUCCESS);
}

/*
 * FUNCTION :	addpair()
 *
 * DESCRIPTION:	Adds a single string entry to a dbm database. This is a copy of
 *		a function from makedbm but is useful enough to be put into
 *		shared code.
 *
 * GIVEN:	Database handle
 *		Key
 *		Value
 *
 * RETURNS :	SUCCESS = Value written
 *		FAILURE = Value not written.
 */
suc_code
addpair(DBM *fdb, char *str1, char *str2)
{
	datum key;
	datum content;

	key.dptr = str1;
	key.dsize = strlen(str1);
	content.dptr  = str2;
	content.dsize = strlen(str2);
	errno = 0;
	if (dbm_store(fdb, key, content, DBM_REPLACE) != 0) {
		logmsg(MSG_NOTIMECHECK, LOG_ERR, "Problem storing %.*s %.*s "
					"(errno=%d)",
					key.dptr, content.dptr, errno);
		return (FAILURE);
	}
	return (SUCCESS);
}

/*
 * FUNCTION :	dump_datum()
 *
 * DESCRIPTION:	Prints out a datum as a text string with no line feed.
 */
void
dump_datum(datum *dat)
{
	int	i;

	if (NULL == dat) {
		printf("NULL datum");
		return;
	}

	if (NULL == dat->dptr) {
		printf("NULL dptr");
		return;
	}
	for (i = 0; i < dat->dsize; i++)
		putchar(dat->dptr[i]);
}

/*
 * FUNCTION :	update_timestamp()
 *
 * DESCRIPTION:	Adds, or updates, a maps last modified timestamp.
 *
 * GIVEN :	Pointer to an open DBM file.
 *
 * RETURNS :	SUCCESS = Entry created
 *		FAILURE = Entry not created
 */
suc_code
update_timestamp(DBM *db)
{
	char time_string[MAX_ASCII_ORDER_NUMBER_LENGTH];
	struct timeval	now;

	if (0 != gettimeofday(&now, NULL)) {
		logmsg(MSG_NOTIMECHECK, LOG_ERR, "Could not get time of day");
		return (FAILURE);
	}
	sprintf(time_string, "%010ld", (long)now.tv_sec);
	if (SUCCESS != addpair(db, yp_last_modified, time_string))
		return (FAILURE);

	return (SUCCESS);
}