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