~ubuntu-branches/ubuntu/saucy/clamav/saucy-backports

« back to all changes in this revision

Viewing changes to clamdscan/client.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-07-15 01:08:10 UTC
  • mfrom: (0.35.47 sid)
  • Revision ID: package-import@ubuntu.com-20140715010810-ru66ek4fun2iseba
Tags: 0.98.4+dfsg-2~ubuntu13.10.1
No-change backport to saucy (LP: #1341962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
#include <sys/uio.h>
53
53
#endif
54
54
 
 
55
#include <openssl/ssl.h>
 
56
#include <openssl/err.h>
 
57
#include "libclamav/crypto.h"
 
58
 
55
59
#include "shared/optparser.h"
56
60
#include "shared/output.h"
57
61
#include "shared/misc.h"
64
68
#include "client.h"
65
69
#include "proto.h"
66
70
 
67
 
#ifndef INADDR_LOOPBACK
68
 
#define INADDR_LOOPBACK 0x7f000001
69
 
#endif
70
 
 
71
 
struct sockaddr *mainsa = NULL;
72
 
int mainsasz;
73
71
unsigned long int maxstream;
74
72
#ifndef _WIN32
75
 
static struct sockaddr_un nixsock;
 
73
struct sockaddr_un nixsock;
76
74
#endif
77
 
static struct sockaddr_in tcpsock;
78
75
extern struct optstruct *clamdopts;
79
76
 
80
77
/* Inits the communication layer
83
80
    int s, ret;
84
81
    const struct optstruct *opt;
85
82
    static struct sockaddr_in testsock;
 
83
    char *ipaddr, port[10];
 
84
    struct addrinfo hints, *info, *p;
 
85
    int res;
86
86
 
87
87
#ifndef _WIN32
88
88
    if((opt = optget(clamdopts, "LocalSocket"))->enabled) {
89
 
        memset((void *)&nixsock, 0, sizeof(nixsock));
90
 
        nixsock.sun_family = AF_UNIX;
91
 
        strncpy(nixsock.sun_path, opt->strarg, sizeof(nixsock.sun_path));
92
 
        nixsock.sun_path[sizeof(nixsock.sun_path)-1]='\0';
93
 
        mainsa = (struct sockaddr *)&nixsock;
94
 
        mainsasz = sizeof(nixsock);
95
 
        return 0;
 
89
        memset((void *)&nixsock, 0, sizeof(nixsock));
 
90
        nixsock.sun_family = AF_UNIX;
 
91
        strncpy(nixsock.sun_path, opt->strarg, sizeof(nixsock.sun_path));
 
92
        nixsock.sun_path[sizeof(nixsock.sun_path)-1]='\0';
 
93
        return 0;
96
94
    }
97
95
#endif
98
96
    if(!(opt = optget(clamdopts, "TCPSocket"))->enabled)
99
 
        return 0;
100
 
 
101
 
    mainsa = (struct sockaddr *)&tcpsock;
102
 
    mainsasz = sizeof(tcpsock);
103
 
 
104
 
    if (cfg_tcpsock(clamdopts, &tcpsock, INADDR_LOOPBACK) == -1) {
105
 
        logg("!Can't lookup clamd hostname: %s.\n", strerror(errno));
106
 
        mainsa = NULL;
107
 
        return 0;
108
 
    }
109
 
    memcpy((void *)&testsock, (void *)&tcpsock, sizeof(testsock));
110
 
    testsock.sin_port = htons(INADDR_ANY);
111
 
    if((s = socket(testsock.sin_family, SOCK_STREAM, 0)) < 0) {
112
 
      logg("isremote: socket() returning: %s.\n", strerror(errno));
113
 
      mainsa = NULL;
114
 
      return 0;
115
 
    }
116
 
    ret = (bind(s, (struct sockaddr *)&testsock, (socklen_t)sizeof(testsock)) != 0);
117
 
    closesocket(s);
118
 
    return ret;
 
97
        return 0;
 
98
 
 
99
    snprintf(port, sizeof(port), "%lld", optget(clamdopts, "TCPSocket")->numarg);
 
100
 
 
101
    opt = optget(clamdopts, "TCPAddr");
 
102
    while (opt) {
 
103
        ipaddr = NULL;
 
104
        if (opt->strarg)
 
105
            ipaddr = (!strcmp(opt->strarg, "any") ? NULL : opt->strarg);
 
106
 
 
107
        memset(&hints, 0x00, sizeof(struct addrinfo));
 
108
        hints.ai_family = AF_UNSPEC;
 
109
        hints.ai_socktype = SOCK_STREAM;
 
110
        hints.ai_flags = AI_PASSIVE;
 
111
 
 
112
        if ((res = getaddrinfo(ipaddr, port, &hints, &info))) {
 
113
            logg("!Can't lookup clamd hostname: %s\n", gai_strerror(res));
 
114
            opt = opt->nextarg;
 
115
            continue;
 
116
        }
 
117
 
 
118
        for (p = info; p != NULL; p = p->ai_next) {
 
119
            if((s = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
 
120
                logg("isremote: socket() returning: %s.\n", strerror(errno));
 
121
                continue;
 
122
            }
 
123
 
 
124
            switch (p->ai_family) {
 
125
            case AF_INET:
 
126
                ((struct sockaddr_in *)(p->ai_addr))->sin_port = htons(INADDR_ANY);
 
127
                break;
 
128
            case AF_INET6:
 
129
                ((struct sockaddr_in6 *)(p->ai_addr))->sin6_port = htons(INADDR_ANY);
 
130
                break;
 
131
            default:
 
132
                break;
 
133
            }
 
134
 
 
135
            ret = bind(s, p->ai_addr, p->ai_addrlen);
 
136
            if (ret) {
 
137
                if (errno == EADDRINUSE) {
 
138
                    /* 
 
139
                     * If we can't bind, then either we're attempting to listen on an IP that isn't
 
140
                     * ours or that clamd is already listening on.
 
141
                     */
 
142
                    closesocket(s);
 
143
                    freeaddrinfo(info);
 
144
                    return 0;
 
145
                }
 
146
 
 
147
                closesocket(s);
 
148
                freeaddrinfo(info);
 
149
                return 1;
 
150
            }
 
151
 
 
152
            closesocket(s);
 
153
        }
 
154
 
 
155
        freeaddrinfo(info);
 
156
 
 
157
        opt = opt->nextarg;
 
158
    }
 
159
 
 
160
    return 0;
119
161
}
120
162
 
121
163
 
174
216
        struct RCVLN rcv;
175
217
 
176
218
    isremote(opts);
177
 
    if(!mainsa) return 2;
178
219
    if((sockd = dconnect()) < 0) return 2;
179
220
    recvlninit(&rcv, sockd);
180
221
 
202
243
        struct RCVLN rcv;
203
244
 
204
245
    isremote(opts);
205
 
    if(!mainsa) return 2;
206
246
    if((sockd = dconnect()) < 0) return 2;
207
247
    recvlninit(&rcv, sockd);
208
248
 
249
289
        flags |= CLI_FTW_FOLLOW_FILE_SYMLINK;
250
290
    flags |= CLI_FTW_TRIM_SLASHES;
251
291
 
252
 
    if(!mainsa) {
253
 
        logg("!Clamd is not configured properly.\n");
254
 
        return 2;
255
 
    }
256
 
 
257
292
    *infected = 0;
258
293
 
259
294
    if(scandash) {