1/* db_testw32.c--SASL win32 test/dummy interface
2 * G. Diskin    NOTE THIS IS FOR TEST PURPOSES ONLY FOR WIN32
3 * $Id: db_testw32.c,v 1.3 2004/07/07 22:53:42 snsimon Exp $
4 */
5/*
6 * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in
17 *    the documentation and/or other materials provided with the
18 *    distribution.
19 *
20 * 3. The name "Carnegie Mellon University" must not be used to
21 *    endorse or promote products derived from this software without
22 *    prior written permission. For permission or any other legal
23 *    details, please contact
24 *      Office of Technology Transfer
25 *      Carnegie Mellon University
26 *      5000 Forbes Avenue
27 *      Pittsburgh, PA  15213-3890
28 *      (412) 268-4387, fax: (412) 268-7395
29 *      tech-transfer@andrew.cmu.edu
30 *
31 * 4. Redistributions of any form whatsoever must retain the following
32 *    acknowledgment:
33 *    "This product includes software developed by Computing Services
34 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
35 *
36 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
37 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
38 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
39 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
41 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
42 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43 */
44
45#ifdef WIN32
46/*
47**  Disable warning messages for differences in parameter lists.
48**  The Microsoft compiler spits out a warning message if a
49**  function pointer is assigned to another function pointer,
50**  but the formal parameter lists of the functions do not agree.
51**  The assignments are compiled without modification though,
52**  so it's safe to disable this warning message.
53*/
54#pragma warning( disable : 4113 )
55
56#include <config.h>
57#include "sasl.h"
58#include "saslint.h"
59#include <stdio.h>
60
61#error "db_testw32.c uses an obsolete sasldb interface that will need to be updated when we do the win32 port of SASLv2"
62
63/* This provides a version of _sasl_db_getsecret and
64 * _sasl_db_putsecret which can be used to test the code on win32.
65 * Currently the CRAM, SCRAM, and DIGEST mechanisms need to get a user's
66 * secret and match to the user's input.  The win32 saslpwd program will
67 * call the putsecret function to store each mech's encoding of the pw.
68 *  Note that currently a file is created for each mech and the encoding
69 * is stored to and retrieved from the file. */
70
71
72static int
73getsecret(const sasl_utils_t *utils,
74	  sasl_conn_t *context __attribute__((unused)),
75	  const char *auth_identity,
76	  sasl_secret_t ** secret)
77{
78  int result = SASL_OK;
79  FILE *db;
80  long the_len;
81  char the_secret[256],filename[100];
82  int rvalue;
83
84  if (!auth_identity || !secret)
85    return SASL_FAIL;
86
87  strcpy(filename, "c:\\tmp\\sasldata.txt");
88  db = fopen(filename, "rb");
89
90  if (! db) {
91    result = SASL_FAIL;
92    goto cleanup;
93  }
94
95  rvalue = fread(&the_len, sizeof(long), 1, db);
96  if (ferror(db)) {
97	  printf("Error reading secret length\n");
98	  result = SASL_FAIL;
99	  goto cleanup;
100  }
101  rvalue = fread(the_secret, 1, the_len, db);
102  if (ferror(db)) {
103	  printf("Error reading secret data\n");
104	  result = SASL_FAIL;
105	  goto cleanup;
106  }
107
108  fclose(db);
109
110  *secret = utils->malloc(sizeof(sasl_secret_t)
111			  + the_len
112			  + 1);
113  if (! *secret) {
114    result = SASL_NOMEM;
115    goto cleanup;
116  }
117  (*secret)->len = the_len;
118  memcpy(&(*secret)->data, the_secret, the_len);
119  (*secret)->data[(*secret)->len] = '\0'; /* sanity */
120
121 cleanup:
122
123  return result;
124}
125
126static int
127putsecret(const sasl_utils_t *utils,
128	  sasl_conn_t *context __attribute__((unused)),
129	  const char *auth_identity,
130	  const sasl_secret_t * secret)
131{
132  int result = SASL_OK;
133  FILE *db;
134  char filename[100];
135
136  if (!auth_identity)
137      return SASL_FAIL;
138
139  strcpy(filename, "c:\\tmp\\sasldata.txt");
140  db = fopen(filename, "wb");
141
142  if (! db) {
143      utils->log(NULL, SASL_LOG_ERR,
144		"error opening password file. Do you have write permissions?");
145    result = SASL_FAIL;
146    goto cleanup;
147  }
148
149  fwrite(&secret->len, sizeof(long), 1, db);
150  if (ferror(db)) {
151	  printf("Error writing secret length\n");
152	  exit(-1);
153  }
154  fwrite(secret->data, 1, secret->len, db);
155  if (ferror(db)) {
156	  printf("Error writing secret data\n");
157	  exit(-1);
158  }
159  fclose(db);
160 cleanup:
161
162  return result;
163}
164
165sasl_server_getsecret_t *_sasl_db_getsecret = &getsecret;
166sasl_server_putsecret_t *_sasl_db_putsecret = &putsecret;
167
168
169int _sasl_check_db(const sasl_utils_t *utils, sasl_conn_t *conn)
170{
171    return SASL_OK;
172}
173
174/*
175**  Restore the generation of code-generation warning message 4113.
176*/
177#pragma warning( default : 4113 )
178
179#endif /*win32*/
180