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 /*
28*7c478bd9Sstevel@tonic-gate  * DESCRIPTION: Contains helper functions for N2L
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  * Includes. WE WANT TO USE REAL DBM FUNCTIONS SO DO NOT INCLUDE SHIM_HOOKS.H.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <syslog.h>
36*7c478bd9Sstevel@tonic-gate #include <ndbm.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
38*7c478bd9Sstevel@tonic-gate #include <errno.h>
39*7c478bd9Sstevel@tonic-gate #include <strings.h>
40*7c478bd9Sstevel@tonic-gate #include "ypsym.h"
41*7c478bd9Sstevel@tonic-gate #include "ypdefs.h"
42*7c478bd9Sstevel@tonic-gate #include "shim.h"
43*7c478bd9Sstevel@tonic-gate #include "yptol.h"
44*7c478bd9Sstevel@tonic-gate #include "stdio.h"
45*7c478bd9Sstevel@tonic-gate #include "../ldap_util.h"
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /* Enable standard YP code features defined in ypdefs.h */
48*7c478bd9Sstevel@tonic-gate USE_YP_PREFIX
49*7c478bd9Sstevel@tonic-gate USE_YP_MASTER_NAME
50*7c478bd9Sstevel@tonic-gate USE_YP_LAST_MODIFIED
51*7c478bd9Sstevel@tonic-gate USE_YP_INPUT_FILE
52*7c478bd9Sstevel@tonic-gate USE_YP_OUTPUT_NAME
53*7c478bd9Sstevel@tonic-gate USE_YP_DOMAIN_NAME
54*7c478bd9Sstevel@tonic-gate USE_YP_SECURE
55*7c478bd9Sstevel@tonic-gate USE_YP_INTERDOMAIN
56*7c478bd9Sstevel@tonic-gate USE_DBM
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * FUNCTION :	alloc_temp_names()
60*7c478bd9Sstevel@tonic-gate  *
61*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:	Creates the set of temporary names for update files. It is
62*7c478bd9Sstevel@tonic-gate  *		the caller responsibility to free these.
63*7c478bd9Sstevel@tonic-gate  *
64*7c478bd9Sstevel@tonic-gate  * GIVEN :	Name of map (fully qualified)
65*7c478bd9Sstevel@tonic-gate  *
66*7c478bd9Sstevel@tonic-gate  * RETURNS :	SUCCESS with all names allocated.
67*7c478bd9Sstevel@tonic-gate  *		FAILURE with no names allocated.
68*7c478bd9Sstevel@tonic-gate  */
69*7c478bd9Sstevel@tonic-gate suc_code
alloc_temp_names(char * name,char ** temp_entries,char ** temp_ttl)70*7c478bd9Sstevel@tonic-gate alloc_temp_names(char *name, char **temp_entries, char **temp_ttl)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	char *myself = "alloc_temp_names";
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	*temp_entries = (char *)am(myself, strlen(name) +
75*7c478bd9Sstevel@tonic-gate 						strlen(TEMP_POSTFIX) + 1);
76*7c478bd9Sstevel@tonic-gate 	if (NULL == *temp_entries) {
77*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	*temp_ttl = (char *)am(myself, strlen(TEMP_POSTFIX) + strlen(name) +
81*7c478bd9Sstevel@tonic-gate 						strlen(TTL_POSTFIX) + 1);
82*7c478bd9Sstevel@tonic-gate 	if (NULL == *temp_ttl) {
83*7c478bd9Sstevel@tonic-gate 		sfree(*temp_entries);
84*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	strcpy(*temp_entries, name);
88*7c478bd9Sstevel@tonic-gate 	strcat(*temp_entries, TEMP_POSTFIX);
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	strcpy(*temp_ttl, name);
91*7c478bd9Sstevel@tonic-gate 	strcat(*temp_ttl, TTL_POSTFIX);
92*7c478bd9Sstevel@tonic-gate 	strcat(*temp_ttl, TEMP_POSTFIX);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate /*
98*7c478bd9Sstevel@tonic-gate  * FUNCTION :	addpair()
99*7c478bd9Sstevel@tonic-gate  *
100*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:	Adds a single string entry to a dbm database. This is a copy of
101*7c478bd9Sstevel@tonic-gate  *		a function from makedbm but is useful enough to be put into
102*7c478bd9Sstevel@tonic-gate  *		shared code.
103*7c478bd9Sstevel@tonic-gate  *
104*7c478bd9Sstevel@tonic-gate  * GIVEN:	Database handle
105*7c478bd9Sstevel@tonic-gate  *		Key
106*7c478bd9Sstevel@tonic-gate  *		Value
107*7c478bd9Sstevel@tonic-gate  *
108*7c478bd9Sstevel@tonic-gate  * RETURNS :	SUCCESS = Value written
109*7c478bd9Sstevel@tonic-gate  *		FAILURE = Value not written.
110*7c478bd9Sstevel@tonic-gate  */
111*7c478bd9Sstevel@tonic-gate suc_code
addpair(DBM * fdb,char * str1,char * str2)112*7c478bd9Sstevel@tonic-gate addpair(DBM *fdb, char *str1, char *str2)
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	datum key;
115*7c478bd9Sstevel@tonic-gate 	datum content;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	key.dptr = str1;
118*7c478bd9Sstevel@tonic-gate 	key.dsize = strlen(str1);
119*7c478bd9Sstevel@tonic-gate 	content.dptr  = str2;
120*7c478bd9Sstevel@tonic-gate 	content.dsize = strlen(str2);
121*7c478bd9Sstevel@tonic-gate 	errno = 0;
122*7c478bd9Sstevel@tonic-gate 	if (dbm_store(fdb, key, content, DBM_REPLACE) != 0) {
123*7c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR, "Problem storing %.*s %.*s "
124*7c478bd9Sstevel@tonic-gate 					"(errno=%d)",
125*7c478bd9Sstevel@tonic-gate 					key.dptr, content.dptr, errno);
126*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate /*
132*7c478bd9Sstevel@tonic-gate  * FUNCTION :	dump_datum()
133*7c478bd9Sstevel@tonic-gate  *
134*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:	Prints out a datum as a text string with no line feed.
135*7c478bd9Sstevel@tonic-gate  */
136*7c478bd9Sstevel@tonic-gate void
dump_datum(datum * dat)137*7c478bd9Sstevel@tonic-gate dump_datum(datum *dat)
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate 	int	i;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	if (NULL == dat) {
142*7c478bd9Sstevel@tonic-gate 		printf("NULL datum");
143*7c478bd9Sstevel@tonic-gate 		return;
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if (NULL == dat->dptr) {
147*7c478bd9Sstevel@tonic-gate 		printf("NULL dptr");
148*7c478bd9Sstevel@tonic-gate 		return;
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < dat->dsize; i++)
151*7c478bd9Sstevel@tonic-gate 		putchar(dat->dptr[i]);
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /*
155*7c478bd9Sstevel@tonic-gate  * FUNCTION :	update_timestamp()
156*7c478bd9Sstevel@tonic-gate  *
157*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:	Adds, or updates, a maps last modified timestamp.
158*7c478bd9Sstevel@tonic-gate  *
159*7c478bd9Sstevel@tonic-gate  * GIVEN :	Pointer to an open DBM file.
160*7c478bd9Sstevel@tonic-gate  *
161*7c478bd9Sstevel@tonic-gate  * RETURNS :	SUCCESS = Entry created
162*7c478bd9Sstevel@tonic-gate  *		FAILURE = Entry not created
163*7c478bd9Sstevel@tonic-gate  */
164*7c478bd9Sstevel@tonic-gate suc_code
update_timestamp(DBM * db)165*7c478bd9Sstevel@tonic-gate update_timestamp(DBM *db)
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	char time_string[MAX_ASCII_ORDER_NUMBER_LENGTH];
168*7c478bd9Sstevel@tonic-gate 	struct timeval	now;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	if (0 != gettimeofday(&now, NULL)) {
171*7c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR, "Could not get time of day");
172*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate 	sprintf(time_string, "%010ld", (long)now.tv_sec);
175*7c478bd9Sstevel@tonic-gate 	if (SUCCESS != addpair(db, yp_last_modified, time_string))
176*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
179*7c478bd9Sstevel@tonic-gate }
180