069c185c |
1 | /* -*-c-*- |
2 | * |
95959d10 |
3 | * $Id: pixie-client.c,v 1.2 2000/06/17 11:49:37 mdw Exp $ |
069c185c |
4 | * |
5 | * Simple passphrase pixie client (Unix-specific) |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: pixie-client.c,v $ |
95959d10 |
33 | * Revision 1.2 2000/06/17 11:49:37 mdw |
34 | * New pixie protocol allowing application to request passphrases and send |
35 | * them to the pixie. |
36 | * |
069c185c |
37 | * Revision 1.1 1999/12/22 15:58:41 mdw |
38 | * Passphrase pixie support. |
39 | * |
40 | */ |
41 | |
42 | /*----- Header files ------------------------------------------------------*/ |
43 | |
44 | #include <errno.h> |
45 | #include <stdio.h> |
46 | #include <stdlib.h> |
47 | #include <string.h> |
48 | |
49 | #include <sys/types.h> |
50 | #include <unistd.h> |
51 | #include <fcntl.h> |
52 | #include <pwd.h> |
53 | |
54 | #include <sys/socket.h> |
55 | #include <sys/un.h> |
56 | |
57 | #include <mLib/dstr.h> |
58 | #include <mLib/fdflags.h> |
59 | #include <mLib/str.h> |
60 | |
61 | #include "passphrase.h" |
62 | #include "pixie.h" |
63 | |
64 | /*----- Main code ---------------------------------------------------------*/ |
65 | |
66 | /* --- @pixie_open@ --- * |
67 | * |
68 | * Arguments: @const char *sock@ = path to pixie socket |
69 | * |
70 | * Returns: Less than zero if it failed, or file descriptor. |
71 | * |
72 | * Use: Opens a connection to a passphrase pixie. |
73 | */ |
74 | |
75 | int pixie_open(const char *sock) |
76 | { |
77 | struct sockaddr_un *sun; |
78 | size_t sz; |
79 | int fd; |
80 | |
81 | /* --- Open the connection --- */ |
82 | |
83 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
84 | goto fail_0; |
85 | sun = pixie_address(sock, &sz); |
86 | if (connect(fd, (struct sockaddr *)sun, sz)) |
87 | goto fail_1; |
88 | free(sun); |
89 | return (fd); |
90 | |
91 | /* --- Tidy up if things went wrong --- */ |
92 | |
93 | fail_1: |
94 | free(sun); |
95 | close(fd); |
96 | fail_0: |
97 | return (-1); |
98 | } |
99 | |
100 | /* --- @pixie_read@ --- * |
101 | * |
102 | * Arguments: @int fd@ = connection to passphrase pixie |
103 | * @const char *tag@ = pointer to tag string |
104 | * @unsigned mode@ = reading mode |
105 | * @char *buf@ = pointer to destination buffer |
106 | * @size_t sz@ = size of the buffer |
107 | * |
95959d10 |
108 | * Returns: Zero if all went well, @-1@ if the read fails, @+1@ to |
109 | * request the passphrase from the user. |
069c185c |
110 | * |
111 | * Use: Reads a passphrase from the pixie. |
112 | */ |
113 | |
114 | int pixie_read(int fd, const char *tag, unsigned mode, char *buf, size_t sz) |
115 | { |
116 | dstr d = DSTR_INIT; |
117 | char *p, *q; |
118 | |
119 | /* --- Send the request --- */ |
120 | |
121 | dstr_putf(&d, "%s %s\n", mode == PMODE_READ ? "PASS" : "VERIFY", tag); |
122 | write(fd, d.buf, d.len); |
123 | dstr_destroy(&d); |
124 | |
125 | /* --- Sort out the result --- */ |
126 | |
127 | again: |
128 | pixie_fdline(fd, buf, sz); |
129 | p = buf; |
130 | if ((q = str_getword(&p)) == 0) |
131 | return (-1); |
132 | if (strcmp(q, "INFO") == 0) |
133 | goto again; |
95959d10 |
134 | else if (strcmp(q, "MISSING") == 0) |
135 | return (+1); |
069c185c |
136 | else if (strcmp(q, "OK") != 0) |
137 | return (-1); |
138 | |
139 | /* --- Return the final answer --- */ |
140 | |
141 | if (p) |
142 | memmove(buf, p, strlen(p) + 1); |
143 | else |
144 | *buf = 0; |
145 | return (0); |
146 | } |
147 | |
95959d10 |
148 | /* --- @pixie_set@ --- * |
149 | * |
150 | * Arguments: @int fd@ = pixie file descriptor |
151 | * @const char *tag@ = pointer to tag string |
152 | * @const char *phrase@ = pointer to passphrase string |
153 | * |
154 | * Returns: --- |
155 | * |
156 | * Use: Sends a passphrase to the passphrase pixie. |
157 | */ |
158 | |
159 | void pixie_set(int fd, const char *tag, const char *phrase) |
160 | { |
161 | dstr d = DSTR_INIT; |
162 | char buf[16]; |
163 | size_t sz = strlen(phrase); |
164 | char nl = '\n'; |
165 | char *p, *q; |
166 | |
167 | /* --- Send the request --- * |
168 | * |
169 | * I didn't want to copy it out of the caller's buffer. @writev@ may |
170 | * produce a copy, too, so I didn't do that either. |
171 | */ |
172 | |
173 | dstr_putf(&d, "SET %s -- ", tag); |
174 | write(fd, d.buf, d.len); |
175 | write(fd, phrase, sz); |
176 | write(fd, &nl, 1); |
177 | dstr_destroy(&d); |
178 | |
179 | /* --- Pick up the pieces --- */ |
180 | |
181 | again: |
182 | pixie_fdline(fd, buf, sizeof(buf)); |
183 | p = buf; |
184 | if ((q = str_getword(&p)) != 0 && strcmp(q, "INFO") == 0) |
185 | goto again; |
186 | } |
187 | |
069c185c |
188 | /* --- @pixie_cancel@ --- * |
189 | * |
190 | * Arguments: @int fd@ = pixie file descriptor |
191 | * @const char *tag@ = pointer to tag string |
192 | * |
193 | * Returns: --- |
194 | * |
195 | * Use: Cancels a passphrase if it turns out to be bogus. |
196 | */ |
197 | |
198 | void pixie_cancel(int fd, const char *tag) |
199 | { |
200 | dstr d = DSTR_INIT; |
201 | char buf[16]; |
202 | char *p, *q; |
203 | |
204 | /* --- Send the request --- */ |
205 | |
206 | dstr_putf(&d, "FLUSH %s\n", tag); |
207 | write(fd, d.buf, d.len); |
208 | dstr_destroy(&d); |
209 | |
210 | /* --- Sort out the result --- */ |
211 | |
212 | again: |
213 | pixie_fdline(fd, buf, sizeof(buf)); |
214 | p = buf; |
215 | if ((q = str_getword(&p)) != 0 && strcmp(q, "INFO") == 0) |
216 | goto again; |
217 | } |
218 | |
219 | /*----- That's all, folks -------------------------------------------------*/ |