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