3 * $Id: pixie-common.c,v 1.2 2004/04/08 01:36:15 mdw Exp $
5 * Common code for Pixie client and server (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 ------------------------------------------------------*/
38 #include <sys/types.h>
44 #include <sys/socket.h>
47 #include <mLib/alloc.h>
48 #include <mLib/dstr.h>
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @pixie_address@ --- *
56 * Arguments: @const char *sock@ = pointer to socket name
57 * @size_t *psz@ = where to write the address size
59 * Returns: Pointer to filled-in Unix-domain socket address.
61 * Use: Returns a Unix-domain socket address to use to find the
65 struct sockaddr_un
*pixie_address(const char *sock
, size_t *psz
)
69 /* --- Get the default socket path if none specified --- */
72 sock
= getenv("CATACOMB_PIXIE_SOCKET");
74 sock
= "%h/.catacomb/pixie";
76 /* --- Substitute interesting sequences in the path --- */
94 qq
= getenv("LOGNAME");
96 struct passwd
*pw
= getpwuid(getuid());
107 struct passwd
*pw
= getpwuid(getuid());
125 /* --- Allocate and initialize the socket address --- */
128 struct sockaddr_un
*sun
;
129 size_t bsz
= offsetof(struct sockaddr_un
, sun_path
);
130 *psz
= bsz
+ d
.len
+ 1;
131 sun
= xmalloc(bsz
+ d
.len
+ 1);
133 sun
->sun_family
= AF_UNIX
;
134 memcpy(sun
->sun_path
, d
.buf
, d
.len
+ 1);
140 /* --- @pixie_fdline@ --- *
142 * Arguments: @int fd@ = file descriptor to read from
143 * @char *buf@ = pointer to buffer
144 * @size_t sz@ = size of buffer
148 * Use: Reads a line from a file descriptor. The read is done one
149 * character at a time. If the entire line won't fit, the end
150 * is truncated. The line is null terminated.
153 void pixie_fdline(int fd
, char *buf
, size_t sz
)
156 char *q
= p
+ sz
- 1;
160 if (read(fd
, &c
, 1) < 1)
170 /* --- @pixie_getpass@ --- *
172 * Arguments: @const char *prompt@ = pointer to prompt string
173 * @char *buf@ = pointer to buffer
174 * @size_t sz@ = size of buffer
176 * Returns: Zero if it worked OK, nonzero otherwise.
178 * Use: Reads a passphrase from the terminal or some other requested
182 int pixie_getpass(const char *prompt
, char *buf
, size_t sz
)
184 const char *pfd
= getenv("CATACOMB_PASSPHRASE_FD");
187 /* --- See whether a terminal is what's wanted --- */
191 pixie_fdline(fd
, buf
, sz
);
197 if ((fd
= open("/dev/tty", O_RDWR
)) < 0)
199 if (tcgetattr(fd
, &ta
) < 0)
202 ta
.c_lflag
&= ~(ECHO
| ISIG
);
203 if (tcsetattr(fd
, TCSAFLUSH
, &ta
))
205 write(fd
, prompt
, strlen(prompt
));
206 pixie_fdline(fd
, buf
, sz
);
207 tcsetattr(fd
, TCSAFLUSH
, &ota
);
213 /* --- Tidy up if things went wrong --- */
222 /*----- That's all, folks -------------------------------------------------*/