xref: /titanic_50/usr/src/lib/libnisdb/yptol/yptol_utils.c (revision 9f758caf94c37c3ad28c48cfe503f9fc830a66d5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * DESCRIPTION: Contains helper functions for N2L
31  */
32 
33 /*
34  * Includes. WE WANT TO USE REAL DBM FUNCTIONS SO DO NOT INCLUDE SHIM_HOOKS.H.
35  */
36 #include <unistd.h>
37 #include <syslog.h>
38 #include <ndbm.h>
39 #include <sys/systeminfo.h>
40 #include <errno.h>
41 #include <strings.h>
42 #include "ypsym.h"
43 #include "ypdefs.h"
44 #include "shim.h"
45 #include "yptol.h"
46 #include "stdio.h"
47 #include "../ldap_util.h"
48 
49 /* Enable standard YP code features defined in ypdefs.h */
50 USE_YP_PREFIX
51 USE_YP_MASTER_NAME
52 USE_YP_LAST_MODIFIED
53 USE_YP_INPUT_FILE
54 USE_YP_OUTPUT_NAME
55 USE_YP_DOMAIN_NAME
56 USE_YP_SECURE
57 USE_YP_INTERDOMAIN
58 USE_DBM
59 
60 /*
61  * FUNCTION :	alloc_temp_names()
62  *
63  * DESCRIPTION:	Creates the set of temporary names for update files. It is
64  *		the caller responsibility to free these.
65  *
66  * GIVEN :	Name of map (fully qualified)
67  *
68  * RETURNS :	SUCCESS with all names allocated.
69  *		FAILURE with no names allocated.
70  */
71 suc_code
72 alloc_temp_names(char *name, char **temp_entries, char **temp_ttl)
73 {
74 	char *myself = "alloc_temp_names";
75 
76 	*temp_entries = (char *)am(myself, strlen(name) +
77 						strlen(TEMP_POSTFIX) + 1);
78 	if (NULL == *temp_entries) {
79 		return (FAILURE);
80 	}
81 
82 	*temp_ttl = (char *)am(myself, strlen(TEMP_POSTFIX) + strlen(name) +
83 						strlen(TTL_POSTFIX) + 1);
84 	if (NULL == *temp_ttl) {
85 		sfree(*temp_entries);
86 		return (FAILURE);
87 	}
88 
89 	strcpy(*temp_entries, name);
90 	strcat(*temp_entries, TEMP_POSTFIX);
91 
92 	strcpy(*temp_ttl, name);
93 	strcat(*temp_ttl, TTL_POSTFIX);
94 	strcat(*temp_ttl, TEMP_POSTFIX);
95 
96 	return (SUCCESS);
97 }
98 
99 /*
100  * FUNCTION :	addpair()
101  *
102  * DESCRIPTION:	Adds a single string entry to a dbm database. This is a copy of
103  *		a function from makedbm but is useful enough to be put into
104  *		shared code.
105  *
106  * GIVEN:	Database handle
107  *		Key
108  *		Value
109  *
110  * RETURNS :	SUCCESS = Value written
111  *		FAILURE = Value not written.
112  */
113 suc_code
114 addpair(DBM *fdb, char *str1, char *str2)
115 {
116 	datum key;
117 	datum content;
118 
119 	key.dptr = str1;
120 	key.dsize = strlen(str1);
121 	content.dptr  = str2;
122 	content.dsize = strlen(str2);
123 	errno = 0;
124 	if (dbm_store(fdb, key, content, DBM_REPLACE) != 0) {
125 		logmsg(MSG_NOTIMECHECK, LOG_ERR, "Problem storing %.*s %.*s "
126 					"(errno=%d)",
127 					key.dptr, content.dptr, errno);
128 		return (FAILURE);
129 	}
130 	return (SUCCESS);
131 }
132 
133 /*
134  * FUNCTION :	dump_datum()
135  *
136  * DESCRIPTION:	Prints out a datum as a text string with no line feed.
137  */
138 void
139 dump_datum(datum *dat)
140 {
141 	int	i;
142 
143 	if (NULL == dat) {
144 		printf("NULL datum");
145 		return;
146 	}
147 
148 	if (NULL == dat->dptr) {
149 		printf("NULL dptr");
150 		return;
151 	}
152 	for (i = 0; i < dat->dsize; i++)
153 		putchar(dat->dptr[i]);
154 }
155 
156 /*
157  * FUNCTION :	update_timestamp()
158  *
159  * DESCRIPTION:	Adds, or updates, a maps last modified timestamp.
160  *
161  * GIVEN :	Pointer to an open DBM file.
162  *
163  * RETURNS :	SUCCESS = Entry created
164  *		FAILURE = Entry not created
165  */
166 suc_code
167 update_timestamp(DBM *db)
168 {
169 	char time_string[MAX_ASCII_ORDER_NUMBER_LENGTH];
170 	struct timeval	now;
171 
172 	if (0 != gettimeofday(&now, NULL)) {
173 		logmsg(MSG_NOTIMECHECK, LOG_ERR, "Could not get time of day");
174 		return (FAILURE);
175 	}
176 	sprintf(time_string, "%010ld", (long)now.tv_sec);
177 	if (SUCCESS != addpair(db, yp_last_modified, time_string))
178 		return (FAILURE);
179 
180 	return (SUCCESS);
181 }
182