1*0a6a1f1dSLionel Sambuc /* $NetBSD: otp_db.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #ifdef HAVE_CONFIG_H
37ebfedea0SLionel Sambuc #include "config.h"
38*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
39ebfedea0SLionel Sambuc #endif
40ebfedea0SLionel Sambuc
41ebfedea0SLionel Sambuc #include "otp_locl.h"
42ebfedea0SLionel Sambuc
43ebfedea0SLionel Sambuc #if !defined(HAVE_NDBM) && !defined(HAVE_DB_NDBM)
44ebfedea0SLionel Sambuc #include "ndbm_wrap.h"
45ebfedea0SLionel Sambuc #endif
46ebfedea0SLionel Sambuc
47ebfedea0SLionel Sambuc #define RETRIES 5
48ebfedea0SLionel Sambuc
49ebfedea0SLionel Sambuc void *
otp_db_open(void)50ebfedea0SLionel Sambuc otp_db_open (void)
51ebfedea0SLionel Sambuc {
52ebfedea0SLionel Sambuc int lock;
53ebfedea0SLionel Sambuc int i;
54ebfedea0SLionel Sambuc void *ret;
55ebfedea0SLionel Sambuc
56ebfedea0SLionel Sambuc for(i = 0; i < RETRIES; ++i) {
57ebfedea0SLionel Sambuc struct stat statbuf;
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc lock = open (OTP_DB_LOCK, O_WRONLY | O_CREAT | O_EXCL, 0666);
60ebfedea0SLionel Sambuc if (lock >= 0) {
61ebfedea0SLionel Sambuc close(lock);
62ebfedea0SLionel Sambuc break;
63ebfedea0SLionel Sambuc }
64ebfedea0SLionel Sambuc if (stat (OTP_DB_LOCK, &statbuf) == 0) {
65ebfedea0SLionel Sambuc if (time(NULL) - statbuf.st_mtime > OTP_DB_TIMEOUT)
66ebfedea0SLionel Sambuc unlink (OTP_DB_LOCK);
67ebfedea0SLionel Sambuc else
68ebfedea0SLionel Sambuc sleep (1);
69ebfedea0SLionel Sambuc }
70ebfedea0SLionel Sambuc }
71ebfedea0SLionel Sambuc if (i == RETRIES)
72ebfedea0SLionel Sambuc return NULL;
73ebfedea0SLionel Sambuc ret = dbm_open (OTP_DB, O_RDWR | O_CREAT, 0600);
74ebfedea0SLionel Sambuc if (ret == NULL)
75ebfedea0SLionel Sambuc unlink (OTP_DB_LOCK);
76ebfedea0SLionel Sambuc return ret;
77ebfedea0SLionel Sambuc }
78ebfedea0SLionel Sambuc
79ebfedea0SLionel Sambuc void
otp_db_close(void * dbm)80ebfedea0SLionel Sambuc otp_db_close (void *dbm)
81ebfedea0SLionel Sambuc {
82ebfedea0SLionel Sambuc dbm_close ((DBM *)dbm);
83ebfedea0SLionel Sambuc unlink (OTP_DB_LOCK);
84ebfedea0SLionel Sambuc }
85ebfedea0SLionel Sambuc
86ebfedea0SLionel Sambuc /*
87ebfedea0SLionel Sambuc * Remove this entry from the database.
88ebfedea0SLionel Sambuc * return 0 if ok.
89ebfedea0SLionel Sambuc */
90ebfedea0SLionel Sambuc
91ebfedea0SLionel Sambuc int
otp_delete(void * v,OtpContext * ctx)92ebfedea0SLionel Sambuc otp_delete (void *v, OtpContext *ctx)
93ebfedea0SLionel Sambuc {
94ebfedea0SLionel Sambuc DBM *dbm = (DBM *)v;
95ebfedea0SLionel Sambuc datum key;
96ebfedea0SLionel Sambuc
97ebfedea0SLionel Sambuc key.dsize = strlen(ctx->user);
98ebfedea0SLionel Sambuc key.dptr = ctx->user;
99ebfedea0SLionel Sambuc
100ebfedea0SLionel Sambuc return dbm_delete(dbm, key);
101ebfedea0SLionel Sambuc }
102ebfedea0SLionel Sambuc
103ebfedea0SLionel Sambuc /*
104ebfedea0SLionel Sambuc * Read this entry from the database and lock it if lockp.
105ebfedea0SLionel Sambuc */
106ebfedea0SLionel Sambuc
107ebfedea0SLionel Sambuc static int
otp_get_internal(void * v,OtpContext * ctx,int lockp)108ebfedea0SLionel Sambuc otp_get_internal (void *v, OtpContext *ctx, int lockp)
109ebfedea0SLionel Sambuc {
110ebfedea0SLionel Sambuc DBM *dbm = (DBM *)v;
111ebfedea0SLionel Sambuc datum dat, key;
112ebfedea0SLionel Sambuc char *p;
113ebfedea0SLionel Sambuc time_t now, then;
114ebfedea0SLionel Sambuc
115ebfedea0SLionel Sambuc key.dsize = strlen(ctx->user);
116ebfedea0SLionel Sambuc key.dptr = ctx->user;
117ebfedea0SLionel Sambuc
118ebfedea0SLionel Sambuc dat = dbm_fetch (dbm, key);
119ebfedea0SLionel Sambuc if (dat.dptr == NULL) {
120ebfedea0SLionel Sambuc ctx->err = "Entry not found";
121ebfedea0SLionel Sambuc return -1;
122ebfedea0SLionel Sambuc }
123ebfedea0SLionel Sambuc p = dat.dptr;
124ebfedea0SLionel Sambuc
125ebfedea0SLionel Sambuc memcpy (&then, p, sizeof(then));
126ebfedea0SLionel Sambuc ctx->lock_time = then;
127ebfedea0SLionel Sambuc if (lockp) {
128ebfedea0SLionel Sambuc time(&now);
129ebfedea0SLionel Sambuc if (then && now - then < OTP_USER_TIMEOUT) {
130ebfedea0SLionel Sambuc ctx->err = "Entry locked";
131ebfedea0SLionel Sambuc return -1;
132ebfedea0SLionel Sambuc }
133ebfedea0SLionel Sambuc memcpy (p, &now, sizeof(now));
134ebfedea0SLionel Sambuc }
135ebfedea0SLionel Sambuc p += sizeof(now);
136ebfedea0SLionel Sambuc ctx->alg = otp_find_alg (p);
137ebfedea0SLionel Sambuc if (ctx->alg == NULL) {
138ebfedea0SLionel Sambuc ctx->err = "Bad algorithm";
139ebfedea0SLionel Sambuc return -1;
140ebfedea0SLionel Sambuc }
141ebfedea0SLionel Sambuc p += strlen(p) + 1;
142ebfedea0SLionel Sambuc {
143ebfedea0SLionel Sambuc unsigned char *up = (unsigned char *)p;
144ebfedea0SLionel Sambuc ctx->n = (up[0] << 24) | (up[1] << 16) | (up[2] << 8) | up[3];
145ebfedea0SLionel Sambuc }
146ebfedea0SLionel Sambuc p += 4;
147ebfedea0SLionel Sambuc memcpy (ctx->key, p, OTPKEYSIZE);
148ebfedea0SLionel Sambuc p += OTPKEYSIZE;
149ebfedea0SLionel Sambuc strlcpy (ctx->seed, p, sizeof(ctx->seed));
150ebfedea0SLionel Sambuc if (lockp)
151ebfedea0SLionel Sambuc return dbm_store (dbm, key, dat, DBM_REPLACE);
152ebfedea0SLionel Sambuc else
153ebfedea0SLionel Sambuc return 0;
154ebfedea0SLionel Sambuc }
155ebfedea0SLionel Sambuc
156ebfedea0SLionel Sambuc /*
157ebfedea0SLionel Sambuc * Get and lock.
158ebfedea0SLionel Sambuc */
159ebfedea0SLionel Sambuc
160ebfedea0SLionel Sambuc int
otp_get(void * v,OtpContext * ctx)161ebfedea0SLionel Sambuc otp_get (void *v, OtpContext *ctx)
162ebfedea0SLionel Sambuc {
163ebfedea0SLionel Sambuc return otp_get_internal (v, ctx, 1);
164ebfedea0SLionel Sambuc }
165ebfedea0SLionel Sambuc
166ebfedea0SLionel Sambuc /*
167ebfedea0SLionel Sambuc * Get and don't lock.
168ebfedea0SLionel Sambuc */
169ebfedea0SLionel Sambuc
170ebfedea0SLionel Sambuc int
otp_simple_get(void * v,OtpContext * ctx)171ebfedea0SLionel Sambuc otp_simple_get (void *v, OtpContext *ctx)
172ebfedea0SLionel Sambuc {
173ebfedea0SLionel Sambuc return otp_get_internal (v, ctx, 0);
174ebfedea0SLionel Sambuc }
175ebfedea0SLionel Sambuc
176ebfedea0SLionel Sambuc /*
177ebfedea0SLionel Sambuc * Write this entry to the database.
178ebfedea0SLionel Sambuc */
179ebfedea0SLionel Sambuc
180ebfedea0SLionel Sambuc int
otp_put(void * v,OtpContext * ctx)181ebfedea0SLionel Sambuc otp_put (void *v, OtpContext *ctx)
182ebfedea0SLionel Sambuc {
183ebfedea0SLionel Sambuc DBM *dbm = (DBM *)v;
184ebfedea0SLionel Sambuc datum dat, key;
185ebfedea0SLionel Sambuc char buf[1024], *p;
186ebfedea0SLionel Sambuc time_t zero = 0;
187ebfedea0SLionel Sambuc size_t len, rem;
188ebfedea0SLionel Sambuc
189ebfedea0SLionel Sambuc key.dsize = strlen(ctx->user);
190ebfedea0SLionel Sambuc key.dptr = ctx->user;
191ebfedea0SLionel Sambuc
192ebfedea0SLionel Sambuc p = buf;
193ebfedea0SLionel Sambuc rem = sizeof(buf);
194ebfedea0SLionel Sambuc
195ebfedea0SLionel Sambuc if (rem < sizeof(zero))
196ebfedea0SLionel Sambuc return -1;
197ebfedea0SLionel Sambuc memcpy (p, &zero, sizeof(zero));
198ebfedea0SLionel Sambuc p += sizeof(zero);
199ebfedea0SLionel Sambuc rem -= sizeof(zero);
200ebfedea0SLionel Sambuc len = strlen(ctx->alg->name) + 1;
201ebfedea0SLionel Sambuc
202ebfedea0SLionel Sambuc if (rem < len)
203ebfedea0SLionel Sambuc return -1;
204ebfedea0SLionel Sambuc strlcpy (p, ctx->alg->name, rem);
205ebfedea0SLionel Sambuc p += len;
206ebfedea0SLionel Sambuc rem -= len;
207ebfedea0SLionel Sambuc
208ebfedea0SLionel Sambuc if (rem < 4)
209ebfedea0SLionel Sambuc return -1;
210ebfedea0SLionel Sambuc {
211ebfedea0SLionel Sambuc unsigned char *up = (unsigned char *)p;
212ebfedea0SLionel Sambuc *up++ = (ctx->n >> 24) & 0xFF;
213ebfedea0SLionel Sambuc *up++ = (ctx->n >> 16) & 0xFF;
214ebfedea0SLionel Sambuc *up++ = (ctx->n >> 8) & 0xFF;
215ebfedea0SLionel Sambuc *up++ = (ctx->n >> 0) & 0xFF;
216ebfedea0SLionel Sambuc }
217ebfedea0SLionel Sambuc p += 4;
218ebfedea0SLionel Sambuc rem -= 4;
219ebfedea0SLionel Sambuc
220ebfedea0SLionel Sambuc if (rem < OTPKEYSIZE)
221ebfedea0SLionel Sambuc return -1;
222ebfedea0SLionel Sambuc memcpy (p, ctx->key, OTPKEYSIZE);
223ebfedea0SLionel Sambuc p += OTPKEYSIZE;
224ebfedea0SLionel Sambuc rem -= OTPKEYSIZE;
225ebfedea0SLionel Sambuc
226ebfedea0SLionel Sambuc len = strlen(ctx->seed) + 1;
227ebfedea0SLionel Sambuc if (rem < len)
228ebfedea0SLionel Sambuc return -1;
229ebfedea0SLionel Sambuc strlcpy (p, ctx->seed, rem);
230ebfedea0SLionel Sambuc p += len;
231ebfedea0SLionel Sambuc rem -= len;
232ebfedea0SLionel Sambuc dat.dptr = buf;
233ebfedea0SLionel Sambuc dat.dsize = p - buf;
234ebfedea0SLionel Sambuc return dbm_store (dbm, key, dat, DBM_REPLACE);
235ebfedea0SLionel Sambuc }
236