chiark / gitweb /
xfoo => mfoo, add comment
[inn-innduct.git] / lib / localopen.c
1 /*  $Id: localopen.c 6155 2003-01-19 19:58:25Z rra $
2 **
3 */
4
5 #include "config.h"
6 #include "clibrary.h"
7 #include <errno.h>
8 #include <sys/socket.h>
9
10 #include "inn/innconf.h"
11 #include "libinn.h"
12 #include "nntp.h"
13 #include "paths.h"
14
15 #if HAVE_UNIX_DOMAIN_SOCKETS
16 # include <sys/un.h>
17 #endif
18
19
20 /*
21 **  Open a connection to the local InterNetNews NNTP server and optionally
22 **  create stdio FILE's for talking to it.  Return -1 on error.
23 */
24 int
25 NNTPlocalopen(FILE **FromServerp, FILE **ToServerp, char *errbuff)
26 {
27 #if     defined(HAVE_UNIX_DOMAIN_SOCKETS)
28     int                 i;
29     int                 j;
30     int                 oerrno;
31     struct sockaddr_un  server;
32     FILE                *F;
33     char                mybuff[NNTP_STRLEN + 2];
34     char                *buff;
35
36     buff = errbuff ? errbuff : mybuff;
37     *buff = '\0';
38
39     /* Create a socket. */
40     if ((i = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
41         return -1;
42
43     /* Connect to the server. */
44     memset(&server, 0, sizeof server);
45     server.sun_family = AF_UNIX;
46     strlcpy(server.sun_path, innconf->pathrun, sizeof(server.sun_path));
47     strlcat(server.sun_path, "/", sizeof(server.sun_path));
48     strlcat(server.sun_path, _PATH_NNTPCONNECT, sizeof(server.sun_path));
49     if (connect(i, (struct sockaddr *)&server, SUN_LEN(&server)) < 0) {
50         oerrno = errno;
51         close(i);
52         errno = oerrno;
53         return -1;
54     }
55
56     /* Connected -- now make sure we can post. */
57     if ((F = fdopen(i, "r")) == NULL) {
58         oerrno = errno;
59         close(i);
60         errno = oerrno;
61         return -1;
62     }
63     if (fgets(buff, sizeof mybuff, F) == NULL) {
64         oerrno = errno;
65         fclose(F);
66         errno = oerrno;
67         return -1;
68     }
69     j = atoi(buff);
70     if (j != NNTP_POSTOK_VAL && j != NNTP_NOPOSTOK_VAL) {
71         fclose(F);
72         /* This seems like a reasonable error code to use... */
73         errno = EPERM;
74         return -1;
75     }
76
77     *FromServerp = F;
78     if ((*ToServerp = fdopen(dup(i), "w")) == NULL) {
79         oerrno = errno;
80         fclose(F);
81         errno = oerrno;
82         return -1;
83     }
84     return 0;
85 #else
86     return NNTPconnect("127.0.0.1", innconf->port, FromServerp, ToServerp,
87                        errbuff);
88 #endif  /* defined(HAVE_UNIX_DOMAIN_SOCKETS) */
89 }