1/*-
2 * Copyright (c) 2014 Michihiro NAKAJIMA
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "archive_platform.h"
27
28#ifdef HAVE_ERRNO_H
29#include <errno.h>
30#endif
31#include "archive_write_private.h"
32
33static int
34set_passphrase(struct archive_write *a, const char *p)
35{
36	if (p == NULL || p[0] == '\0') {
37		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
38		    "Empty passphrase is unacceptable");
39		return (ARCHIVE_FAILED);
40	}
41	free(a->passphrase);
42	a->passphrase = strdup(p);
43	if (a->passphrase == NULL) {
44		archive_set_error(&a->archive, ENOMEM,
45		    "Can't allocate data for passphrase");
46		return (ARCHIVE_FATAL);
47	}
48	return (ARCHIVE_OK);
49}
50
51
52int
53archive_write_set_passphrase(struct archive *_a, const char *p)
54{
55	struct archive_write *a = (struct archive_write *)_a;
56
57	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
58		"archive_write_set_passphrase");
59
60	return (set_passphrase(a, p));
61}
62
63
64int
65archive_write_set_passphrase_callback(struct archive *_a, void *client_data,
66    archive_passphrase_callback *cb)
67{
68	struct archive_write *a = (struct archive_write *)_a;
69
70	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
71		"archive_write_set_passphrase_callback");
72
73	a->passphrase_callback = cb;
74	a->passphrase_client_data = client_data;
75	return (ARCHIVE_OK);
76}
77
78
79const char *
80__archive_write_get_passphrase(struct archive_write *a)
81{
82
83	if (a->passphrase != NULL)
84		return (a->passphrase);
85
86	if (a->passphrase_callback != NULL) {
87		const char *p;
88		p = a->passphrase_callback(&a->archive,
89		    a->passphrase_client_data);
90		set_passphrase(a, p);
91		a->passphrase_callback = NULL;
92		a->passphrase_client_data = NULL;
93	}
94	return (a->passphrase);
95}
96