1 /* $OpenBSD: ypdb.c,v 1.11 2003/07/15 06:10:46 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Margo Seltzer. 10 * 11 * This code is derived from ndbm module of BSD4.4 db (hash) by 12 * Mats O Jansson 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 27 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 30 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/types.h> 41 #include <stdio.h> 42 #include <string.h> 43 44 #include <rpcsvc/yp.h> 45 46 #include "ypdb.h" 47 48 /* 49 * Returns: 50 * *DBM on success 51 * NULL on failure 52 */ 53 extern DBM * 54 ypdb_open(const char *file, int flags, int mode) 55 { 56 BTREEINFO info; 57 char path[MAXPATHLEN]; 58 DBM *db; 59 60 memset(&info, 0, sizeof info); 61 info.flags = 0; 62 info.cachesize = 0; 63 info.maxkeypage = 0; 64 info.minkeypage = 0; 65 info.psize = 0; 66 info.compare = NULL; 67 info.prefix = NULL; 68 info.lorder = 0; 69 snprintf(path, sizeof(path), "%s%s", file, YPDB_SUFFIX); 70 db = (DBM *)dbopen(path, flags, mode, DB_BTREE, (void *)&info); 71 return (db); 72 } 73 74 /* 75 * Returns: 76 * *DBM on success 77 * NULL on failure 78 */ 79 extern DBM * 80 ypdb_open_suf(const char *file, int flags, int mode) 81 { 82 BTREEINFO info; 83 DBM *db; 84 85 memset(&info, 0, sizeof info); 86 info.flags = 0; 87 info.cachesize = 0; 88 info.maxkeypage = 0; 89 info.minkeypage = 0; 90 info.psize = 0; 91 info.compare = NULL; 92 info.prefix = NULL; 93 info.lorder = 0; 94 db = (DBM *)dbopen(file, flags, mode, DB_BTREE, (void *)&info); 95 return (db); 96 } 97 98 extern void 99 ypdb_close(DBM *db) 100 { 101 (void)(db->close)(db); 102 } 103 104 /* 105 * Returns: 106 * DATUM on success 107 * NULL on failure 108 */ 109 extern datum 110 ypdb_fetch(DBM *db, datum key) 111 { 112 datum retval; 113 DBT nk, nd; 114 int status; 115 116 nk.data = key.dptr; 117 nk.size = key.dsize; 118 119 status = (db->get)(db, &nk, &nd, 0); 120 if (status) { 121 retval.dptr = NULL; 122 retval.dsize = 0; 123 } else { 124 retval.dptr = nd.data; 125 retval.dsize = nd.size; 126 } 127 return (retval); 128 } 129 130 /* 131 * Returns: 132 * DATUM on success 133 * NULL on failure 134 */ 135 136 extern datum 137 ypdb_firstkey(DBM *db) 138 { 139 int status; 140 datum retkey; 141 DBT nk, nd; 142 143 status = (db->seq)(db, &nk, &nd, R_FIRST); 144 if (status) { 145 retkey.dptr = NULL; 146 retkey.dsize = 0; 147 } else { 148 retkey.dptr = nk.data; 149 retkey.dsize = nk.size; 150 } 151 return (retkey); 152 } 153 154 /* 155 * Returns: 156 * DATUM on success 157 * NULL on failure 158 */ 159 160 extern datum 161 ypdb_nextkey(DBM *db) 162 { 163 int status; 164 datum retkey; 165 DBT nk, nd; 166 167 status = (db->seq)(db, &nk, &nd, R_NEXT); 168 if (status) { 169 retkey.dptr = NULL; 170 retkey.dsize = 0; 171 } else { 172 retkey.dptr = nk.data; 173 retkey.dsize = nk.size; 174 } 175 return (retkey); 176 } 177 178 /* 179 * Returns: 180 * DATUM on success 181 * NULL on failure 182 */ 183 184 extern datum 185 ypdb_setkey(DBM *db, datum key) 186 { 187 int status; 188 DBT nk, nd; 189 190 nk.data = key.dptr; 191 nk.size = key.dsize; 192 status = (db->seq)(db, &nk, &nd, R_CURSOR); 193 if (status) { 194 key.dptr = NULL; 195 key.dsize = 0; 196 } 197 return (key); 198 } 199 200 /* 201 * Returns: 202 * 0 on success 203 * <0 failure 204 * 1 if YPDB_INSERT and entry exists 205 */ 206 int 207 ypdb_store(DBM *db, datum key, datum content, int flags) 208 { 209 DBT nk, nd; 210 211 if (key.dsize > YPMAXRECORD || content.dsize > YPMAXRECORD) 212 return -1; 213 nk.data = key.dptr; 214 nk.size = key.dsize; 215 nd.data = content.dptr; 216 nd.size = content.dsize; 217 return ((db->put)(db, &nk, &nd, 218 (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0)); 219 } 220