Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / libnisdb / yptol / yptol_utils.c
bloba48f3cffca2f1364eba37c54ebee1dd85cea6e0e
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * DESCRIPTION: Contains helper functions for N2L
34 * Includes. WE WANT TO USE REAL DBM FUNCTIONS SO DO NOT INCLUDE SHIM_HOOKS.H.
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"
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
61 * FUNCTION : alloc_temp_names()
63 * DESCRIPTION: Creates the set of temporary names for update files. It is
64 * the caller responsibility to free these.
66 * GIVEN : Name of map (fully qualified)
68 * RETURNS : SUCCESS with all names allocated.
69 * FAILURE with no names allocated.
71 suc_code
72 alloc_temp_names(char *name, char **temp_entries, char **temp_ttl)
74 char *myself = "alloc_temp_names";
76 *temp_entries = (char *)am(myself, strlen(name) +
77 strlen(TEMP_POSTFIX) + 1);
78 if (NULL == *temp_entries) {
79 return (FAILURE);
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);
89 strcpy(*temp_entries, name);
90 strcat(*temp_entries, TEMP_POSTFIX);
92 strcpy(*temp_ttl, name);
93 strcat(*temp_ttl, TTL_POSTFIX);
94 strcat(*temp_ttl, TEMP_POSTFIX);
96 return (SUCCESS);
100 * FUNCTION : addpair()
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.
106 * GIVEN: Database handle
107 * Key
108 * Value
110 * RETURNS : SUCCESS = Value written
111 * FAILURE = Value not written.
113 suc_code
114 addpair(DBM *fdb, char *str1, char *str2)
116 datum key;
117 datum content;
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);
130 return (SUCCESS);
134 * FUNCTION : dump_datum()
136 * DESCRIPTION: Prints out a datum as a text string with no line feed.
138 void
139 dump_datum(datum *dat)
141 int i;
142 char *datstr;
144 if (NULL == dat) {
145 printf("NULL datum");
146 return;
149 if (NULL == dat->dptr) {
150 printf("NULL dptr");
151 return;
153 datstr = dat->dptr;
154 for (i = 0; i < dat->dsize; i++)
155 putchar(datstr[i]);
159 * FUNCTION : update_timestamp()
161 * DESCRIPTION: Adds, or updates, a maps last modified timestamp.
163 * GIVEN : Pointer to an open DBM file.
165 * RETURNS : SUCCESS = Entry created
166 * FAILURE = Entry not created
168 suc_code
169 update_timestamp(DBM *db)
171 char time_string[MAX_ASCII_ORDER_NUMBER_LENGTH];
172 struct timeval now;
174 if (0 != gettimeofday(&now, NULL)) {
175 logmsg(MSG_NOTIMECHECK, LOG_ERR, "Could not get time of day");
176 return (FAILURE);
178 sprintf(time_string, "%010ld", (long)now.tv_sec);
179 if (SUCCESS != addpair(db, yp_last_modified, time_string))
180 return (FAILURE);
182 return (SUCCESS);