184865Sobrien/*	$NetBSD$	*/
2218822Sdim
384865Sobrien/*
484865Sobrien * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska H��gskolan
584865Sobrien * (Royal Institute of Technology, Stockholm, Sweden).
684865Sobrien * All rights reserved.
784865Sobrien *
884865Sobrien * Redistribution and use in source and binary forms, with or without
984865Sobrien * modification, are permitted provided that the following conditions
1084865Sobrien * are met:
1184865Sobrien *
1284865Sobrien * 1. Redistributions of source code must retain the above copyright
1384865Sobrien *    notice, this list of conditions and the following disclaimer.
1484865Sobrien *
1584865Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1684865Sobrien *    notice, this list of conditions and the following disclaimer in the
1784865Sobrien *    documentation and/or other materials provided with the distribution.
1884865Sobrien *
19218822Sdim * 3. Neither the name of the Institute nor the names of its contributors
2084865Sobrien *    may be used to endorse or promote products derived from this software
21218822Sdim *    without specific prior written permission.
2284865Sobrien *
2384865Sobrien * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2484865Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2584865Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2684865Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2784865Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2884865Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2984865Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3084865Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3184865Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3284865Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3384865Sobrien * SUCH DAMAGE.
3484865Sobrien */
3584865Sobrien
3684865Sobrien#ifdef HAVE_CONFIG_H
3784865Sobrien#include "config.h"
3884865Sobrien__RCSID("$NetBSD$");
3984865Sobrien#endif
4084865Sobrien
4184865Sobrien#include "otp_locl.h"
4284865Sobrien
4384865Sobrienint
4484865Sobrienotp_challenge (OtpContext *ctx, char *user, char *str, size_t len)
4584865Sobrien{
4684865Sobrien  void *dbm;
4784865Sobrien  int ret;
4884865Sobrien
4984865Sobrien  ctx->challengep = 0;
5084865Sobrien  ctx->err = NULL;
5184865Sobrien  ctx->user = strdup(user);
52130561Sobrien  if (ctx->user == NULL) {
5384865Sobrien    ctx->err = "Out of memory";
5484865Sobrien    return -1;
55130561Sobrien  }
5689857Sobrien  dbm = otp_db_open ();
57130561Sobrien  if (dbm == NULL) {
5884865Sobrien    ctx->err = "Cannot open database";
5984865Sobrien    return -1;
6084865Sobrien  }
6184865Sobrien  ret = otp_get (dbm, ctx);
62130561Sobrien  otp_db_close (dbm);
6384865Sobrien  if (ret)
6484865Sobrien    return ret;
6584865Sobrien  snprintf (str, len,
6684865Sobrien	    "[ otp-%s %u %s ]",
6784865Sobrien	    ctx->alg->name, ctx->n-1, ctx->seed);
6889857Sobrien  ctx->challengep = 1;
6989857Sobrien  return 0;
7084865Sobrien}
7184865Sobrien