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 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
42 #include <sys/socket.h>
45 #include <mLib/dstr.h>
46 #include <mLib/fdflags.h>
49 #include "passphrase.h"
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @pixie_open@ --- *
56 * Arguments: @const char *sock@ = path to pixie socket
58 * Returns: Less than zero if it failed, or file descriptor.
60 * Use: Opens a connection to a passphrase pixie.
63 int pixie_open(const char *sock
)
65 struct sockaddr_un
*sun
;
69 /* --- Open the connection --- */
71 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
73 sun
= pixie_address(sock
, &sz
);
74 if (connect(fd
, (struct sockaddr
*)sun
, sz
))
79 /* --- Tidy up if things went wrong --- */
88 /* --- @pixie_read@ --- *
90 * Arguments: @int fd@ = connection to passphrase pixie
91 * @const char *tag@ = pointer to tag string
92 * @unsigned mode@ = reading mode
93 * @char *buf@ = pointer to destination buffer
94 * @size_t sz@ = size of the buffer
96 * Returns: Zero if all went well, @-1@ if the read fails, @+1@ to
97 * request the passphrase from the user.
99 * Use: Reads a passphrase from the pixie.
102 int pixie_read(int fd
, const char *tag
, unsigned mode
, char *buf
, size_t sz
)
107 /* --- Send the request --- */
109 dstr_putf(&d
, "%s %s\n", mode
== PMODE_READ ?
"PASS" : "VERIFY", tag
);
110 write(fd
, d
.buf
, d
.len
);
113 /* --- Sort out the result --- */
116 pixie_fdline(fd
, buf
, sz
);
118 if ((q
= str_getword(&p
)) == 0)
120 if (strcmp(q
, "INFO") == 0)
122 else if (strcmp(q
, "MISSING") == 0)
124 else if (strcmp(q
, "OK") != 0)
127 /* --- Return the final answer --- */
130 memmove(buf
, p
, strlen(p
) + 1);
136 /* --- @pixie_set@ --- *
138 * Arguments: @int fd@ = pixie file descriptor
139 * @const char *tag@ = pointer to tag string
140 * @const char *phrase@ = pointer to passphrase string
144 * Use: Sends a passphrase to the passphrase pixie.
147 void pixie_set(int fd
, const char *tag
, const char *phrase
)
151 size_t sz
= strlen(phrase
);
155 /* --- Send the request --- *
157 * I didn't want to copy it out of the caller's buffer. @writev@ may
158 * produce a copy, too, so I didn't do that either.
161 dstr_putf(&d
, "SET %s -- ", tag
);
162 write(fd
, d
.buf
, d
.len
);
163 write(fd
, phrase
, sz
);
167 /* --- Pick up the pieces --- */
170 pixie_fdline(fd
, buf
, sizeof(buf
));
172 if ((q
= str_getword(&p
)) != 0 && strcmp(q
, "INFO") == 0)
176 /* --- @pixie_cancel@ --- *
178 * Arguments: @int fd@ = pixie file descriptor
179 * @const char *tag@ = pointer to tag string
183 * Use: Cancels a passphrase if it turns out to be bogus.
186 void pixie_cancel(int fd
, const char *tag
)
192 /* --- Send the request --- */
194 dstr_putf(&d
, "FLUSH %s\n", tag
);
195 write(fd
, d
.buf
, d
.len
);
198 /* --- Sort out the result --- */
201 pixie_fdline(fd
, buf
, sizeof(buf
));
203 if ((q
= str_getword(&p
)) != 0 && strcmp(q
, "INFO") == 0)
207 /*----- That's all, folks -------------------------------------------------*/