chiark / gitweb /
some todos
[inn-innduct.git] / lib / sendpass.c
1 /*  $Id: sendpass.c 7145 2005-04-10 03:28:01Z rra $
2 **
3 */
4
5 #include "config.h"
6 #include "clibrary.h"
7 #include <ctype.h>
8 #include <errno.h>
9
10 #include "inn/innconf.h"
11 #include "libinn.h"
12 #include "nntp.h"
13 #include "paths.h"
14
15
16 /*
17 **  Send authentication information to an NNTP server.
18 */
19 int NNTPsendpassword(char *server, FILE *FromServer, FILE *ToServer)
20 {
21     FILE                *F;
22     char                *p;
23     char                *path;
24     char                buff[SMBUF];
25     char                input[SMBUF];
26     char                *user;
27     char                *pass;
28     char                *style;
29     int                 oerrno;
30
31     /* Default to innconf->server.  If that's not set either, error out.  Fake
32        errno since some of our callers rely on it. */
33     if (server == NULL)
34         server = innconf->server;
35     if (server == NULL) {
36         errno = EINVAL;
37         return -1;
38     }
39
40     /* Open the password file; coarse check on errno, but good enough. */
41     path = concatpath(innconf->pathetc, _PATH_NNTPPASS);
42     F = fopen(path, "r");
43     free(path);
44     if (F == NULL)
45         return errno == EPERM ? -1 : 0;
46
47     /* Scan the file, skipping blank and comment lines. */
48     while (fgets(buff, sizeof buff, F) != NULL) {
49         if ((p = strchr(buff, '\n')) != NULL)
50             *p = '\0';
51         if (buff[0] == '\0' || buff[0] == '#')
52             continue;
53
54         /* Parse the line. */
55         if ((user = strchr(buff, ':')) == NULL)
56             continue;
57         *user++ = '\0';
58         if ((pass = strchr(user, ':')) == NULL)
59             continue;
60         *pass++ = '\0';
61         if ((style = strchr(pass, ':')) != NULL) {
62             *style++ = '\0';
63             if (strcmp(style, "authinfo") != 0) {
64                 errno = EDOM;
65                 break;
66             }
67         }
68
69         if (strcasecmp(server, buff) != 0)
70             continue;
71
72         if (*user) {
73             /* Send the first part of the command, get a reply. */
74             fprintf(ToServer, "authinfo user %s\r\n", user);
75             if (fflush(ToServer) == EOF || ferror(ToServer))
76                 break;
77             if (fgets(input, sizeof input, FromServer) == NULL
78              || atoi(input) != NNTP_AUTH_NEXT_VAL)
79                 break;
80         }
81
82         if (*pass) {
83             /* Send the second part of the command, get a reply. */
84             fprintf(ToServer, "authinfo pass %s\r\n", pass);
85             if (fflush(ToServer) == EOF || ferror(ToServer))
86                 break;
87             if (fgets(input, sizeof input, FromServer) == NULL
88              || atoi(input) != NNTP_AUTH_OK_VAL)
89                 break;
90         }
91
92         /* Authenticated. */
93         fclose(F);
94         return 0;
95     }
96
97     /* End of file without finding a password, that's okay. */
98     if (feof(F)) {
99         fclose(F);
100         return 0;
101     }
102
103     /* Save errno, close the file, fail. */
104     oerrno = errno;
105     fclose(F);
106     errno = oerrno;
107     return -1;
108 }