root / auth_remote.c

View | Annotate | Download

1 1.1 tommy
#include <stdio.h>
2 1.1 tommy
#include <string.h>
3 1.1 tommy
#include <syslog.h>
4 1.1 tommy
5 1.1 tommy
#include "aconfig.h"
6 1.1 tommy
#include "nntpd.h"
7 1.1 tommy
#include "aprotos.h"
8 1.1 tommy
9 1.1 tommy
#define _MAX_LINEBUF 256
10 1.1 tommy
11 1.12 mjo
int connect_to_server (char *server, int port, int timeout)
12 1.9 tommy
{
13 1.9 tommy
        int sock;
14 1.9 tommy
15 1.12 mjo
        if ((sock = connect_socket (server, port)) == -1)
16 1.9 tommy
                return -1;
17 1.12 mjo
18 1.12 mjo
        char response[cfg.BufSize];
19 1.12 mjo
        if (handshake_nntp (sock, response, timeout) != NULL) {
20 1.12 mjo
                close (sock);
21 1.9 tommy
                return -1;
22 1.9 tommy
        }
23 1.9 tommy
24 1.9 tommy
        return sock;
25 1.9 tommy
}
26 1.9 tommy
27 1.11 tommy
PROTO void auth_remote(AUTHRESULT *authres, CONFIG *cf, char *args)
28 1.9 tommy
{
29 1.9 tommy
        char tmp[_MAX_LINEBUF];
30 1.9 tommy
        char server[128];
31 1.9 tommy
        int port;
32 1.9 tommy
        int sock;
33 1.9 tommy
34 1.9 tommy
        if ( sscanf(args, "%128[^:]:%d", server, &port) != 2 )
35 1.9 tommy
        {
36 1.9 tommy
                syslog(LOG_ERR, "auth_remote: Wrong argument syntax \"%s\"", args);
37 1.9 tommy
                authres->message = strdup(MSG_AUTH_ERR);
38 1.9 tommy
                return;
39 1.9 tommy
        }
40 1.9 tommy
41 1.9 tommy
        if ( (sock=connect_to_server(server, port, cf->ServerReadTimeout)) == -1 )
42 1.9 tommy
        {
43 1.9 tommy
                syslog(LOG_ERR, "auth_remote: Error connecting to authentication server");
44 1.9 tommy
                authres->message = strdup(MSG_AUTH_ERR);
45 1.9 tommy
                return;
46 1.9 tommy
        }
47 1.9 tommy
48 1.9 tommy
        sprintf(tmp, "AUTHINFO USER %s\r\n", authres->username_s);
49 1.9 tommy
        write_socket(sock, tmp, strlen(tmp), cf->ServerReadTimeout);
50 1.9 tommy
        if ( read_socket(sock, tmp, _MAX_LINEBUF, cf->ServerReadTimeout) <= 0 )
51 1.9 tommy
        {
52 1.9 tommy
                authres->message = strdup(MSG_AUTH_ERR);
53 1.9 tommy
                return;
54 1.9 tommy
        }
55 1.9 tommy
56 1.9 tommy
        sprintf(tmp, "AUTHINFO PASS %s\r\n", authres->password);
57 1.9 tommy
        write_socket(sock, tmp, strlen(tmp), cf->ServerReadTimeout);
58 1.9 tommy
        if ( read_socket(sock, tmp, _MAX_LINEBUF, cf->ServerReadTimeout) <= 0 )
59 1.9 tommy
        {
60 1.9 tommy
                authres->message = strdup(MSG_AUTH_ERR);
61 1.9 tommy
                return;
62 1.9 tommy
        }
63 1.9 tommy
64 1.9 tommy
        if ( atoi(tmp) == 281 )
65 1.9 tommy
        {
66 1.9 tommy
                authres->authenticated = true;
67 1.9 tommy
                authres->message = strdup(MSG_AUTH_OK);
68 1.9 tommy
                return;
69 1.9 tommy
        }
70 1.9 tommy
71 1.9 tommy
        authres->message = strdup(MSG_AUTH_REJ);
72 1.9 tommy
        return;
73 1.1 tommy
}