069c185c |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: pixie-client.c,v 1.1 1999/12/22 15:58:41 mdw Exp $ |
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 $ |
33 | * Revision 1.1 1999/12/22 15:58:41 mdw |
34 | * Passphrase pixie support. |
35 | * |
36 | */ |
37 | |
38 | /*----- Header files ------------------------------------------------------*/ |
39 | |
40 | #include <errno.h> |
41 | #include <stdio.h> |
42 | #include <stdlib.h> |
43 | #include <string.h> |
44 | |
45 | #include <sys/types.h> |
46 | #include <unistd.h> |
47 | #include <fcntl.h> |
48 | #include <pwd.h> |
49 | |
50 | #include <sys/socket.h> |
51 | #include <sys/un.h> |
52 | |
53 | #include <mLib/dstr.h> |
54 | #include <mLib/fdflags.h> |
55 | #include <mLib/str.h> |
56 | |
57 | #include "passphrase.h" |
58 | #include "pixie.h" |
59 | |
60 | /*----- Main code ---------------------------------------------------------*/ |
61 | |
62 | /* --- @pixie_open@ --- * |
63 | * |
64 | * Arguments: @const char *sock@ = path to pixie socket |
65 | * |
66 | * Returns: Less than zero if it failed, or file descriptor. |
67 | * |
68 | * Use: Opens a connection to a passphrase pixie. |
69 | */ |
70 | |
71 | int pixie_open(const char *sock) |
72 | { |
73 | struct sockaddr_un *sun; |
74 | size_t sz; |
75 | int fd; |
76 | |
77 | /* --- Open the connection --- */ |
78 | |
79 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
80 | goto fail_0; |
81 | sun = pixie_address(sock, &sz); |
82 | if (connect(fd, (struct sockaddr *)sun, sz)) |
83 | goto fail_1; |
84 | free(sun); |
85 | return (fd); |
86 | |
87 | /* --- Tidy up if things went wrong --- */ |
88 | |
89 | fail_1: |
90 | free(sun); |
91 | close(fd); |
92 | fail_0: |
93 | return (-1); |
94 | } |
95 | |
96 | /* --- @pixie_read@ --- * |
97 | * |
98 | * Arguments: @int fd@ = connection to passphrase pixie |
99 | * @const char *tag@ = pointer to tag string |
100 | * @unsigned mode@ = reading mode |
101 | * @char *buf@ = pointer to destination buffer |
102 | * @size_t sz@ = size of the buffer |
103 | * |
104 | * Returns: Zero if all went well, nonzero if the read fails. |
105 | * |
106 | * Use: Reads a passphrase from the pixie. |
107 | */ |
108 | |
109 | int pixie_read(int fd, const char *tag, unsigned mode, char *buf, size_t sz) |
110 | { |
111 | dstr d = DSTR_INIT; |
112 | char *p, *q; |
113 | |
114 | /* --- Send the request --- */ |
115 | |
116 | dstr_putf(&d, "%s %s\n", mode == PMODE_READ ? "PASS" : "VERIFY", tag); |
117 | write(fd, d.buf, d.len); |
118 | dstr_destroy(&d); |
119 | |
120 | /* --- Sort out the result --- */ |
121 | |
122 | again: |
123 | pixie_fdline(fd, buf, sz); |
124 | p = buf; |
125 | if ((q = str_getword(&p)) == 0) |
126 | return (-1); |
127 | if (strcmp(q, "INFO") == 0) |
128 | goto again; |
129 | else if (strcmp(q, "OK") != 0) |
130 | return (-1); |
131 | |
132 | /* --- Return the final answer --- */ |
133 | |
134 | if (p) |
135 | memmove(buf, p, strlen(p) + 1); |
136 | else |
137 | *buf = 0; |
138 | return (0); |
139 | } |
140 | |
141 | /* --- @pixie_cancel@ --- * |
142 | * |
143 | * Arguments: @int fd@ = pixie file descriptor |
144 | * @const char *tag@ = pointer to tag string |
145 | * |
146 | * Returns: --- |
147 | * |
148 | * Use: Cancels a passphrase if it turns out to be bogus. |
149 | */ |
150 | |
151 | void pixie_cancel(int fd, const char *tag) |
152 | { |
153 | dstr d = DSTR_INIT; |
154 | char buf[16]; |
155 | char *p, *q; |
156 | |
157 | /* --- Send the request --- */ |
158 | |
159 | dstr_putf(&d, "FLUSH %s\n", tag); |
160 | write(fd, d.buf, d.len); |
161 | dstr_destroy(&d); |
162 | |
163 | /* --- Sort out the result --- */ |
164 | |
165 | again: |
166 | pixie_fdline(fd, buf, sizeof(buf)); |
167 | p = buf; |
168 | if ((q = str_getword(&p)) != 0 && strcmp(q, "INFO") == 0) |
169 | goto again; |
170 | } |
171 | |
172 | /*----- That's all, folks -------------------------------------------------*/ |