Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-read-password-plugin.c
blobd8faea4e32ebca655e47982eb38c263e9ccf73c7
1 #if 0
2 exec nbdkit cc "$0" "$@" EXTRA_CFLAGS="-I.. -I${SRCDIR:-.}/../include"
3 #endif
4 /* nbdkit
5 * Copyright Red Hat
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * * Neither the name of Red Hat nor the names of its contributors may be
19 * used to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 /* See test-read-password.sh and test-read-password-interactive.sh */
38 #include <config.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #define NBDKIT_API_VERSION 2
45 #include <nbdkit-plugin.h>
47 static char *password;
48 static char *file;
50 static void
51 password_unload (void)
53 free (password);
54 free (file);
57 static int
58 password_config (const char *key, const char *value)
60 if (strcmp (key, "password") == 0) {
61 if (nbdkit_read_password (value, &password) == -1)
62 return -1;
64 else if (strcmp (key, "file") == 0) {
65 file = nbdkit_absolute_path (value);
66 if (file == NULL)
67 return -1;
69 else {
70 nbdkit_error ("unknown parameter: %s", key);
71 return -1;
73 return 0;
76 static int
77 password_config_complete (void)
79 FILE *fp;
81 if (!file || !password) {
82 nbdkit_error ("file and password parameters are required");
83 return -1;
85 fp = fopen (file, "w");
86 if (!fp) {
87 nbdkit_error ("%s: %m", file);
88 return -1;
90 fprintf (fp, "%s", password);
91 fclose (fp);
92 return 0;
95 static int
96 password_get_ready (void)
98 /* This plugin is for testing, so it never serves any data. */
99 nbdkit_shutdown ();
100 return 0;
103 static void *
104 password_open (int readonly)
106 abort ();
109 static int64_t
110 password_get_size (void *handle)
112 abort ();
115 #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL
117 static int
118 password_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
119 uint32_t flags)
121 abort ();
124 static struct nbdkit_plugin plugin = {
125 .name = "password",
126 .version = PACKAGE_VERSION,
127 .unload = password_unload,
128 .config = password_config,
129 .config_complete = password_config_complete,
130 .get_ready = password_get_ready,
131 .open = password_open,
132 .get_size = password_get_size,
133 .pread = password_pread,
136 NBDKIT_REGISTER_PLUGIN (plugin)