3 * Common code for Pixie client and server (Unix-specific)
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
36 #include <sys/types.h>
42 #include <sys/socket.h>
45 #include <mLib/alloc.h>
46 #include <mLib/dstr.h>
51 /*----- Main code ---------------------------------------------------------*/
53 /* --- @pixie_address@ --- *
55 * Arguments: @const char *sock@ = pointer to socket name
56 * @size_t *psz@ = where to write the address size
58 * Returns: Pointer to filled-in Unix-domain socket address.
60 * Use: Returns a Unix-domain socket address to use to find the
64 struct sockaddr_un *pixie_address(const char *sock, size_t *psz)
68 /* --- Get the default socket path if none specified --- */
71 sock = getenv("CATACOMB_PIXIE_SOCKET");
73 sock = "%h/.catacomb/pixie";
75 /* --- Substitute interesting sequences in the path --- */
93 qq = getenv("LOGNAME");
95 struct passwd *pw = getpwuid(getuid());
106 struct passwd *pw = getpwuid(getuid());
124 /* --- Allocate and initialize the socket address --- */
127 struct sockaddr_un *sun;
128 size_t bsz = offsetof(struct sockaddr_un, sun_path);
129 *psz = bsz + d.len + 1;
130 sun = xmalloc(bsz + d.len + 1);
132 sun->sun_family = AF_UNIX;
133 memcpy(sun->sun_path, d.buf, d.len + 1);
139 /* --- @pixie_fdline@ --- *
141 * Arguments: @int fd@ = file descriptor to read from
142 * @char *buf@ = pointer to buffer
143 * @size_t sz@ = size of buffer
147 * Use: Reads a line from a file descriptor. The read is done one
148 * character at a time. If the entire line won't fit, the end
149 * is truncated. The line is null terminated.
152 void pixie_fdline(int fd, char *buf, size_t sz)
155 char *q = p + sz - 1;
159 if (read(fd, &c, 1) < 1)
169 /* --- @pixie_getpass@ --- *
171 * Arguments: @const char *prompt@ = pointer to prompt string
172 * @char *buf@ = pointer to buffer
173 * @size_t sz@ = size of buffer
175 * Returns: Zero if it worked OK, nonzero otherwise.
177 * Use: Reads a passphrase from the terminal or some other requested
181 int pixie_getpass(const char *prompt, char *buf, size_t sz)
183 const char *pfd = getenv("CATACOMB_PASSPHRASE_FD");
189 /* --- See whether a terminal is what's wanted --- */
193 pixie_fdline(fd, buf, sz);
195 if ((fd = open("/dev/tty", O_RDWR)) < 0)
197 if (tcgetattr(fd, &ta) < 0)
200 ta.c_lflag &= ~(ECHO | ISIG);
201 if (tcsetattr(fd, TCSAFLUSH, &ta)) goto fail_1;
202 if (write(fd, prompt, strlen(prompt)) < 0) goto fail_2;
203 pixie_fdline(fd, buf, sz);
204 tcsetattr(fd, TCSAFLUSH, &ota);
205 if (write(fd, &nl, 1) < 0) goto fail_1;
210 /* --- Tidy up if things went wrong --- */
213 tcsetattr(fd, TCSAFLUSH, &ota);
220 /* --- @pixie_open@ --- *
222 * Arguments: @const char *sock@ = path to pixie socket
224 * Returns: Less than zero if it failed, or file descriptor.
226 * Use: Opens a connection to a passphrase pixie.
229 int pixie_open(const char *sock)
231 struct sockaddr_un *sun;
235 /* --- Open the connection --- */
237 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
239 sun = pixie_address(sock, &sz);
240 if (connect(fd, (struct sockaddr *)sun, sz))
245 /* --- Tidy up if things went wrong --- */
254 /* --- @pixie_read@ --- *
256 * Arguments: @int fd@ = connection to passphrase pixie
257 * @const char *tag@ = pointer to tag string
258 * @unsigned mode@ = reading mode
259 * @char *buf@ = pointer to destination buffer
260 * @size_t sz@ = size of the buffer
262 * Returns: Zero if all went well, @-1@ if the read fails, @+1@ to
263 * request the passphrase from the user.
265 * Use: Reads a passphrase from the pixie.
268 int pixie_read(int fd, const char *tag, unsigned mode, char *buf, size_t sz)
273 /* --- Send the request --- */
275 dstr_putf(&d, "%s %s\n", mode == PMODE_READ ? "PASS" : "VERIFY", tag);
276 if (write(fd, d.buf, d.len) < 0) return (-1);
279 /* --- Sort out the result --- */
282 pixie_fdline(fd, buf, sz);
284 if ((q = str_getword(&p)) == 0)
286 if (strcmp(q, "INFO") == 0)
288 else if (strcmp(q, "MISSING") == 0)
290 else if (strcmp(q, "OK") != 0)
293 /* --- Return the final answer --- */
296 memmove(buf, p, strlen(p) + 1);
302 /* --- @pixie_set@ --- *
304 * Arguments: @int fd@ = pixie file descriptor
305 * @const char *tag@ = pointer to tag string
306 * @const char *phrase@ = pointer to passphrase string
310 * Use: Sends a passphrase to the passphrase pixie.
313 void pixie_set(int fd, const char *tag, const char *phrase)
317 size_t sz = strlen(phrase);
321 /* --- Send the request --- *
323 * I didn't want to copy it out of the caller's buffer. @writev@ may
324 * produce a copy, too, so I didn't do that either.
327 dstr_putf(&d, "SET %s -- ", tag);
328 if (write(fd, d.buf, d.len) < 0 ||
329 write(fd, phrase, sz) < 0 ||
330 write(fd, &nl, 1) < 0)
334 /* --- Pick up the pieces --- */
337 pixie_fdline(fd, buf, sizeof(buf));
339 if ((q = str_getword(&p)) != 0 && strcmp(q, "INFO") == 0)
343 /* --- @pixie_cancel@ --- *
345 * Arguments: @int fd@ = pixie file descriptor
346 * @const char *tag@ = pointer to tag string
350 * Use: Cancels a passphrase if it turns out to be bogus.
353 void pixie_cancel(int fd, const char *tag)
359 /* --- Send the request --- */
361 dstr_putf(&d, "FLUSH %s\n", tag);
362 if (write(fd, d.buf, d.len) < 0) return;
365 /* --- Sort out the result --- */
368 pixie_fdline(fd, buf, sizeof(buf));
370 if ((q = str_getword(&p)) != 0 && strcmp(q, "INFO") == 0)
374 /*----- That's all, folks -------------------------------------------------*/