1 /* Copyright 1999 Peter Schlaile.
2 * Copyright 1999-2002,2006,2007,2009 Alain Knaff.
3 * This file is part of mtools.
5 * Mtools is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * Mtools is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
18 * Small install-test utility to check if a floppyd-server is running on the
25 * udbz@rz.uni-karlsruhe.de
29 #include "sysincludes.h"
34 #include "partition.h"
35 #include "floppyd_io.h"
38 #include <sys/socket.h>
39 #include <arpa/inet.h>
42 /* ######################################################################## */
44 typedef unsigned char Byte;
45 typedef unsigned long Dword;
47 const char* AuthErrors[] = {
49 "Auth failed: Packet oversized!",
50 "Auth failed: X-Cookie doesn't match!",
51 "Auth failed: Wrong transmission protocol version!",
52 "Auth failed: Device locked!"
55 #include "byte_dword.h"
56 #include "read_dword.h"
58 static int write_dword(int handle, Dword parm)
62 dword2byte(parm, val);
64 if(write(handle, val, 4) < 4)
70 /* ######################################################################## */
72 static int authenticate_to_floppyd(char fullauth, int sock, char *display,
77 const char *command[] = { "xauth", "xauth", "extract", "-", 0, 0 };
85 filelen=strlen(display);
88 xcookie = (char *) safe_malloc(filelen+4);
89 filelen = safePopenOut(command, xcookie+4, filelen);
91 return AUTH_AUTHFAILED;
94 dword2byte(protoversion,buf+4);
95 if(write(sock, buf, 8) < 8)
98 bytesRead = read_dword(sock);
100 if (bytesRead != 4 && bytesRead != 12) {
101 return AUTH_WRONGVERSION;
105 errcode = read_dword(sock);
107 if (errcode != AUTH_SUCCESS) {
113 protoversion = read_dword(sock);
117 fprintf(stderr, "Protocol Version=%d\n", protoversion);
120 dword2byte(filelen, (Byte *) xcookie);
121 if(write(sock, xcookie, filelen+4) < filelen+4)
122 return AUTH_IO_ERROR;
124 if (read_dword(sock) != 4) {
125 return AUTH_PACKETOVERSIZE;
128 errcode = read_dword(sock);
136 /* ######################################################################## */
138 static int get_host_and_port(const char* name, char** hostname, char **display,
141 char* newname = strdup(name);
146 while (*p != '/' && *p) p++;
153 *port = FLOPPYD_DEFAULT_PORT;
156 *display = strdup(newname);
159 while (*p != ':' && *p) p++;
164 *port += atoi(p); /* add display number to the port */
166 if (!*newname || strcmp(newname, "unix") == 0) {
168 newname = strdup("localhost");
176 * * Return the IP address of the specified host.
178 static IPaddr_t getipaddress(char *ipaddr)
181 struct hostent *host;
184 if (((ip = inet_addr(ipaddr)) == INADDR_NONE) &&
185 (strcmp(ipaddr, "255.255.255.255") != 0)) {
187 if ((host = gethostbyname(ipaddr)) != NULL) {
188 memcpy(&ip, host->h_addr, sizeof(ip));
195 fprintf(stderr, "IP lookup %s -> 0x%08lx\n", ipaddr, ip);
202 * * Connect to the floppyd server.
204 static int connect_to_server(IPaddr_t ip, short port)
207 struct sockaddr_in addr;
213 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
218 * Set the address to connect to.
221 addr.sin_family = AF_INET;
222 addr.sin_port = htons(port);
223 addr.sin_addr.s_addr = ip;
226 * Connect our socket to the above address.
228 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
233 * Set the keepalive socket option to on.
237 setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE,
238 (char *)&on, sizeof(on));
245 int main (int argc, char** argv)
256 Byte opcode = OP_CLOSE;
259 puts("Usage: floppyd_installtest [-f] Connect-String\n"
260 "-f\tDo full X-Cookie-Authentication");
265 if (strcmp(name, "-f") == 0) {
270 rval = get_host_and_port(name, &hostname, &display, &port);
272 if (!rval) return -1;
274 sock = connect_to_server(getipaddress(hostname), port);
278 "Can't connect to floppyd server on %s, port %i!\n",
283 protoversion = FLOPPYD_PROTOCOL_VERSION;
285 reply = authenticate_to_floppyd(fullauth, sock, display,
287 if(protoversion == FLOPPYD_PROTOCOL_VERSION_OLD)
289 if(reply == AUTH_WRONGVERSION) {
290 /* fall back on old version */
291 protoversion = FLOPPYD_PROTOCOL_VERSION_OLD;
299 "Connection to floppyd failed:\n"
300 "%s\n", AuthErrors[reply]);
307 if(write_dword(sock, 1) < 0) {
309 "Short write to floppyd:\n"
310 "%s\n", strerror(errno));
313 if(write(sock, &opcode, 1) < 0) {
315 "Short write to floppyd:\n"
316 "%s\n", strerror(errno));