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

« back to all changes in this revision

Viewing changes to clamdscan/proto.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:
22
22
#include "clamav-config.h"
23
23
#endif
24
24
 
 
25
#if defined(C_SOLARIS)
 
26
#define __EXTENSIONS__
 
27
#endif
 
28
 
25
29
/* must be first because it may define _XOPEN_SOURCE */
26
30
#include "shared/fdpassing.h"
27
31
#include <stdio.h>
41
45
#ifndef _WIN32
42
46
#include <arpa/inet.h>
43
47
#include <sys/socket.h>
 
48
#include <netdb.h>
44
49
#endif
45
50
 
 
51
#include <openssl/ssl.h>
 
52
#include <openssl/err.h>
 
53
#include "libclamav/crypto.h"
 
54
 
46
55
#include "libclamav/others.h"
47
56
#include "shared/actions.h"
48
57
#include "shared/output.h"
52
61
#include "proto.h"
53
62
#include "client.h"
54
63
 
55
 
extern struct sockaddr *mainsa;
56
 
extern int mainsasz;
57
64
extern unsigned long int maxstream;
58
65
int printinfected;
59
66
extern struct optstruct *clamdopts;
 
67
#ifndef _WIN32
 
68
extern struct sockaddr_un nixsock;
 
69
#endif
60
70
 
61
71
static const char *scancmd[] = { "CONTSCAN", "MULTISCAN", "INSTREAM", "FILDES", "ALLMATCHSCAN" };
62
72
 
63
73
/* Connects to clamd 
64
74
 * Returns a FD or -1 on error */
65
75
int dconnect() {
66
 
    int sockd;
67
 
 
68
 
    if((sockd = socket(mainsa->sa_family, SOCK_STREAM, 0)) < 0) {
69
 
        logg("!Can't create the socket: %s\n", strerror(errno));
70
 
        return -1;
71
 
    }
72
 
 
73
 
    if(connect(sockd, (struct sockaddr *)mainsa, mainsasz) < 0) {
74
 
        closesocket(sockd);
75
 
        logg("!Can't connect to clamd: %s\n", strerror(errno));
76
 
        return -1;
77
 
    }
78
 
    return sockd;
 
76
    int sockd, res;
 
77
    const struct optstruct *opt;
 
78
    struct addrinfo hints, *info, *p;
 
79
    char port[10];
 
80
    char *ipaddr;
 
81
 
 
82
#ifndef _WIN32
 
83
    opt = optget(clamdopts, "LocalSocket");
 
84
    if (opt->enabled) {
 
85
        if ((sockd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0) {
 
86
            if (connect(sockd, (struct sockaddr *)&nixsock, sizeof(nixsock)) == 0)
 
87
                return sockd;
 
88
            else
 
89
                close(sockd);
 
90
        }
 
91
    }
 
92
#endif
 
93
 
 
94
    snprintf(port, sizeof(port), "%lld", optget(clamdopts, "TCPSocket")->numarg);
 
95
 
 
96
    opt = optget(clamdopts, "TCPAddr");
 
97
    while (opt) {
 
98
        ipaddr = NULL;
 
99
        if (opt->strarg)
 
100
            ipaddr = (!strcmp(opt->strarg, "any") ? NULL : opt->strarg);
 
101
 
 
102
        memset(&hints, 0x00, sizeof(struct addrinfo));
 
103
        hints.ai_family = AF_UNSPEC;
 
104
        hints.ai_socktype = SOCK_STREAM;
 
105
        hints.ai_flags = AI_PASSIVE;
 
106
 
 
107
        if ((res = getaddrinfo(ipaddr, port, &hints, &info))) {
 
108
            logg("!Could not lookup %s: %s\n", opt->strarg, gai_strerror(res));
 
109
            opt = opt->nextarg;
 
110
            continue;
 
111
        }
 
112
 
 
113
        for (p = info; p != NULL; p = p->ai_next) {
 
114
            if((sockd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
 
115
                logg("!Can't create the socket: %s\n", strerror(errno));
 
116
                continue;
 
117
            }
 
118
 
 
119
            if(connect(sockd, p->ai_addr, p->ai_addrlen) < 0) {
 
120
                logg("!Could not connect to clamd on %s: %s\n", opt->strarg, strerror(errno));
 
121
                closesocket(sockd);
 
122
                continue;
 
123
            }
 
124
 
 
125
            freeaddrinfo(info);
 
126
            return sockd;
 
127
        }
 
128
 
 
129
        freeaddrinfo(info);
 
130
 
 
131
        opt = opt->nextarg;
 
132
    }
 
133
 
 
134
    return -1;
79
135
}
80
136
 
81
137
/* Issues an INSTREAM command to clamd and streams the given file