3 * $Id: pixie-client.c,v 1.2 2000/06/17 11:49:37 mdw Exp $
5 * Simple passphrase pixie client (Unix-specific)
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: pixie-client.c,v $
33 * Revision 1.2 2000/06/17 11:49:37 mdw
34 * New pixie protocol allowing application to request passphrases and send
37 * Revision 1.1 1999/12/22 15:58:41 mdw
38 * Passphrase pixie support.
42 /*----- Header files ------------------------------------------------------*/
49 #include <sys/types.h>
54 #include <sys/socket.h>
57 #include <mLib/dstr.h>
58 #include <mLib/fdflags.h>
61 #include "passphrase.h"
64 /*----- Main code ---------------------------------------------------------*/
66 /* --- @pixie_open@ --- *
68 * Arguments: @const char *sock@ = path to pixie socket
70 * Returns: Less than zero if it failed, or file descriptor.
72 * Use: Opens a connection to a passphrase pixie.
75 int pixie_open(const char *sock
)
77 struct sockaddr_un
*sun
;
81 /* --- Open the connection --- */
83 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
85 sun
= pixie_address(sock
, &sz
);
86 if (connect(fd
, (struct sockaddr
*)sun
, sz
))
91 /* --- Tidy up if things went wrong --- */
100 /* --- @pixie_read@ --- *
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
108 * Returns: Zero if all went well, @-1@ if the read fails, @+1@ to
109 * request the passphrase from the user.
111 * Use: Reads a passphrase from the pixie.
114 int pixie_read(int fd
, const char *tag
, unsigned mode
, char *buf
, size_t sz
)
119 /* --- Send the request --- */
121 dstr_putf(&d
, "%s %s\n", mode
== PMODE_READ ?
"PASS" : "VERIFY", tag
);
122 write(fd
, d
.buf
, d
.len
);
125 /* --- Sort out the result --- */
128 pixie_fdline(fd
, buf
, sz
);
130 if ((q
= str_getword(&p
)) == 0)
132 if (strcmp(q
, "INFO") == 0)
134 else if (strcmp(q
, "MISSING") == 0)
136 else if (strcmp(q
, "OK") != 0)
139 /* --- Return the final answer --- */
142 memmove(buf
, p
, strlen(p
) + 1);
148 /* --- @pixie_set@ --- *
150 * Arguments: @int fd@ = pixie file descriptor
151 * @const char *tag@ = pointer to tag string
152 * @const char *phrase@ = pointer to passphrase string
156 * Use: Sends a passphrase to the passphrase pixie.
159 void pixie_set(int fd
, const char *tag
, const char *phrase
)
163 size_t sz
= strlen(phrase
);
167 /* --- Send the request --- *
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.
173 dstr_putf(&d
, "SET %s -- ", tag
);
174 write(fd
, d
.buf
, d
.len
);
175 write(fd
, phrase
, sz
);
179 /* --- Pick up the pieces --- */
182 pixie_fdline(fd
, buf
, sizeof(buf
));
184 if ((q
= str_getword(&p
)) != 0 && strcmp(q
, "INFO") == 0)
188 /* --- @pixie_cancel@ --- *
190 * Arguments: @int fd@ = pixie file descriptor
191 * @const char *tag@ = pointer to tag string
195 * Use: Cancels a passphrase if it turns out to be bogus.
198 void pixie_cancel(int fd
, const char *tag
)
204 /* --- Send the request --- */
206 dstr_putf(&d
, "FLUSH %s\n", tag
);
207 write(fd
, d
.buf
, d
.len
);
210 /* --- Sort out the result --- */
213 pixie_fdline(fd
, buf
, sizeof(buf
));
215 if ((q
= str_getword(&p
)) != 0 && strcmp(q
, "INFO") == 0)
219 /*----- That's all, folks -------------------------------------------------*/