Browse code

Reformat code

Tom Judge authored on 2012/08/01 06:30:19
Showing 13 changed files
... ...
@@ -41,119 +41,137 @@
41 41
 #define PACKETSZ 512
42 42
 #endif
43 43
 
44
-char *dnsquery(const char *domain, int qtype, unsigned int *ttl)
44
+char *
45
+dnsquery (const char *domain, int qtype, unsigned int *ttl)
45 46
 {
46
-	unsigned char answer[PACKETSZ], *answend, *pt;
47
-	char *txt, host[128];
48
-	int len, type;
49
-	unsigned int cttl, size, txtlen = 0;
50
-
51
-
52
-    if(ttl)
53
-	*ttl = 0;
54
-    if(res_init() < 0) {
55
-	logg("^res_init failed\n");
56
-	return NULL;
47
+    unsigned char answer[PACKETSZ], *answend, *pt;
48
+    char *txt, host[128];
49
+    int len, type;
50
+    unsigned int cttl, size, txtlen = 0;
51
+
52
+
53
+    if (ttl)
54
+        *ttl = 0;
55
+    if (res_init () < 0)
56
+    {
57
+        logg ("^res_init failed\n");
58
+        return NULL;
57 59
     }
58 60
 
59
-    logg("*Querying %s\n", domain);
61
+    logg ("*Querying %s\n", domain);
60 62
 
61
-    memset(answer, 0, PACKETSZ);
62
-    if((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0 || len > PACKETSZ) {
63
+    memset (answer, 0, PACKETSZ);
64
+    if ((len = res_query (domain, C_IN, qtype, answer, PACKETSZ)) < 0
65
+        || len > PACKETSZ)
66
+    {
63 67
 #ifdef FRESHCLAM_DNS_FIX
64
-	/*  The DNS server in the SpeedTouch Alcatel 510 modem can't
65
-	 *  handle a TXT-query, but it can resolve an ANY-query to a
66
-	 *  TXT-record, so we try an ANY-query now.  The thing we try
67
-	 *  to resolve normally only has a TXT-record anyway.  
68
-	 */
69
-	memset(answer, 0, PACKETSZ);
70
-	if(qtype == T_TXT)
71
-	    qtype = T_ANY;
72
-	if((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0) {
73
-	    logg("%cCan't query %s\n", (qtype == T_TXT || qtype == T_ANY) ? '^' : '*', domain);
74
-	    return NULL;
75
-	}
68
+        /*  The DNS server in the SpeedTouch Alcatel 510 modem can't
69
+         *  handle a TXT-query, but it can resolve an ANY-query to a
70
+         *  TXT-record, so we try an ANY-query now.  The thing we try
71
+         *  to resolve normally only has a TXT-record anyway.  
72
+         */
73
+        memset (answer, 0, PACKETSZ);
74
+        if (qtype == T_TXT)
75
+            qtype = T_ANY;
76
+        if ((len = res_query (domain, C_IN, qtype, answer, PACKETSZ)) < 0)
77
+        {
78
+            logg ("%cCan't query %s\n",
79
+                  (qtype == T_TXT || qtype == T_ANY) ? '^' : '*', domain);
80
+            return NULL;
81
+        }
76 82
 #else
77
-	logg("%cCan't query %s\n", (qtype == T_TXT) ? '^' : '*', domain);
78
-	return NULL;
83
+        logg ("%cCan't query %s\n", (qtype == T_TXT) ? '^' : '*', domain);
84
+        return NULL;
79 85
 #endif
80 86
     }
81
-    if(qtype != T_TXT && qtype != T_ANY) {
82
-	if(ttl)
83
-	    *ttl = 2;
84
-	return NULL;
87
+    if (qtype != T_TXT && qtype != T_ANY)
88
+    {
89
+        if (ttl)
90
+            *ttl = 2;
91
+        return NULL;
85 92
     }
86 93
 
87 94
     answend = answer + len;
88
-    pt = answer + sizeof(HEADER);
95
+    pt = answer + sizeof (HEADER);
89 96
 
90
-    if((len = dn_expand(answer, answend, pt, host, sizeof(host))) < 0) {
91
-	logg("^dn_expand failed\n");
92
-	return NULL;
97
+    if ((len = dn_expand (answer, answend, pt, host, sizeof (host))) < 0)
98
+    {
99
+        logg ("^dn_expand failed\n");
100
+        return NULL;
93 101
     }
94 102
 
95 103
     pt += len;
96
-    if(pt > answend-4) {
97
-	logg("^Bad (too short) DNS reply\n");
98
-	return NULL;
104
+    if (pt > answend - 4)
105
+    {
106
+        logg ("^Bad (too short) DNS reply\n");
107
+        return NULL;
99 108
     }
100 109
 
101
-    GETSHORT(type, pt);
102
-    if(type != qtype) {
103
-	logg("^Broken DNS reply.\n");
104
-	return NULL;
110
+    GETSHORT (type, pt);
111
+    if (type != qtype)
112
+    {
113
+        logg ("^Broken DNS reply.\n");
114
+        return NULL;
105 115
     }
106 116
 
107
-    pt += INT16SZ; /* class */
117
+    pt += INT16SZ;              /* class */
108 118
     size = 0;
109
-    do { /* recurse through CNAME rr's */
110
-	pt += size;
111
-    	if((len = dn_expand(answer, answend, pt, host, sizeof(host))) < 0) {
112
-	    logg("^second dn_expand failed\n");
113
-	    return NULL;
114
-	}
115
-	pt += len;
116
-	if(pt > answend-10) {
117
-	    logg("^Bad (too short) DNS reply\n");
118
-	    return NULL;
119
-	}
120
-	GETSHORT(type, pt);
121
-	pt += INT16SZ; /* class */
122
-	GETLONG(cttl, pt);
123
-	GETSHORT(size, pt);
124
-	if(pt + size < answer || pt + size > answend) {
125
-	    logg("^DNS rr overflow\n");
126
-	    return NULL;
127
-	}
128
-    } while(type == T_CNAME);
129
-
130
-    if(type != T_TXT) {
131
-	logg("^Not a TXT record\n");
132
-	return NULL;
119
+    do
120
+    {                           /* recurse through CNAME rr's */
121
+        pt += size;
122
+        if ((len = dn_expand (answer, answend, pt, host, sizeof (host))) < 0)
123
+        {
124
+            logg ("^second dn_expand failed\n");
125
+            return NULL;
126
+        }
127
+        pt += len;
128
+        if (pt > answend - 10)
129
+        {
130
+            logg ("^Bad (too short) DNS reply\n");
131
+            return NULL;
132
+        }
133
+        GETSHORT (type, pt);
134
+        pt += INT16SZ;          /* class */
135
+        GETLONG (cttl, pt);
136
+        GETSHORT (size, pt);
137
+        if (pt + size < answer || pt + size > answend)
138
+        {
139
+            logg ("^DNS rr overflow\n");
140
+            return NULL;
141
+        }
142
+    }
143
+    while (type == T_CNAME);
144
+
145
+    if (type != T_TXT)
146
+    {
147
+        logg ("^Not a TXT record\n");
148
+        return NULL;
133 149
     }
134 150
 
135
-    if(!size || (txtlen = *pt) >= size || !txtlen) {
136
-	logg("^Broken TXT record (txtlen = %d, size = %d)\n", txtlen, size);
137
-	return NULL;
151
+    if (!size || (txtlen = *pt) >= size || !txtlen)
152
+    {
153
+        logg ("^Broken TXT record (txtlen = %d, size = %d)\n", txtlen, size);
154
+        return NULL;
138 155
     }
139 156
 
140
-    if(!(txt = (char *) malloc(txtlen + 1)))
141
-	return NULL;
157
+    if (!(txt = (char *) malloc (txtlen + 1)))
158
+        return NULL;
142 159
 
143
-    memcpy(txt, pt+1, txtlen);
160
+    memcpy (txt, pt + 1, txtlen);
144 161
     txt[txtlen] = 0;
145
-    if(ttl)
146
-	*ttl = cttl;
162
+    if (ttl)
163
+        *ttl = cttl;
147 164
 
148 165
     return txt;
149 166
 }
150 167
 
151 168
 #else
152 169
 
153
-char *dnsquery(const char *domain, int qtype, unsigned int *ttl)
170
+char *
171
+dnsquery (const char *domain, int qtype, unsigned int *ttl)
154 172
 {
155
-    if(ttl)
156
-	*ttl = 1;  /* ttl of 1 combined with a NULL return distinguishes a failed lookup from DNS queries not being available */
173
+    if (ttl)
174
+        *ttl = 1;               /* ttl of 1 combined with a NULL return distinguishes a failed lookup from DNS queries not being available */
157 175
     return NULL;
158 176
 }
159 177
 
... ...
@@ -32,6 +32,6 @@
32 32
 #include <resolv.h>
33 33
 #endif
34 34
 
35
-char *dnsquery(const char *domain, int qtype, unsigned int *ttl);
35
+char *dnsquery (const char *domain, int qtype, unsigned int *ttl);
36 36
 
37 37
 #endif
... ...
@@ -37,44 +37,52 @@
37 37
 
38 38
 int active_children;
39 39
 
40
-void execute( const char *type, const char *text, const struct optstruct *opts )
40
+void
41
+execute (const char *type, const char *text, const struct optstruct *opts)
41 42
 {
42
-	int ret;
43
+    int ret;
43 44
 
44
-    if(!optget(opts, "daemon")->enabled) {
45
-	if(sscanf(text, "EXIT_%d", &ret) == 1) {
46
-	    logg("*%s: EXIT_%d\n", type, ret);
47
-	    exit(ret);
48
-	}
49
-	if(system(text) == -1)
50
-	    logg("%s: system(%s) failed\n", type, text);
45
+    if (!optget (opts, "daemon")->enabled)
46
+    {
47
+        if (sscanf (text, "EXIT_%d", &ret) == 1)
48
+        {
49
+            logg ("*%s: EXIT_%d\n", type, ret);
50
+            exit (ret);
51
+        }
52
+        if (system (text) == -1)
53
+            logg ("%s: system(%s) failed\n", type, text);
51 54
 
52
-	return;
55
+        return;
53 56
     }
54 57
 
55 58
 #ifdef _WIN32
56
-    if(spawnlp(_P_NOWAIT, text, text, NULL) == -1) {
57
-	logg("^%s: couldn't execute \"%s\".\n", type, text);
58
-	return;
59
+    if (spawnlp (_P_NOWAIT, text, text, NULL) == -1)
60
+    {
61
+        logg ("^%s: couldn't execute \"%s\".\n", type, text);
62
+        return;
59 63
     }
60 64
 #else
61
-    if ( active_children<MAX_CHILDREN ) {
62
-	pid_t pid;
63
-	switch( pid=fork() ) {
64
-	case 0:
65
-		if ( -1==system(text) )
66
-		{
67
-		logg("^%s: couldn't execute \"%s\".\n", type, text);
68
-		}
69
-		exit(0);
70
-	case -1:
71
-		logg("^%s::fork() failed, %s.\n", type, strerror(errno));
72
-		break;
73
-	default:
74
-		active_children++;
75
-	}
76
-    } else {
77
-		logg("^%s: already %d processes active.\n", type, active_children);
65
+    if (active_children < MAX_CHILDREN)
66
+    {
67
+        pid_t pid;
68
+        switch (pid = fork ())
69
+        {
70
+        case 0:
71
+            if (-1 == system (text))
72
+            {
73
+                logg ("^%s: couldn't execute \"%s\".\n", type, text);
74
+            }
75
+            exit (0);
76
+        case -1:
77
+            logg ("^%s::fork() failed, %s.\n", type, strerror (errno));
78
+            break;
79
+        default:
80
+            active_children++;
81
+        }
82
+    }
83
+    else
84
+    {
85
+        logg ("^%s: already %d processes active.\n", type, active_children);
78 86
     }
79 87
 #endif
80 88
 }
... ...
@@ -23,6 +23,7 @@
23 23
 
24 24
 #include "shared/optparser.h"
25 25
 
26
-void execute( const char *type, const char *text, const struct optstruct *opts );
26
+void execute (const char *type, const char *text,
27
+              const struct optstruct *opts);
27 28
 
28 29
 #endif
... ...
@@ -68,539 +68,656 @@ char updtmpdir[512], dbdir[512];
68 68
 int sigchld_wait = 1;
69 69
 const char *pidfile = NULL;
70 70
 
71
-static void sighandler(int sig) {
71
+static void
72
+sighandler (int sig)
73
+{
72 74
 
73
-    switch(sig) {
75
+    switch (sig)
76
+    {
74 77
 #ifdef	SIGCHLD
75
-	case SIGCHLD:
76
-	    if (sigchld_wait)
77
-		waitpid(-1, NULL, WNOHANG);
78
-	    active_children--;
79
-	    break;
78
+    case SIGCHLD:
79
+        if (sigchld_wait)
80
+            waitpid (-1, NULL, WNOHANG);
81
+        active_children--;
82
+        break;
80 83
 #endif
81 84
 
82 85
 #ifdef SIGPIPE
83
-	case SIGPIPE:
84
-	    /* no action, app will get EPIPE */
85
-	    break;
86
+    case SIGPIPE:
87
+        /* no action, app will get EPIPE */
88
+        break;
86 89
 #endif
87 90
 
88 91
 #ifdef	SIGALRM
89
-	case SIGALRM:
90
-		terminate = -1;
91
-	    break;
92
+    case SIGALRM:
93
+        terminate = -1;
94
+        break;
92 95
 #endif
93 96
 #ifdef	SIGUSR1
94
-	case SIGUSR1:
95
-		terminate = -1;
96
-	    break;
97
+    case SIGUSR1:
98
+        terminate = -1;
99
+        break;
97 100
 #endif
98 101
 
99 102
 #ifdef	SIGHUP
100
-	case SIGHUP:
101
-	    terminate = -2;
102
-	    break;
103
+    case SIGHUP:
104
+        terminate = -2;
105
+        break;
103 106
 #endif
104 107
 
105
-	default:
106
-	    if(*updtmpdir)
107
-		cli_rmdirs(updtmpdir);
108
-	    if(pidfile)
109
-		unlink(pidfile);
110
-	    logg("Update process terminated\n");
111
-	    exit(2);
108
+    default:
109
+        if (*updtmpdir)
110
+            cli_rmdirs (updtmpdir);
111
+        if (pidfile)
112
+            unlink (pidfile);
113
+        logg ("Update process terminated\n");
114
+        exit (2);
112 115
     }
113 116
 
114 117
     return;
115 118
 }
116 119
 
117
-static void writepid(const char *pidfile)
120
+static void
121
+writepid (const char *pidfile)
118 122
 {
119
-	FILE *fd;
120
-	int old_umask;
121
-    old_umask = umask(0006);
122
-    if((fd = fopen(pidfile, "w")) == NULL) {
123
-	logg("!Can't save PID to file %s: %s\n", pidfile, strerror(errno));
124
-    } else {
125
-	fprintf(fd, "%d", (int) getpid());
126
-	fclose(fd);
123
+    FILE *fd;
124
+    int old_umask;
125
+    old_umask = umask (0006);
126
+    if ((fd = fopen (pidfile, "w")) == NULL)
127
+    {
128
+        logg ("!Can't save PID to file %s: %s\n", pidfile, strerror (errno));
129
+    }
130
+    else
131
+    {
132
+        fprintf (fd, "%d", (int) getpid ());
133
+        fclose (fd);
127 134
     }
128
-    umask(old_umask);
135
+    umask (old_umask);
129 136
 }
130 137
 
131
-static void help(void)
138
+static void
139
+help (void)
132 140
 {
133 141
     mprintf_stdout = 1;
134 142
 
135
-    mprintf("\n");
136
-    mprintf("                   Clam AntiVirus: freshclam  %s\n", get_version());
137
-    printf("           By The ClamAV Team: http://www.clamav.net/team\n");
138
-    printf("           (C) 2007-2009 Sourcefire, Inc. et al.\n\n");
139
-
140
-    mprintf("    --help               -h              show help\n");
141
-    mprintf("    --version            -V              print version number and exit\n");
142
-    mprintf("    --verbose            -v              be verbose\n");
143
-    mprintf("    --debug                              enable debug messages\n");
144
-    mprintf("    --quiet                              only output error messages\n");
145
-    mprintf("    --no-warnings                        don't print and log warnings\n");
146
-    mprintf("    --stdout                             write to stdout instead of stderr\n");
147
-    mprintf("\n");
148
-    mprintf("    --config-file=FILE                   read configuration from FILE.\n");
149
-    mprintf("    --log=FILE           -l FILE         log into FILE\n");
143
+    mprintf ("\n");
144
+    mprintf ("                   Clam AntiVirus: freshclam  %s\n",
145
+             get_version ());
146
+    printf ("           By The ClamAV Team: http://www.clamav.net/team\n");
147
+    printf ("           (C) 2007-2009 Sourcefire, Inc. et al.\n\n");
148
+
149
+    mprintf ("    --help               -h              show help\n");
150
+    mprintf
151
+        ("    --version            -V              print version number and exit\n");
152
+    mprintf ("    --verbose            -v              be verbose\n");
153
+    mprintf
154
+        ("    --debug                              enable debug messages\n");
155
+    mprintf
156
+        ("    --quiet                              only output error messages\n");
157
+    mprintf
158
+        ("    --no-warnings                        don't print and log warnings\n");
159
+    mprintf
160
+        ("    --stdout                             write to stdout instead of stderr\n");
161
+    mprintf ("\n");
162
+    mprintf
163
+        ("    --config-file=FILE                   read configuration from FILE.\n");
164
+    mprintf ("    --log=FILE           -l FILE         log into FILE\n");
150 165
 #ifndef _WIN32
151
-    mprintf("    --daemon             -d              run in daemon mode\n");
152
-    mprintf("    --pid=FILE           -p FILE         save daemon's pid in FILE\n");
153
-    mprintf("    --user=USER          -u USER         run as USER\n");
166
+    mprintf ("    --daemon             -d              run in daemon mode\n");
167
+    mprintf
168
+        ("    --pid=FILE           -p FILE         save daemon's pid in FILE\n");
169
+    mprintf ("    --user=USER          -u USER         run as USER\n");
154 170
 #endif
155
-    mprintf("    --no-dns                             force old non-DNS verification method\n");
156
-    mprintf("    --checks=#n          -c #n           number of checks per day, 1 <= n <= 50\n");
157
-    mprintf("    --datadir=DIRECTORY                  download new databases into DIRECTORY\n");
171
+    mprintf
172
+        ("    --no-dns                             force old non-DNS verification method\n");
173
+    mprintf
174
+        ("    --checks=#n          -c #n           number of checks per day, 1 <= n <= 50\n");
175
+    mprintf
176
+        ("    --datadir=DIRECTORY                  download new databases into DIRECTORY\n");
158 177
 #ifdef BUILD_CLAMD
159
-    mprintf("    --daemon-notify[=/path/clamd.conf]   send RELOAD command to clamd\n");
178
+    mprintf
179
+        ("    --daemon-notify[=/path/clamd.conf]   send RELOAD command to clamd\n");
160 180
 #endif
161
-    mprintf("    --local-address=IP   -a IP           bind to IP for HTTP downloads\n");
162
-    mprintf("    --on-update-execute=COMMAND          execute COMMAND after successful update\n");
163
-    mprintf("    --on-error-execute=COMMAND           execute COMMAND if errors occured\n");
164
-    mprintf("    --on-outdated-execute=COMMAND        execute COMMAND when software is outdated\n");
165
-    mprintf("    --list-mirrors                       print mirrors from mirrors.dat\n");
166
-    mprintf("    --submit-stats[=/path/clamd.conf]    only submit detection statistics\n");
167
-    mprintf("    --update-db=DBNAME                   only update database DBNAME\n");
168
-
169
-    mprintf("\n");
181
+    mprintf
182
+        ("    --local-address=IP   -a IP           bind to IP for HTTP downloads\n");
183
+    mprintf
184
+        ("    --on-update-execute=COMMAND          execute COMMAND after successful update\n");
185
+    mprintf
186
+        ("    --on-error-execute=COMMAND           execute COMMAND if errors occured\n");
187
+    mprintf
188
+        ("    --on-outdated-execute=COMMAND        execute COMMAND when software is outdated\n");
189
+    mprintf
190
+        ("    --list-mirrors                       print mirrors from mirrors.dat\n");
191
+    mprintf
192
+        ("    --submit-stats[=/path/clamd.conf]    only submit detection statistics\n");
193
+    mprintf
194
+        ("    --update-db=DBNAME                   only update database DBNAME\n");
195
+
196
+    mprintf ("\n");
170 197
 }
171 198
 
172
-static int download(const struct optstruct *opts, const char *cfgfile)
199
+static int
200
+download (const struct optstruct *opts, const char *cfgfile)
173 201
 {
174
-	int ret = 0, try = 1, maxattempts = 0;
175
-	const struct optstruct *opt;
202
+    int ret = 0, try = 1, maxattempts = 0;
203
+    const struct optstruct *opt;
176 204
 
177 205
 
178
-    maxattempts = optget(opts, "MaxAttempts")->numarg;
179
-    logg("*Max retries == %d\n", maxattempts);
206
+    maxattempts = optget (opts, "MaxAttempts")->numarg;
207
+    logg ("*Max retries == %d\n", maxattempts);
180 208
 
181
-    if(!(opt = optget(opts, "DatabaseMirror"))->enabled) {
182
-	logg("^You must specify at least one database mirror in %s\n", cfgfile);
183
-	return 56;
184
-    } else {
185
-	while(opt) {
186
-	    ret = downloadmanager(opts, opt->strarg, try);
209
+    if (!(opt = optget (opts, "DatabaseMirror"))->enabled)
210
+    {
211
+        logg ("^You must specify at least one database mirror in %s\n",
212
+              cfgfile);
213
+        return 56;
214
+    }
215
+    else
216
+    {
217
+        while (opt)
218
+        {
219
+            ret = downloadmanager (opts, opt->strarg, try);
187 220
 #ifndef _WIN32
188
-	    alarm(0);
221
+            alarm (0);
189 222
 #endif
190
-	    if(ret == 52 || ret == 54 || ret == 58 || ret == 59) {
191
-		if(try < maxattempts) {
192
-		    logg("Trying again in 5 secs...\n");
193
-		    try++;
194
-		    sleep(5);
195
-		    continue;
196
-		} else {
197
-		    logg("Giving up on %s...\n", opt->strarg);
198
-		    opt = (struct optstruct *) opt->nextarg;
199
-		    if(!opt) {
200
-			logg("Update failed. Your network may be down or none of the mirrors listed in %s is working. Check http://www.clamav.net/support/mirror-problem for possible reasons.\n", cfgfile);
201
-		    }
202
-		}
203
-
204
-	    } else {
205
-		return ret;
206
-	    }
207
-	}
223
+            if (ret == 52 || ret == 54 || ret == 58 || ret == 59)
224
+            {
225
+                if (try < maxattempts)
226
+                {
227
+                    logg ("Trying again in 5 secs...\n");
228
+                    try++;
229
+                    sleep (5);
230
+                    continue;
231
+                }
232
+                else
233
+                {
234
+                    logg ("Giving up on %s...\n", opt->strarg);
235
+                    opt = (struct optstruct *) opt->nextarg;
236
+                    if (!opt)
237
+                    {
238
+                        logg ("Update failed. Your network may be down or none of the mirrors listed in %s is working. Check http://www.clamav.net/support/mirror-problem for possible reasons.\n", cfgfile);
239
+                    }
240
+                }
241
+
242
+            }
243
+            else
244
+            {
245
+                return ret;
246
+            }
247
+        }
208 248
     }
209 249
 
210 250
     return ret;
211 251
 }
212 252
 
213
-static void msg_callback(enum cl_msg severity, const char *fullmsg, const char *msg, void *ctx)
253
+static void
254
+msg_callback (enum cl_msg severity, const char *fullmsg, const char *msg,
255
+              void *ctx)
214 256
 {
215
-    switch (severity) {
216
-	case CL_MSG_ERROR:
217
-	    logg("^[LibClamAV] %s", msg);
218
-	    break;
219
-	case CL_MSG_WARN:
220
-	    logg("~[LibClamAV] %s", msg);
221
-	default:
222
-	    logg("*[LibClamAV] %s", msg);
223
-	    break;
257
+    switch (severity)
258
+    {
259
+    case CL_MSG_ERROR:
260
+        logg ("^[LibClamAV] %s", msg);
261
+        break;
262
+    case CL_MSG_WARN:
263
+        logg ("~[LibClamAV] %s", msg);
264
+    default:
265
+        logg ("*[LibClamAV] %s", msg);
266
+        break;
224 267
     }
225 268
 }
226 269
 
227
-int main(int argc, char **argv)
270
+int
271
+main (int argc, char **argv)
228 272
 {
229
-	int ret = 52, retcl;
230
-	const char *cfgfile, *arg = NULL;
231
-	char *pt;
232
-	struct optstruct *opts;
233
-	const struct optstruct *opt;
273
+    int ret = 52, retcl;
274
+    const char *cfgfile, *arg = NULL;
275
+    char *pt;
276
+    struct optstruct *opts;
277
+    const struct optstruct *opt;
234 278
 #ifndef	_WIN32
235
-	struct sigaction sigact;
236
-	struct sigaction oldact;
279
+    struct sigaction sigact;
280
+    struct sigaction oldact;
237 281
 #endif
238 282
 #ifdef HAVE_PWD_H
239
-	const char *dbowner;
240
-	struct passwd *user;
283
+    const char *dbowner;
284
+    struct passwd *user;
241 285
 #endif
242
-	STATBUF statbuf;
243
-	struct mirdat mdat;
286
+    STATBUF statbuf;
287
+    struct mirdat mdat;
244 288
 
245
-    if(check_flevel())
246
-	exit(40);
289
+    if (check_flevel ())
290
+        exit (40);
247 291
 
248
-    if((retcl = cl_init(CL_INIT_DEFAULT))) {
249
-        mprintf("!Can't initialize libclamav: %s\n", cl_strerror(retcl));
250
-	return 40;
292
+    if ((retcl = cl_init (CL_INIT_DEFAULT)))
293
+    {
294
+        mprintf ("!Can't initialize libclamav: %s\n", cl_strerror (retcl));
295
+        return 40;
251 296
     }
252 297
 
253
-    if((opts = optparse(NULL, argc, argv, 1, OPT_FRESHCLAM, 0, NULL)) == NULL) {
254
-	mprintf("!Can't parse command line options\n");
255
-	return 40;
298
+    if ((opts =
299
+         optparse (NULL, argc, argv, 1, OPT_FRESHCLAM, 0, NULL)) == NULL)
300
+    {
301
+        mprintf ("!Can't parse command line options\n");
302
+        return 40;
256 303
     }
257 304
 
258
-    if(optget(opts, "help")->enabled) {
259
-    	help();
260
-	optfree(opts);
261
-	return 0;
305
+    if (optget (opts, "help")->enabled)
306
+    {
307
+        help ();
308
+        optfree (opts);
309
+        return 0;
262 310
     }
263 311
 
264 312
     /* parse the config file */
265
-    cfgfile = optget(opts, "config-file")->strarg;
266
-    pt = strdup(cfgfile);
267
-    if((opts = optparse(cfgfile, 0, NULL, 1, OPT_FRESHCLAM, 0, opts)) == NULL) {
268
-	fprintf(stderr, "ERROR: Can't open/parse the config file %s\n", pt);
269
-	free(pt);
270
-	return 40;
313
+    cfgfile = optget (opts, "config-file")->strarg;
314
+    pt = strdup (cfgfile);
315
+    if ((opts =
316
+         optparse (cfgfile, 0, NULL, 1, OPT_FRESHCLAM, 0, opts)) == NULL)
317
+    {
318
+        fprintf (stderr, "ERROR: Can't open/parse the config file %s\n", pt);
319
+        free (pt);
320
+        return 40;
271 321
     }
272
-    free(pt);
322
+    free (pt);
273 323
 
274
-    if(optget(opts, "version")->enabled) {
275
-	print_version(optget(opts, "DatabaseDirectory")->strarg);
276
-	optfree(opts);
277
-	return 0;
324
+    if (optget (opts, "version")->enabled)
325
+    {
326
+        print_version (optget (opts, "DatabaseDirectory")->strarg);
327
+        optfree (opts);
328
+        return 0;
278 329
     }
279 330
 
280
-    if(optget(opts, "HTTPProxyPassword")->enabled) {
281
-	if(STAT(cfgfile, &statbuf) == -1) {
282
-	    logg("^Can't stat %s (critical error)\n", cfgfile);
283
-	    optfree(opts);
284
-	    return 56;
285
-	}
331
+    if (optget (opts, "HTTPProxyPassword")->enabled)
332
+    {
333
+        if (STAT (cfgfile, &statbuf) == -1)
334
+        {
335
+            logg ("^Can't stat %s (critical error)\n", cfgfile);
336
+            optfree (opts);
337
+            return 56;
338
+        }
286 339
 
287 340
 #ifndef _WIN32
288
-	if(statbuf.st_mode & (S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)) {
289
-	    logg("^Insecure permissions (for HTTPProxyPassword): %s must have no more than 0700 permissions.\n", cfgfile);
290
-	    optfree(opts);
291
-	    return 56;
292
-	}
341
+        if (statbuf.
342
+            st_mode & (S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH |
343
+                       S_IXOTH))
344
+        {
345
+            logg ("^Insecure permissions (for HTTPProxyPassword): %s must have no more than 0700 permissions.\n", cfgfile);
346
+            optfree (opts);
347
+            return 56;
348
+        }
293 349
 #endif
294 350
     }
295 351
 
296 352
 #ifdef HAVE_PWD_H
297 353
     /* freshclam shouldn't work with root privileges */
298
-    dbowner = optget(opts, "DatabaseOwner")->strarg;
299
-
300
-    if(!geteuid()) {
301
-	if((user = getpwnam(dbowner)) == NULL) {
302
-	    logg("^Can't get information about user %s.\n", dbowner);
303
-	    optfree(opts);
304
-	    return 60;
305
-	}
354
+    dbowner = optget (opts, "DatabaseOwner")->strarg;
355
+
356
+    if (!geteuid ())
357
+    {
358
+        if ((user = getpwnam (dbowner)) == NULL)
359
+        {
360
+            logg ("^Can't get information about user %s.\n", dbowner);
361
+            optfree (opts);
362
+            return 60;
363
+        }
306 364
 
307
-	if(optget(opts, "AllowSupplementaryGroups")->enabled) {
365
+        if (optget (opts, "AllowSupplementaryGroups")->enabled)
366
+        {
308 367
 #ifdef HAVE_INITGROUPS
309
-	    if(initgroups(dbowner, user->pw_gid)) {
310
-		logg("^initgroups() failed.\n");
311
-		optfree(opts);
312
-		return 61;
313
-	    }
368
+            if (initgroups (dbowner, user->pw_gid))
369
+            {
370
+                logg ("^initgroups() failed.\n");
371
+                optfree (opts);
372
+                return 61;
373
+            }
314 374
 #endif
315
-	} else {
375
+        }
376
+        else
377
+        {
316 378
 #ifdef HAVE_SETGROUPS
317
-	    if(setgroups(1, &user->pw_gid)) {
318
-		logg("^setgroups() failed.\n");
319
-		optfree(opts);
320
-		return 61;
321
-	    }
379
+            if (setgroups (1, &user->pw_gid))
380
+            {
381
+                logg ("^setgroups() failed.\n");
382
+                optfree (opts);
383
+                return 61;
384
+            }
322 385
 #endif
323
-	}
324
-
325
-	if(setgid(user->pw_gid)) {
326
-	    logg("^setgid(%d) failed.\n", (int) user->pw_gid);
327
-	    optfree(opts);
328
-	    return 61;
329
-	}
330
-
331
-	if(setuid(user->pw_uid)) {
332
-	    logg("^setuid(%d) failed.\n", (int) user->pw_uid);
333
-	    optfree(opts);
334
-	    return 61;
335
-	}
386
+        }
387
+
388
+        if (setgid (user->pw_gid))
389
+        {
390
+            logg ("^setgid(%d) failed.\n", (int) user->pw_gid);
391
+            optfree (opts);
392
+            return 61;
393
+        }
394
+
395
+        if (setuid (user->pw_uid))
396
+        {
397
+            logg ("^setuid(%d) failed.\n", (int) user->pw_uid);
398
+            optfree (opts);
399
+            return 61;
400
+        }
336 401
     }
337 402
 #endif /* HAVE_PWD_H */
338 403
 
339 404
     /* initialize some important variables */
340 405
 
341
-    if(optget(opts, "Debug")->enabled || optget(opts, "debug")->enabled)
342
-	cl_debug();
406
+    if (optget (opts, "Debug")->enabled || optget (opts, "debug")->enabled)
407
+        cl_debug ();
343 408
 
344
-    if(optget(opts, "verbose")->enabled)
345
-	mprintf_verbose = 1;
409
+    if (optget (opts, "verbose")->enabled)
410
+        mprintf_verbose = 1;
346 411
 
347
-    if(optget(opts, "quiet")->enabled)
348
-	mprintf_quiet = 1;
412
+    if (optget (opts, "quiet")->enabled)
413
+        mprintf_quiet = 1;
349 414
 
350
-    if(optget(opts, "no-warnings")->enabled) {
351
-	mprintf_nowarn = 1;
352
-	logg_nowarn = 1;
415
+    if (optget (opts, "no-warnings")->enabled)
416
+    {
417
+        mprintf_nowarn = 1;
418
+        logg_nowarn = 1;
353 419
     }
354 420
 
355
-    if(optget(opts, "stdout")->enabled)
356
-	mprintf_stdout = 1;
421
+    if (optget (opts, "stdout")->enabled)
422
+        mprintf_stdout = 1;
357 423
 
358 424
     /* initialize logger */
359
-    logg_verbose = mprintf_verbose ? 1 : optget(opts, "LogVerbose")->enabled;
360
-    logg_time = optget(opts, "LogTime")->enabled;
361
-    logg_size = optget(opts, "LogFileMaxSize")->numarg;
362
-
363
-    if((opt = optget(opts, "UpdateLogFile"))->enabled) {
364
-	logg_file = opt->strarg; 
365
-	if(logg("#--------------------------------------\n")) {
366
-	    mprintf("!Problem with internal logger (UpdateLogFile = %s).\n", logg_file);
367
-	    optfree(opts);
368
-	    return 62;
369
-	}
370
-    } else
371
-	logg_file = NULL;
425
+    logg_verbose = mprintf_verbose ? 1 : optget (opts, "LogVerbose")->enabled;
426
+    logg_time = optget (opts, "LogTime")->enabled;
427
+    logg_size = optget (opts, "LogFileMaxSize")->numarg;
428
+
429
+    if ((opt = optget (opts, "UpdateLogFile"))->enabled)
430
+    {
431
+        logg_file = opt->strarg;
432
+        if (logg ("#--------------------------------------\n"))
433
+        {
434
+            mprintf ("!Problem with internal logger (UpdateLogFile = %s).\n",
435
+                     logg_file);
436
+            optfree (opts);
437
+            return 62;
438
+        }
439
+    }
440
+    else
441
+        logg_file = NULL;
372 442
 
373 443
 #if defined(USE_SYSLOG) && !defined(C_AIX)
374
-    if(optget(opts, "LogSyslog")->enabled) {
375
-	    int fac = LOG_LOCAL6;
376
-
377
-	if((opt = optget(opts, "LogFacility"))->enabled) {
378
-	    if((fac = logg_facility(opt->strarg)) == -1) {
379
-		mprintf("!LogFacility: %s: No such facility.\n", opt->strarg);
380
-		optfree(opts);
381
-		return 62;
382
-	    }
383
-	}
384
-
385
-	openlog("freshclam", LOG_PID, fac);
386
-	logg_syslog = 1;
444
+    if (optget (opts, "LogSyslog")->enabled)
445
+    {
446
+        int fac = LOG_LOCAL6;
447
+
448
+        if ((opt = optget (opts, "LogFacility"))->enabled)
449
+        {
450
+            if ((fac = logg_facility (opt->strarg)) == -1)
451
+            {
452
+                mprintf ("!LogFacility: %s: No such facility.\n",
453
+                         opt->strarg);
454
+                optfree (opts);
455
+                return 62;
456
+            }
457
+        }
458
+
459
+        openlog ("freshclam", LOG_PID, fac);
460
+        logg_syslog = 1;
387 461
     }
388 462
 #endif
389 463
 
390
-    cl_set_clcb_msg(msg_callback);
464
+    cl_set_clcb_msg (msg_callback);
391 465
     /* change the current working directory */
392
-    if(chdir(optget(opts, "DatabaseDirectory")->strarg)) {
393
-	logg("!Can't change dir to %s\n", optget(opts, "DatabaseDirectory")->strarg);
394
-	optfree(opts);
395
-	return 50;
396
-    } else {
397
-	if(!getcwd(dbdir, sizeof(dbdir))) {
398
-	    logg("!getcwd() failed\n");
399
-	    optfree(opts);
400
-	    return 50;
401
-	}
402
-	logg("*Current working dir is %s\n", dbdir);
466
+    if (chdir (optget (opts, "DatabaseDirectory")->strarg))
467
+    {
468
+        logg ("!Can't change dir to %s\n",
469
+              optget (opts, "DatabaseDirectory")->strarg);
470
+        optfree (opts);
471
+        return 50;
472
+    }
473
+    else
474
+    {
475
+        if (!getcwd (dbdir, sizeof (dbdir)))
476
+        {
477
+            logg ("!getcwd() failed\n");
478
+            optfree (opts);
479
+            return 50;
480
+        }
481
+        logg ("*Current working dir is %s\n", dbdir);
403 482
     }
404 483
 
405 484
 
406
-    if(optget(opts, "list-mirrors")->enabled) {
407
-	if(mirman_read("mirrors.dat", &mdat, 1) == -1) {
408
-	    printf("Can't read mirrors.dat\n");
409
-	    optfree(opts);
410
-	    return 55;
411
-	}
412
-	mirman_list(&mdat);
413
-	mirman_free(&mdat);
414
-	optfree(opts);
415
-	return 0;
485
+    if (optget (opts, "list-mirrors")->enabled)
486
+    {
487
+        if (mirman_read ("mirrors.dat", &mdat, 1) == -1)
488
+        {
489
+            printf ("Can't read mirrors.dat\n");
490
+            optfree (opts);
491
+            return 55;
492
+        }
493
+        mirman_list (&mdat);
494
+        mirman_free (&mdat);
495
+        optfree (opts);
496
+        return 0;
416 497
     }
417 498
 
418
-    if((opt = optget(opts, "PrivateMirror"))->enabled) {
419
-	    struct optstruct *dbm, *opth;
420
-
421
-	dbm = (struct optstruct *) optget(opts, "DatabaseMirror");
422
-	dbm->active = dbm->enabled = 1;
423
-	do {
424
-	    if(cli_strbcasestr(opt->strarg, ".clamav.net")) {
425
-		logg("!PrivateMirror: *.clamav.net is not allowed in this mode\n");
426
-		optfree(opts);
427
-		return 45;
428
-	    }
429
-
430
-	    if(dbm->strarg)
431
-		free(dbm->strarg);
432
-	    dbm->strarg = strdup(opt->strarg);
433
-	    if(!dbm->strarg) {
434
-		logg("!strdup() failed\n");
435
-		optfree(opts);
436
-		return 75;
437
-	    }
438
-	    if(!dbm->nextarg) {
439
-		dbm->nextarg = (struct optstruct *) calloc(1, sizeof(struct optstruct));
440
-		if(!dbm->nextarg) {
441
-		    logg("!calloc() failed\n");
442
-		    optfree(opts);
443
-		    return 75;
444
-		}
445
-	    }
446
-	    opth = dbm;
447
-	    dbm = dbm->nextarg;
448
-	} while((opt = opt->nextarg));
449
-
450
-	opth->nextarg = NULL;
451
-	while(dbm) {
452
-	    free(dbm->name);
453
-	    free(dbm->cmd);
454
-	    free(dbm->strarg);
455
-	    opth = dbm;
456
-	    dbm = dbm->nextarg;
457
-	    free(opth);
458
-	}
459
-
460
-	/* disable DNS db checks */
461
-	opth = (struct optstruct *) optget(opts, "no-dns");
462
-	opth->active = opth->enabled = 1;
463
-
464
-	/* disable scripted updates */
465
-	opth = (struct optstruct *) optget(opts, "ScriptedUpdates");
466
-	opth->active = opth->enabled = 0;
499
+    if ((opt = optget (opts, "PrivateMirror"))->enabled)
500
+    {
501
+        struct optstruct *dbm, *opth;
502
+
503
+        dbm = (struct optstruct *) optget (opts, "DatabaseMirror");
504
+        dbm->active = dbm->enabled = 1;
505
+        do
506
+        {
507
+            if (cli_strbcasestr (opt->strarg, ".clamav.net"))
508
+            {
509
+                logg ("!PrivateMirror: *.clamav.net is not allowed in this mode\n");
510
+                optfree (opts);
511
+                return 45;
512
+            }
513
+
514
+            if (dbm->strarg)
515
+                free (dbm->strarg);
516
+            dbm->strarg = strdup (opt->strarg);
517
+            if (!dbm->strarg)
518
+            {
519
+                logg ("!strdup() failed\n");
520
+                optfree (opts);
521
+                return 75;
522
+            }
523
+            if (!dbm->nextarg)
524
+            {
525
+                dbm->nextarg =
526
+                    (struct optstruct *) calloc (1,
527
+                                                 sizeof (struct optstruct));
528
+                if (!dbm->nextarg)
529
+                {
530
+                    logg ("!calloc() failed\n");
531
+                    optfree (opts);
532
+                    return 75;
533
+                }
534
+            }
535
+            opth = dbm;
536
+            dbm = dbm->nextarg;
537
+        }
538
+        while ((opt = opt->nextarg));
539
+
540
+        opth->nextarg = NULL;
541
+        while (dbm)
542
+        {
543
+            free (dbm->name);
544
+            free (dbm->cmd);
545
+            free (dbm->strarg);
546
+            opth = dbm;
547
+            dbm = dbm->nextarg;
548
+            free (opth);
549
+        }
550
+
551
+        /* disable DNS db checks */
552
+        opth = (struct optstruct *) optget (opts, "no-dns");
553
+        opth->active = opth->enabled = 1;
554
+
555
+        /* disable scripted updates */
556
+        opth = (struct optstruct *) optget (opts, "ScriptedUpdates");
557
+        opth->active = opth->enabled = 0;
467 558
     }
468 559
 
469 560
     *updtmpdir = 0;
470 561
 
471 562
 #ifdef _WIN32
472
-    signal(SIGINT, sighandler);
563
+    signal (SIGINT, sighandler);
473 564
 #else
474
-    memset(&sigact, 0, sizeof(struct sigaction));
565
+    memset (&sigact, 0, sizeof (struct sigaction));
475 566
     sigact.sa_handler = sighandler;
476
-    sigaction(SIGINT, &sigact, NULL);
477
-    sigaction(SIGPIPE, &sigact, NULL);
567
+    sigaction (SIGINT, &sigact, NULL);
568
+    sigaction (SIGPIPE, &sigact, NULL);
478 569
 #endif
479
-    if(optget(opts, "daemon")->enabled) {
480
-	    int bigsleep, checks;
570
+    if (optget (opts, "daemon")->enabled)
571
+    {
572
+        int bigsleep, checks;
481 573
 #ifndef	_WIN32
482
-	    time_t now, wakeup;
574
+        time_t now, wakeup;
483 575
 
484
-	sigaction(SIGTERM, &sigact, NULL);
485
-	sigaction(SIGHUP, &sigact, NULL);
486
-	sigaction(SIGCHLD, &sigact, NULL);
576
+        sigaction (SIGTERM, &sigact, NULL);
577
+        sigaction (SIGHUP, &sigact, NULL);
578
+        sigaction (SIGCHLD, &sigact, NULL);
487 579
 #endif
488 580
 
489
-	checks = optget(opts, "Checks")->numarg;
581
+        checks = optget (opts, "Checks")->numarg;
490 582
 
491
-	if(checks <= 0) {
492
-	    logg("^Number of checks must be a positive integer.\n");
493
-	    optfree(opts);
494
-	    return 41;
495
-	}
583
+        if (checks <= 0)
584
+        {
585
+            logg ("^Number of checks must be a positive integer.\n");
586
+            optfree (opts);
587
+            return 41;
588
+        }
496 589
 
497
-	if(!optget(opts, "DNSDatabaseInfo")->enabled || optget(opts, "no-dns")->enabled) {
498
-	    if(checks > 50) {
499
-		logg("^Number of checks must be between 1 and 50.\n");
500
-		optfree(opts);
501
-		return 41;
502
-	    }
503
-	}
590
+        if (!optget (opts, "DNSDatabaseInfo")->enabled
591
+            || optget (opts, "no-dns")->enabled)
592
+        {
593
+            if (checks > 50)
594
+            {
595
+                logg ("^Number of checks must be between 1 and 50.\n");
596
+                optfree (opts);
597
+                return 41;
598
+            }
599
+        }
504 600
 
505
-	bigsleep = 24 * 3600 / checks;
601
+        bigsleep = 24 * 3600 / checks;
506 602
 
507 603
 #ifndef _WIN32
508
-	if(!optget(opts, "Foreground")->enabled) {
509
-	    if(daemonize() == -1) {
510
-		logg("!daemonize() failed\n");
511
-		optfree(opts);
512
-		return 70; /* FIXME */
513
-	    }
604
+        if (!optget (opts, "Foreground")->enabled)
605
+        {
606
+            if (daemonize () == -1)
607
+            {
608
+                logg ("!daemonize() failed\n");
609
+                optfree (opts);
610
+                return 70;      /* FIXME */
611
+            }
514 612
             foreground = 0;
515
-	    mprintf_disabled = 1;
613
+            mprintf_disabled = 1;
516 614
         }
517 615
 #endif
518 616
 
519
-	if((opt = optget(opts, "PidFile"))->enabled) {
520
-	    pidfile = opt->strarg;
521
-	    writepid(pidfile);
522
-	}
617
+        if ((opt = optget (opts, "PidFile"))->enabled)
618
+        {
619
+            pidfile = opt->strarg;
620
+            writepid (pidfile);
621
+        }
523 622
 
524
-	active_children = 0;
623
+        active_children = 0;
525 624
 
526
-	logg("#freshclam daemon %s (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n", get_version());
625
+        logg ("#freshclam daemon %s (OS: " TARGET_OS_TYPE ", ARCH: "
626
+              TARGET_ARCH_TYPE ", CPU: " TARGET_CPU_TYPE ")\n",
627
+              get_version ());
527 628
 
528
-	while(!terminate) {
529
-	    ret = download(opts, cfgfile);
629
+        while (!terminate)
630
+        {
631
+            ret = download (opts, cfgfile);
530 632
 
531
-	    if(ret <= 1) {
532
-		if((opt = optget(opts, "SubmitDetectionStats"))->enabled)
533
-		    submitstats(opt->strarg, opts);
534
-            } else  {
535
-		if((opt = optget(opts, "OnErrorExecute"))->enabled)
536
-		    arg = opt->strarg;
633
+            if (ret <= 1)
634
+            {
635
+                if ((opt = optget (opts, "SubmitDetectionStats"))->enabled)
636
+                    submitstats (opt->strarg, opts);
637
+            }
638
+            else
639
+            {
640
+                if ((opt = optget (opts, "OnErrorExecute"))->enabled)
641
+                    arg = opt->strarg;
537 642
 
538
-		if(arg)
539
-		    execute("OnErrorExecute", arg, opts);
643
+                if (arg)
644
+                    execute ("OnErrorExecute", arg, opts);
540 645
 
541
-		arg = NULL;
542
-	    }
646
+                arg = NULL;
647
+            }
543 648
 
544
-	    logg("#--------------------------------------\n");
649
+            logg ("#--------------------------------------\n");
545 650
 #ifdef	SIGALRM
546
-	    sigaction(SIGALRM, &sigact, &oldact);
651
+            sigaction (SIGALRM, &sigact, &oldact);
547 652
 #endif
548 653
 #ifdef	SIGUSR1
549
-	    sigaction(SIGUSR1, &sigact, &oldact);
654
+            sigaction (SIGUSR1, &sigact, &oldact);
550 655
 #endif
551 656
 
552 657
 #ifdef	_WIN32
553
-	    sleep(bigsleep);
554
-#else   
555
-	    time(&wakeup);
556
-	    wakeup += bigsleep;
557
-	    alarm(bigsleep);
558
-	    do {
559
-		pause();
560
-		time(&now);
561
-	    } while (!terminate && now < wakeup);
562
-
563
-	    if (terminate == -1) {
564
-		logg("Received signal: wake up\n");
565
-		terminate = 0;
566
-	    } else if (terminate == -2) {
567
-		logg("Received signal: re-opening log file\n");
568
-		terminate = 0;
569
-		logg_close();
570
-	    }
658
+            sleep (bigsleep);
659
+#else
660
+            time (&wakeup);
661
+            wakeup += bigsleep;
662
+            alarm (bigsleep);
663
+            do
664
+            {
665
+                pause ();
666
+                time (&now);
667
+            }
668
+            while (!terminate && now < wakeup);
669
+
670
+            if (terminate == -1)
671
+            {
672
+                logg ("Received signal: wake up\n");
673
+                terminate = 0;
674
+            }
675
+            else if (terminate == -2)
676
+            {
677
+                logg ("Received signal: re-opening log file\n");
678
+                terminate = 0;
679
+                logg_close ();
680
+            }
571 681
 #endif
572 682
 
573 683
 #ifdef	SIGALRM
574
-	    sigaction(SIGALRM, &oldact, NULL);
684
+            sigaction (SIGALRM, &oldact, NULL);
575 685
 #endif
576 686
 #ifdef	SIGUSR1
577
-	    sigaction(SIGUSR1, &oldact, NULL);
578
-#endif	    
579
-	}
580
-
581
-    } else {
582
-	if((opt = optget(opts, "submit-stats"))->active) {
583
-	    if(!optget(opts, "no-warnings")->enabled)
584
-		logg(" *** Virus databases are not updated in this mode ***\n");
585
-	    ret = submitstats(opt->strarg, opts);
586
-	} else {
587
-	    ret = download(opts, cfgfile);
588
-
589
-	    if((opt = optget(opts, "SubmitDetectionStats"))->enabled)
590
-		submitstats(opt->strarg, opts);
591
-	}
687
+            sigaction (SIGUSR1, &oldact, NULL);
688
+#endif
689
+        }
690
+
691
+    }
692
+    else
693
+    {
694
+        if ((opt = optget (opts, "submit-stats"))->active)
695
+        {
696
+            if (!optget (opts, "no-warnings")->enabled)
697
+                logg (" *** Virus databases are not updated in this mode ***\n");
698
+            ret = submitstats (opt->strarg, opts);
699
+        }
700
+        else
701
+        {
702
+            ret = download (opts, cfgfile);
703
+
704
+            if ((opt = optget (opts, "SubmitDetectionStats"))->enabled)
705
+                submitstats (opt->strarg, opts);
706
+        }
592 707
     }
593 708
 
594
-    if(ret > 1) {
595
-	if((opt = optget(opts, "OnErrorExecute"))->enabled)
596
-            execute("OnErrorExecute", opt->strarg, opts);
709
+    if (ret > 1)
710
+    {
711
+        if ((opt = optget (opts, "OnErrorExecute"))->enabled)
712
+            execute ("OnErrorExecute", opt->strarg, opts);
597 713
     }
598 714
 
599
-    if (pidfile) {
600
-        unlink(pidfile);
715
+    if (pidfile)
716
+    {
717
+        unlink (pidfile);
601 718
     }
602 719
 
603
-    optfree(opts);
720
+    optfree (opts);
604 721
 
605
-    return(ret);
722
+    return (ret);
606 723
 }
... ...
@@ -21,7 +21,7 @@
21 21
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 22
  *  MA 02110-1301, USA.
23 23
  */
24
- 
24
+
25 25
 #if HAVE_CONFIG_H
26 26
 #include "clamav-config.h"
27 27
 #endif
... ...
@@ -90,96 +90,119 @@ extern char updtmpdir[512], dbdir[512];
90 90
 	    logg("!Can't chdir to %s\n", x);
91 91
 
92 92
 #ifndef HAVE_GETADDRINFO
93
-static const char *ghbn_err(int err) /* hstrerror() */
93
+static const char *
94
+ghbn_err (int err)              /* hstrerror() */
94 95
 {
95
-    switch(err) {
96
-	case HOST_NOT_FOUND:
97
-	    return "Host not found";
96
+    switch (err)
97
+    {
98
+    case HOST_NOT_FOUND:
99
+        return "Host not found";
98 100
 
99
-	case NO_DATA:
100
-	    return "No IP address";
101
+    case NO_DATA:
102
+        return "No IP address";
101 103
 
102
-	case NO_RECOVERY:
103
-	    return "Unrecoverable DNS error";
104
+    case NO_RECOVERY:
105
+        return "Unrecoverable DNS error";
104 106
 
105
-	case TRY_AGAIN:
106
-	    return "Temporary DNS error";
107
+    case TRY_AGAIN:
108
+        return "Temporary DNS error";
107 109
 
108
-	default:
109
-	    return "Unknown error";
110
+    default:
111
+        return "Unknown error";
110 112
     }
111 113
 }
112 114
 #endif
113 115
 
114
-static int getclientsock(const char *localip, int prot)
116
+static int
117
+getclientsock (const char *localip, int prot)
115 118
 {
116
-	int socketfd = -1;
119
+    int socketfd = -1;
117 120
 
118 121
 #ifdef SUPPORT_IPv6
119
-    if(prot == AF_INET6)
120
-	socketfd = socket(AF_INET6, SOCK_STREAM, 0);
122
+    if (prot == AF_INET6)
123
+        socketfd = socket (AF_INET6, SOCK_STREAM, 0);
121 124
     else
122 125
 #endif
123
-	socketfd = socket(AF_INET, SOCK_STREAM, 0);
124
-    if(socketfd < 0) {
125
-	logg("!Can't create new socket\n");
126
-	return -1;
126
+        socketfd = socket (AF_INET, SOCK_STREAM, 0);
127
+    if (socketfd < 0)
128
+    {
129
+        logg ("!Can't create new socket\n");
130
+        return -1;
127 131
     }
128 132
 
129
-    if(localip) {
133
+    if (localip)
134
+    {
130 135
 #ifdef HAVE_GETADDRINFO
131
-	    struct addrinfo *res;
132
-	    int ret;
133
-
134
-	ret = getaddrinfo(localip, NULL, NULL, &res);
135
-	if(ret) {
136
-	    logg("!Could not resolve local ip address '%s': %s\n", localip, gai_strerror(ret));
137
-	    logg("^Using standard local ip address and port for fetching.\n");
138
-	} else {
139
-		char ipaddr[46];
140
-
141
-	    if(bind(socketfd, res->ai_addr, res->ai_addrlen) != 0) {
142
-		logg("!Could not bind to local ip address '%s': %s\n", localip, strerror(errno));
143
-		logg("^Using default client ip.\n");
144
-	    } else {
145
-		    void *addr;
136
+        struct addrinfo *res;
137
+        int ret;
138
+
139
+        ret = getaddrinfo (localip, NULL, NULL, &res);
140
+        if (ret)
141
+        {
142
+            logg ("!Could not resolve local ip address '%s': %s\n", localip,
143
+                  gai_strerror (ret));
144
+            logg ("^Using standard local ip address and port for fetching.\n");
145
+        }
146
+        else
147
+        {
148
+            char ipaddr[46];
149
+
150
+            if (bind (socketfd, res->ai_addr, res->ai_addrlen) != 0)
151
+            {
152
+                logg ("!Could not bind to local ip address '%s': %s\n",
153
+                      localip, strerror (errno));
154
+                logg ("^Using default client ip.\n");
155
+            }
156
+            else
157
+            {
158
+                void *addr;
146 159
 
147 160
 #ifdef SUPPORT_IPv6
148
-		if(res->ai_family == AF_INET6)
149
-		    addr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
150
-		else
161
+                if (res->ai_family == AF_INET6)
162
+                    addr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
163
+                else
151 164
 #endif
152
-		    addr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
165
+                    addr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
153 166
 
154
-		if(inet_ntop(res->ai_family, addr, ipaddr, sizeof(ipaddr)))
155
-		    logg("*Using ip '%s' for fetching.\n", ipaddr);
156
-	    }
157
-	    freeaddrinfo(res);
158
-	}
167
+                if (inet_ntop (res->ai_family, addr, ipaddr, sizeof (ipaddr)))
168
+                    logg ("*Using ip '%s' for fetching.\n", ipaddr);
169
+            }
170
+            freeaddrinfo (res);
171
+        }
159 172
 
160 173
 #else /* IPv4 */
161
-	    struct hostent *he;
162
-
163
-	if(!(he = gethostbyname(localip))) {
164
-	    logg("!Could not resolve local ip address '%s': %s\n", localip, ghbn_err(h_errno));
165
-	    logg("^Using standard local ip address and port for fetching.\n");
166
-	} else {
167
-		struct sockaddr_in client;
168
-		unsigned char *ia;
169
-		char ipaddr[16];
170
-
171
-	    memset((char *) &client, 0, sizeof(client));
172
-	    client.sin_family = AF_INET;
173
-	    client.sin_addr = *(struct in_addr *) he->h_addr_list[0];
174
-	    if(bind(socketfd, (struct sockaddr *) &client, sizeof(struct sockaddr_in)) != 0) {
175
-		logg("!Could not bind to local ip address '%s': %s\n", localip, strerror(errno));
176
-		logg("^Using default client ip.\n");
177
-	    } else {
178
-		ia = (unsigned char *) he->h_addr_list[0];
179
-		sprintf(ipaddr, "%u.%u.%u.%u", ia[0], ia[1], ia[2], ia[3]);
180
-		logg("*Using ip '%s' for fetching.\n", ipaddr);
181
-	    }
182
-	}
174
+        struct hostent *he;
175
+
176
+        if (!(he = gethostbyname (localip)))
177
+        {
178
+            logg ("!Could not resolve local ip address '%s': %s\n", localip,
179
+                  ghbn_err (h_errno));
180
+            logg ("^Using standard local ip address and port for fetching.\n");
181
+        }
182
+        else
183
+        {
184
+            struct sockaddr_in client;
185
+            unsigned char *ia;
186
+            char ipaddr[16];
187
+
188
+            memset ((char *) &client, 0, sizeof (client));
189
+            client.sin_family = AF_INET;
190
+            client.sin_addr = *(struct in_addr *) he->h_addr_list[0];
191
+            if (bind
192
+                (socketfd, (struct sockaddr *) &client,
193
+                 sizeof (struct sockaddr_in)) != 0)
194
+            {
195
+                logg ("!Could not bind to local ip address '%s': %s\n",
196
+                      localip, strerror (errno));
197
+                logg ("^Using default client ip.\n");
198
+            }
199
+            else
200
+            {
201
+                ia = (unsigned char *) he->h_addr_list[0];
202
+                sprintf (ipaddr, "%u.%u.%u.%u", ia[0], ia[1], ia[2], ia[3]);
203
+                logg ("*Using ip '%s' for fetching.\n", ipaddr);
204
+            }
205
+        }
183 206
 #endif
184 207
     }
185 208
 
... ...
@@ -187,1257 +210,1576 @@ static int getclientsock(const char *localip, int prot)
187 187
 }
188 188
 
189 189
 #ifdef HAVE_GETADDRINFO
190
-static int qcompare(const void *a, const void *b)
190
+static int
191
+qcompare (const void *a, const void *b)
191 192
 {
192
-    return (*(const struct addrinfo **) a)->ai_flags - (*(const struct addrinfo **) b)->ai_flags;
193
+    return (*(const struct addrinfo **) a)->ai_flags -
194
+        (*(const struct addrinfo **) b)->ai_flags;
193 195
 }
194 196
 #endif
195 197
 
196
-static int wwwconnect(const char *server, const char *proxy, int pport, char *ip, const char *localip, int ctimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist, unsigned int attempt)
198
+static int
199
+wwwconnect (const char *server, const char *proxy, int pport, char *ip,
200
+            const char *localip, int ctimeout, struct mirdat *mdat,
201
+            int logerr, unsigned int can_whitelist, unsigned int attempt)
197 202
 {
198
-	int socketfd, port, ret;
199
-	unsigned int ips = 0, ignored = 0, i;
203
+    int socketfd, port, ret;
204
+    unsigned int ips = 0, ignored = 0, i;
200 205
 #ifdef HAVE_GETADDRINFO
201
-	struct addrinfo hints, *res = NULL, *rp, *loadbal_rp = NULL, *addrs[128];
202
-	char port_s[6], loadbal_ipaddr[46];
203
-	uint32_t loadbal = 1, minsucc = 0xffffffff, minfail = 0xffffffff, addrnum = 0;
204
-	int ipv4start = -1, ipv4end = -1;
205
-	struct mirdat_ip *md;
206
+    struct addrinfo hints, *res = NULL, *rp, *loadbal_rp = NULL, *addrs[128];
207
+    char port_s[6], loadbal_ipaddr[46];
208
+    uint32_t loadbal = 1, minsucc = 0xffffffff, minfail =
209
+        0xffffffff, addrnum = 0;
210
+    int ipv4start = -1, ipv4end = -1;
211
+    struct mirdat_ip *md;
206 212
 #else
207
-	struct sockaddr_in name;
208
-	struct hostent *host;
209
-	unsigned char *ia;
213
+    struct sockaddr_in name;
214
+    struct hostent *host;
215
+    unsigned char *ia;
210 216
 #endif
211
-	char ipaddr[46];
212
-	const char *hostpt;
217
+    char ipaddr[46];
218
+    const char *hostpt;
213 219
 
214
-    if(ip)
215
-	strcpy(ip, "UNKNOWN");
220
+    if (ip)
221
+        strcpy (ip, "UNKNOWN");
216 222
 
217
-    if(proxy) {
218
-	hostpt = proxy;
223
+    if (proxy)
224
+    {
225
+        hostpt = proxy;
219 226
 
220
-	if(!(port = pport)) {
221
-		const struct servent *webcache = getservbyname("webcache", "TCP");
227
+        if (!(port = pport))
228
+        {
229
+            const struct servent *webcache =
230
+                getservbyname ("webcache", "TCP");
222 231
 
223
-		if(webcache)
224
-			port = ntohs(webcache->s_port);
225
-		else
226
-			port = 8080;
232
+            if (webcache)
233
+                port = ntohs (webcache->s_port);
234
+            else
235
+                port = 8080;
227 236
 
228
-		endservent();
229
-	}
237
+            endservent ();
238
+        }
230 239
 
231
-    } else {
232
-	hostpt = server;
233
-	port = 80;
240
+    }
241
+    else
242
+    {
243
+        hostpt = server;
244
+        port = 80;
234 245
     }
235 246
 
236 247
 #ifdef HAVE_GETADDRINFO
237
-    memset(&hints, 0, sizeof(hints));
248
+    memset (&hints, 0, sizeof (hints));
238 249
 #ifdef SUPPORT_IPv6
239 250
     hints.ai_family = AF_UNSPEC;
240 251
 #else
241 252
     hints.ai_family = AF_INET;
242 253
 #endif
243 254
     hints.ai_socktype = SOCK_STREAM;
244
-    snprintf(port_s, sizeof(port_s), "%d", port);
245
-    port_s[sizeof(port_s) - 1] = 0;
246
-    ret = getaddrinfo(hostpt, port_s, &hints, &res);
247
-    if(ret) {
248
-	logg("%cCan't get information about %s: %s\n", logerr ? '!' : '^', hostpt, gai_strerror(ret));
249
-	return -1;
250
-    }
251
-
252
-    for(rp = res; rp && addrnum < 128; rp = rp->ai_next) {
253
-	rp->ai_flags = cli_rndnum(1024);
254
-	addrs[addrnum] = rp;
255
-	if(rp->ai_family == AF_INET) {
256
-	    if(ipv4start == -1)
257
-		ipv4start = addrnum;
258
-	} else if(ipv4end == -1 && ipv4start != -1) {
259
-	    ipv4end = addrnum - 1;
260
-	}
261
-	if(!rp->ai_next && ipv4end == -1)
262
-	    ipv4end = addrnum;
263
-	addrnum++;
264
-    }
265
-    if(ipv4end != -1 && ipv4start != -1 && ipv4end - ipv4start + 1 > 1)
266
-	qsort(&addrs[ipv4start], ipv4end - ipv4start + 1, sizeof(struct addrinfo *), qcompare);
267
-
268
-    if(attempt > 1)
269
-	loadbal = 0;
270
-
271
-    for(i = 0; i < addrnum; ) {
272
-	    void *addr;
273
-
274
-	rp = addrs[i];
275
-	ips++;
255
+    snprintf (port_s, sizeof (port_s), "%d", port);
256
+    port_s[sizeof (port_s) - 1] = 0;
257
+    ret = getaddrinfo (hostpt, port_s, &hints, &res);
258
+    if (ret)
259
+    {
260
+        logg ("%cCan't get information about %s: %s\n", logerr ? '!' : '^',
261
+              hostpt, gai_strerror (ret));
262
+        return -1;
263
+    }
264
+
265
+    for (rp = res; rp && addrnum < 128; rp = rp->ai_next)
266
+    {
267
+        rp->ai_flags = cli_rndnum (1024);
268
+        addrs[addrnum] = rp;
269
+        if (rp->ai_family == AF_INET)
270
+        {
271
+            if (ipv4start == -1)
272
+                ipv4start = addrnum;
273
+        }
274
+        else if (ipv4end == -1 && ipv4start != -1)
275
+        {
276
+            ipv4end = addrnum - 1;
277
+        }
278
+        if (!rp->ai_next && ipv4end == -1)
279
+            ipv4end = addrnum;
280
+        addrnum++;
281
+    }
282
+    if (ipv4end != -1 && ipv4start != -1 && ipv4end - ipv4start + 1 > 1)
283
+        qsort (&addrs[ipv4start], ipv4end - ipv4start + 1,
284
+               sizeof (struct addrinfo *), qcompare);
285
+
286
+    if (attempt > 1)
287
+        loadbal = 0;
288
+
289
+    for (i = 0; i < addrnum;)
290
+    {
291
+        void *addr;
292
+
293
+        rp = addrs[i];
294
+        ips++;
276 295
 #ifdef SUPPORT_IPv6
277
-	if(rp->ai_family == AF_INET6)
278
-	    addr = &((struct sockaddr_in6 *) rp->ai_addr)->sin6_addr;
279
-	else
296
+        if (rp->ai_family == AF_INET6)
297
+            addr = &((struct sockaddr_in6 *) rp->ai_addr)->sin6_addr;
298
+        else
280 299
 #endif
281
-	    addr = &((struct sockaddr_in *) rp->ai_addr)->sin_addr;
282
-
283
-	if(!inet_ntop(rp->ai_family, addr, ipaddr, sizeof(ipaddr))) {
284
-	    logg("%cinet_ntop() failed\n", logerr ? '!' : '^');
285
-	    freeaddrinfo(res);
286
-	    return -1;
287
-	}
288
-
289
-	if(mdat && (ret = mirman_check(addr, rp->ai_family, mdat, &md))) {
290
-	    if(ret == 1)
291
-		logg("*Ignoring mirror %s (due to previous errors)\n", ipaddr);
292
-	    else
293
-		logg("*Ignoring mirror %s (has connected too many times with an outdated version)\n", ipaddr);
294
-
295
-	    ignored++;
296
-	    if(!loadbal || i + 1 < addrnum) {
297
-		i++;
298
-		continue;
299
-	    }
300
-	}
301
-
302
-	if(mdat && loadbal) {
303
-	    if(!ret) {
304
-		if(!md) {
305
-		    loadbal_rp = rp;
306
-		    strncpy(loadbal_ipaddr, ipaddr, sizeof(loadbal_ipaddr));
307
-		} else {
308
-		    if(md->succ <= minsucc && md->fail <= minfail) {
309
-			minsucc = md->succ;
310
-			minfail = md->fail;
311
-			loadbal_rp = rp;
312
-			strncpy(loadbal_ipaddr, ipaddr, sizeof(loadbal_ipaddr));
313
-		    }
314
-		    if(i + 1 < addrnum) {
315
-			i++;
316
-			continue;
317
-		    }
318
-		}
319
-	    }
320
-
321
-	    if(!loadbal_rp) {
322
-		if(i + 1 == addrnum) {
323
-		    loadbal = 0;
324
-		    i = 0;
325
-		}
326
-		continue;
327
-	    }
328
-	    rp = loadbal_rp;
329
-	    strncpy(ipaddr, loadbal_ipaddr, sizeof(ipaddr));
300
+            addr = &((struct sockaddr_in *) rp->ai_addr)->sin_addr;
301
+
302
+        if (!inet_ntop (rp->ai_family, addr, ipaddr, sizeof (ipaddr)))
303
+        {
304
+            logg ("%cinet_ntop() failed\n", logerr ? '!' : '^');
305
+            freeaddrinfo (res);
306
+            return -1;
307
+        }
308
+
309
+        if (mdat && (ret = mirman_check (addr, rp->ai_family, mdat, &md)))
310
+        {
311
+            if (ret == 1)
312
+                logg ("*Ignoring mirror %s (due to previous errors)\n",
313
+                      ipaddr);
314
+            else
315
+                logg ("*Ignoring mirror %s (has connected too many times with an outdated version)\n", ipaddr);
316
+
317
+            ignored++;
318
+            if (!loadbal || i + 1 < addrnum)
319
+            {
320
+                i++;
321
+                continue;
322
+            }
323
+        }
324
+
325
+        if (mdat && loadbal)
326
+        {
327
+            if (!ret)
328
+            {
329
+                if (!md)
330
+                {
331
+                    loadbal_rp = rp;
332
+                    strncpy (loadbal_ipaddr, ipaddr, sizeof (loadbal_ipaddr));
333
+                }
334
+                else
335
+                {
336
+                    if (md->succ <= minsucc && md->fail <= minfail)
337
+                    {
338
+                        minsucc = md->succ;
339
+                        minfail = md->fail;
340
+                        loadbal_rp = rp;
341
+                        strncpy (loadbal_ipaddr, ipaddr,
342
+                                 sizeof (loadbal_ipaddr));
343
+                    }
344
+                    if (i + 1 < addrnum)
345
+                    {
346
+                        i++;
347
+                        continue;
348
+                    }
349
+                }
350
+            }
351
+
352
+            if (!loadbal_rp)
353
+            {
354
+                if (i + 1 == addrnum)
355
+                {
356
+                    loadbal = 0;
357
+                    i = 0;
358
+                }
359
+                continue;
360
+            }
361
+            rp = loadbal_rp;
362
+            strncpy (ipaddr, loadbal_ipaddr, sizeof (ipaddr));
330 363
 #ifdef SUPPORT_IPv6
331
-	    if(rp->ai_family == AF_INET6)
332
-		addr = &((struct sockaddr_in6 *) rp->ai_addr)->sin6_addr;
333
-	    else
364
+            if (rp->ai_family == AF_INET6)
365
+                addr = &((struct sockaddr_in6 *) rp->ai_addr)->sin6_addr;
366
+            else
334 367
 #endif
335
-		addr = &((struct sockaddr_in *) rp->ai_addr)->sin_addr;
336
-	} else if(loadbal_rp == rp) {
337
-	    i++;
338
-	    continue;
339
-	}
368
+                addr = &((struct sockaddr_in *) rp->ai_addr)->sin_addr;
369
+        }
370
+        else if (loadbal_rp == rp)
371
+        {
372
+            i++;
373
+            continue;
374
+        }
340 375
 
341
-	if(ip)
342
-	    strcpy(ip, ipaddr);
376
+        if (ip)
377
+            strcpy (ip, ipaddr);
343 378
 
344
-	if(rp != loadbal_rp && rp != addrs[0])
345
-	    logg("Trying host %s (%s)...\n", hostpt, ipaddr);
379
+        if (rp != loadbal_rp && rp != addrs[0])
380
+            logg ("Trying host %s (%s)...\n", hostpt, ipaddr);
346 381
 
347
-	socketfd = getclientsock(localip, rp->ai_family);
348
-	if(socketfd < 0) {
349
-	    freeaddrinfo(res);
350
-	    return -1;
351
-	}
382
+        socketfd = getclientsock (localip, rp->ai_family);
383
+        if (socketfd < 0)
384
+        {
385
+            freeaddrinfo (res);
386
+            return -1;
387
+        }
352 388
 
353 389
 #ifdef SO_ERROR
354
-	if(wait_connect(socketfd, rp->ai_addr, rp->ai_addrlen, ctimeout) == -1) {
390
+        if (wait_connect (socketfd, rp->ai_addr, rp->ai_addrlen, ctimeout) ==
391
+            -1)
392
+        {
355 393
 #else
356
-	if(connect(socketfd, rp->ai_addr, rp->ai_addrlen) == -1) {
394
+        if (connect (socketfd, rp->ai_addr, rp->ai_addrlen) == -1)
395
+        {
357 396
 #endif
358
-	    logg("Can't connect to port %d of host %s (IP: %s)\n", port, hostpt, ipaddr);
359
-	    closesocket(socketfd);
360
-	    if(loadbal) {
361
-		loadbal = 0;
362
-		i = 0;
363
-	    } else i++;
364
-	    mirman_update(addr, rp->ai_family, mdat, 2);
365
-	    continue;
366
-	} else {
367
-	    if(mdat) {
368
-		if(rp->ai_family == AF_INET)
369
-		    mdat->currip[0] = *((uint32_t *) addr);
370
-		else
371
-		    memcpy(mdat->currip, addr, 4 * sizeof(uint32_t));
372
-		mdat->af = rp->ai_family;
373
-	    }
374
-	    freeaddrinfo(res);
375
-	    return socketfd;
376
-	}
377
-	i++;
378
-    }
379
-    freeaddrinfo(res);
397
+            logg ("Can't connect to port %d of host %s (IP: %s)\n", port,
398
+                  hostpt, ipaddr);
399
+            closesocket (socketfd);
400
+            if (loadbal)
401
+            {
402
+                loadbal = 0;
403
+                i = 0;
404
+            }
405
+            else
406
+                i++;
407
+            mirman_update (addr, rp->ai_family, mdat, 2);
408
+            continue;
409
+        }
410
+        else
411
+        {
412
+            if (mdat)
413
+            {
414
+                if (rp->ai_family == AF_INET)
415
+                    mdat->currip[0] = *((uint32_t *) addr);
416
+                else
417
+                    memcpy (mdat->currip, addr, 4 * sizeof (uint32_t));
418
+                mdat->af = rp->ai_family;
419
+            }
420
+            freeaddrinfo (res);
421
+            return socketfd;
422
+        }
423
+        i++;
424
+    }
425
+    freeaddrinfo (res);
380 426
 
381 427
 #else /* IPv4 */
382 428
 
383
-    if((host = gethostbyname(hostpt)) == NULL) {
384
-        logg("%cCan't get information about %s: %s\n", logerr ? '!' : '^', hostpt, ghbn_err(h_errno));
385
-	return -1;
429
+    if ((host = gethostbyname (hostpt)) == NULL)
430
+    {
431
+        logg ("%cCan't get information about %s: %s\n", logerr ? '!' : '^',
432
+              hostpt, ghbn_err (h_errno));
433
+        return -1;
386 434
     }
387 435
 
388
-    for(i = 0; host->h_addr_list[i] != 0; i++) {
389
-	/* this dirty hack comes from pink - Nosuid TCP/IP ping 1.6 */
390
-	ia = (unsigned char *) host->h_addr_list[i];
391
-	sprintf(ipaddr, "%u.%u.%u.%u", ia[0], ia[1], ia[2], ia[3]);
392
-
393
-	ips++;
394
-	if(mdat && (ret = mirman_check(&((struct in_addr *) ia)->s_addr, AF_INET, mdat, NULL))) {
395
-	    if(ret == 1)
396
-		logg("*Ignoring mirror %s (due to previous errors)\n", ipaddr);
397
-	    else
398
-		logg("*Ignoring mirror %s (has connected too many times with an outdated version)\n", ipaddr);
399
-	    ignored++;
400
-	    continue;
401
-	}
436
+    for (i = 0; host->h_addr_list[i] != 0; i++)
437
+    {
438
+        /* this dirty hack comes from pink - Nosuid TCP/IP ping 1.6 */
439
+        ia = (unsigned char *) host->h_addr_list[i];
440
+        sprintf (ipaddr, "%u.%u.%u.%u", ia[0], ia[1], ia[2], ia[3]);
441
+
442
+        ips++;
443
+        if (mdat
444
+            && (ret =
445
+                mirman_check (&((struct in_addr *) ia)->s_addr, AF_INET, mdat,
446
+                              NULL)))
447
+        {
448
+            if (ret == 1)
449
+                logg ("*Ignoring mirror %s (due to previous errors)\n",
450
+                      ipaddr);
451
+            else
452
+                logg ("*Ignoring mirror %s (has connected too many times with an outdated version)\n", ipaddr);
453
+            ignored++;
454
+            continue;
455
+        }
402 456
 
403
-	if(ip)
404
-	    strcpy(ip, ipaddr);
457
+        if (ip)
458
+            strcpy (ip, ipaddr);
405 459
 
406
-	if(i > 0)
407
-	    logg("Trying host %s (%s)...\n", hostpt, ipaddr);
460
+        if (i > 0)
461
+            logg ("Trying host %s (%s)...\n", hostpt, ipaddr);
408 462
 
409
-	memset ((char *) &name, 0, sizeof(name));
410
-	name.sin_family = AF_INET;
411
-	name.sin_addr = *((struct in_addr *) host->h_addr_list[i]);
412
-	name.sin_port = htons(port);
463
+        memset ((char *) &name, 0, sizeof (name));
464
+        name.sin_family = AF_INET;
465
+        name.sin_addr = *((struct in_addr *) host->h_addr_list[i]);
466
+        name.sin_port = htons (port);
413 467
 
414
-	socketfd = getclientsock(localip, AF_INET);
415
-	if(socketfd < 0)
416
-	    return -1;
468
+        socketfd = getclientsock (localip, AF_INET);
469
+        if (socketfd < 0)
470
+            return -1;
417 471
 
418 472
 #ifdef SO_ERROR
419
-	if(wait_connect(socketfd, (struct sockaddr *) &name, sizeof(struct sockaddr_in), ctimeout) == -1) {
473
+        if (wait_connect
474
+            (socketfd, (struct sockaddr *) &name, sizeof (struct sockaddr_in),
475
+             ctimeout) == -1)
476
+        {
420 477
 #else
421
-	if(connect(socketfd, (struct sockaddr *) &name, sizeof(struct sockaddr_in)) == -1) {
478
+        if (connect
479
+            (socketfd, (struct sockaddr *) &name,
480
+             sizeof (struct sockaddr_in)) == -1)
481
+        {
422 482
 #endif
423
-	    logg("Can't connect to port %d of host %s (IP: %s)\n", port, hostpt, ipaddr);
424
-	    closesocket(socketfd);
425
-	    continue;
426
-	} else {
427
-	    if(mdat) {
428
-		mdat->currip[0] = ((struct in_addr *) ia)->s_addr;
429
-		mdat->af = AF_INET;
430
-	    }
431
-	    return socketfd;
432
-	}
483
+            logg ("Can't connect to port %d of host %s (IP: %s)\n", port,
484
+                  hostpt, ipaddr);
485
+            closesocket (socketfd);
486
+            continue;
487
+        }
488
+        else
489
+        {
490
+            if (mdat)
491
+            {
492
+                mdat->currip[0] = ((struct in_addr *) ia)->s_addr;
493
+                mdat->af = AF_INET;
494
+            }
495
+            return socketfd;
496
+        }
433 497
     }
434 498
 #endif
435 499
 
436
-    if(mdat && can_whitelist && ips && (ips == ignored))
437
-	mirman_whitelist(mdat, 1);
500
+    if (mdat && can_whitelist && ips && (ips == ignored))
501
+        mirman_whitelist (mdat, 1);
438 502
 
439 503
     return -2;
440 504
 }
441 505
 
442
-static unsigned int fmt_base64(char *dest, const char *src, unsigned int len)
506
+static unsigned int
507
+fmt_base64 (char *dest, const char *src, unsigned int len)
443 508
 {
444
-	unsigned short bits = 0,temp = 0;
445
-	unsigned long written = 0;
446
-	unsigned int i;
447
-	const char base64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
509
+    unsigned short bits = 0, temp = 0;
510
+    unsigned long written = 0;
511
+    unsigned int i;
512
+    const char base64[] =
513
+        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
448 514
 
449 515
 
450
-    for(i = 0; i < len; i++) {
451
-	temp <<= 8;
452
-	temp += src[i];
453
-	bits += 8;
454
-	while(bits > 6) {
455
-	    dest[written] = base64[((temp >> (bits - 6)) & 63)];
456
-	    written++;
457
-	    bits -= 6;
458
-	}
516
+    for (i = 0; i < len; i++)
517
+    {
518
+        temp <<= 8;
519
+        temp += src[i];
520
+        bits += 8;
521
+        while (bits > 6)
522
+        {
523
+            dest[written] = base64[((temp >> (bits - 6)) & 63)];
524
+            written++;
525
+            bits -= 6;
526
+        }
459 527
     }
460 528
 
461
-    if(bits) {
462
-	temp <<= (6 - bits);
463
-	dest[written] = base64[temp & 63];
464
-	written++;
529
+    if (bits)
530
+    {
531
+        temp <<= (6 - bits);
532
+        dest[written] = base64[temp & 63];
533
+        written++;
465 534
     }
466 535
 
467
-    while(written & 3) {
468
-	dest[written] = '=';
469
-	written++;
536
+    while (written & 3)
537
+    {
538
+        dest[written] = '=';
539
+        written++;
470 540
     }
471 541
 
472 542
     return written;
473 543
 }
474 544
 
475
-static char *proxyauth(const char *user, const char *pass)
545
+static char *
546
+proxyauth (const char *user, const char *pass)
476 547
 {
477
-	int len;
478
-	char *buf, *userpass, *auth;
548
+    int len;
549
+    char *buf, *userpass, *auth;
479 550
 
480 551
 
481
-    userpass = malloc(strlen(user) + strlen(pass) + 2);
482
-    if(!userpass) {
483
-	logg("!proxyauth: Can't allocate memory for 'userpass'\n");
484
-	return NULL;
552
+    userpass = malloc (strlen (user) + strlen (pass) + 2);
553
+    if (!userpass)
554
+    {
555
+        logg ("!proxyauth: Can't allocate memory for 'userpass'\n");
556
+        return NULL;
485 557
     }
486
-    sprintf(userpass, "%s:%s", user, pass);
558
+    sprintf (userpass, "%s:%s", user, pass);
487 559
 
488
-    buf = malloc((strlen(pass) + strlen(user)) * 2 + 4);
489
-    if(!buf) {
490
-	logg("!proxyauth: Can't allocate memory for 'buf'\n");
491
-	free(userpass);
492
-	return NULL;
560
+    buf = malloc ((strlen (pass) + strlen (user)) * 2 + 4);
561
+    if (!buf)
562
+    {
563
+        logg ("!proxyauth: Can't allocate memory for 'buf'\n");
564
+        free (userpass);
565
+        return NULL;
493 566
     }
494 567
 
495
-    len = fmt_base64(buf, userpass, strlen(userpass));
496
-    free(userpass);
568
+    len = fmt_base64 (buf, userpass, strlen (userpass));
569
+    free (userpass);
497 570
     buf[len] = '\0';
498
-    auth = malloc(strlen(buf) + 30);
499
-    if(!auth) {
500
-	free(buf);
501
-	logg("!proxyauth: Can't allocate memory for 'authorization'\n");
502
-	return NULL;
571
+    auth = malloc (strlen (buf) + 30);
572
+    if (!auth)
573
+    {
574
+        free (buf);
575
+        logg ("!proxyauth: Can't allocate memory for 'authorization'\n");
576
+        return NULL;
503 577
     }
504 578
 
505
-    sprintf(auth, "Proxy-Authorization: Basic %s\r\n", buf);
506
-    free(buf);
579
+    sprintf (auth, "Proxy-Authorization: Basic %s\r\n", buf);
580
+    free (buf);
507 581
 
508 582
     return auth;
509 583
 }
510 584
 
511 585
 #if BUILD_CLAMD
512
-int submitstats(const char *clamdcfg, const struct optstruct *opts)
586
+int
587
+submitstats (const char *clamdcfg, const struct optstruct *opts)
513 588
 {
514
-	int sd, clamsockd, bread, cnt, ret;
515
-	char post[SUBMIT_MIN_ENTRIES * 256 + 512];
516
-	char query[SUBMIT_MIN_ENTRIES * 256];
517
-	char uastr[128], *line;
518
-	char *pt, *auth = NULL;
519
-	const char *country = NULL, *user, *proxy = NULL, *hostid = NULL;
520
-	const struct optstruct *opt;
521
-	unsigned int qcnt, entries, submitted = 0, permfail = 0, port = 0;
522
-        struct RCVLN rcv;
523
-	const char *tokens[5];
524
-
525
-
526
-    if((opt = optget(opts, "DetectionStatsCountry"))->enabled) {
527
-	if(strlen(opt->strarg) != 2 || !isalpha(opt->strarg[0]) || !isalpha(opt->strarg[1])) {
528
-	    logg("!SubmitDetectionStats: DetectionStatsCountry requires a two-letter country code\n");
529
-	    return 56;
530
-	}
531
-	country = opt->strarg;
532
-    }
533
-
534
-    if((opt = optget(opts, "DetectionStatsHostID"))->enabled) {
535
-	if(strlen(opt->strarg) != 32) {
536
-	    logg("!SubmitDetectionStats: The unique ID must be 32 characters long\n");
537
-	    return 56;
538
-	}
539
-	hostid = opt->strarg;
540
-    }
541
-
542
-    if((opt = optget(opts, "HTTPUserAgent"))->enabled)
543
-        strncpy(uastr, opt->strarg, sizeof(uastr));
544
-    else
545
-	snprintf(uastr, sizeof(uastr), PACKAGE"/%s (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE"):%s:%s", get_version(), country ? country : "", hostid ? hostid : "");
546
-    uastr[sizeof(uastr) - 1] = 0;
589
+    int sd, clamsockd, bread, cnt, ret;
590
+    char post[SUBMIT_MIN_ENTRIES * 256 + 512];
591
+    char query[SUBMIT_MIN_ENTRIES * 256];
592
+    char uastr[128], *line;
593
+    char *pt, *auth = NULL;
594
+    const char *country = NULL, *user, *proxy = NULL, *hostid = NULL;
595
+    const struct optstruct *opt;
596
+    unsigned int qcnt, entries, submitted = 0, permfail = 0, port = 0;
597
+    struct RCVLN rcv;
598
+    const char *tokens[5];
599
+
600
+
601
+    if ((opt = optget (opts, "DetectionStatsCountry"))->enabled)
602
+    {
603
+        if (strlen (opt->strarg) != 2 || !isalpha (opt->strarg[0])
604
+            || !isalpha (opt->strarg[1]))
605
+        {
606
+            logg ("!SubmitDetectionStats: DetectionStatsCountry requires a two-letter country code\n");
607
+            return 56;
608
+        }
609
+        country = opt->strarg;
610
+    }
547 611
 
548
-    if((opt = optget(opts, "HTTPProxyServer"))->enabled) {
549
-	proxy = opt->strarg;
550
-	if(!strncasecmp(proxy, "http://", 7))
551
-	    proxy += 7;
612
+    if ((opt = optget (opts, "DetectionStatsHostID"))->enabled)
613
+    {
614
+        if (strlen (opt->strarg) != 32)
615
+        {
616
+            logg ("!SubmitDetectionStats: The unique ID must be 32 characters long\n");
617
+            return 56;
618
+        }
619
+        hostid = opt->strarg;
620
+    }
552 621
 
553
-	if((opt = optget(opts, "HTTPProxyUsername"))->enabled) {
554
-	    user = opt->strarg;
555
-	    if(!(opt = optget(opts, "HTTPProxyPassword"))->enabled) {
556
-		logg("!SubmitDetectionStats: HTTPProxyUsername requires HTTPProxyPassword\n");
557
-		return 56;
558
-	    }
559
-	    auth = proxyauth(user, opt->strarg);
560
-	    if(!auth)
561
-		return 56;
562
-	}
622
+    if ((opt = optget (opts, "HTTPUserAgent"))->enabled)
623
+        strncpy (uastr, opt->strarg, sizeof (uastr));
624
+    else
625
+        snprintf (uastr, sizeof (uastr),
626
+                  PACKAGE "/%s (OS: " TARGET_OS_TYPE ", ARCH: "
627
+                  TARGET_ARCH_TYPE ", CPU: " TARGET_CPU_TYPE "):%s:%s",
628
+                  get_version (), country ? country : "",
629
+                  hostid ? hostid : "");
630
+    uastr[sizeof (uastr) - 1] = 0;
631
+
632
+    if ((opt = optget (opts, "HTTPProxyServer"))->enabled)
633
+    {
634
+        proxy = opt->strarg;
635
+        if (!strncasecmp (proxy, "http://", 7))
636
+            proxy += 7;
637
+
638
+        if ((opt = optget (opts, "HTTPProxyUsername"))->enabled)
639
+        {
640
+            user = opt->strarg;
641
+            if (!(opt = optget (opts, "HTTPProxyPassword"))->enabled)
642
+            {
643
+                logg ("!SubmitDetectionStats: HTTPProxyUsername requires HTTPProxyPassword\n");
644
+                return 56;
645
+            }
646
+            auth = proxyauth (user, opt->strarg);
647
+            if (!auth)
648
+                return 56;
649
+        }
563 650
 
564
-	if((opt = optget(opts, "HTTPProxyPort"))->enabled)
565
-	    port = opt->numarg;
651
+        if ((opt = optget (opts, "HTTPProxyPort"))->enabled)
652
+            port = opt->numarg;
566 653
 
567
-	logg("*Connecting via %s\n", proxy);
654
+        logg ("*Connecting via %s\n", proxy);
568 655
     }
569 656
 
570
-    if((clamsockd = clamd_connect(clamdcfg, "SubmitDetectionStats")) < 0)
571
-	return 52;
657
+    if ((clamsockd = clamd_connect (clamdcfg, "SubmitDetectionStats")) < 0)
658
+        return 52;
572 659
 
573
-    recvlninit(&rcv, clamsockd);
574
-    if(sendln(clamsockd, "zDETSTATS", 10)) {
575
-        closesocket(clamsockd);
576
-	return 52;
660
+    recvlninit (&rcv, clamsockd);
661
+    if (sendln (clamsockd, "zDETSTATS", 10))
662
+    {
663
+        closesocket (clamsockd);
664
+        return 52;
577 665
     }
578 666
 
579 667
     ret = 0;
580
-    memset(query, 0, sizeof(query));
668
+    memset (query, 0, sizeof (query));
581 669
     qcnt = 0;
582 670
     entries = 0;
583
-    while(recvln(&rcv, &line, NULL) > 0) {
584
-        if(cli_strtokenize(line, ':', 5, tokens) != 5) {
585
-	    logg("!SubmitDetectionStats: Invalid data format\n");
586
-	    ret = 52;
587
-	    break;
588
-	}
589
-
590
-	qcnt += snprintf(&query[qcnt], sizeof(query) - qcnt, "ts[]=%s&fname[]=%s&virus[]=%s(%s:%s)&", tokens[0], tokens[4], tokens[3], tokens[1], tokens[2]);
591
-	entries++;
592
-
593
-	if(entries == SUBMIT_MIN_ENTRIES) {
594
-	    sd = wwwconnect("stats.clamav.net", proxy, port, NULL, optget(opts, "LocalIPAddress")->strarg, optget(opts, "ConnectTimeout")->numarg, NULL, 0, 0, 1);
595
-	    if(sd < 0) {
596
-		logg("!SubmitDetectionStats: Can't connect to server\n");
597
-		ret = 52;
598
-		break;
599
-	    }
600
-	    query[sizeof(query) - 1] = 0;
601
-	    if(mdprintf(sd,
602
-		"POST http://stats.clamav.net/submit.php HTTP/1.0\r\n"
603
-		"Host: stats.clamav.net\r\n%s%s%s%s"
604
-		"Content-Type: application/x-www-form-urlencoded\r\n"
605
-		"User-Agent: %s\r\n"
606
-		"Content-Length: %u\r\n\r\n"
607
-		"%s",
608
-		auth ? auth : "", hostid ? "X-HostID: " : "", hostid ? hostid : "", hostid ? "\r\n" : "", uastr, (unsigned int) strlen(query), query) < 0)
609
-	    {
610
-		logg("!SubmitDetectionStats: Can't write to socket\n");
611
-		ret = 52;
612
-		closesocket(sd);
613
-		break;
614
-	    }
615
-
616
-	    pt = post;
617
-	    cnt = sizeof(post) - 1;
671
+    while (recvln (&rcv, &line, NULL) > 0)
672
+    {
673
+        if (cli_strtokenize (line, ':', 5, tokens) != 5)
674
+        {
675
+            logg ("!SubmitDetectionStats: Invalid data format\n");
676
+            ret = 52;
677
+            break;
678
+        }
679
+
680
+        qcnt +=
681
+            snprintf (&query[qcnt], sizeof (query) - qcnt,
682
+                      "ts[]=%s&fname[]=%s&virus[]=%s(%s:%s)&", tokens[0],
683
+                      tokens[4], tokens[3], tokens[1], tokens[2]);
684
+        entries++;
685
+
686
+        if (entries == SUBMIT_MIN_ENTRIES)
687
+        {
688
+            sd = wwwconnect ("stats.clamav.net", proxy, port, NULL,
689
+                             optget (opts, "LocalIPAddress")->strarg,
690
+                             optget (opts, "ConnectTimeout")->numarg, NULL, 0,
691
+                             0, 1);
692
+            if (sd < 0)
693
+            {
694
+                logg ("!SubmitDetectionStats: Can't connect to server\n");
695
+                ret = 52;
696
+                break;
697
+            }
698
+            query[sizeof (query) - 1] = 0;
699
+            if (mdprintf (sd,
700
+                          "POST http://stats.clamav.net/submit.php HTTP/1.0\r\n"
701
+                          "Host: stats.clamav.net\r\n%s%s%s%s"
702
+                          "Content-Type: application/x-www-form-urlencoded\r\n"
703
+                          "User-Agent: %s\r\n"
704
+                          "Content-Length: %u\r\n\r\n"
705
+                          "%s",
706
+                          auth ? auth : "", hostid ? "X-HostID: " : "",
707
+                          hostid ? hostid : "", hostid ? "\r\n" : "", uastr,
708
+                          (unsigned int) strlen (query), query) < 0)
709
+            {
710
+                logg ("!SubmitDetectionStats: Can't write to socket\n");
711
+                ret = 52;
712
+                closesocket (sd);
713
+                break;
714
+            }
715
+
716
+            pt = post;
717
+            cnt = sizeof (post) - 1;
618 718
 #ifdef SO_ERROR
619
-	    while((bread = wait_recv(sd, pt, cnt, 0, optget(opts, "ReceiveTimeout")->numarg)) > 0) {
719
+            while ((bread =
720
+                    wait_recv (sd, pt, cnt, 0,
721
+                               optget (opts, "ReceiveTimeout")->numarg)) > 0)
722
+            {
620 723
 #else
621
-	    while((bread = recv(sd, pt, cnt, 0)) > 0) {
724
+            while ((bread = recv (sd, pt, cnt, 0)) > 0)
725
+            {
622 726
 #endif
623
-		pt += bread;
624
-		cnt -= bread;
625
-		if(cnt <= 0)
626
-		    break;
627
-	    }
628
-	    *pt = 0;
629
-	    closesocket(sd);
630
-
631
-	    if(bread < 0) {
632
-		logg("!SubmitDetectionStats: Can't read from socket\n");
633
-		ret = 52;
634
-		break;
635
-	    }
636
-
637
-	    if(strstr(post, "SUBMIT_OK")) {
638
-		submitted += entries;
639
-		if(submitted + SUBMIT_MIN_ENTRIES > SUBMIT_MAX_ENTRIES)
640
-		    break;
641
-		qcnt = 0;
642
-		entries = 0;
643
-		memset(query, 0, sizeof(query));
644
-		continue;
645
-	    }
646
-
647
-	    ret = 52;
648
-	    if((pt = strstr(post, "SUBMIT_PERMANENT_FAILURE"))) {
649
-		if(!submitted) {
650
-		    permfail = 1;
651
-		    if((pt + 32 <= post + sizeof(post)) && pt[24] == ':')
652
-			logg("!SubmitDetectionStats: Remote server reported permanent failure: %s\n", &pt[25]);
653
-		    else
654
-			logg("!SubmitDetectionStats: Remote server reported permanent failure\n");
655
-		}
656
-	    } else if((pt = strstr(post, "SUBMIT_TEMPORARY_FAILURE"))) {
657
-		if(!submitted) {
658
-		    if((pt + 32 <= post + sizeof(post)) && pt[24] == ':')
659
-			logg("!SubmitDetectionStats: Remote server reported temporary failure: %s\n", &pt[25]);
660
-		    else
661
-			logg("!SubmitDetectionStats: Remote server reported temporary failure\n");
662
-		}
663
-	    } else {
664
-		if(!submitted)
665
-		    logg("!SubmitDetectionStats: Incorrect answer from server\n");
666
-	    }
667
-
668
-	    break;
669
-	}
670
-    }
671
-    closesocket(clamsockd);
672
-    if(auth)
673
-	free(auth);
674
-
675
-    if(ret == 0) {
676
-	if(!submitted) {
677
-	    logg("SubmitDetectionStats: Not enough recent data for submission\n");
678
-	} else {
679
-	    logg("SubmitDetectionStats: Submitted %u records\n", submitted);
680
-	    if((clamsockd = clamd_connect(clamdcfg, "SubmitDetectionStats")) != -1) {
681
-		sendln(clamsockd, "DETSTATSCLEAR", 14);
682
-		recv(clamsockd, query, sizeof(query), 0);
683
-		closesocket(clamsockd);
684
-	    }
685
-	}
727
+                pt += bread;
728
+                cnt -= bread;
729
+                if (cnt <= 0)
730
+                    break;
731
+            }
732
+            *pt = 0;
733
+            closesocket (sd);
734
+
735
+            if (bread < 0)
736
+            {
737
+                logg ("!SubmitDetectionStats: Can't read from socket\n");
738
+                ret = 52;
739
+                break;
740
+            }
741
+
742
+            if (strstr (post, "SUBMIT_OK"))
743
+            {
744
+                submitted += entries;
745
+                if (submitted + SUBMIT_MIN_ENTRIES > SUBMIT_MAX_ENTRIES)
746
+                    break;
747
+                qcnt = 0;
748
+                entries = 0;
749
+                memset (query, 0, sizeof (query));
750
+                continue;
751
+            }
752
+
753
+            ret = 52;
754
+            if ((pt = strstr (post, "SUBMIT_PERMANENT_FAILURE")))
755
+            {
756
+                if (!submitted)
757
+                {
758
+                    permfail = 1;
759
+                    if ((pt + 32 <= post + sizeof (post)) && pt[24] == ':')
760
+                        logg ("!SubmitDetectionStats: Remote server reported permanent failure: %s\n", &pt[25]);
761
+                    else
762
+                        logg ("!SubmitDetectionStats: Remote server reported permanent failure\n");
763
+                }
764
+            }
765
+            else if ((pt = strstr (post, "SUBMIT_TEMPORARY_FAILURE")))
766
+            {
767
+                if (!submitted)
768
+                {
769
+                    if ((pt + 32 <= post + sizeof (post)) && pt[24] == ':')
770
+                        logg ("!SubmitDetectionStats: Remote server reported temporary failure: %s\n", &pt[25]);
771
+                    else
772
+                        logg ("!SubmitDetectionStats: Remote server reported temporary failure\n");
773
+                }
774
+            }
775
+            else
776
+            {
777
+                if (!submitted)
778
+                    logg ("!SubmitDetectionStats: Incorrect answer from server\n");
779
+            }
780
+
781
+            break;
782
+        }
783
+    }
784
+    closesocket (clamsockd);
785
+    if (auth)
786
+        free (auth);
787
+
788
+    if (ret == 0)
789
+    {
790
+        if (!submitted)
791
+        {
792
+            logg ("SubmitDetectionStats: Not enough recent data for submission\n");
793
+        }
794
+        else
795
+        {
796
+            logg ("SubmitDetectionStats: Submitted %u records\n", submitted);
797
+            if ((clamsockd =
798
+                 clamd_connect (clamdcfg, "SubmitDetectionStats")) != -1)
799
+            {
800
+                sendln (clamsockd, "DETSTATSCLEAR", 14);
801
+                recv (clamsockd, query, sizeof (query), 0);
802
+                closesocket (clamsockd);
803
+            }
804
+        }
686 805
     }
687 806
     return ret;
688 807
 }
689 808
 #else
690
-int submitstats(const char *clamdcfg, const struct optstruct *opts)
809
+int
810
+submitstats (const char *clamdcfg, const struct optstruct *opts)
691 811
 {
692
-    logg("clamd not built, no statistics");
812
+    logg ("clamd not built, no statistics");
693 813
     return 52;
694 814
 }
695 815
 #endif
696 816
 
697
-static int Rfc2822DateTime(char *buf, time_t mtime)
817
+static int
818
+Rfc2822DateTime (char *buf, time_t mtime)
698 819
 {
699
-	struct tm *gmt;
820
+    struct tm *gmt;
700 821
 
701
-    gmt = gmtime(&mtime);
702
-    if (!gmt) {
703
-	logg("gmtime: %s\n", strerror(errno));
704
-	strcpy(buf, "ERROR");
705
-	return -1;
822
+    gmt = gmtime (&mtime);
823
+    if (!gmt)
824
+    {
825
+        logg ("gmtime: %s\n", strerror (errno));
826
+        strcpy (buf, "ERROR");
827
+        return -1;
706 828
     }
707
-    return strftime(buf, 36, "%a, %d %b %Y %X GMT", gmt);
829
+    return strftime (buf, 36, "%a, %d %b %Y %X GMT", gmt);
708 830
 }
709 831
 
710
-static struct cl_cvd *remote_cvdhead(const char *cvdfile, const char *localfile, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int *ims, int ctimeout, int rtimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist, unsigned int attempt)
832
+static struct cl_cvd *
833
+remote_cvdhead (const char *cvdfile, const char *localfile,
834
+                const char *hostname, char *ip, const char *localip,
835
+                const char *proxy, int port, const char *user,
836
+                const char *pass, const char *uas, int *ims, int ctimeout,
837
+                int rtimeout, struct mirdat *mdat, int logerr,
838
+                unsigned int can_whitelist, unsigned int attempt)
711 839
 {
712
-	char cmd[512], head[513], buffer[FILEBUFF], ipaddr[46], *ch, *tmp;
713
-	int bread, cnt, sd;
714
-	unsigned int i, j;
715
-	char *remotename = NULL, *authorization = NULL;
716
-	struct cl_cvd *cvd;
717
-	char last_modified[36], uastr[128];
718
-
719
-
720
-    if(proxy) {
721
-        remotename = malloc(strlen(hostname) + 8);
722
-	if(!remotename) {
723
-	    logg("!remote_cvdhead: Can't allocate memory for 'remotename'\n");
724
-	    return NULL;
725
-	}
726
-        sprintf(remotename, "http://%s", hostname);
727
-
728
-	if(user) {
729
-	    authorization = proxyauth(user, pass);
730
-	    if(!authorization) {
731
-		free(remotename);
732
-		return NULL;
733
-	    }
734
-	}
735
-    }
736
-
737
-    if(!access(localfile, R_OK)) {
738
-	cvd = cl_cvdhead(localfile);
739
-	if(!cvd) {
740
-	    logg("!remote_cvdhead: Can't parse file %s\n", localfile);
741
-	    free(remotename);
742
-	    free(authorization);
743
-	    return NULL;
744
-	}
745
-	Rfc2822DateTime(last_modified, (time_t) cvd->stime);
746
-	cl_cvdfree(cvd);
747
-    } else {
748
-	    time_t mtime = 1104119530;
749
-
750
-	Rfc2822DateTime(last_modified, mtime);
751
-	logg("*Assuming modification time in the past\n");
752
-    }
753
-
754
-    logg("*If-Modified-Since: %s\n", last_modified);
755
-
756
-    logg("Reading CVD header (%s): ", cvdfile);
757
-
758
-    if(uas)
759
-	strncpy(uastr, uas, sizeof(uastr));
840
+    char cmd[512], head[513], buffer[FILEBUFF], ipaddr[46], *ch, *tmp;
841
+    int bread, cnt, sd;
842
+    unsigned int i, j;
843
+    char *remotename = NULL, *authorization = NULL;
844
+    struct cl_cvd *cvd;
845
+    char last_modified[36], uastr[128];
846
+
847
+
848
+    if (proxy)
849
+    {
850
+        remotename = malloc (strlen (hostname) + 8);
851
+        if (!remotename)
852
+        {
853
+            logg ("!remote_cvdhead: Can't allocate memory for 'remotename'\n");
854
+            return NULL;
855
+        }
856
+        sprintf (remotename, "http://%s", hostname);
857
+
858
+        if (user)
859
+        {
860
+            authorization = proxyauth (user, pass);
861
+            if (!authorization)
862
+            {
863
+                free (remotename);
864
+                return NULL;
865
+            }
866
+        }
867
+    }
868
+
869
+    if (!access (localfile, R_OK))
870
+    {
871
+        cvd = cl_cvdhead (localfile);
872
+        if (!cvd)
873
+        {
874
+            logg ("!remote_cvdhead: Can't parse file %s\n", localfile);
875
+            free (remotename);
876
+            free (authorization);
877
+            return NULL;
878
+        }
879
+        Rfc2822DateTime (last_modified, (time_t) cvd->stime);
880
+        cl_cvdfree (cvd);
881
+    }
760 882
     else
761
-	snprintf(uastr, sizeof(uastr), PACKAGE"/%s (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")", get_version());
762
-    uastr[sizeof(uastr) - 1] = 0;
883
+    {
884
+        time_t mtime = 1104119530;
763 885
 
764
-    snprintf(cmd, sizeof(cmd),
765
-	"GET %s/%s HTTP/1.0\r\n"
766
-	"Host: %s\r\n%s"
767
-	"User-Agent: %s\r\n"
768
-	"Connection: close\r\n"
769
-	"Range: bytes=0-511\r\n"
770
-        "If-Modified-Since: %s\r\n"
771
-        "\r\n", (remotename != NULL) ? remotename : "", cvdfile, hostname, (authorization != NULL) ? authorization : "", uastr, last_modified);
886
+        Rfc2822DateTime (last_modified, mtime);
887
+        logg ("*Assuming modification time in the past\n");
888
+    }
772 889
 
773
-    free(remotename);
774
-    free(authorization);
890
+    logg ("*If-Modified-Since: %s\n", last_modified);
775 891
 
776
-    memset(ipaddr, 0, sizeof(ipaddr));
892
+    logg ("Reading CVD header (%s): ", cvdfile);
777 893
 
778
-    if(ip[0]) /* use ip to connect */
779
-	sd = wwwconnect(ip, proxy, port, ipaddr, localip, ctimeout, mdat, logerr, can_whitelist, attempt);
894
+    if (uas)
895
+        strncpy (uastr, uas, sizeof (uastr));
896
+    else
897
+        snprintf (uastr, sizeof (uastr),
898
+                  PACKAGE "/%s (OS: " TARGET_OS_TYPE ", ARCH: "
899
+                  TARGET_ARCH_TYPE ", CPU: " TARGET_CPU_TYPE ")",
900
+                  get_version ());
901
+    uastr[sizeof (uastr) - 1] = 0;
902
+
903
+    snprintf (cmd, sizeof (cmd),
904
+              "GET %s/%s HTTP/1.0\r\n"
905
+              "Host: %s\r\n%s"
906
+              "User-Agent: %s\r\n"
907
+              "Connection: close\r\n"
908
+              "Range: bytes=0-511\r\n"
909
+              "If-Modified-Since: %s\r\n"
910
+              "\r\n", (remotename != NULL) ? remotename : "", cvdfile,
911
+              hostname, (authorization != NULL) ? authorization : "", uastr,
912
+              last_modified);
913
+
914
+    free (remotename);
915
+    free (authorization);
916
+
917
+    memset (ipaddr, 0, sizeof (ipaddr));
918
+
919
+    if (ip[0])                  /* use ip to connect */
920
+        sd = wwwconnect (ip, proxy, port, ipaddr, localip, ctimeout, mdat,
921
+                         logerr, can_whitelist, attempt);
780 922
     else
781
-	sd = wwwconnect(hostname, proxy, port, ipaddr, localip, ctimeout, mdat, logerr, can_whitelist, attempt);
923
+        sd = wwwconnect (hostname, proxy, port, ipaddr, localip, ctimeout,
924
+                         mdat, logerr, can_whitelist, attempt);
782 925
 
783
-    if(sd < 0) {
784
-	return NULL;
785
-    } else {
786
-	logg("*Connected to %s (IP: %s).\n", hostname, ipaddr);
787
-	logg("*Trying to retrieve CVD header of http://%s/%s\n", hostname, cvdfile);
926
+    if (sd < 0)
927
+    {
928
+        return NULL;
929
+    }
930
+    else
931
+    {
932
+        logg ("*Connected to %s (IP: %s).\n", hostname, ipaddr);
933
+        logg ("*Trying to retrieve CVD header of http://%s/%s\n", hostname,
934
+              cvdfile);
788 935
     }
789 936
 
790
-    if(!ip[0])
791
-	strcpy(ip, ipaddr);
937
+    if (!ip[0])
938
+        strcpy (ip, ipaddr);
792 939
 
793
-    if(send(sd, cmd, strlen(cmd), 0) < 0) {
794
-	logg("%cremote_cvdhead: write failed\n", logerr ? '!' : '^');
795
-	closesocket(sd);
796
-	return NULL;
940
+    if (send (sd, cmd, strlen (cmd), 0) < 0)
941
+    {
942
+        logg ("%cremote_cvdhead: write failed\n", logerr ? '!' : '^');
943
+        closesocket (sd);
944
+        return NULL;
797 945
     }
798 946
 
799 947
     tmp = buffer;
800 948
     cnt = FILEBUFF;
801 949
 #ifdef SO_ERROR
802
-    while((bread = wait_recv(sd, tmp, cnt, 0, rtimeout)) > 0) {
950
+    while ((bread = wait_recv (sd, tmp, cnt, 0, rtimeout)) > 0)
951
+    {
803 952
 #else
804
-    while((bread = recv(sd, tmp, cnt, 0)) > 0) {
953
+    while ((bread = recv (sd, tmp, cnt, 0)) > 0)
954
+    {
805 955
 #endif
806
-	tmp += bread;
807
-	cnt -= bread;
808
-	if(cnt <= 0)
809
-	    break;
956
+        tmp += bread;
957
+        cnt -= bread;
958
+        if (cnt <= 0)
959
+            break;
810 960
     }
811
-    closesocket(sd);
961
+    closesocket (sd);
812 962
 
813
-    if(bread == -1) {
814
-	logg("%cremote_cvdhead: Error while reading CVD header from %s\n", logerr ? '!' : '^', hostname);
815
-	mirman_update(mdat->currip, mdat->af, mdat, 1);
816
-	return NULL;
963
+    if (bread == -1)
964
+    {
965
+        logg ("%cremote_cvdhead: Error while reading CVD header from %s\n",
966
+              logerr ? '!' : '^', hostname);
967
+        mirman_update (mdat->currip, mdat->af, mdat, 1);
968
+        return NULL;
817 969
     }
818 970
 
819
-    if((strstr(buffer, "HTTP/1.1 404")) != NULL || (strstr(buffer, "HTTP/1.0 404")) != NULL) { 
820
-	logg("%c%s not found on remote server\n", logerr ? '!' : '^', cvdfile);
821
-	mirman_update(mdat->currip, mdat->af, mdat, 2);
822
-	return NULL;
971
+    if ((strstr (buffer, "HTTP/1.1 404")) != NULL
972
+        || (strstr (buffer, "HTTP/1.0 404")) != NULL)
973
+    {
974
+        logg ("%c%s not found on remote server\n", logerr ? '!' : '^',
975
+              cvdfile);
976
+        mirman_update (mdat->currip, mdat->af, mdat, 2);
977
+        return NULL;
823 978
     }
824 979
 
825 980
     /* check whether the resource is up-to-date */
826
-    if((strstr(buffer, "HTTP/1.1 304")) != NULL || (strstr(buffer, "HTTP/1.0 304")) != NULL) { 
827
-	*ims = 0;
828
-	logg("OK (IMS)\n");
829
-	mirman_update(mdat->currip, mdat->af, mdat, 0);
830
-	return NULL;
831
-    } else {
832
-	*ims = 1;
981
+    if ((strstr (buffer, "HTTP/1.1 304")) != NULL
982
+        || (strstr (buffer, "HTTP/1.0 304")) != NULL)
983
+    {
984
+        *ims = 0;
985
+        logg ("OK (IMS)\n");
986
+        mirman_update (mdat->currip, mdat->af, mdat, 0);
987
+        return NULL;
988
+    }
989
+    else
990
+    {
991
+        *ims = 1;
833 992
     }
834 993
 
835
-    if(!strstr(buffer, "HTTP/1.1 200") && !strstr(buffer, "HTTP/1.0 200") &&
836
-       !strstr(buffer, "HTTP/1.1 206") && !strstr(buffer, "HTTP/1.0 206")) {
837
-	logg("%cUnknown response from remote server\n", logerr ? '!' : '^');
838
-	mirman_update(mdat->currip, mdat->af, mdat, 1);
839
-	return NULL;
994
+    if (!strstr (buffer, "HTTP/1.1 200") && !strstr (buffer, "HTTP/1.0 200")
995
+        && !strstr (buffer, "HTTP/1.1 206")
996
+        && !strstr (buffer, "HTTP/1.0 206"))
997
+    {
998
+        logg ("%cUnknown response from remote server\n", logerr ? '!' : '^');
999
+        mirman_update (mdat->currip, mdat->af, mdat, 1);
1000
+        return NULL;
840 1001
     }
841 1002
 
842 1003
     i = 3;
843 1004
     ch = buffer + i;
844
-    while(i < sizeof(buffer)) {
845
-	if (*ch == '\n' && *(ch - 1) == '\r' && *(ch - 2) == '\n' && *(ch - 3) == '\r') {
846
-	    ch++;
847
-	    i++;
848
-	    break;
849
-	}
850
-	ch++;
851
-	i++;
1005
+    while (i < sizeof (buffer))
1006
+    {
1007
+        if (*ch == '\n' && *(ch - 1) == '\r' && *(ch - 2) == '\n'
1008
+            && *(ch - 3) == '\r')
1009
+        {
1010
+            ch++;
1011
+            i++;
1012
+            break;
1013
+        }
1014
+        ch++;
1015
+        i++;
852 1016
     }
853 1017
 
854
-    if(sizeof(buffer) - i < 512) {
855
-	logg("%cremote_cvdhead: Malformed CVD header (too short)\n", logerr ? '!' : '^');
856
-	mirman_update(mdat->currip, mdat->af, mdat, 1);
857
-	return NULL;
1018
+    if (sizeof (buffer) - i < 512)
1019
+    {
1020
+        logg ("%cremote_cvdhead: Malformed CVD header (too short)\n",
1021
+              logerr ? '!' : '^');
1022
+        mirman_update (mdat->currip, mdat->af, mdat, 1);
1023
+        return NULL;
858 1024
     }
859 1025
 
860
-    memset(head, 0, sizeof(head));
1026
+    memset (head, 0, sizeof (head));
861 1027
 
862
-    for(j = 0; j < 512; j++) {
863
-	if(!ch || (ch && !*ch) || (ch && !isprint(ch[j]))) {
864
-	    logg("%cremote_cvdhead: Malformed CVD header (bad chars)\n", logerr ? '!' : '^');
865
-	    mirman_update(mdat->currip, mdat->af, mdat, 1);
866
-	    return NULL;
867
-	}
868
-	head[j] = ch[j];
1028
+    for (j = 0; j < 512; j++)
1029
+    {
1030
+        if (!ch || (ch && !*ch) || (ch && !isprint (ch[j])))
1031
+        {
1032
+            logg ("%cremote_cvdhead: Malformed CVD header (bad chars)\n",
1033
+                  logerr ? '!' : '^');
1034
+            mirman_update (mdat->currip, mdat->af, mdat, 1);
1035
+            return NULL;
1036
+        }
1037
+        head[j] = ch[j];
869 1038
     }
870 1039
 
871
-    if(!(cvd = cl_cvdparse(head))) {
872
-	logg("%cremote_cvdhead: Malformed CVD header (can't parse)\n", logerr ? '!' : '^');
873
-	mirman_update(mdat->currip, mdat->af, mdat, 1);
874
-    } else {
875
-	logg("OK\n");
876
-	mirman_update(mdat->currip, mdat->af, mdat, 0);
1040
+    if (!(cvd = cl_cvdparse (head)))
1041
+    {
1042
+        logg ("%cremote_cvdhead: Malformed CVD header (can't parse)\n",
1043
+              logerr ? '!' : '^');
1044
+        mirman_update (mdat->currip, mdat->af, mdat, 1);
1045
+    }
1046
+    else
1047
+    {
1048
+        logg ("OK\n");
1049
+        mirman_update (mdat->currip, mdat->af, mdat, 0);
877 1050
     }
878 1051
 
879 1052
     return cvd;
880 1053
 }
881 1054
 
882
-static int getfile_mirman(const char *srcfile, const char *destfile, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int ctimeout, int rtimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist, const char *ims, const char *ipaddr, int sd)
1055
+static int
1056
+getfile_mirman (const char *srcfile, const char *destfile,
1057
+                const char *hostname, char *ip, const char *localip,
1058
+                const char *proxy, int port, const char *user,
1059
+                const char *pass, const char *uas, int ctimeout, int rtimeout,
1060
+                struct mirdat *mdat, int logerr, unsigned int can_whitelist,
1061
+                const char *ims, const char *ipaddr, int sd)
883 1062
 {
884
-	char cmd[512], uastr[128], buffer[FILEBUFF], *ch;
885
-	int bread, fd, totalsize = 0,  rot = 0, totaldownloaded = 0,
886
-	    percentage = 0;
887
-	unsigned int i;
888
-	char *remotename = NULL, *authorization = NULL, *headerline;
889
-	const char *rotation = "|/-\\", *fname;
890
-
891
-
892
-    if(proxy) {
893
-        remotename = malloc(strlen(hostname) + 8);
894
-	if(!remotename) {
895
-	    logg("!getfile: Can't allocate memory for 'remotename'\n");
896
-	    return 75; /* FIXME */
897
-	}
898
-        sprintf(remotename, "http://%s", hostname);
899
-
900
-	if(user) {
901
-	    authorization = proxyauth(user, pass);
902
-	    if(!authorization) {
903
-		free(remotename);
904
-		return 75; /* FIXME */
905
-	    }
906
-	}
907
-    }
908
-
909
-    if(ims)
910
-	logg("*If-Modified-Since: %s\n", ims);
911
-
912
-    if(uas)
913
-	strncpy(uastr, uas, sizeof(uastr));
914
-    else
915
-	snprintf(uastr, sizeof(uastr), PACKAGE"/%s (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")", get_version());
916
-    uastr[sizeof(uastr) - 1] = 0;
1063
+    char cmd[512], uastr[128], buffer[FILEBUFF], *ch;
1064
+    int bread, fd, totalsize = 0, rot = 0, totaldownloaded = 0,
1065
+        percentage = 0;
1066
+    unsigned int i;
1067
+    char *remotename = NULL, *authorization = NULL, *headerline;
1068
+    const char *rotation = "|/-\\", *fname;
917 1069
 
918
-    snprintf(cmd, sizeof(cmd),
919
-	"GET %s/%s HTTP/1.0\r\n"
920
-	"Host: %s\r\n%s"
921
-	"User-Agent: %s\r\n"
1070
+
1071
+    if (proxy)
1072
+    {
1073
+        remotename = malloc (strlen (hostname) + 8);
1074
+        if (!remotename)
1075
+        {
1076
+            logg ("!getfile: Can't allocate memory for 'remotename'\n");
1077
+            return 75;          /* FIXME */
1078
+        }
1079
+        sprintf (remotename, "http://%s", hostname);
1080
+
1081
+        if (user)
1082
+        {
1083
+            authorization = proxyauth (user, pass);
1084
+            if (!authorization)
1085
+            {
1086
+                free (remotename);
1087
+                return 75;      /* FIXME */
1088
+            }
1089
+        }
1090
+    }
1091
+
1092
+    if (ims)
1093
+        logg ("*If-Modified-Since: %s\n", ims);
1094
+
1095
+    if (uas)
1096
+        strncpy (uastr, uas, sizeof (uastr));
1097
+    else
1098
+        snprintf (uastr, sizeof (uastr),
1099
+                  PACKAGE "/%s (OS: " TARGET_OS_TYPE ", ARCH: "
1100
+                  TARGET_ARCH_TYPE ", CPU: " TARGET_CPU_TYPE ")",
1101
+                  get_version ());
1102
+    uastr[sizeof (uastr) - 1] = 0;
1103
+
1104
+    snprintf (cmd, sizeof (cmd),
1105
+              "GET %s/%s HTTP/1.0\r\n" "Host: %s\r\n%s" "User-Agent: %s\r\n"
922 1106
 #ifdef FRESHCLAM_NO_CACHE
923
-	"Cache-Control: no-cache\r\n"
1107
+              "Cache-Control: no-cache\r\n"
924 1108
 #endif
925
-	"Connection: close\r\n"
926
-	"%s%s%s"
927
-	"\r\n", (remotename != NULL) ? remotename : "", srcfile, hostname, (authorization != NULL) ? authorization : "", uastr, ims ? "If-Modified-Since: " : "", ims ? ims : "", ims ? "\r\n": "");
1109
+              "Connection: close\r\n"
1110
+              "%s%s%s"
1111
+              "\r\n", (remotename != NULL) ? remotename : "", srcfile,
1112
+              hostname, (authorization != NULL) ? authorization : "", uastr,
1113
+              ims ? "If-Modified-Since: " : "", ims ? ims : "",
1114
+              ims ? "\r\n" : "");
928 1115
 
929
-    if(remotename)
930
-	free(remotename);
1116
+    if (remotename)
1117
+        free (remotename);
931 1118
 
932
-    if(authorization)
933
-	free(authorization);
1119
+    if (authorization)
1120
+        free (authorization);
934 1121
 
935
-    logg("*Trying to download http://%s/%s (IP: %s)\n", hostname, srcfile, ipaddr);
1122
+    logg ("*Trying to download http://%s/%s (IP: %s)\n", hostname, srcfile,
1123
+          ipaddr);
936 1124
 
937
-    if(ip && !ip[0])
938
-	strcpy(ip, ipaddr);
1125
+    if (ip && !ip[0])
1126
+        strcpy (ip, ipaddr);
939 1127
 
940
-    if(send(sd, cmd, strlen(cmd), 0) < 0) {
941
-	logg("%cgetfile: Can't write to socket\n", logerr ? '!' : '^');
942
-	return 52;
1128
+    if (send (sd, cmd, strlen (cmd), 0) < 0)
1129
+    {
1130
+        logg ("%cgetfile: Can't write to socket\n", logerr ? '!' : '^');
1131
+        return 52;
943 1132
     }
944 1133
 
945 1134
     /* read http headers */
946 1135
     ch = buffer;
947 1136
     i = 0;
948
-    while(1) {
949
-	/* recv one byte at a time, until we reach \r\n\r\n */
1137
+    while (1)
1138
+    {
1139
+        /* recv one byte at a time, until we reach \r\n\r\n */
950 1140
 #ifdef SO_ERROR
951
-	if((i >= sizeof(buffer) - 1) || wait_recv(sd, buffer + i, 1, 0, rtimeout) == -1) {
1141
+        if ((i >= sizeof (buffer) - 1)
1142
+            || wait_recv (sd, buffer + i, 1, 0, rtimeout) == -1)
1143
+        {
952 1144
 #else
953
-	if((i >= sizeof(buffer) - 1) || recv(sd, buffer + i, 1, 0) == -1) {
1145
+        if ((i >= sizeof (buffer) - 1) || recv (sd, buffer + i, 1, 0) == -1)
1146
+        {
954 1147
 #endif
955
-	    logg("%cgetfile: Error while reading database from %s (IP: %s): %s\n", logerr ? '!' : '^', hostname, ipaddr, strerror(errno));
956
-	    if(mdat)
957
-		mirman_update(mdat->currip, mdat->af, mdat, 1);
958
-	    return 52;
959
-	}
1148
+            logg ("%cgetfile: Error while reading database from %s (IP: %s): %s\n", logerr ? '!' : '^', hostname, ipaddr, strerror (errno));
1149
+            if (mdat)
1150
+                mirman_update (mdat->currip, mdat->af, mdat, 1);
1151
+            return 52;
1152
+        }
960 1153
 
961
-	if(i > 2 && *ch == '\n' && *(ch - 1) == '\r' && *(ch - 2) == '\n' && *(ch - 3) == '\r') {
962
-	    i++;
963
-	    break;
964
-	}
965
-	ch++;
966
-	i++;
1154
+        if (i > 2 && *ch == '\n' && *(ch - 1) == '\r' && *(ch - 2) == '\n'
1155
+            && *(ch - 3) == '\r')
1156
+        {
1157
+            i++;
1158
+            break;
1159
+        }
1160
+        ch++;
1161
+        i++;
967 1162
     }
968 1163
 
969 1164
     buffer[i] = 0;
970 1165
 
971 1166
     /* check whether the resource actually existed or not */
972
-    if((strstr(buffer, "HTTP/1.1 404")) != NULL || (strstr(buffer, "HTTP/1.0 404")) != NULL) { 
973
-	logg("^getfile: %s not found on remote server (IP: %s)\n", srcfile, ipaddr);
974
-	if(mdat)
975
-	    mirman_update(mdat->currip, mdat->af, mdat, 2);
976
-	return 58;
1167
+    if ((strstr (buffer, "HTTP/1.1 404")) != NULL
1168
+        || (strstr (buffer, "HTTP/1.0 404")) != NULL)
1169
+    {
1170
+        logg ("^getfile: %s not found on remote server (IP: %s)\n", srcfile,
1171
+              ipaddr);
1172
+        if (mdat)
1173
+            mirman_update (mdat->currip, mdat->af, mdat, 2);
1174
+        return 58;
977 1175
     }
978 1176
 
979 1177
     /* If-Modified-Since */
980
-    if(strstr(buffer, "HTTP/1.1 304") || strstr(buffer, "HTTP/1.0 304")) { 
981
-	if(mdat)
982
-	    mirman_update(mdat->currip, mdat->af, mdat, 0);
983
-	return 1;
1178
+    if (strstr (buffer, "HTTP/1.1 304") || strstr (buffer, "HTTP/1.0 304"))
1179
+    {
1180
+        if (mdat)
1181
+            mirman_update (mdat->currip, mdat->af, mdat, 0);
1182
+        return 1;
984 1183
     }
985 1184
 
986
-    if(!strstr(buffer, "HTTP/1.1 200") && !strstr(buffer, "HTTP/1.0 200") &&
987
-       !strstr(buffer, "HTTP/1.1 206") && !strstr(buffer, "HTTP/1.0 206")) {
988
-	logg("%cgetfile: Unknown response from remote server (IP: %s)\n", logerr ? '!' : '^', ipaddr);
989
-	if(mdat)
990
-	    mirman_update(mdat->currip, mdat->af, mdat, 1);
991
-	return 58;
1185
+    if (!strstr (buffer, "HTTP/1.1 200") && !strstr (buffer, "HTTP/1.0 200")
1186
+        && !strstr (buffer, "HTTP/1.1 206")
1187
+        && !strstr (buffer, "HTTP/1.0 206"))
1188
+    {
1189
+        logg ("%cgetfile: Unknown response from remote server (IP: %s)\n",
1190
+              logerr ? '!' : '^', ipaddr);
1191
+        if (mdat)
1192
+            mirman_update (mdat->currip, mdat->af, mdat, 1);
1193
+        return 58;
992 1194
     }
993 1195
 
994 1196
     /* get size of resource */
995
-    for(i = 0; (headerline = cli_strtok(buffer, i, "\n")); i++){
996
-        if(strstr(headerline, "Content-Length:")) { 
997
-	    if((ch = cli_strtok(headerline, 1, ": "))) {
998
-		totalsize = atoi(ch);
999
-		free(ch);
1000
-	    } else {
1001
-		totalsize = 0;
1002
-	    }
1197
+    for (i = 0; (headerline = cli_strtok (buffer, i, "\n")); i++)
1198
+    {
1199
+        if (strstr (headerline, "Content-Length:"))
1200
+        {
1201
+            if ((ch = cli_strtok (headerline, 1, ": ")))
1202
+            {
1203
+                totalsize = atoi (ch);
1204
+                free (ch);
1205
+            }
1206
+            else
1207
+            {
1208
+                totalsize = 0;
1209
+            }
1003 1210
         }
1004
-	free(headerline);
1211
+        free (headerline);
1005 1212
     }
1006 1213
 
1007
-    if((fd = open(destfile, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644)) == -1) {
1008
-	    char currdir[512];
1214
+    if ((fd =
1215
+         open (destfile, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0644)) == -1)
1216
+    {
1217
+        char currdir[512];
1009 1218
 
1010
-	if(getcwd(currdir, sizeof(currdir)))
1011
-	    logg("!getfile: Can't create new file %s in %s\n", destfile, currdir);
1012
-	else
1013
-	    logg("!getfile: Can't create new file %s in the current directory\n", destfile);
1219
+        if (getcwd (currdir, sizeof (currdir)))
1220
+            logg ("!getfile: Can't create new file %s in %s\n", destfile,
1221
+                  currdir);
1222
+        else
1223
+            logg ("!getfile: Can't create new file %s in the current directory\n", destfile);
1014 1224
 
1015
-	logg("Hint: The database directory must be writable for UID %d or GID %d\n", getuid(), getgid());
1016
-	return 57;
1225
+        logg ("Hint: The database directory must be writable for UID %d or GID %d\n", getuid (), getgid ());
1226
+        return 57;
1017 1227
     }
1018 1228
 
1019
-    if((fname = strrchr(srcfile, '/')))
1020
-	fname++;
1229
+    if ((fname = strrchr (srcfile, '/')))
1230
+        fname++;
1021 1231
     else
1022
-	fname = srcfile;
1232
+        fname = srcfile;
1023 1233
 
1024 1234
 #ifdef SO_ERROR
1025
-    while((bread = wait_recv(sd, buffer, FILEBUFF, 0, rtimeout)) > 0) {
1235
+    while ((bread = wait_recv (sd, buffer, FILEBUFF, 0, rtimeout)) > 0)
1236
+    {
1026 1237
 #else
1027
-    while((bread = recv(sd, buffer, FILEBUFF, 0)) > 0) {
1238
+    while ((bread = recv (sd, buffer, FILEBUFF, 0)) > 0)
1239
+    {
1028 1240
 #endif
1029
-        if(write(fd, buffer, bread) != bread) {
1030
-	    logg("getfile: Can't write %d bytes to %s\n", bread, destfile);
1031
-	    close(fd);
1032
-	    unlink(destfile);
1033
-	    return 57; /* FIXME */
1034
-	}
1035
-
1036
-	totaldownloaded += bread;
1037
-        if(totalsize > 0)
1241
+        if (write (fd, buffer, bread) != bread)
1242
+        {
1243
+            logg ("getfile: Can't write %d bytes to %s\n", bread, destfile);
1244
+            close (fd);
1245
+            unlink (destfile);
1246
+            return 57;          /* FIXME */
1247
+        }
1248
+
1249
+        totaldownloaded += bread;
1250
+        if (totalsize > 0)
1038 1251
             percentage = (int) (100 * (float) totaldownloaded / totalsize);
1039 1252
 
1040
-        if(!mprintf_quiet) {
1041
-            if(totalsize > 0) {
1042
-                mprintf("Downloading %s [%3i%%]\r", fname, percentage);
1043
-            } else {
1044
-                mprintf("Downloading %s [%c]\r", fname, rotation[rot]);
1253
+        if (!mprintf_quiet)
1254
+        {
1255
+            if (totalsize > 0)
1256
+            {
1257
+                mprintf ("Downloading %s [%3i%%]\r", fname, percentage);
1258
+            }
1259
+            else
1260
+            {
1261
+                mprintf ("Downloading %s [%c]\r", fname, rotation[rot]);
1045 1262
                 rot++;
1046 1263
                 rot %= 4;
1047 1264
             }
1048
-            fflush(stdout);
1265
+            fflush (stdout);
1049 1266
         }
1050 1267
     }
1051
-    close(fd);
1268
+    close (fd);
1052 1269
 
1053
-    if(bread == -1) {
1054
-	logg("%cgetfile: Download interrupted: %s (IP: %s)\n", logerr ? '!' : '^', strerror(errno), ipaddr);
1055
-	if(mdat)
1056
-	    mirman_update(mdat->currip, mdat->af, mdat, 2);
1057
-	return 52;
1270
+    if (bread == -1)
1271
+    {
1272
+        logg ("%cgetfile: Download interrupted: %s (IP: %s)\n",
1273
+              logerr ? '!' : '^', strerror (errno), ipaddr);
1274
+        if (mdat)
1275
+            mirman_update (mdat->currip, mdat->af, mdat, 2);
1276
+        return 52;
1058 1277
     }
1059 1278
 
1060
-    if(!totaldownloaded)
1061
-	return 53;
1279
+    if (!totaldownloaded)
1280
+        return 53;
1062 1281
 
1063
-    if(totalsize > 0)
1064
-        logg("Downloading %s [100%%]\n", fname);
1282
+    if (totalsize > 0)
1283
+        logg ("Downloading %s [100%%]\n", fname);
1065 1284
     else
1066
-        logg("Downloading %s [*]\n", fname);
1285
+        logg ("Downloading %s [*]\n", fname);
1067 1286
 
1068
-    if(mdat)
1069
-	mirman_update(mdat->currip, mdat->af, mdat, 0);
1287
+    if (mdat)
1288
+        mirman_update (mdat->currip, mdat->af, mdat, 0);
1070 1289
     return 0;
1071 1290
 }
1072 1291
 
1073
-static int getfile(const char *srcfile, const char *destfile, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int ctimeout, int rtimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist, const char *ims, const struct optstruct *opts, unsigned int attempt)
1292
+static int
1293
+getfile (const char *srcfile, const char *destfile, const char *hostname,
1294
+         char *ip, const char *localip, const char *proxy, int port,
1295
+         const char *user, const char *pass, const char *uas, int ctimeout,
1296
+         int rtimeout, struct mirdat *mdat, int logerr,
1297
+         unsigned int can_whitelist, const char *ims,
1298
+         const struct optstruct *opts, unsigned int attempt)
1074 1299
 {
1075
-	int ret, sd;
1076
-	char ipaddr[46];
1300
+    int ret, sd;
1301
+    char ipaddr[46];
1077 1302
 
1078
-    memset(ipaddr, 0, sizeof(ipaddr));
1079
-    if(ip && ip[0]) /* use ip to connect */
1080
-	sd = wwwconnect(ip, proxy, port, ipaddr, localip, ctimeout, mdat, logerr, can_whitelist, attempt);
1303
+    memset (ipaddr, 0, sizeof (ipaddr));
1304
+    if (ip && ip[0])            /* use ip to connect */
1305
+        sd = wwwconnect (ip, proxy, port, ipaddr, localip, ctimeout, mdat,
1306
+                         logerr, can_whitelist, attempt);
1081 1307
     else
1082
-	sd = wwwconnect(hostname, proxy, port, ipaddr, localip, ctimeout, mdat, logerr, can_whitelist, attempt);
1308
+        sd = wwwconnect (hostname, proxy, port, ipaddr, localip, ctimeout,
1309
+                         mdat, logerr, can_whitelist, attempt);
1083 1310
 
1084
-    if(sd < 0)
1085
-	return 52;
1311
+    if (sd < 0)
1312
+        return 52;
1086 1313
 
1087
-    if(mdat) {
1088
-	mirman_update_sf(mdat->currip, mdat->af, mdat, 0, 1);
1089
-	mirman_write("mirrors.dat", dbdir, mdat);
1314
+    if (mdat)
1315
+    {
1316
+        mirman_update_sf (mdat->currip, mdat->af, mdat, 0, 1);
1317
+        mirman_write ("mirrors.dat", dbdir, mdat);
1090 1318
     }
1091 1319
 
1092
-    ret = getfile_mirman(srcfile, destfile, hostname, ip, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, mdat, logerr, can_whitelist, ims, ipaddr, sd);
1093
-    closesocket(sd);
1320
+    ret =
1321
+        getfile_mirman (srcfile, destfile, hostname, ip, localip, proxy, port,
1322
+                        user, pass, uas, ctimeout, rtimeout, mdat, logerr,
1323
+                        can_whitelist, ims, ipaddr, sd);
1324
+    closesocket (sd);
1094 1325
 
1095
-    if(mdat) {
1096
-	mirman_update_sf(mdat->currip, mdat->af, mdat, 0, -1);
1097
-	mirman_write("mirrors.dat", dbdir, mdat);
1326
+    if (mdat)
1327
+    {
1328
+        mirman_update_sf (mdat->currip, mdat->af, mdat, 0, -1);
1329
+        mirman_write ("mirrors.dat", dbdir, mdat);
1098 1330
     }
1099 1331
 
1100 1332
     return ret;
1101 1333
 }
1102 1334
 
1103
-static int getcvd(const char *cvdfile, const char *newfile, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, unsigned int newver, int ctimeout, int rtimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist, const struct optstruct *opts, unsigned int attempt)
1335
+static int
1336
+getcvd (const char *cvdfile, const char *newfile, const char *hostname,
1337
+        char *ip, const char *localip, const char *proxy, int port,
1338
+        const char *user, const char *pass, const char *uas,
1339
+        unsigned int newver, int ctimeout, int rtimeout, struct mirdat *mdat,
1340
+        int logerr, unsigned int can_whitelist, const struct optstruct *opts,
1341
+        unsigned int attempt)
1104 1342
 {
1105
-	struct cl_cvd *cvd;
1106
-	int ret;
1343
+    struct cl_cvd *cvd;
1344
+    int ret;
1107 1345
 
1108 1346
 
1109
-    logg("*Retrieving http://%s/%s\n", hostname, cvdfile);
1347
+    logg ("*Retrieving http://%s/%s\n", hostname, cvdfile);
1110 1348
 
1111
-    if((ret = getfile(cvdfile, newfile, hostname, ip, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, mdat, logerr, can_whitelist, NULL, opts, attempt))) {
1112
-        logg("%cCan't download %s from %s\n", logerr ? '!' : '^', cvdfile, hostname);
1113
-        unlink(newfile);
1349
+    if ((ret =
1350
+         getfile (cvdfile, newfile, hostname, ip, localip, proxy, port, user,
1351
+                  pass, uas, ctimeout, rtimeout, mdat, logerr, can_whitelist,
1352
+                  NULL, opts, attempt)))
1353
+    {
1354
+        logg ("%cCan't download %s from %s\n", logerr ? '!' : '^', cvdfile,
1355
+              hostname);
1356
+        unlink (newfile);
1114 1357
         return ret;
1115 1358
     }
1116 1359
 
1117
-    if((ret = cl_cvdverify(newfile))) {
1118
-        logg("!Verification: %s\n", cl_strerror(ret));
1119
-        unlink(newfile);
1360
+    if ((ret = cl_cvdverify (newfile)))
1361
+    {
1362
+        logg ("!Verification: %s\n", cl_strerror (ret));
1363
+        unlink (newfile);
1120 1364
         return 54;
1121 1365
     }
1122 1366
 
1123
-    if(!(cvd = cl_cvdhead(newfile))) {
1124
-	logg("!Can't read CVD header of new %s database.\n", cvdfile);
1125
-	unlink(newfile);
1126
-	return 54;
1367
+    if (!(cvd = cl_cvdhead (newfile)))
1368
+    {
1369
+        logg ("!Can't read CVD header of new %s database.\n", cvdfile);
1370
+        unlink (newfile);
1371
+        return 54;
1127 1372
     }
1128 1373
 
1129
-    if(cvd->version < newver) {
1130
-	logg("^Mirror %s is not synchronized.\n", ip);
1131
-	mirman_update(mdat->currip, mdat->af, mdat, 2);
1132
-    	cl_cvdfree(cvd);
1133
-	unlink(newfile);
1134
-	return 59;
1374
+    if (cvd->version < newver)
1375
+    {
1376
+        logg ("^Mirror %s is not synchronized.\n", ip);
1377
+        mirman_update (mdat->currip, mdat->af, mdat, 2);
1378
+        cl_cvdfree (cvd);
1379
+        unlink (newfile);
1380
+        return 59;
1135 1381
     }
1136 1382
 
1137
-    cl_cvdfree(cvd);
1383
+    cl_cvdfree (cvd);
1138 1384
     return 0;
1139 1385
 }
1140 1386
 
1141
-static int chdir_tmp(const char *dbname, const char *tmpdir)
1387
+static int
1388
+chdir_tmp (const char *dbname, const char *tmpdir)
1142 1389
 {
1143
-	char cvdfile[32];
1390
+    char cvdfile[32];
1144 1391
 
1145 1392
 
1146
-    if(access(tmpdir, R_OK|W_OK) == -1) {
1147
-	sprintf(cvdfile, "%s.cvd", dbname);
1148
-	if(access(cvdfile, R_OK) == -1) {
1149
-	    sprintf(cvdfile, "%s.cld", dbname);
1150
-	    if(access(cvdfile, R_OK) == -1) {
1151
-		logg("!chdir_tmp: Can't access local %s database\n", dbname);
1152
-		return -1;
1153
-	    }
1154
-	}
1393
+    if (access (tmpdir, R_OK | W_OK) == -1)
1394
+    {
1395
+        sprintf (cvdfile, "%s.cvd", dbname);
1396
+        if (access (cvdfile, R_OK) == -1)
1397
+        {
1398
+            sprintf (cvdfile, "%s.cld", dbname);
1399
+            if (access (cvdfile, R_OK) == -1)
1400
+            {
1401
+                logg ("!chdir_tmp: Can't access local %s database\n", dbname);
1402
+                return -1;
1403
+            }
1404
+        }
1155 1405
 
1156
-	if(mkdir(tmpdir, 0755) == -1) {
1157
-	    logg("!chdir_tmp: Can't create directory %s\n", tmpdir);
1158
-	    return -1;
1159
-	}
1406
+        if (mkdir (tmpdir, 0755) == -1)
1407
+        {
1408
+            logg ("!chdir_tmp: Can't create directory %s\n", tmpdir);
1409
+            return -1;
1410
+        }
1160 1411
 
1161
-	if(cli_cvdunpack(cvdfile, tmpdir) == -1) {
1162
-	    logg("!chdir_tmp: Can't unpack %s into %s\n", cvdfile, tmpdir);
1163
-	    cli_rmdirs(tmpdir);
1164
-	    return -1;
1165
-	}
1412
+        if (cli_cvdunpack (cvdfile, tmpdir) == -1)
1413
+        {
1414
+            logg ("!chdir_tmp: Can't unpack %s into %s\n", cvdfile, tmpdir);
1415
+            cli_rmdirs (tmpdir);
1416
+            return -1;
1417
+        }
1166 1418
     }
1167 1419
 
1168
-    if(chdir(tmpdir) == -1) {
1169
-	logg("!chdir_tmp: Can't change directory to %s\n", tmpdir);
1170
-	return -1;
1420
+    if (chdir (tmpdir) == -1)
1421
+    {
1422
+        logg ("!chdir_tmp: Can't change directory to %s\n", tmpdir);
1423
+        return -1;
1171 1424
     }
1172 1425
 
1173 1426
     return 0;
1174 1427
 }
1175 1428
 
1176
-static int getpatch(const char *dbname, const char *tmpdir, int version, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int ctimeout, int rtimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist, const struct optstruct *opts, unsigned int attempt)
1429
+static int
1430
+getpatch (const char *dbname, const char *tmpdir, int version,
1431
+          const char *hostname, char *ip, const char *localip,
1432
+          const char *proxy, int port, const char *user, const char *pass,
1433
+          const char *uas, int ctimeout, int rtimeout, struct mirdat *mdat,
1434
+          int logerr, unsigned int can_whitelist,
1435
+          const struct optstruct *opts, unsigned int attempt)
1177 1436
 {
1178
-	char *tempname, patch[32], olddir[512];
1179
-	int ret, fd;
1437
+    char *tempname, patch[32], olddir[512];
1438
+    int ret, fd;
1180 1439
 
1181 1440
 
1182
-    if(!getcwd(olddir, sizeof(olddir))) {
1183
-	logg("!getpatch: Can't get path of current working directory\n");
1184
-	return 50; /* FIXME */
1441
+    if (!getcwd (olddir, sizeof (olddir)))
1442
+    {
1443
+        logg ("!getpatch: Can't get path of current working directory\n");
1444
+        return 50;              /* FIXME */
1185 1445
     }
1186 1446
 
1187
-    if(chdir_tmp(dbname, tmpdir) == -1)
1188
-	return 50;
1447
+    if (chdir_tmp (dbname, tmpdir) == -1)
1448
+        return 50;
1189 1449
 
1190
-    tempname = cli_gentemp(".");
1191
-    snprintf(patch, sizeof(patch), "%s-%d.cdiff", dbname, version);
1450
+    tempname = cli_gentemp (".");
1451
+    snprintf (patch, sizeof (patch), "%s-%d.cdiff", dbname, version);
1192 1452
 
1193
-    logg("*Retrieving http://%s/%s\n", hostname, patch);
1194
-    if((ret = getfile(patch, tempname, hostname, ip, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, mdat, logerr, can_whitelist, NULL, opts, attempt))) {
1195
-	if(ret == 53)
1196
-	    logg("Empty script %s, need to download entire database\n", patch);
1197
-	else
1198
-	    logg("%cgetpatch: Can't download %s from %s\n", logerr ? '!' : '^', patch, hostname);
1199
-        unlink(tempname);
1200
-        free(tempname);
1201
-	CHDIR_ERR(olddir);
1453
+    logg ("*Retrieving http://%s/%s\n", hostname, patch);
1454
+    if ((ret =
1455
+         getfile (patch, tempname, hostname, ip, localip, proxy, port, user,
1456
+                  pass, uas, ctimeout, rtimeout, mdat, logerr, can_whitelist,
1457
+                  NULL, opts, attempt)))
1458
+    {
1459
+        if (ret == 53)
1460
+            logg ("Empty script %s, need to download entire database\n",
1461
+                  patch);
1462
+        else
1463
+            logg ("%cgetpatch: Can't download %s from %s\n",
1464
+                  logerr ? '!' : '^', patch, hostname);
1465
+        unlink (tempname);
1466
+        free (tempname);
1467
+        CHDIR_ERR (olddir);
1202 1468
         return ret;
1203 1469
     }
1204 1470
 
1205
-    if((fd = open(tempname, O_RDONLY|O_BINARY)) == -1) {
1206
-	logg("!getpatch: Can't open %s for reading\n", tempname);
1207
-        unlink(tempname);
1208
-        free(tempname);
1209
-	CHDIR_ERR(olddir);
1210
-	return 55;
1211
-    }
1212
-
1213
-    if(cdiff_apply(fd, 1) == -1) {
1214
-	logg("!getpatch: Can't apply patch\n");
1215
-	close(fd);
1216
-        unlink(tempname);
1217
-        free(tempname);
1218
-	CHDIR_ERR(olddir);
1219
-	return 70; /* FIXME */
1471
+    if ((fd = open (tempname, O_RDONLY | O_BINARY)) == -1)
1472
+    {
1473
+        logg ("!getpatch: Can't open %s for reading\n", tempname);
1474
+        unlink (tempname);
1475
+        free (tempname);
1476
+        CHDIR_ERR (olddir);
1477
+        return 55;
1220 1478
     }
1221 1479
 
1222
-    close(fd);
1223
-    unlink(tempname);
1224
-    free(tempname);
1225
-    if(chdir(olddir) == -1) {
1226
-	logg("!getpatch: Can't chdir to %s\n", olddir);
1227
-	return 50; /* FIXME */
1480
+    if (cdiff_apply (fd, 1) == -1)
1481
+    {
1482
+        logg ("!getpatch: Can't apply patch\n");
1483
+        close (fd);
1484
+        unlink (tempname);
1485
+        free (tempname);
1486
+        CHDIR_ERR (olddir);
1487
+        return 70;              /* FIXME */
1488
+    }
1489
+
1490
+    close (fd);
1491
+    unlink (tempname);
1492
+    free (tempname);
1493
+    if (chdir (olddir) == -1)
1494
+    {
1495
+        logg ("!getpatch: Can't chdir to %s\n", olddir);
1496
+        return 50;              /* FIXME */
1228 1497
     }
1229 1498
     return 0;
1230 1499
 }
1231 1500
 
1232
-static struct cl_cvd *currentdb(const char *dbname, char *localname)
1501
+static struct cl_cvd *
1502
+currentdb (const char *dbname, char *localname)
1233 1503
 {
1234
-	char db[32];
1235
-	struct cl_cvd *cvd = NULL;
1504
+    char db[32];
1505
+    struct cl_cvd *cvd = NULL;
1236 1506
 
1237 1507
 
1238
-    snprintf(db, sizeof(db), "%s.cvd", dbname);
1239
-    if(localname)
1240
-	strcpy(localname, db);
1508
+    snprintf (db, sizeof (db), "%s.cvd", dbname);
1509
+    if (localname)
1510
+        strcpy (localname, db);
1241 1511
 
1242
-    if(access(db, R_OK) == -1) {
1243
-	snprintf(db, sizeof(db), "%s.cld", dbname);
1244
-	if(localname)
1245
-	    strcpy(localname, db);
1512
+    if (access (db, R_OK) == -1)
1513
+    {
1514
+        snprintf (db, sizeof (db), "%s.cld", dbname);
1515
+        if (localname)
1516
+            strcpy (localname, db);
1246 1517
     }
1247 1518
 
1248
-    if(!access(db, R_OK))
1249
-	cvd = cl_cvdhead(db);
1519
+    if (!access (db, R_OK))
1520
+        cvd = cl_cvdhead (db);
1250 1521
 
1251 1522
     return cvd;
1252 1523
 }
1253 1524
 
1254
-static int buildcld(const char *tmpdir, const char *dbname, const char *newfile, unsigned int compr)
1525
+static int
1526
+buildcld (const char *tmpdir, const char *dbname, const char *newfile,
1527
+          unsigned int compr)
1255 1528
 {
1256
-	DIR *dir;
1257
-	char cwd[512], info[32], buff[513], *pt;
1258
-	struct dirent *dent;
1259
-	int fd, err = 0;
1260
-	gzFile gzs = NULL;
1529
+    DIR *dir;
1530
+    char cwd[512], info[32], buff[513], *pt;
1531
+    struct dirent *dent;
1532
+    int fd, err = 0;
1533
+    gzFile gzs = NULL;
1261 1534
 
1262
-    if(!getcwd(cwd, sizeof(cwd))) {
1263
-	logg("!buildcld: Can't get path of current working directory\n");
1264
-	return -1;
1535
+    if (!getcwd (cwd, sizeof (cwd)))
1536
+    {
1537
+        logg ("!buildcld: Can't get path of current working directory\n");
1538
+        return -1;
1265 1539
     }
1266 1540
 
1267
-    if(chdir(tmpdir) == -1) {
1268
-	logg("!buildcld: Can't access directory %s\n", tmpdir);
1269
-	return -1;
1541
+    if (chdir (tmpdir) == -1)
1542
+    {
1543
+        logg ("!buildcld: Can't access directory %s\n", tmpdir);
1544
+        return -1;
1270 1545
     }
1271 1546
 
1272
-    snprintf(info, sizeof(info), "%s.info", dbname);
1273
-    if((fd = open(info, O_RDONLY|O_BINARY)) == -1) {
1274
-	logg("!buildcld: Can't open %s\n", info);
1275
-	CHDIR_ERR(cwd);
1276
-	return -1;
1547
+    snprintf (info, sizeof (info), "%s.info", dbname);
1548
+    if ((fd = open (info, O_RDONLY | O_BINARY)) == -1)
1549
+    {
1550
+        logg ("!buildcld: Can't open %s\n", info);
1551
+        CHDIR_ERR (cwd);
1552
+        return -1;
1277 1553
     }
1278 1554
 
1279
-    if(read(fd, buff, 512) == -1) {
1280
-	logg("!buildcld: Can't read %s\n", info);
1281
-	CHDIR_ERR(cwd);
1282
-	close(fd);
1283
-	return -1;
1555
+    if (read (fd, buff, 512) == -1)
1556
+    {
1557
+        logg ("!buildcld: Can't read %s\n", info);
1558
+        CHDIR_ERR (cwd);
1559
+        close (fd);
1560
+        return -1;
1284 1561
     }
1285 1562
     buff[512] = 0;
1286
-    close(fd);
1287
-
1288
-    if(!(pt = strchr(buff, '\n'))) {
1289
-	logg("!buildcld: Bad format of %s\n", info);
1290
-	CHDIR_ERR(cwd);
1291
-	return -1;
1292
-    }
1293
-    memset(pt, ' ', 512 + buff - pt);
1294
-
1295
-    if((fd = open(newfile, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644)) == -1) {
1296
-	logg("!buildcld: Can't open %s for writing\n", newfile);
1297
-	CHDIR_ERR(cwd);
1298
-	return -1;
1299
-    }
1300
-    if(write(fd, buff, 512) != 512) {
1301
-	logg("!buildcld: Can't write to %s\n", newfile);
1302
-	CHDIR_ERR(cwd);
1303
-	close(fd);
1304
-	unlink(newfile);
1305
-	return -1;
1306
-    }
1307
-
1308
-    if((dir = opendir(".")) == NULL) {
1309
-	logg("!buildcld: Can't open directory %s\n", tmpdir);
1310
-	CHDIR_ERR(cwd);
1311
-	close(fd);
1312
-	unlink(newfile);
1313
-	return -1;
1314
-    }
1315
-
1316
-    if(compr) {
1317
-	close(fd);
1318
-	if(!(gzs = gzopen(newfile, "ab9f"))) {
1319
-	    logg("!buildcld: gzopen() failed for %s\n", newfile);
1320
-	    CHDIR_ERR(cwd);
1321
-	    closedir(dir);
1322
-	    unlink(newfile);
1323
-	    return -1;
1324
-	}
1325
-    }
1326
-
1327
-    if(access("COPYING", R_OK)) {
1328
-	logg("!buildcld: COPYING file not found\n");
1329
-	err = 1;
1330
-    } else {
1331
-	if(tar_addfile(fd, gzs, "COPYING") == -1) {
1332
-	    logg("!buildcld: Can't add COPYING to new %s.cld - please check if there is enough disk space available\n", dbname);
1333
-	    if(!strcmp(dbname, "main") || !strcmp(dbname, "safebrowsing"))
1334
-		logg("Updates to main.cvd or safebrowsing.cvd may require 200MB of disk space or more\n");
1335
-	    err = 1;
1336
-	}
1337
-    }
1338
-
1339
-    if(!err && !access(info, R_OK)) {
1340
-	if(tar_addfile(fd, gzs, info) == -1) {
1341
-	    logg("!buildcld: Can't add %s to new %s.cld - please check if there is enough disk space available\n", info, dbname);
1342
-	    if(!strcmp(dbname, "main") || !strcmp(dbname, "safebrowsing"))
1343
-		logg("Updates to main.cvd or safebrowsing.cvd may require 200MB of disk space or more\n");
1344
-	    err = 1;
1345
-	}
1346
-    }
1347
-
1348
-    if(!err && !access("daily.cfg", R_OK)) {
1349
-	if(tar_addfile(fd, gzs, "daily.cfg") == -1) {
1350
-	    logg("!buildcld: Can't add daily.cfg to new %s.cld - please check if there is enough disk space available\n", dbname);
1351
-	    err = 1;
1352
-	}
1353
-    }
1354
-
1355
-    if(err) {
1356
-	CHDIR_ERR(cwd);
1357
-	if(gzs)
1358
-	    gzclose(gzs);
1359
-	else
1360
-	    close(fd);
1361
-	closedir(dir);
1362
-	unlink(newfile);
1363
-	return -1;
1364
-    }
1365
-
1366
-    while((dent = readdir(dir))) {
1367
-	if(dent->d_ino)
1368
-	{
1369
-	    if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..") || !strcmp(dent->d_name, "COPYING") || !strcmp(dent->d_name, "daily.cfg") || !strcmp(dent->d_name, info))
1370
-		continue;
1371
-
1372
-	    if(tar_addfile(fd, gzs, dent->d_name) == -1) {
1373
-		logg("!buildcld: Can't add %s to new %s.cld - please check if there is enough disk space available\n", dent->d_name, dbname);
1374
-		if(!strcmp(dbname, "main") || !strcmp(dbname, "safebrowsing"))
1375
-		    logg("Updates to main.cvd or safebrowsing.cvd may require 200MB of disk space or more\n");
1376
-		CHDIR_ERR(cwd);
1377
-		if(gzs)
1378
-		    gzclose(gzs);
1379
-		else
1380
-		    close(fd);
1381
-		closedir(dir);
1382
-		unlink(newfile);
1383
-		return -1;
1384
-	    }
1385
-	}
1386
-    }
1387
-    closedir(dir);
1388
-
1389
-    if(gzs) {
1390
-	if(gzclose(gzs)) {
1391
-	    logg("!buildcld: gzclose() failed for %s\n", newfile);
1392
-	    unlink(newfile);
1393
-	    return -1;
1394
-	}
1395
-    } else {
1396
-	if(close(fd) == -1) {
1397
-	    logg("!buildcld: close() failed for %s\n", newfile);
1398
-	    unlink(newfile);
1399
-	    return -1;
1400
-	}
1401
-    }
1402
-
1403
-    if(chdir(cwd) == -1) {
1404
-	logg("!buildcld: Can't return to previous directory %s\n", cwd);
1405
-	return -1;
1563
+    close (fd);
1564
+
1565
+    if (!(pt = strchr (buff, '\n')))
1566
+    {
1567
+        logg ("!buildcld: Bad format of %s\n", info);
1568
+        CHDIR_ERR (cwd);
1569
+        return -1;
1570
+    }
1571
+    memset (pt, ' ', 512 + buff - pt);
1572
+
1573
+    if ((fd =
1574
+         open (newfile, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0644)) == -1)
1575
+    {
1576
+        logg ("!buildcld: Can't open %s for writing\n", newfile);
1577
+        CHDIR_ERR (cwd);
1578
+        return -1;
1579
+    }
1580
+    if (write (fd, buff, 512) != 512)
1581
+    {
1582
+        logg ("!buildcld: Can't write to %s\n", newfile);
1583
+        CHDIR_ERR (cwd);
1584
+        close (fd);
1585
+        unlink (newfile);
1586
+        return -1;
1587
+    }
1588
+
1589
+    if ((dir = opendir (".")) == NULL)
1590
+    {
1591
+        logg ("!buildcld: Can't open directory %s\n", tmpdir);
1592
+        CHDIR_ERR (cwd);
1593
+        close (fd);
1594
+        unlink (newfile);
1595
+        return -1;
1596
+    }
1597
+
1598
+    if (compr)
1599
+    {
1600
+        close (fd);
1601
+        if (!(gzs = gzopen (newfile, "ab9f")))
1602
+        {
1603
+            logg ("!buildcld: gzopen() failed for %s\n", newfile);
1604
+            CHDIR_ERR (cwd);
1605
+            closedir (dir);
1606
+            unlink (newfile);
1607
+            return -1;
1608
+        }
1609
+    }
1610
+
1611
+    if (access ("COPYING", R_OK))
1612
+    {
1613
+        logg ("!buildcld: COPYING file not found\n");
1614
+        err = 1;
1615
+    }
1616
+    else
1617
+    {
1618
+        if (tar_addfile (fd, gzs, "COPYING") == -1)
1619
+        {
1620
+            logg ("!buildcld: Can't add COPYING to new %s.cld - please check if there is enough disk space available\n", dbname);
1621
+            if (!strcmp (dbname, "main") || !strcmp (dbname, "safebrowsing"))
1622
+                logg ("Updates to main.cvd or safebrowsing.cvd may require 200MB of disk space or more\n");
1623
+            err = 1;
1624
+        }
1625
+    }
1626
+
1627
+    if (!err && !access (info, R_OK))
1628
+    {
1629
+        if (tar_addfile (fd, gzs, info) == -1)
1630
+        {
1631
+            logg ("!buildcld: Can't add %s to new %s.cld - please check if there is enough disk space available\n", info, dbname);
1632
+            if (!strcmp (dbname, "main") || !strcmp (dbname, "safebrowsing"))
1633
+                logg ("Updates to main.cvd or safebrowsing.cvd may require 200MB of disk space or more\n");
1634
+            err = 1;
1635
+        }
1636
+    }
1637
+
1638
+    if (!err && !access ("daily.cfg", R_OK))
1639
+    {
1640
+        if (tar_addfile (fd, gzs, "daily.cfg") == -1)
1641
+        {
1642
+            logg ("!buildcld: Can't add daily.cfg to new %s.cld - please check if there is enough disk space available\n", dbname);
1643
+            err = 1;
1644
+        }
1645
+    }
1646
+
1647
+    if (err)
1648
+    {
1649
+        CHDIR_ERR (cwd);
1650
+        if (gzs)
1651
+            gzclose (gzs);
1652
+        else
1653
+            close (fd);
1654
+        closedir (dir);
1655
+        unlink (newfile);
1656
+        return -1;
1657
+    }
1658
+
1659
+    while ((dent = readdir (dir)))
1660
+    {
1661
+        if (dent->d_ino)
1662
+        {
1663
+            if (!strcmp (dent->d_name, ".") || !strcmp (dent->d_name, "..")
1664
+                || !strcmp (dent->d_name, "COPYING")
1665
+                || !strcmp (dent->d_name, "daily.cfg")
1666
+                || !strcmp (dent->d_name, info))
1667
+                continue;
1668
+
1669
+            if (tar_addfile (fd, gzs, dent->d_name) == -1)
1670
+            {
1671
+                logg ("!buildcld: Can't add %s to new %s.cld - please check if there is enough disk space available\n", dent->d_name, dbname);
1672
+                if (!strcmp (dbname, "main")
1673
+                    || !strcmp (dbname, "safebrowsing"))
1674
+                    logg ("Updates to main.cvd or safebrowsing.cvd may require 200MB of disk space or more\n");
1675
+                CHDIR_ERR (cwd);
1676
+                if (gzs)
1677
+                    gzclose (gzs);
1678
+                else
1679
+                    close (fd);
1680
+                closedir (dir);
1681
+                unlink (newfile);
1682
+                return -1;
1683
+            }
1684
+        }
1685
+    }
1686
+    closedir (dir);
1687
+
1688
+    if (gzs)
1689
+    {
1690
+        if (gzclose (gzs))
1691
+        {
1692
+            logg ("!buildcld: gzclose() failed for %s\n", newfile);
1693
+            unlink (newfile);
1694
+            return -1;
1695
+        }
1696
+    }
1697
+    else
1698
+    {
1699
+        if (close (fd) == -1)
1700
+        {
1701
+            logg ("!buildcld: close() failed for %s\n", newfile);
1702
+            unlink (newfile);
1703
+            return -1;
1704
+        }
1705
+    }
1706
+
1707
+    if (chdir (cwd) == -1)
1708
+    {
1709
+        logg ("!buildcld: Can't return to previous directory %s\n", cwd);
1710
+        return -1;
1406 1711
     }
1407 1712
 
1408 1713
     return 0;
1409 1714
 }
1410 1715
 
1411
-static int test_database(const char *newfile, const char *newdb, int bytecode)
1716
+static int
1717
+test_database (const char *newfile, const char *newdb, int bytecode)
1412 1718
 {
1413 1719
     struct cl_engine *engine;
1414 1720
     unsigned newsigs = 0;
1415 1721
     int ret;
1416 1722
 
1417
-    logg("*Loading signatures from %s\n", newdb);
1418
-    if(!(engine = cl_engine_new())) {
1419
-	return 55;
1723
+    logg ("*Loading signatures from %s\n", newdb);
1724
+    if (!(engine = cl_engine_new ()))
1725
+    {
1726
+        return 55;
1420 1727
     }
1421 1728
 
1422
-    if((ret = cl_load(newfile, engine, &newsigs, CL_DB_PHISHING | CL_DB_PHISHING_URLS | CL_DB_BYTECODE | CL_DB_PUA)) != CL_SUCCESS) {
1423
-	logg("!Failed to load new database: %s\n", cl_strerror(ret));
1424
-	cl_engine_free(engine);
1425
-	return 55;
1426
-    }
1427
-    if(bytecode && (ret = cli_bytecode_prepare2(engine, &engine->bcs, engine->dconf->bytecode/*FIXME: dconf has no sense here*/))) {
1428
-	logg("!Failed to compile/load bytecode: %s\n", cl_strerror(ret));
1429
-	cl_engine_free(engine);
1430
-	return 55;
1431
-    }
1432
-    logg("*Properly loaded %u signatures from new %s\n", newsigs, newdb);
1433
-    if(engine->domainlist_matcher && engine->domainlist_matcher->sha256_pfx_set.keys)
1434
-	cli_hashset_destroy(&engine->domainlist_matcher->sha256_pfx_set);
1435
-    cl_engine_free(engine);
1729
+    if ((ret =
1730
+         cl_load (newfile, engine, &newsigs,
1731
+                  CL_DB_PHISHING | CL_DB_PHISHING_URLS | CL_DB_BYTECODE |
1732
+                  CL_DB_PUA)) != CL_SUCCESS)
1733
+    {
1734
+        logg ("!Failed to load new database: %s\n", cl_strerror (ret));
1735
+        cl_engine_free (engine);
1736
+        return 55;
1737
+    }
1738
+    if (bytecode
1739
+        && (ret =
1740
+            cli_bytecode_prepare2 (engine, &engine->bcs,
1741
+                                   engine->dconf->
1742
+                                   bytecode
1743
+                                   /*FIXME: dconf has no sense here */ )))
1744
+    {
1745
+        logg ("!Failed to compile/load bytecode: %s\n", cl_strerror (ret));
1746
+        cl_engine_free (engine);
1747
+        return 55;
1748
+    }
1749
+    logg ("*Properly loaded %u signatures from new %s\n", newsigs, newdb);
1750
+    if (engine->domainlist_matcher
1751
+        && engine->domainlist_matcher->sha256_pfx_set.keys)
1752
+        cli_hashset_destroy (&engine->domainlist_matcher->sha256_pfx_set);
1753
+    cl_engine_free (engine);
1436 1754
     return 0;
1437 1755
 }
1438 1756
 
1439 1757
 #ifndef WIN32
1440
-static int test_database_wrap(const char *file, const char *newdb, int bytecode)
1758
+static int
1759
+test_database_wrap (const char *file, const char *newdb, int bytecode)
1441 1760
 {
1442 1761
     char firstline[256];
1443 1762
     char lastline[256];
... ...
@@ -1446,981 +1788,1270 @@ static int test_database_wrap(const char *file, const char *newdb, int bytecode)
1446 1446
     int status = 0, ret;
1447 1447
     FILE *f;
1448 1448
 
1449
-    if (pipe(pipefd) == -1) {
1450
-	logg("^pipe() failed: %s\n", strerror(errno));
1451
-	return test_database(file, newdb, bytecode);
1452
-    }
1453
-
1454
-    switch ( pid = fork() ) {
1455
-	case 0:
1456
-	    close(pipefd[0]);
1457
-	    dup2(pipefd[1], 2);
1458
-	    exit(test_database(file, newdb, bytecode));
1459
-	case -1:
1460
-	    close(pipefd[0]);
1461
-	    close(pipefd[1]);
1462
-	    logg("^fork() failed: %s\n", strerror(errno));
1463
-	    return test_database(file, newdb, bytecode);
1464
-	default:
1465
-	    /* read first / last line printed by child*/
1466
-	    close(pipefd[1]);
1467
-	    f = fdopen(pipefd[0], "r");
1468
-	    firstline[0] = 0;
1469
-	    lastline[0] = 0;
1470
-	    do {
1471
-		if (!fgets(firstline, sizeof(firstline), f))
1472
-		    break;
1473
-		/* ignore warning messages, otherwise the outdated warning will
1474
-		 * make us miss the important part of the error message */
1475
-	    } while (!strncmp(firstline, "LibClamAV Warning:", 18));
1476
-	    /* must read entire output, child doesn't like EPIPE */
1477
-	    while (fgets(lastline, sizeof(firstline), f)) {
1478
-		/* print the full output only when LogVerbose or -v is given */
1479
-		logg("*%s", lastline);
1480
-	    }
1481
-	    fclose(f);
1482
-
1483
-	    while ((ret = waitpid(pid, &status, 0)) == -1 && errno == EINTR);
1484
-	    if (ret == -1 && errno != ECHILD)
1485
-		logg("^waitpid() failed: %s\n", strerror(errno));
1486
-	    cli_chomp(firstline);
1487
-	    cli_chomp(lastline);
1488
-	    if (firstline[0]) {
1489
-		logg("!During database load : %s%s%s\n",
1490
-		     firstline, lastline[0] ? " [...] " : "",
1491
-		     lastline);
1492
-	    }
1493
-	    if (WIFEXITED(status)) {
1494
-		ret = WEXITSTATUS(status);
1495
-		if (ret) {
1496
-		    logg("^Database load exited with status %d\n", ret);
1497
-		    return ret;
1498
-		}
1499
-		if (firstline[0])
1500
-		    logg("^Database successfully loaded, but there is stderr output\n");
1501
-		return 0;
1502
-	    }
1503
-	    if (WIFSIGNALED(status)) {
1504
-		logg("!Database load killed by signal %d\n", WTERMSIG(status));
1505
-		return 55;
1506
-	    }
1507
-	    logg("^Unknown status from wait: %d\n", status);
1508
-	    return 55;
1449
+    if (pipe (pipefd) == -1)
1450
+    {
1451
+        logg ("^pipe() failed: %s\n", strerror (errno));
1452
+        return test_database (file, newdb, bytecode);
1453
+    }
1454
+
1455
+    switch (pid = fork ())
1456
+    {
1457
+    case 0:
1458
+        close (pipefd[0]);
1459
+        dup2 (pipefd[1], 2);
1460
+        exit (test_database (file, newdb, bytecode));
1461
+    case -1:
1462
+        close (pipefd[0]);
1463
+        close (pipefd[1]);
1464
+        logg ("^fork() failed: %s\n", strerror (errno));
1465
+        return test_database (file, newdb, bytecode);
1466
+    default:
1467
+        /* read first / last line printed by child */
1468
+        close (pipefd[1]);
1469
+        f = fdopen (pipefd[0], "r");
1470
+        firstline[0] = 0;
1471
+        lastline[0] = 0;
1472
+        do
1473
+        {
1474
+            if (!fgets (firstline, sizeof (firstline), f))
1475
+                break;
1476
+            /* ignore warning messages, otherwise the outdated warning will
1477
+             * make us miss the important part of the error message */
1478
+        }
1479
+        while (!strncmp (firstline, "LibClamAV Warning:", 18));
1480
+        /* must read entire output, child doesn't like EPIPE */
1481
+        while (fgets (lastline, sizeof (firstline), f))
1482
+        {
1483
+            /* print the full output only when LogVerbose or -v is given */
1484
+            logg ("*%s", lastline);
1485
+        }
1486
+        fclose (f);
1487
+
1488
+        while ((ret = waitpid (pid, &status, 0)) == -1 && errno == EINTR);
1489
+        if (ret == -1 && errno != ECHILD)
1490
+            logg ("^waitpid() failed: %s\n", strerror (errno));
1491
+        cli_chomp (firstline);
1492
+        cli_chomp (lastline);
1493
+        if (firstline[0])
1494
+        {
1495
+            logg ("!During database load : %s%s%s\n",
1496
+                  firstline, lastline[0] ? " [...] " : "", lastline);
1497
+        }
1498
+        if (WIFEXITED (status))
1499
+        {
1500
+            ret = WEXITSTATUS (status);
1501
+            if (ret)
1502
+            {
1503
+                logg ("^Database load exited with status %d\n", ret);
1504
+                return ret;
1505
+            }
1506
+            if (firstline[0])
1507
+                logg ("^Database successfully loaded, but there is stderr output\n");
1508
+            return 0;
1509
+        }
1510
+        if (WIFSIGNALED (status))
1511
+        {
1512
+            logg ("!Database load killed by signal %d\n", WTERMSIG (status));
1513
+            return 55;
1514
+        }
1515
+        logg ("^Unknown status from wait: %d\n", status);
1516
+        return 55;
1509 1517
     }
1510 1518
 }
1511 1519
 #else
1512
-static int test_database_wrap(const char *file, const char *newdb, int bytecode)
1520
+static int
1521
+test_database_wrap (const char *file, const char *newdb, int bytecode)
1513 1522
 {
1514 1523
     int ret = 55;
1515 1524
     __try
1516 1525
     {
1517
-	ret = test_database(file, newdb, bytecode);
1526
+        ret = test_database (file, newdb, bytecode);
1527
+    }
1528
+    __except (logg ("!Exception during database testing, code %08x\n",
1529
+                    GetExceptionCode ()), EXCEPTION_CONTINUE_SEARCH)
1530
+    {
1518 1531
     }
1519
-    __except (logg("!Exception during database testing, code %08x\n",
1520
-		 GetExceptionCode()),
1521
-	      EXCEPTION_CONTINUE_SEARCH)
1522
-    { }
1523 1532
     return ret;
1524 1533
 }
1525 1534
 #endif
1526 1535
 
1527
-static int checkdbdir(void)
1536
+static int
1537
+checkdbdir (void)
1528 1538
 {
1529
-	DIR *dir;
1530
-	struct dirent *dent;
1531
-	char fname[512], broken[513];
1532
-	int ret, fret = 0;
1533
-
1534
-    if(!(dir = opendir(dbdir))) {
1535
-	logg("!checkdbdir: Can't open directory %s\n", dbdir);
1536
-	return -1;
1537
-    }
1538
-
1539
-    while((dent = readdir(dir))) {
1540
-	if(dent->d_ino) {
1541
-	    if(cli_strbcasestr(dent->d_name, ".cld") || cli_strbcasestr(dent->d_name, ".cvd")) {
1542
-		snprintf(fname, sizeof(fname), "%s"PATHSEP"%s", dbdir, dent->d_name);
1543
-		if((ret = cl_cvdverify(fname))) {
1544
-		    fret = -1;
1545
-		    mprintf("!Corrupted database file %s: %s\n", fname, cl_strerror(ret));
1546
-		    snprintf(broken, sizeof(broken), "%s.broken", fname);
1547
-		    if(!access(broken, R_OK))
1548
-			unlink(broken);
1549
-		    if(rename(fname, broken)) {
1550
-			if(unlink(fname))
1551
-			    mprintf("!Can't remove broken database file %s, please delete it manually and restart freshclam\n", fname);
1552
-		    } else {
1553
-			mprintf("Corrupted database file renamed to %s\n", broken);
1554
-		    }
1555
-		}
1556
-	    }
1557
-	}
1558
-    }
1559
-
1560
-    closedir(dir);
1539
+    DIR *dir;
1540
+    struct dirent *dent;
1541
+    char fname[512], broken[513];
1542
+    int ret, fret = 0;
1543
+
1544
+    if (!(dir = opendir (dbdir)))
1545
+    {
1546
+        logg ("!checkdbdir: Can't open directory %s\n", dbdir);
1547
+        return -1;
1548
+    }
1549
+
1550
+    while ((dent = readdir (dir)))
1551
+    {
1552
+        if (dent->d_ino)
1553
+        {
1554
+            if (cli_strbcasestr (dent->d_name, ".cld")
1555
+                || cli_strbcasestr (dent->d_name, ".cvd"))
1556
+            {
1557
+                snprintf (fname, sizeof (fname), "%s" PATHSEP "%s", dbdir,
1558
+                          dent->d_name);
1559
+                if ((ret = cl_cvdverify (fname)))
1560
+                {
1561
+                    fret = -1;
1562
+                    mprintf ("!Corrupted database file %s: %s\n", fname,
1563
+                             cl_strerror (ret));
1564
+                    snprintf (broken, sizeof (broken), "%s.broken", fname);
1565
+                    if (!access (broken, R_OK))
1566
+                        unlink (broken);
1567
+                    if (rename (fname, broken))
1568
+                    {
1569
+                        if (unlink (fname))
1570
+                            mprintf
1571
+                                ("!Can't remove broken database file %s, please delete it manually and restart freshclam\n",
1572
+                                 fname);
1573
+                    }
1574
+                    else
1575
+                    {
1576
+                        mprintf ("Corrupted database file renamed to %s\n",
1577
+                                 broken);
1578
+                    }
1579
+                }
1580
+            }
1581
+        }
1582
+    }
1583
+
1584
+    closedir (dir);
1561 1585
     return fret;
1562 1586
 }
1563 1587
 
1564 1588
 extern int sigchld_wait;
1565 1589
 
1566
-static int updatedb(const char *dbname, const char *hostname, char *ip, int *signo, const struct optstruct *opts, const char *dnsreply, char *localip, int outdated, struct mirdat *mdat, int logerr, int extra, unsigned int attempt)
1590
+static int
1591
+updatedb (const char *dbname, const char *hostname, char *ip, int *signo,
1592
+          const struct optstruct *opts, const char *dnsreply, char *localip,
1593
+          int outdated, struct mirdat *mdat, int logerr, int extra,
1594
+          unsigned int attempt)
1567 1595
 {
1568
-	struct cl_cvd *current, *remote;
1569
-	const struct optstruct *opt;
1570
-	unsigned int nodb = 0, currver = 0, newver = 0, port = 0, i, j;
1571
-	int ret, ims = -1, hascld = 0;
1572
-	char *pt, cvdfile[32], cldfile[32], localname[32], *tmpdir = NULL, *newfile, *newfile2, newdb[32];
1573
-	char extradbinfo[64], *extradnsreply = NULL, squery[64];
1574
-	const char *proxy = NULL, *user = NULL, *pass = NULL, *uas = NULL;
1575
-	unsigned int flevel = cl_retflevel(), remote_flevel = 0, maxattempts;
1576
-	unsigned int can_whitelist = 0, mirror_stats = 0;
1596
+    struct cl_cvd *current, *remote;
1597
+    const struct optstruct *opt;
1598
+    unsigned int nodb = 0, currver = 0, newver = 0, port = 0, i, j;
1599
+    int ret, ims = -1, hascld = 0;
1600
+    char *pt, cvdfile[32], cldfile[32], localname[32], *tmpdir =
1601
+        NULL, *newfile, *newfile2, newdb[32];
1602
+    char extradbinfo[64], *extradnsreply = NULL, squery[64];
1603
+    const char *proxy = NULL, *user = NULL, *pass = NULL, *uas = NULL;
1604
+    unsigned int flevel = cl_retflevel (), remote_flevel = 0, maxattempts;
1605
+    unsigned int can_whitelist = 0, mirror_stats = 0;
1577 1606
 #ifdef _WIN32
1578
-	unsigned int w32 = 1;
1607
+    unsigned int w32 = 1;
1579 1608
 #else
1580
-	unsigned int w32 = 0;
1609
+    unsigned int w32 = 0;
1581 1610
 #endif
1582
-	int ctimeout, rtimeout;
1583
-
1584
-
1585
-    if(cli_strbcasestr(hostname, ".clamav.net"))
1586
-	mirror_stats = 1;
1587
-
1588
-    snprintf(cvdfile, sizeof(cvdfile), "%s.cvd", dbname);
1589
-    snprintf(cldfile, sizeof(cldfile), "%s.cld", dbname);
1590
-
1591
-    if(!(current = currentdb(dbname, localname))) {
1592
-	nodb = 1;
1593
-    } else {
1594
-	mdat->dbflevel = current->fl;
1595
-    }
1596
-
1597
-    if(!nodb && !extra && dnsreply) {
1598
-	    int field = 0;
1599
-
1600
-	if(!strcmp(dbname, "main")) {
1601
-	    field = 1;
1602
-	} else if(!strcmp(dbname, "daily")) {
1603
-	    field = 2;
1604
-	} else if(!strcmp(dbname, "safebrowsing")) {
1605
-	    field = 6;
1606
-	} else if(!strcmp(dbname, "bytecode")) {
1607
-	    field = 7;
1608
-	} else {
1609
-	    logg("!updatedb: Unknown database name (%s) passed.\n", dbname);
1610
-	    cl_cvdfree(current);
1611
-	    return 70;
1612
-	}
1613
-
1614
-	if(field && (pt = cli_strtok(dnsreply, field, ":"))) {
1615
-	    if(!cli_isnumber(pt)) {
1616
-		logg("^Broken database version in TXT record.\n");
1617
-	    } else {
1618
-		newver = atoi(pt);
1619
-		logg("*%s version from DNS: %d\n", cvdfile, newver);
1620
-	    }
1621
-	    free(pt);
1622
-	} else {
1623
-	    logg("^Invalid DNS reply. Falling back to HTTP mode.\n");
1624
-	}
1611
+    int ctimeout, rtimeout;
1612
+
1613
+
1614
+    if (cli_strbcasestr (hostname, ".clamav.net"))
1615
+        mirror_stats = 1;
1616
+
1617
+    snprintf (cvdfile, sizeof (cvdfile), "%s.cvd", dbname);
1618
+    snprintf (cldfile, sizeof (cldfile), "%s.cld", dbname);
1619
+
1620
+    if (!(current = currentdb (dbname, localname)))
1621
+    {
1622
+        nodb = 1;
1623
+    }
1624
+    else
1625
+    {
1626
+        mdat->dbflevel = current->fl;
1627
+    }
1628
+
1629
+    if (!nodb && !extra && dnsreply)
1630
+    {
1631
+        int field = 0;
1632
+
1633
+        if (!strcmp (dbname, "main"))
1634
+        {
1635
+            field = 1;
1636
+        }
1637
+        else if (!strcmp (dbname, "daily"))
1638
+        {
1639
+            field = 2;
1640
+        }
1641
+        else if (!strcmp (dbname, "safebrowsing"))
1642
+        {
1643
+            field = 6;
1644
+        }
1645
+        else if (!strcmp (dbname, "bytecode"))
1646
+        {
1647
+            field = 7;
1648
+        }
1649
+        else
1650
+        {
1651
+            logg ("!updatedb: Unknown database name (%s) passed.\n", dbname);
1652
+            cl_cvdfree (current);
1653
+            return 70;
1654
+        }
1655
+
1656
+        if (field && (pt = cli_strtok (dnsreply, field, ":")))
1657
+        {
1658
+            if (!cli_isnumber (pt))
1659
+            {
1660
+                logg ("^Broken database version in TXT record.\n");
1661
+            }
1662
+            else
1663
+            {
1664
+                newver = atoi (pt);
1665
+                logg ("*%s version from DNS: %d\n", cvdfile, newver);
1666
+            }
1667
+            free (pt);
1668
+        }
1669
+        else
1670
+        {
1671
+            logg ("^Invalid DNS reply. Falling back to HTTP mode.\n");
1672
+        }
1625 1673
     }
1626 1674
 #ifdef HAVE_RESOLV_H
1627
-    else if(!nodb && extra && !optget(opts, "no-dns")->enabled) {
1628
-	snprintf(extradbinfo, sizeof(extradbinfo), "%s.cvd.clamav.net", dbname);
1629
-	if((extradnsreply = dnsquery(extradbinfo, T_TXT, NULL))) {
1630
-	    if((pt = cli_strtok(extradnsreply, 1, ":"))) {
1631
-		    int rt;
1632
-		    time_t ct;
1633
-
1634
-		rt = atoi(pt);
1635
-		free(pt);
1636
-		time(&ct);
1637
-		if((int) ct - rt > 10800) {
1638
-		    logg("^DNS record is older than 3 hours.\n");
1639
-		    free(extradnsreply);
1640
-		    extradnsreply = NULL;
1641
-		}
1642
-	    } else {
1643
-		logg("^No timestamp in TXT record for %s\n", cvdfile);
1644
-		free(extradnsreply);
1645
-		extradnsreply = NULL;
1646
-	    }
1647
-	    if((pt = cli_strtok(extradnsreply, 0, ":"))) {
1648
-		if(!cli_isnumber(pt)) {
1649
-		    logg("^Broken database version in TXT record for %s\n", cvdfile);
1650
-		} else {
1651
-		    newver = atoi(pt);
1652
-		    logg("*%s version from DNS: %d\n", cvdfile, newver);
1653
-		}
1654
-		free(pt);
1655
-	    } else {
1656
-		logg("^Invalid DNS reply. Falling back to HTTP mode.\n");
1657
-	    }
1658
-	}
1675
+    else if (!nodb && extra && !optget (opts, "no-dns")->enabled)
1676
+    {
1677
+        snprintf (extradbinfo, sizeof (extradbinfo), "%s.cvd.clamav.net",
1678
+                  dbname);
1679
+        if ((extradnsreply = dnsquery (extradbinfo, T_TXT, NULL)))
1680
+        {
1681
+            if ((pt = cli_strtok (extradnsreply, 1, ":")))
1682
+            {
1683
+                int rt;
1684
+                time_t ct;
1685
+
1686
+                rt = atoi (pt);
1687
+                free (pt);
1688
+                time (&ct);
1689
+                if ((int) ct - rt > 10800)
1690
+                {
1691
+                    logg ("^DNS record is older than 3 hours.\n");
1692
+                    free (extradnsreply);
1693
+                    extradnsreply = NULL;
1694
+                }
1695
+            }
1696
+            else
1697
+            {
1698
+                logg ("^No timestamp in TXT record for %s\n", cvdfile);
1699
+                free (extradnsreply);
1700
+                extradnsreply = NULL;
1701
+            }
1702
+            if ((pt = cli_strtok (extradnsreply, 0, ":")))
1703
+            {
1704
+                if (!cli_isnumber (pt))
1705
+                {
1706
+                    logg ("^Broken database version in TXT record for %s\n",
1707
+                          cvdfile);
1708
+                }
1709
+                else
1710
+                {
1711
+                    newver = atoi (pt);
1712
+                    logg ("*%s version from DNS: %d\n", cvdfile, newver);
1713
+                }
1714
+                free (pt);
1715
+            }
1716
+            else
1717
+            {
1718
+                logg ("^Invalid DNS reply. Falling back to HTTP mode.\n");
1719
+            }
1720
+        }
1659 1721
     }
1660 1722
 #endif
1661 1723
 
1662
-    if(dnsreply && !extra) {
1663
-	if((pt = cli_strtok(dnsreply, 5, ":"))) {
1664
-	    remote_flevel = atoi(pt);
1665
-	    free(pt);
1666
-	    if(remote_flevel && (remote_flevel - flevel < 4))
1667
-		can_whitelist = 1;
1668
-	}
1724
+    if (dnsreply && !extra)
1725
+    {
1726
+        if ((pt = cli_strtok (dnsreply, 5, ":")))
1727
+        {
1728
+            remote_flevel = atoi (pt);
1729
+            free (pt);
1730
+            if (remote_flevel && (remote_flevel - flevel < 4))
1731
+                can_whitelist = 1;
1732
+        }
1669 1733
     }
1670 1734
 
1671 1735
     /* Initialize proxy settings */
1672
-    if((opt = optget(opts, "HTTPProxyServer"))->enabled) {
1673
-	proxy = opt->strarg;
1674
-	if(strncasecmp(proxy, "http://", 7) == 0)
1675
-	    proxy += 7;
1676
-
1677
-	if((opt = optget(opts, "HTTPProxyUsername"))->enabled) {
1678
-	    user = opt->strarg;
1679
-	    if((opt = optget(opts, "HTTPProxyPassword"))->enabled) {
1680
-		pass = opt->strarg;
1681
-	    } else {
1682
-		logg("HTTPProxyUsername requires HTTPProxyPassword\n");
1683
-		if(current)
1684
-		    cl_cvdfree(current);
1685
-		return 56;
1686
-	    }
1687
-	}
1688
-
1689
-	if((opt = optget(opts, "HTTPProxyPort"))->enabled)
1690
-	    port = opt->numarg;
1691
-
1692
-	logg("Connecting via %s\n", proxy);
1693
-    }
1694
-
1695
-    if((opt = optget(opts, "HTTPUserAgent"))->enabled)
1696
-	uas = opt->strarg;
1697
-
1698
-    ctimeout = optget(opts, "ConnectTimeout")->numarg;
1699
-    rtimeout = optget(opts, "ReceiveTimeout")->numarg;
1700
-
1701
-    if(!nodb && !newver) {
1702
-	if(optget(opts, "PrivateMirror")->enabled) {
1703
-	    remote = remote_cvdhead(cldfile, localname, hostname, ip, localip, proxy, port, user, pass, uas, &ims, ctimeout, rtimeout, mdat, logerr, can_whitelist, attempt);
1704
-	    if(remote)
1705
-		hascld = 1;
1706
-	    else
1707
-		remote = remote_cvdhead(cvdfile, localname, hostname, ip, localip, proxy, port, user, pass, uas, &ims, ctimeout, rtimeout, mdat, logerr, can_whitelist, attempt);
1708
-	} else
1709
-	    remote = remote_cvdhead(cvdfile, localname, hostname, ip, localip, proxy, port, user, pass, uas, &ims, ctimeout, rtimeout, mdat, logerr, can_whitelist, attempt);
1710
-
1711
-	if(!nodb && !ims) {
1712
-	    logg("%s is up to date (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder);
1713
-	    *signo += current->sigs;
1736
+    if ((opt = optget (opts, "HTTPProxyServer"))->enabled)
1737
+    {
1738
+        proxy = opt->strarg;
1739
+        if (strncasecmp (proxy, "http://", 7) == 0)
1740
+            proxy += 7;
1741
+
1742
+        if ((opt = optget (opts, "HTTPProxyUsername"))->enabled)
1743
+        {
1744
+            user = opt->strarg;
1745
+            if ((opt = optget (opts, "HTTPProxyPassword"))->enabled)
1746
+            {
1747
+                pass = opt->strarg;
1748
+            }
1749
+            else
1750
+            {
1751
+                logg ("HTTPProxyUsername requires HTTPProxyPassword\n");
1752
+                if (current)
1753
+                    cl_cvdfree (current);
1754
+                return 56;
1755
+            }
1756
+        }
1757
+
1758
+        if ((opt = optget (opts, "HTTPProxyPort"))->enabled)
1759
+            port = opt->numarg;
1760
+
1761
+        logg ("Connecting via %s\n", proxy);
1762
+    }
1763
+
1764
+    if ((opt = optget (opts, "HTTPUserAgent"))->enabled)
1765
+        uas = opt->strarg;
1766
+
1767
+    ctimeout = optget (opts, "ConnectTimeout")->numarg;
1768
+    rtimeout = optget (opts, "ReceiveTimeout")->numarg;
1769
+
1770
+    if (!nodb && !newver)
1771
+    {
1772
+        if (optget (opts, "PrivateMirror")->enabled)
1773
+        {
1774
+            remote =
1775
+                remote_cvdhead (cldfile, localname, hostname, ip, localip,
1776
+                                proxy, port, user, pass, uas, &ims, ctimeout,
1777
+                                rtimeout, mdat, logerr, can_whitelist,
1778
+                                attempt);
1779
+            if (remote)
1780
+                hascld = 1;
1781
+            else
1782
+                remote =
1783
+                    remote_cvdhead (cvdfile, localname, hostname, ip, localip,
1784
+                                    proxy, port, user, pass, uas, &ims,
1785
+                                    ctimeout, rtimeout, mdat, logerr,
1786
+                                    can_whitelist, attempt);
1787
+        }
1788
+        else
1789
+            remote =
1790
+                remote_cvdhead (cvdfile, localname, hostname, ip, localip,
1791
+                                proxy, port, user, pass, uas, &ims, ctimeout,
1792
+                                rtimeout, mdat, logerr, can_whitelist,
1793
+                                attempt);
1794
+
1795
+        if (!nodb && !ims)
1796
+        {
1797
+            logg ("%s is up to date (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder);
1798
+            *signo += current->sigs;
1714 1799
 #ifdef HAVE_RESOLV_H
1715
-	    if(mirror_stats && strlen(ip)) {
1716
-		snprintf(squery, sizeof(squery), "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname, current->version, flevel, 1, w32, ip);
1717
-		dnsquery(squery, T_A, NULL);
1718
-	    }
1800
+            if (mirror_stats && strlen (ip))
1801
+            {
1802
+                snprintf (squery, sizeof (squery),
1803
+                          "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname,
1804
+                          current->version, flevel, 1, w32, ip);
1805
+                dnsquery (squery, T_A, NULL);
1806
+            }
1719 1807
 #endif
1720
-	    cl_cvdfree(current);
1721
-	    return 1;
1722
-	}
1808
+            cl_cvdfree (current);
1809
+            return 1;
1810
+        }
1723 1811
 
1724
-	if(!remote) {
1725
-	    logg("^Can't read %s header from %s (IP: %s)\n", cvdfile, hostname, ip);
1812
+        if (!remote)
1813
+        {
1814
+            logg ("^Can't read %s header from %s (IP: %s)\n", cvdfile,
1815
+                  hostname, ip);
1726 1816
 #ifdef HAVE_RESOLV_H
1727
-	    if(mirror_stats && strlen(ip)) {
1728
-		snprintf(squery, sizeof(squery), "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname, current->version + 1, flevel, 0, w32, ip);
1729
-		dnsquery(squery, T_A, NULL);
1730
-	    }
1817
+            if (mirror_stats && strlen (ip))
1818
+            {
1819
+                snprintf (squery, sizeof (squery),
1820
+                          "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname,
1821
+                          current->version + 1, flevel, 0, w32, ip);
1822
+                dnsquery (squery, T_A, NULL);
1823
+            }
1731 1824
 #endif
1732
-	    cl_cvdfree(current);
1733
-	    return 58;
1734
-	}
1825
+            cl_cvdfree (current);
1826
+            return 58;
1827
+        }
1735 1828
 
1736
-	newver = remote->version;
1737
-	cl_cvdfree(remote);
1829
+        newver = remote->version;
1830
+        cl_cvdfree (remote);
1738 1831
     }
1739 1832
 
1740
-    if(!nodb && (current->version >= newver)) {
1741
-	logg("%s is up to date (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder);
1742
-
1743
-	if(!outdated && flevel < current->fl) {
1744
-	    /* display warning even for already installed database */
1745
-	    logg("^Current functionality level = %d, recommended = %d\n", flevel, current->fl);
1746
-	    logg("Please check if ClamAV tools are linked against the proper version of libclamav\n");
1747
-	    logg("DON'T PANIC! Read http://www.clamav.net/support/faq\n");
1748
-	}
1833
+    if (!nodb && (current->version >= newver))
1834
+    {
1835
+        logg ("%s is up to date (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder);
1836
+
1837
+        if (!outdated && flevel < current->fl)
1838
+        {
1839
+            /* display warning even for already installed database */
1840
+            logg ("^Current functionality level = %d, recommended = %d\n",
1841
+                  flevel, current->fl);
1842
+            logg ("Please check if ClamAV tools are linked against the proper version of libclamav\n");
1843
+            logg ("DON'T PANIC! Read http://www.clamav.net/support/faq\n");
1844
+        }
1749 1845
 
1750
-	*signo += current->sigs;
1751
-	cl_cvdfree(current);
1752
-	return 1;
1846
+        *signo += current->sigs;
1847
+        cl_cvdfree (current);
1848
+        return 1;
1753 1849
     }
1754 1850
 
1755
-    if(current) {
1756
-	currver = current->version;
1757
-	cl_cvdfree(current);
1851
+    if (current)
1852
+    {
1853
+        currver = current->version;
1854
+        cl_cvdfree (current);
1758 1855
     }
1759 1856
 
1760
-    if(!optget(opts, "ScriptedUpdates")->enabled)
1761
-	nodb = 1;
1857
+    if (!optget (opts, "ScriptedUpdates")->enabled)
1858
+        nodb = 1;
1762 1859
 
1763
-    newfile = cli_gentemp(updtmpdir);
1764
-    if(nodb) {
1765
-	if(optget(opts, "PrivateMirror")->enabled) {
1766
-	    ret = 0;
1767
-	    if(hascld)
1768
-		ret = getcvd(cldfile, newfile, hostname, ip, localip, proxy, port, user, pass, uas, newver, ctimeout, rtimeout, mdat, logerr, can_whitelist, opts, attempt);
1769
-	    if(ret || !hascld)
1770
-		ret = getcvd(cvdfile, newfile, hostname, ip, localip, proxy, port, user, pass, uas, newver, ctimeout, rtimeout, mdat, logerr, can_whitelist, opts, attempt);
1771
-	} else
1772
-	    ret = getcvd(cvdfile, newfile, hostname, ip, localip, proxy, port, user, pass, uas, newver, ctimeout, rtimeout, mdat, logerr, can_whitelist, opts, attempt);
1773
-
1774
-	if(ret) {
1860
+    newfile = cli_gentemp (updtmpdir);
1861
+    if (nodb)
1862
+    {
1863
+        if (optget (opts, "PrivateMirror")->enabled)
1864
+        {
1865
+            ret = 0;
1866
+            if (hascld)
1867
+                ret =
1868
+                    getcvd (cldfile, newfile, hostname, ip, localip, proxy,
1869
+                            port, user, pass, uas, newver, ctimeout, rtimeout,
1870
+                            mdat, logerr, can_whitelist, opts, attempt);
1871
+            if (ret || !hascld)
1872
+                ret =
1873
+                    getcvd (cvdfile, newfile, hostname, ip, localip, proxy,
1874
+                            port, user, pass, uas, newver, ctimeout, rtimeout,
1875
+                            mdat, logerr, can_whitelist, opts, attempt);
1876
+        }
1877
+        else
1878
+            ret =
1879
+                getcvd (cvdfile, newfile, hostname, ip, localip, proxy, port,
1880
+                        user, pass, uas, newver, ctimeout, rtimeout, mdat,
1881
+                        logerr, can_whitelist, opts, attempt);
1882
+
1883
+        if (ret)
1884
+        {
1775 1885
 #ifdef HAVE_RESOLV_H
1776
-	    if(mirror_stats && strlen(ip)) {
1777
-		snprintf(squery, sizeof(squery), "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname, 0, flevel, 0, w32, ip);
1778
-		dnsquery(squery, T_A, NULL);
1779
-	    }
1886
+            if (mirror_stats && strlen (ip))
1887
+            {
1888
+                snprintf (squery, sizeof (squery),
1889
+                          "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname, 0,
1890
+                          flevel, 0, w32, ip);
1891
+                dnsquery (squery, T_A, NULL);
1892
+            }
1780 1893
 #endif
1781
-	    memset(ip, 0, 16);
1782
-	    free(newfile);
1783
-	    return ret;
1784
-	}
1785
-	snprintf(newdb, sizeof(newdb), "%s.cvd", dbname);
1786
-
1787
-    } else {
1788
-	ret = 0;
1789
-
1790
-	tmpdir = cli_gentemp(updtmpdir);
1791
-	maxattempts = optget(opts, "MaxAttempts")->numarg;
1792
-	for(i = currver + 1; i <= newver; i++) {
1793
-	    for(j = 1; j <= maxattempts; j++) {
1794
-		    int llogerr = logerr;
1795
-		if(logerr)
1796
-		    llogerr = (j == maxattempts);
1797
-		ret = getpatch(dbname, tmpdir, i, hostname, ip, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, mdat, llogerr, can_whitelist, opts, attempt == 1 ? j : attempt);
1798
-		if(ret == 52 || ret == 58) {
1894
+            memset (ip, 0, 16);
1895
+            free (newfile);
1896
+            return ret;
1897
+        }
1898
+        snprintf (newdb, sizeof (newdb), "%s.cvd", dbname);
1899
+
1900
+    }
1901
+    else
1902
+    {
1903
+        ret = 0;
1904
+
1905
+        tmpdir = cli_gentemp (updtmpdir);
1906
+        maxattempts = optget (opts, "MaxAttempts")->numarg;
1907
+        for (i = currver + 1; i <= newver; i++)
1908
+        {
1909
+            for (j = 1; j <= maxattempts; j++)
1910
+            {
1911
+                int llogerr = logerr;
1912
+                if (logerr)
1913
+                    llogerr = (j == maxattempts);
1914
+                ret =
1915
+                    getpatch (dbname, tmpdir, i, hostname, ip, localip, proxy,
1916
+                              port, user, pass, uas, ctimeout, rtimeout, mdat,
1917
+                              llogerr, can_whitelist, opts,
1918
+                              attempt == 1 ? j : attempt);
1919
+                if (ret == 52 || ret == 58)
1920
+                {
1799 1921
 #ifdef HAVE_RESOLV_H
1800
-		    if(mirror_stats && strlen(ip)) {
1801
-			snprintf(squery, sizeof(squery), "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname, i, flevel, 0, w32, ip);
1802
-			dnsquery(squery, T_A, NULL);
1803
-		    }
1922
+                    if (mirror_stats && strlen (ip))
1923
+                    {
1924
+                        snprintf (squery, sizeof (squery),
1925
+                                  "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname,
1926
+                                  i, flevel, 0, w32, ip);
1927
+                        dnsquery (squery, T_A, NULL);
1928
+                    }
1804 1929
 #endif
1805
-		    memset(ip, 0, 16);
1806
-		    continue;
1807
-		} else {
1808
-		    break;
1809
-		}
1810
-	    }
1811
-	    if(ret)
1812
-		break;
1813
-	}
1814
-
1815
-	if(ret) {
1816
-	    cli_rmdirs(tmpdir);
1817
-	    free(tmpdir);
1818
-	    if(ret != 53)
1819
-		logg("^Incremental update failed, trying to download %s\n", cvdfile);
1820
-	    mirman_whitelist(mdat, 2);
1821
-	    ret = getcvd(cvdfile, newfile, hostname, ip, localip, proxy, port, user, pass, uas, newver, ctimeout, rtimeout, mdat, logerr, can_whitelist, opts, attempt);
1822
-	    if(ret) {
1930
+                    memset (ip, 0, 16);
1931
+                    continue;
1932
+                }
1933
+                else
1934
+                {
1935
+                    break;
1936
+                }
1937
+            }
1938
+            if (ret)
1939
+                break;
1940
+        }
1941
+
1942
+        if (ret)
1943
+        {
1944
+            cli_rmdirs (tmpdir);
1945
+            free (tmpdir);
1946
+            if (ret != 53)
1947
+                logg ("^Incremental update failed, trying to download %s\n",
1948
+                      cvdfile);
1949
+            mirman_whitelist (mdat, 2);
1950
+            ret =
1951
+                getcvd (cvdfile, newfile, hostname, ip, localip, proxy, port,
1952
+                        user, pass, uas, newver, ctimeout, rtimeout, mdat,
1953
+                        logerr, can_whitelist, opts, attempt);
1954
+            if (ret)
1955
+            {
1823 1956
 #ifdef HAVE_RESOLV_H
1824
-		if(mirror_stats && strlen(ip)) {
1825
-		    snprintf(squery, sizeof(squery), "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname, 0, flevel, 0, w32, ip);
1826
-		    dnsquery(squery, T_A, NULL);
1827
-		}
1957
+                if (mirror_stats && strlen (ip))
1958
+                {
1959
+                    snprintf (squery, sizeof (squery),
1960
+                              "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname, 0,
1961
+                              flevel, 0, w32, ip);
1962
+                    dnsquery (squery, T_A, NULL);
1963
+                }
1828 1964
 #endif
1829
-		free(newfile);
1830
-		return ret;
1831
-	    }
1832
-	    snprintf(newdb, sizeof(newdb), "%s.cvd", dbname);
1833
-	} else {
1834
-	    if(buildcld(tmpdir, dbname, newfile, optget(opts, "CompressLocalDatabase")->enabled) == -1) {
1835
-		logg("!Can't create local database\n");
1836
-		cli_rmdirs(tmpdir);
1837
-		free(tmpdir);
1838
-		free(newfile);
1839
-		return 70; /* FIXME */
1840
-	    }
1841
-	    snprintf(newdb, sizeof(newdb), "%s.cld", dbname);
1842
-	    cli_rmdirs(tmpdir);
1843
-	    free(tmpdir);
1844
-	}
1845
-    }
1846
-
1847
-    if(!(current = cl_cvdhead(newfile))) {
1848
-	logg("!Can't parse new database %s\n", newfile);
1849
-	unlink(newfile);
1850
-	free(newfile);
1851
-	return 55; /* FIXME */
1852
-    }
1853
-
1854
-    if(optget(opts, "TestDatabases")->enabled && strlen(newfile) > 4) {
1855
-	newfile2 = strdup(newfile);
1856
-	if(!newfile2) {
1857
-	    logg("!Can't allocate memory for filename!\n");
1858
-	    unlink(newfile);
1859
-	    free(newfile);
1860
-	    return 55;
1861
-	}
1862
-	newfile2[strlen(newfile2) - 4] = '.';
1863
-	newfile2[strlen(newfile2) - 3] = 'c';
1864
-	newfile2[strlen(newfile2) - 2] = strstr(newdb, ".cld") ? 'l' : 'v';
1865
-	newfile2[strlen(newfile2) - 1] = 'd';
1866
-	if(rename(newfile, newfile2) == -1) {
1867
-	    logg("!Can't rename %s to %s: %s\n", newfile, newfile2, strerror(errno));
1868
-	    unlink(newfile);
1869
-	    free(newfile);
1870
-	    free(newfile2);
1871
-	    return 57;
1872
-	}
1873
-	free(newfile);
1874
-	newfile = newfile2;
1875
-	sigchld_wait = 0;/* we need to wait() for the child ourselves */
1876
-	if (test_database_wrap(newfile, newdb, optget(opts, "Bytecode")->enabled)) {
1877
-	    logg("!Failed to load new database\n");
1878
-	    unlink(newfile);
1879
-	    free(newfile);
1880
-	    return 55;
1881
-	}
1882
-	sigchld_wait = 1;
1965
+                free (newfile);
1966
+                return ret;
1967
+            }
1968
+            snprintf (newdb, sizeof (newdb), "%s.cvd", dbname);
1969
+        }
1970
+        else
1971
+        {
1972
+            if (buildcld
1973
+                (tmpdir, dbname, newfile,
1974
+                 optget (opts, "CompressLocalDatabase")->enabled) == -1)
1975
+            {
1976
+                logg ("!Can't create local database\n");
1977
+                cli_rmdirs (tmpdir);
1978
+                free (tmpdir);
1979
+                free (newfile);
1980
+                return 70;      /* FIXME */
1981
+            }
1982
+            snprintf (newdb, sizeof (newdb), "%s.cld", dbname);
1983
+            cli_rmdirs (tmpdir);
1984
+            free (tmpdir);
1985
+        }
1986
+    }
1987
+
1988
+    if (!(current = cl_cvdhead (newfile)))
1989
+    {
1990
+        logg ("!Can't parse new database %s\n", newfile);
1991
+        unlink (newfile);
1992
+        free (newfile);
1993
+        return 55;              /* FIXME */
1994
+    }
1995
+
1996
+    if (optget (opts, "TestDatabases")->enabled && strlen (newfile) > 4)
1997
+    {
1998
+        newfile2 = strdup (newfile);
1999
+        if (!newfile2)
2000
+        {
2001
+            logg ("!Can't allocate memory for filename!\n");
2002
+            unlink (newfile);
2003
+            free (newfile);
2004
+            return 55;
2005
+        }
2006
+        newfile2[strlen (newfile2) - 4] = '.';
2007
+        newfile2[strlen (newfile2) - 3] = 'c';
2008
+        newfile2[strlen (newfile2) - 2] = strstr (newdb, ".cld") ? 'l' : 'v';
2009
+        newfile2[strlen (newfile2) - 1] = 'd';
2010
+        if (rename (newfile, newfile2) == -1)
2011
+        {
2012
+            logg ("!Can't rename %s to %s: %s\n", newfile, newfile2,
2013
+                  strerror (errno));
2014
+            unlink (newfile);
2015
+            free (newfile);
2016
+            free (newfile2);
2017
+            return 57;
2018
+        }
2019
+        free (newfile);
2020
+        newfile = newfile2;
2021
+        sigchld_wait = 0;       /* we need to wait() for the child ourselves */
2022
+        if (test_database_wrap
2023
+            (newfile, newdb, optget (opts, "Bytecode")->enabled))
2024
+        {
2025
+            logg ("!Failed to load new database\n");
2026
+            unlink (newfile);
2027
+            free (newfile);
2028
+            return 55;
2029
+        }
2030
+        sigchld_wait = 1;
1883 2031
     }
1884 2032
 
1885 2033
 #ifdef _WIN32
1886
-    if(!access(newdb, R_OK) && unlink(newdb)) {
1887
-	logg("!Can't unlink %s. Please fix the problem manually and try again.\n", newdb);
1888
-	unlink(newfile);
1889
-	free(newfile);
1890
-	cl_cvdfree(current);
1891
-	return 53;
2034
+    if (!access (newdb, R_OK) && unlink (newdb))
2035
+    {
2036
+        logg ("!Can't unlink %s. Please fix the problem manually and try again.\n", newdb);
2037
+        unlink (newfile);
2038
+        free (newfile);
2039
+        cl_cvdfree (current);
2040
+        return 53;
1892 2041
     }
1893 2042
 #endif
1894 2043
 
1895
-    if(rename(newfile, newdb) == -1) {
1896
-	logg("!Can't rename %s to %s: %s\n", newfile, newdb, strerror(errno));
1897
-	unlink(newfile);
1898
-	free(newfile);
1899
-	cl_cvdfree(current);
1900
-	return 57;
2044
+    if (rename (newfile, newdb) == -1)
2045
+    {
2046
+        logg ("!Can't rename %s to %s: %s\n", newfile, newdb,
2047
+              strerror (errno));
2048
+        unlink (newfile);
2049
+        free (newfile);
2050
+        cl_cvdfree (current);
2051
+        return 57;
1901 2052
     }
1902
-    free(newfile);
2053
+    free (newfile);
1903 2054
 
1904
-    if(!nodb && !access(localname, R_OK) && strcmp(newdb, localname))
1905
-	if(unlink(localname))
1906
-	    logg("^Can't unlink the old database file %s. Please remove it manually.\n", localname);
2055
+    if (!nodb && !access (localname, R_OK) && strcmp (newdb, localname))
2056
+        if (unlink (localname))
2057
+            logg ("^Can't unlink the old database file %s. Please remove it manually.\n", localname);
1907 2058
 
1908
-    if(!optget(opts, "ScriptedUpdates")->enabled) {
1909
-	snprintf(localname, sizeof(localname), "%s.cld", dbname);
1910
-	if(!access(localname, R_OK))
1911
-	    if(unlink(localname))
1912
-		logg("^Can't unlink the old database file %s. Please remove it manually.\n", localname);
2059
+    if (!optget (opts, "ScriptedUpdates")->enabled)
2060
+    {
2061
+        snprintf (localname, sizeof (localname), "%s.cld", dbname);
2062
+        if (!access (localname, R_OK))
2063
+            if (unlink (localname))
2064
+                logg ("^Can't unlink the old database file %s. Please remove it manually.\n", localname);
1913 2065
     }
1914 2066
 
1915
-    logg("%s updated (version: %d, sigs: %d, f-level: %d, builder: %s)\n", newdb, current->version, current->sigs, current->fl, current->builder);
2067
+    logg ("%s updated (version: %d, sigs: %d, f-level: %d, builder: %s)\n",
2068
+          newdb, current->version, current->sigs, current->fl,
2069
+          current->builder);
1916 2070
 
1917
-    if(flevel < current->fl) {
1918
-	logg("^Your ClamAV installation is OUTDATED!\n");
1919
-	logg("^Current functionality level = %d, recommended = %d\n", flevel, current->fl);
1920
-	logg("DON'T PANIC! Read http://www.clamav.net/support/faq\n");
2071
+    if (flevel < current->fl)
2072
+    {
2073
+        logg ("^Your ClamAV installation is OUTDATED!\n");
2074
+        logg ("^Current functionality level = %d, recommended = %d\n", flevel,
2075
+              current->fl);
2076
+        logg ("DON'T PANIC! Read http://www.clamav.net/support/faq\n");
1921 2077
     }
1922 2078
 
1923 2079
     *signo += current->sigs;
1924 2080
 #ifdef HAVE_RESOLV_H
1925
-    if(mirror_stats && strlen(ip)) {
1926
-	snprintf(squery, sizeof(squery), "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname, current->version, flevel, 1, w32, ip);
1927
-	dnsquery(squery, T_A, NULL);
2081
+    if (mirror_stats && strlen (ip))
2082
+    {
2083
+        snprintf (squery, sizeof (squery),
2084
+                  "%s.%u.%u.%u.%u.%s.ping.clamav.net", dbname,
2085
+                  current->version, flevel, 1, w32, ip);
2086
+        dnsquery (squery, T_A, NULL);
1928 2087
     }
1929 2088
 #endif
1930
-    cl_cvdfree(current);
2089
+    cl_cvdfree (current);
1931 2090
     return 0;
1932 2091
 }
1933 2092
 
1934
-static int updatecustomdb(const char *url, int *signo, const struct optstruct *opts, char *localip, int logerr)
2093
+static int
2094
+updatecustomdb (const char *url, int *signo, const struct optstruct *opts,
2095
+                char *localip, int logerr)
1935 2096
 {
1936
-	const struct optstruct *opt;
1937
-	unsigned int port = 0, sigs = 0;
1938
-	int ret;
1939
-	char *pt, *host, urlcpy[256], *newfile = NULL, mtime[36], *newfile2;
1940
-	const char *proxy = NULL, *user = NULL, *pass = NULL, *uas = NULL, *rpath, *dbname;
1941
-	int ctimeout, rtimeout;
1942
-	STATBUF sb;
1943
-	struct cl_cvd *cvd;
1944
-
1945
-    if (strlen(url) > sizeof(urlcpy) - 1) {
1946
-        logg("!DatabaseCustomURL: URL must be shorter than %lu\n", sizeof(urlcpy));
2097
+    const struct optstruct *opt;
2098
+    unsigned int port = 0, sigs = 0;
2099
+    int ret;
2100
+    char *pt, *host, urlcpy[256], *newfile = NULL, mtime[36], *newfile2;
2101
+    const char *proxy = NULL, *user = NULL, *pass = NULL, *uas =
2102
+        NULL, *rpath, *dbname;
2103
+    int ctimeout, rtimeout;
2104
+    STATBUF sb;
2105
+    struct cl_cvd *cvd;
2106
+
2107
+    if (strlen (url) > sizeof (urlcpy) - 1)
2108
+    {
2109
+        logg ("!DatabaseCustomURL: URL must be shorter than %lu\n",
2110
+              sizeof (urlcpy));
1947 2111
         return 70;
1948 2112
     }
1949 2113
 
1950
-    if(!strncasecmp(url, "http://", 7)) {
1951
-	strncpy(urlcpy, url, sizeof(urlcpy));
1952
-	host = &urlcpy[7];
1953
-	if(!(pt = strchr(host, '/'))) {
1954
-	    logg("!DatabaseCustomURL: Incorrect URL\n");
1955
-	    return 70;
1956
-	}
1957
-	*pt = 0;
1958
-	rpath = &url[pt - urlcpy + 1];
1959
-	dbname = strrchr(url, '/') + 1;
1960
-	if(!dbname || strlen(dbname) < 4) {
1961
-	    logg("DatabaseCustomURL: Incorrect URL\n");
1962
-	    return 70;
1963
-	}
1964
-
1965
-	/* Initialize proxy settings */
1966
-	if((opt = optget(opts, "HTTPProxyServer"))->enabled) {
1967
-	    proxy = opt->strarg;
1968
-	    if(strncasecmp(proxy, "http://", 7) == 0)
1969
-		proxy += 7;
1970
-
1971
-	    if((opt = optget(opts, "HTTPProxyUsername"))->enabled) {
1972
-		user = opt->strarg;
1973
-		if((opt = optget(opts, "HTTPProxyPassword"))->enabled) {
1974
-		    pass = opt->strarg;
1975
-		} else {
1976
-		    logg("HTTPProxyUsername requires HTTPProxyPassword\n");
1977
-		    return 56;
1978
-		}
1979
-	    }
1980
-	    if((opt = optget(opts, "HTTPProxyPort"))->enabled)
1981
-		port = opt->numarg;
1982
-	    logg("Connecting via %s\n", proxy);
1983
-	}
1984
-
1985
-	if((opt = optget(opts, "HTTPUserAgent"))->enabled)
1986
-	    uas = opt->strarg;
1987
-
1988
-	ctimeout = optget(opts, "ConnectTimeout")->numarg;
1989
-	rtimeout = optget(opts, "ReceiveTimeout")->numarg;
1990
-
1991
-	*mtime = 0;
1992
-	if(STAT(dbname, &sb) != -1)
1993
-	    Rfc2822DateTime(mtime, sb.st_mtime);
1994
-
1995
-	newfile = cli_gentemp(updtmpdir);
1996
-	ret = getfile(rpath, newfile, host, NULL, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, NULL, logerr, 0, *mtime ? mtime : NULL, opts, 1);
1997
-	if(ret == 1) {
1998
-	    logg("%s is up to date (version: custom database)\n", dbname);
1999
-	    unlink(newfile);
2000
-	    free(newfile);
2001
-	    return 1;
2002
-	} else if(ret > 1) {
2003
-	    logg("%cCan't download %s from %s\n", logerr ? '!' : '^', dbname, host);
2004
-	    unlink(newfile);
2005
-	    free(newfile);
2006
-	    return ret;
2007
-	}
2008
-
2009
-    } else if(!strncasecmp(url, "file://", 7)) {
2010
-	rpath = &url[7];
2114
+    if (!strncasecmp (url, "http://", 7))
2115
+    {
2116
+        strncpy (urlcpy, url, sizeof (urlcpy));
2117
+        host = &urlcpy[7];
2118
+        if (!(pt = strchr (host, '/')))
2119
+        {
2120
+            logg ("!DatabaseCustomURL: Incorrect URL\n");
2121
+            return 70;
2122
+        }
2123
+        *pt = 0;
2124
+        rpath = &url[pt - urlcpy + 1];
2125
+        dbname = strrchr (url, '/') + 1;
2126
+        if (!dbname || strlen (dbname) < 4)
2127
+        {
2128
+            logg ("DatabaseCustomURL: Incorrect URL\n");
2129
+            return 70;
2130
+        }
2131
+
2132
+        /* Initialize proxy settings */
2133
+        if ((opt = optget (opts, "HTTPProxyServer"))->enabled)
2134
+        {
2135
+            proxy = opt->strarg;
2136
+            if (strncasecmp (proxy, "http://", 7) == 0)
2137
+                proxy += 7;
2138
+
2139
+            if ((opt = optget (opts, "HTTPProxyUsername"))->enabled)
2140
+            {
2141
+                user = opt->strarg;
2142
+                if ((opt = optget (opts, "HTTPProxyPassword"))->enabled)
2143
+                {
2144
+                    pass = opt->strarg;
2145
+                }
2146
+                else
2147
+                {
2148
+                    logg ("HTTPProxyUsername requires HTTPProxyPassword\n");
2149
+                    return 56;
2150
+                }
2151
+            }
2152
+            if ((opt = optget (opts, "HTTPProxyPort"))->enabled)
2153
+                port = opt->numarg;
2154
+            logg ("Connecting via %s\n", proxy);
2155
+        }
2156
+
2157
+        if ((opt = optget (opts, "HTTPUserAgent"))->enabled)
2158
+            uas = opt->strarg;
2159
+
2160
+        ctimeout = optget (opts, "ConnectTimeout")->numarg;
2161
+        rtimeout = optget (opts, "ReceiveTimeout")->numarg;
2162
+
2163
+        *mtime = 0;
2164
+        if (STAT (dbname, &sb) != -1)
2165
+            Rfc2822DateTime (mtime, sb.st_mtime);
2166
+
2167
+        newfile = cli_gentemp (updtmpdir);
2168
+        ret =
2169
+            getfile (rpath, newfile, host, NULL, localip, proxy, port, user,
2170
+                     pass, uas, ctimeout, rtimeout, NULL, logerr, 0,
2171
+                     *mtime ? mtime : NULL, opts, 1);
2172
+        if (ret == 1)
2173
+        {
2174
+            logg ("%s is up to date (version: custom database)\n", dbname);
2175
+            unlink (newfile);
2176
+            free (newfile);
2177
+            return 1;
2178
+        }
2179
+        else if (ret > 1)
2180
+        {
2181
+            logg ("%cCan't download %s from %s\n", logerr ? '!' : '^', dbname,
2182
+                  host);
2183
+            unlink (newfile);
2184
+            free (newfile);
2185
+            return ret;
2186
+        }
2187
+
2188
+    }
2189
+    else if (!strncasecmp (url, "file://", 7))
2190
+    {
2191
+        rpath = &url[7];
2011 2192
 #ifdef _WIN32
2012
-	dbname = strrchr(rpath, '\\');
2193
+        dbname = strrchr (rpath, '\\');
2013 2194
 #else
2014
-	dbname = strrchr(rpath, '/');
2195
+        dbname = strrchr (rpath, '/');
2015 2196
 #endif
2016
-	if(!dbname || strlen(dbname++) < 5) {
2017
-	    logg("DatabaseCustomURL: Incorrect URL\n");
2018
-	    return 70;
2019
-	}
2020
-
2021
-	newfile = cli_gentemp(updtmpdir);
2022
-	if(!newfile)
2023
-	    return 70;
2024
-
2025
-	/* FIXME: preserve file permissions, calculate % */
2026
-	logg("Downloading %s [  0%%]\r", dbname);
2027
-	if(cli_filecopy(rpath, newfile) == -1) {
2028
-	    logg("DatabaseCustomURL: Can't copy file %s into database directory\n", rpath);
2029
-	    free(newfile);
2030
-	    return 70;
2031
-	}
2032
-	logg("Downloading %s [100%%]\n", dbname);
2033
-    } else {
2034
-	logg("!DatabaseCustomURL: Not supported protocol\n");
2035
-	return 70;
2036
-    }
2037
-
2038
-    if(optget(opts, "TestDatabases")->enabled && strlen(newfile) > 4) {
2039
-	newfile2 = malloc(strlen(newfile) + strlen(dbname) + 1);
2040
-	if(!newfile2) {
2041
-	    unlink(newfile);
2042
-	    free(newfile);
2043
-	    return 55;
2044
-	}
2045
-	sprintf(newfile2, "%s%s", newfile, dbname);
2046
-	newfile2[strlen(newfile) + strlen(dbname)] = 0;
2047
-	if(rename(newfile, newfile2) == -1) {
2048
-	    logg("!Can't rename %s to %s: %s\n", newfile, newfile2, strerror(errno));
2049
-	    unlink(newfile);
2050
-	    free(newfile);
2051
-	    free(newfile2);
2052
-	    return 57;
2053
-	}
2054
-	free(newfile);
2055
-	newfile = newfile2;
2056
-	sigchld_wait = 0;/* we need to wait() for the child ourselves */
2057
-	if (test_database_wrap(newfile, dbname, optget(opts, "Bytecode")->enabled)) {
2058
-	    logg("!Failed to load new database\n");
2059
-	    unlink(newfile);
2060
-	    free(newfile);
2061
-	    return 55;
2062
-	}
2063
-	sigchld_wait = 1;
2197
+        if (!dbname || strlen (dbname++) < 5)
2198
+        {
2199
+            logg ("DatabaseCustomURL: Incorrect URL\n");
2200
+            return 70;
2201
+        }
2202
+
2203
+        newfile = cli_gentemp (updtmpdir);
2204
+        if (!newfile)
2205
+            return 70;
2206
+
2207
+        /* FIXME: preserve file permissions, calculate % */
2208
+        logg ("Downloading %s [  0%%]\r", dbname);
2209
+        if (cli_filecopy (rpath, newfile) == -1)
2210
+        {
2211
+            logg ("DatabaseCustomURL: Can't copy file %s into database directory\n", rpath);
2212
+            free (newfile);
2213
+            return 70;
2214
+        }
2215
+        logg ("Downloading %s [100%%]\n", dbname);
2216
+    }
2217
+    else
2218
+    {
2219
+        logg ("!DatabaseCustomURL: Not supported protocol\n");
2220
+        return 70;
2221
+    }
2222
+
2223
+    if (optget (opts, "TestDatabases")->enabled && strlen (newfile) > 4)
2224
+    {
2225
+        newfile2 = malloc (strlen (newfile) + strlen (dbname) + 1);
2226
+        if (!newfile2)
2227
+        {
2228
+            unlink (newfile);
2229
+            free (newfile);
2230
+            return 55;
2231
+        }
2232
+        sprintf (newfile2, "%s%s", newfile, dbname);
2233
+        newfile2[strlen (newfile) + strlen (dbname)] = 0;
2234
+        if (rename (newfile, newfile2) == -1)
2235
+        {
2236
+            logg ("!Can't rename %s to %s: %s\n", newfile, newfile2,
2237
+                  strerror (errno));
2238
+            unlink (newfile);
2239
+            free (newfile);
2240
+            free (newfile2);
2241
+            return 57;
2242
+        }
2243
+        free (newfile);
2244
+        newfile = newfile2;
2245
+        sigchld_wait = 0;       /* we need to wait() for the child ourselves */
2246
+        if (test_database_wrap
2247
+            (newfile, dbname, optget (opts, "Bytecode")->enabled))
2248
+        {
2249
+            logg ("!Failed to load new database\n");
2250
+            unlink (newfile);
2251
+            free (newfile);
2252
+            return 55;
2253
+        }
2254
+        sigchld_wait = 1;
2064 2255
     }
2065 2256
 
2066 2257
 #ifdef _WIN32
2067
-    if(!access(dbname, R_OK) && unlink(dbname)) {
2068
-	logg("!Can't unlink %s. Please fix the problem manually and try again.\n", dbname);
2069
-	unlink(newfile);
2070
-	free(newfile);
2071
-	return 53;
2258
+    if (!access (dbname, R_OK) && unlink (dbname))
2259
+    {
2260
+        logg ("!Can't unlink %s. Please fix the problem manually and try again.\n", dbname);
2261
+        unlink (newfile);
2262
+        free (newfile);
2263
+        return 53;
2072 2264
     }
2073 2265
 #endif
2074 2266
 
2075
-    if(rename(newfile, dbname) == -1) {
2076
-	logg("!Can't rename %s to %s: %s\n", newfile, dbname, strerror(errno));
2077
-	unlink(newfile);
2078
-	free(newfile);
2079
-	return 57;
2267
+    if (rename (newfile, dbname) == -1)
2268
+    {
2269
+        logg ("!Can't rename %s to %s: %s\n", newfile, dbname,
2270
+              strerror (errno));
2271
+        unlink (newfile);
2272
+        free (newfile);
2273
+        return 57;
2080 2274
     }
2081
-    free(newfile);
2275
+    free (newfile);
2082 2276
 
2083
-    if(cli_strbcasestr(dbname, ".cld") || cli_strbcasestr(dbname, ".cvd")) {
2084
-	if((cvd = cl_cvdhead(dbname))) {
2085
-	    sigs = cvd->sigs;
2086
-	    cl_cvdfree(cvd);
2087
-	}
2088
-    } else if(cli_strbcasestr(dbname, ".cbc")) {
2089
-	sigs = 1;
2090
-    } else {
2091
-	sigs = countlines(dbname);
2277
+    if (cli_strbcasestr (dbname, ".cld") || cli_strbcasestr (dbname, ".cvd"))
2278
+    {
2279
+        if ((cvd = cl_cvdhead (dbname)))
2280
+        {
2281
+            sigs = cvd->sigs;
2282
+            cl_cvdfree (cvd);
2283
+        }
2284
+    }
2285
+    else if (cli_strbcasestr (dbname, ".cbc"))
2286
+    {
2287
+        sigs = 1;
2288
+    }
2289
+    else
2290
+    {
2291
+        sigs = countlines (dbname);
2092 2292
     }
2093 2293
 
2094
-    logg("%s updated (version: custom database, sigs: %u)\n", dbname, sigs);
2294
+    logg ("%s updated (version: custom database, sigs: %u)\n", dbname, sigs);
2095 2295
     *signo += sigs;
2096 2296
     return 0;
2097 2297
 }
2098 2298
 
2099
-int downloadmanager(const struct optstruct *opts, const char *hostname, unsigned int attempt)
2299
+int
2300
+downloadmanager (const struct optstruct *opts, const char *hostname,
2301
+                 unsigned int attempt)
2100 2302
 {
2101
-	time_t currtime;
2102
-	int ret, custret, updated = 0, outdated = 0, signo = 0, logerr;
2103
-	unsigned int ttl;
2104
-	char ipaddr[46], *dnsreply = NULL, *pt, *localip = NULL, *newver = NULL;
2105
-	const struct optstruct *opt;
2106
-	struct mirdat mdat;
2303
+    time_t currtime;
2304
+    int ret, custret, updated = 0, outdated = 0, signo = 0, logerr;
2305
+    unsigned int ttl;
2306
+    char ipaddr[46], *dnsreply = NULL, *pt, *localip = NULL, *newver = NULL;
2307
+    const struct optstruct *opt;
2308
+    struct mirdat mdat;
2107 2309
 #ifdef HAVE_RESOLV_H
2108
-	const char *dnsdbinfo;
2310
+    const char *dnsdbinfo;
2109 2311
 #endif
2110 2312
 
2111
-    logerr = (optget(opts, "MaxAttempts")->numarg == attempt);
2313
+    logerr = (optget (opts, "MaxAttempts")->numarg == attempt);
2112 2314
 
2113
-    pt = cli_gentemp(dbdir);
2114
-    if(!pt)
2115
-	return 57;
2116
-    strncpy(updtmpdir, pt, sizeof(updtmpdir));
2117
-    updtmpdir[sizeof(updtmpdir)-1] = '\0';
2118
-    free(pt);
2119
-    if(mkdir(updtmpdir, 0755)) {
2120
-	logg("!Can't create temporary directory %s\n", updtmpdir);
2121
-	logg("Hint: The database directory must be writable for UID %d or GID %d\n", getuid(), getgid());
2122
-	return 57;
2315
+    pt = cli_gentemp (dbdir);
2316
+    if (!pt)
2317
+        return 57;
2318
+    strncpy (updtmpdir, pt, sizeof (updtmpdir));
2319
+    updtmpdir[sizeof (updtmpdir) - 1] = '\0';
2320
+    free (pt);
2321
+    if (mkdir (updtmpdir, 0755))
2322
+    {
2323
+        logg ("!Can't create temporary directory %s\n", updtmpdir);
2324
+        logg ("Hint: The database directory must be writable for UID %d or GID %d\n", getuid (), getgid ());
2325
+        return 57;
2123 2326
     }
2124 2327
 
2125
-    time(&currtime);
2126
-    logg("ClamAV update process started at %s", ctime(&currtime));
2328
+    time (&currtime);
2329
+    logg ("ClamAV update process started at %s", ctime (&currtime));
2127 2330
 #ifdef HAVE_GETADDRINFO
2128
-    logg("*Using IPv6 aware code\n");
2331
+    logg ("*Using IPv6 aware code\n");
2129 2332
 #endif
2130 2333
 
2131 2334
 #ifdef HAVE_RESOLV_H
2132
-    dnsdbinfo = optget(opts, "DNSDatabaseInfo")->strarg;
2133
-
2134
-    if(optget(opts, "no-dns")->enabled) {
2135
-	dnsreply = NULL;
2136
-    } else {
2137
-	if((dnsreply = dnsquery(dnsdbinfo, T_TXT, &ttl))) {
2138
-	    logg("*TTL: %d\n", ttl);
2139
-
2140
-	    if((pt = cli_strtok(dnsreply, 3, ":"))) {
2141
-		    int rt;
2142
-		    time_t ct;
2143
-
2144
-		rt = atoi(pt);
2145
-		free(pt);
2146
-		time(&ct);
2147
-		if((int) ct - rt > 10800) {
2148
-		    logg("^DNS record is older than 3 hours.\n");
2149
-		    free(dnsreply);
2150
-		    dnsreply = NULL;
2151
-		}
2152
-
2153
-	    } else {
2154
-		free(dnsreply);
2155
-		dnsreply = NULL;
2156
-	    }
2157
-
2158
-	    if(dnsreply) {
2159
-		    int vwarning = 1;
2160
-
2161
-		if((pt = cli_strtok(dnsreply, 4, ":"))) {
2162
-		    if(*pt == '0')
2163
-			vwarning = 0;
2164
-
2165
-		    free(pt);
2166
-		}
2167
-
2168
-		if((newver = cli_strtok(dnsreply, 0, ":"))) {
2169
-			char vstr[32];
2170
-
2171
-		    logg("*Software version from DNS: %s\n", newver);
2172
-		    strncpy(vstr, get_version(), 32);
2173
-		    vstr[31] = 0;
2174
-		    if(vwarning && !strstr(vstr, "devel") && !strstr(vstr, "rc")) {
2175
-			pt = strchr(vstr, '-');
2176
-			if((pt && strncmp(vstr, newver, pt - vstr)) || (!pt && strcmp(vstr, newver))) {
2177
-			    logg("^Your ClamAV installation is OUTDATED!\n");
2178
-			    logg("^Local version: %s Recommended version: %s\n", vstr, newver);
2179
-			    logg("DON'T PANIC! Read http://www.clamav.net/support/faq\n");
2180
-			    outdated = 1;
2181
-			}
2182
-		    }
2183
-		}
2184
-	    }
2185
-	}
2186
-
2187
-	if(!dnsreply) {
2188
-	    logg("^Invalid DNS reply. Falling back to HTTP mode.\n");
2189
-	}
2335
+    dnsdbinfo = optget (opts, "DNSDatabaseInfo")->strarg;
2336
+
2337
+    if (optget (opts, "no-dns")->enabled)
2338
+    {
2339
+        dnsreply = NULL;
2340
+    }
2341
+    else
2342
+    {
2343
+        if ((dnsreply = dnsquery (dnsdbinfo, T_TXT, &ttl)))
2344
+        {
2345
+            logg ("*TTL: %d\n", ttl);
2346
+
2347
+            if ((pt = cli_strtok (dnsreply, 3, ":")))
2348
+            {
2349
+                int rt;
2350
+                time_t ct;
2351
+
2352
+                rt = atoi (pt);
2353
+                free (pt);
2354
+                time (&ct);
2355
+                if ((int) ct - rt > 10800)
2356
+                {
2357
+                    logg ("^DNS record is older than 3 hours.\n");
2358
+                    free (dnsreply);
2359
+                    dnsreply = NULL;
2360
+                }
2361
+
2362
+            }
2363
+            else
2364
+            {
2365
+                free (dnsreply);
2366
+                dnsreply = NULL;
2367
+            }
2368
+
2369
+            if (dnsreply)
2370
+            {
2371
+                int vwarning = 1;
2372
+
2373
+                if ((pt = cli_strtok (dnsreply, 4, ":")))
2374
+                {
2375
+                    if (*pt == '0')
2376
+                        vwarning = 0;
2377
+
2378
+                    free (pt);
2379
+                }
2380
+
2381
+                if ((newver = cli_strtok (dnsreply, 0, ":")))
2382
+                {
2383
+                    char vstr[32];
2384
+
2385
+                    logg ("*Software version from DNS: %s\n", newver);
2386
+                    strncpy (vstr, get_version (), 32);
2387
+                    vstr[31] = 0;
2388
+                    if (vwarning && !strstr (vstr, "devel")
2389
+                        && !strstr (vstr, "rc"))
2390
+                    {
2391
+                        pt = strchr (vstr, '-');
2392
+                        if ((pt && strncmp (vstr, newver, pt - vstr))
2393
+                            || (!pt && strcmp (vstr, newver)))
2394
+                        {
2395
+                            logg ("^Your ClamAV installation is OUTDATED!\n");
2396
+                            logg ("^Local version: %s Recommended version: %s\n", vstr, newver);
2397
+                            logg ("DON'T PANIC! Read http://www.clamav.net/support/faq\n");
2398
+                            outdated = 1;
2399
+                        }
2400
+                    }
2401
+                }
2402
+            }
2403
+        }
2404
+
2405
+        if (!dnsreply)
2406
+        {
2407
+            logg ("^Invalid DNS reply. Falling back to HTTP mode.\n");
2408
+        }
2190 2409
     }
2191 2410
 #endif /* HAVE_RESOLV_H */
2192 2411
 
2193
-    if((opt = optget(opts, "LocalIPAddress"))->enabled)
2194
-	localip = opt->strarg;
2412
+    if ((opt = optget (opts, "LocalIPAddress"))->enabled)
2413
+        localip = opt->strarg;
2195 2414
 
2196
-    if(optget(opts, "HTTPProxyServer")->enabled || optget(opts, "PrivateMirror")->enabled)
2197
-	mirman_read("mirrors.dat", &mdat, 0);
2415
+    if (optget (opts, "HTTPProxyServer")->enabled
2416
+        || optget (opts, "PrivateMirror")->enabled)
2417
+        mirman_read ("mirrors.dat", &mdat, 0);
2198 2418
     else
2199
-	mirman_read("mirrors.dat", &mdat, 1);
2419
+        mirman_read ("mirrors.dat", &mdat, 1);
2200 2420
 
2201
-    memset(ipaddr, 0, sizeof(ipaddr));
2421
+    memset (ipaddr, 0, sizeof (ipaddr));
2202 2422
 
2203 2423
     /* custom dbs */
2204
-    if((opt = optget(opts, "DatabaseCustomURL"))->enabled) {
2205
-	while(opt) {
2206
-	    if((custret = updatecustomdb(opt->strarg, &signo, opts, localip, logerr)) == 0)
2207
-		updated = 1;
2208
-	    opt = opt->nextarg;
2209
-	}
2210
-    }
2211
-
2212
-    if((opt = optget(opts, "update-db"))->enabled) {
2213
-	    const char *u_dnsreply;
2214
-	    int u_extra;
2215
-
2216
-	while(opt) {
2217
-	    if(!strcmp(opt->strarg, "custom")) {
2218
-		if(!optget(opts, "DatabaseCustomURL")->enabled) {
2219
-		    logg("!--update-db=custom requires DatabaseCustomURL\n");
2220
-		    custret = 56;
2221
-		}
2222
-		free(dnsreply);
2223
-		free(newver);
2224
-		mirman_write("mirrors.dat", dbdir, &mdat);
2225
-		mirman_free(&mdat);
2226
-		cli_rmdirs(updtmpdir);
2227
-		return custret;
2228
-	    }
2229
-
2230
-	    if(!strcmp(opt->strarg, "main") || !strcmp(opt->strarg, "daily") || !strcmp(opt->strarg, "safebrowsing") || !strcmp(opt->strarg, "bytecode")) {
2231
-		u_dnsreply = dnsreply;
2232
-		u_extra = 0;
2233
-	    } else {
2234
-		u_dnsreply = NULL;
2235
-		u_extra = 1;
2236
-	    }
2237
-	    if((ret = updatedb(opt->strarg, hostname, ipaddr, &signo, opts, u_dnsreply, localip, outdated, &mdat, logerr, u_extra, attempt)) > 50) {
2238
-		if(dnsreply)
2239
-		    free(dnsreply);
2240
-		if(newver)
2241
-		    free(newver);
2242
-		mirman_write("mirrors.dat", dbdir, &mdat);
2243
-		mirman_free(&mdat);
2244
-		cli_rmdirs(updtmpdir);
2245
-		return ret;
2246
-	    } else if(ret == 0)
2247
-		updated = 1;
2248
-
2249
-	    opt = opt->nextarg;
2250
-	}
2251
-
2252
-    } else {
2253
-	if((ret = updatedb("main", hostname, ipaddr, &signo, opts, dnsreply, localip, outdated, &mdat, logerr, 0, attempt)) > 50) {
2254
-	    if(dnsreply)
2255
-		free(dnsreply);
2256
-	    if(newver)
2257
-		free(newver);
2258
-	    mirman_write("mirrors.dat", dbdir, &mdat);
2259
-	    mirman_free(&mdat);
2260
-	    cli_rmdirs(updtmpdir);
2261
-	    return ret;
2262
-	} else if(ret == 0)
2263
-	    updated = 1;
2264
-
2265
-	/* if ipaddr[0] != 0 it will use it to connect to the web host */
2266
-	if((ret = updatedb("daily", hostname, ipaddr, &signo, opts, dnsreply, localip, outdated, &mdat, logerr, 0, attempt)) > 50) {
2267
-	    if(dnsreply)
2268
-		free(dnsreply);
2269
-	    if(newver)
2270
-		free(newver);
2271
-	    mirman_write("mirrors.dat", dbdir, &mdat);
2272
-	    mirman_free(&mdat);
2273
-	    cli_rmdirs(updtmpdir);
2274
-	    return ret;
2275
-	} else if(ret == 0)
2276
-	    updated = 1;
2277
-
2278
-	if(!optget(opts, "SafeBrowsing")->enabled) {
2279
-		const char *safedb = NULL;
2280
-
2281
-	    if(!access("safebrowsing.cvd", R_OK))
2282
-		safedb = "safebrowsing.cvd";
2283
-	    else if(!access("safebrowsing.cld", R_OK))
2284
-		safedb = "safebrowsing.cld";
2285
-
2286
-	    if(safedb) {
2287
-		if(unlink(safedb))
2288
-		    logg("^SafeBrowsing is disabled but can't remove old %s\n", safedb);
2289
-		else
2290
-		    logg("*%s removed\n", safedb);
2291
-	    }
2292
-	} else if((ret = updatedb("safebrowsing", hostname, ipaddr, &signo, opts, dnsreply, localip, outdated, &mdat, logerr, 0, attempt)) > 50) {
2293
-	    if(dnsreply)
2294
-		free(dnsreply);
2295
-	    if(newver)
2296
-		free(newver);
2297
-	    mirman_write("mirrors.dat", dbdir, &mdat);
2298
-	    mirman_free(&mdat);
2299
-	    cli_rmdirs(updtmpdir);
2300
-	    return ret;
2301
-	} else if(ret == 0)
2302
-	    updated = 1;
2303
-
2304
-	if(!optget(opts, "Bytecode")->enabled) {
2305
-		const char *dbname = NULL;
2306
-
2307
-	    if(!access("bytecode.cvd", R_OK))
2308
-		dbname = "bytecode.cvd";
2309
-	    else if(!access("bytecode.cld", R_OK))
2310
-		dbname = "bytecode.cld";
2311
-
2312
-	    if(dbname) {
2313
-		if(unlink(dbname))
2314
-		    logg("^Bytecode is disabled but can't remove old %s\n", dbname);
2315
-		else
2316
-		    logg("*%s removed\n", dbname);
2317
-	    }
2318
-	} else if((ret = updatedb("bytecode", hostname, ipaddr, &signo, opts, dnsreply, localip, outdated, &mdat, logerr, 0, attempt)) > 50) {
2319
-	    if(dnsreply)
2320
-		free(dnsreply);
2321
-	    if(newver)
2322
-		free(newver);
2323
-	    mirman_write("mirrors.dat", dbdir, &mdat);
2324
-	    mirman_free(&mdat);
2325
-	    cli_rmdirs(updtmpdir);
2326
-	    return ret;
2327
-	} else if(ret == 0)
2328
-	    updated = 1;
2329
-
2330
-	/* handle extra dbs */
2331
-	if((opt = optget(opts, "ExtraDatabase"))->enabled) {
2332
-	    while(opt) {
2333
-		if((ret = updatedb(opt->strarg, hostname, ipaddr, &signo, opts, NULL, localip, outdated, &mdat, logerr, 1, attempt)) > 50) {
2334
-		    if(dnsreply)
2335
-			free(dnsreply);
2336
-		    if(newver)
2337
-			free(newver);
2338
-		    mirman_write("mirrors.dat", dbdir, &mdat);
2339
-		    mirman_free(&mdat);
2340
-		    cli_rmdirs(updtmpdir);
2341
-		    return ret;
2342
-		} else if(ret == 0)
2343
-		    updated = 1;
2344
-		opt = opt->nextarg;
2345
-	    }
2346
-	}
2347
-    }
2348
-
2349
-    if(dnsreply)
2350
-	free(dnsreply);
2351
-
2352
-    mirman_write("mirrors.dat", dbdir, &mdat);
2353
-    mirman_free(&mdat);
2354
-
2355
-    cli_rmdirs(updtmpdir);
2356
-
2357
-    if(checkdbdir() < 0) {
2358
-	if(newver)
2359
-	    free(newver);
2360
-	return 54;
2361
-    }
2362
-
2363
-    if(updated) {
2364
-	if(optget(opts, "HTTPProxyServer")->enabled || !ipaddr[0]) {
2365
-	    logg("Database updated (%d signatures) from %s\n", signo, hostname);
2366
-	} else {
2367
-	    logg("Database updated (%d signatures) from %s (IP: %s)\n", signo, hostname, ipaddr);
2368
-	}
2424
+    if ((opt = optget (opts, "DatabaseCustomURL"))->enabled)
2425
+    {
2426
+        while (opt)
2427
+        {
2428
+            if ((custret =
2429
+                 updatecustomdb (opt->strarg, &signo, opts, localip,
2430
+                                 logerr)) == 0)
2431
+                updated = 1;
2432
+            opt = opt->nextarg;
2433
+        }
2434
+    }
2435
+
2436
+    if ((opt = optget (opts, "update-db"))->enabled)
2437
+    {
2438
+        const char *u_dnsreply;
2439
+        int u_extra;
2440
+
2441
+        while (opt)
2442
+        {
2443
+            if (!strcmp (opt->strarg, "custom"))
2444
+            {
2445
+                if (!optget (opts, "DatabaseCustomURL")->enabled)
2446
+                {
2447
+                    logg ("!--update-db=custom requires DatabaseCustomURL\n");
2448
+                    custret = 56;
2449
+                }
2450
+                free (dnsreply);
2451
+                free (newver);
2452
+                mirman_write ("mirrors.dat", dbdir, &mdat);
2453
+                mirman_free (&mdat);
2454
+                cli_rmdirs (updtmpdir);
2455
+                return custret;
2456
+            }
2457
+
2458
+            if (!strcmp (opt->strarg, "main")
2459
+                || !strcmp (opt->strarg, "daily")
2460
+                || !strcmp (opt->strarg, "safebrowsing")
2461
+                || !strcmp (opt->strarg, "bytecode"))
2462
+            {
2463
+                u_dnsreply = dnsreply;
2464
+                u_extra = 0;
2465
+            }
2466
+            else
2467
+            {
2468
+                u_dnsreply = NULL;
2469
+                u_extra = 1;
2470
+            }
2471
+            if ((ret =
2472
+                 updatedb (opt->strarg, hostname, ipaddr, &signo, opts,
2473
+                           u_dnsreply, localip, outdated, &mdat, logerr,
2474
+                           u_extra, attempt)) > 50)
2475
+            {
2476
+                if (dnsreply)
2477
+                    free (dnsreply);
2478
+                if (newver)
2479
+                    free (newver);
2480
+                mirman_write ("mirrors.dat", dbdir, &mdat);
2481
+                mirman_free (&mdat);
2482
+                cli_rmdirs (updtmpdir);
2483
+                return ret;
2484
+            }
2485
+            else if (ret == 0)
2486
+                updated = 1;
2487
+
2488
+            opt = opt->nextarg;
2489
+        }
2490
+
2491
+    }
2492
+    else
2493
+    {
2494
+        if ((ret =
2495
+             updatedb ("main", hostname, ipaddr, &signo, opts, dnsreply,
2496
+                       localip, outdated, &mdat, logerr, 0, attempt)) > 50)
2497
+        {
2498
+            if (dnsreply)
2499
+                free (dnsreply);
2500
+            if (newver)
2501
+                free (newver);
2502
+            mirman_write ("mirrors.dat", dbdir, &mdat);
2503
+            mirman_free (&mdat);
2504
+            cli_rmdirs (updtmpdir);
2505
+            return ret;
2506
+        }
2507
+        else if (ret == 0)
2508
+            updated = 1;
2509
+
2510
+        /* if ipaddr[0] != 0 it will use it to connect to the web host */
2511
+        if ((ret =
2512
+             updatedb ("daily", hostname, ipaddr, &signo, opts, dnsreply,
2513
+                       localip, outdated, &mdat, logerr, 0, attempt)) > 50)
2514
+        {
2515
+            if (dnsreply)
2516
+                free (dnsreply);
2517
+            if (newver)
2518
+                free (newver);
2519
+            mirman_write ("mirrors.dat", dbdir, &mdat);
2520
+            mirman_free (&mdat);
2521
+            cli_rmdirs (updtmpdir);
2522
+            return ret;
2523
+        }
2524
+        else if (ret == 0)
2525
+            updated = 1;
2526
+
2527
+        if (!optget (opts, "SafeBrowsing")->enabled)
2528
+        {
2529
+            const char *safedb = NULL;
2530
+
2531
+            if (!access ("safebrowsing.cvd", R_OK))
2532
+                safedb = "safebrowsing.cvd";
2533
+            else if (!access ("safebrowsing.cld", R_OK))
2534
+                safedb = "safebrowsing.cld";
2535
+
2536
+            if (safedb)
2537
+            {
2538
+                if (unlink (safedb))
2539
+                    logg ("^SafeBrowsing is disabled but can't remove old %s\n", safedb);
2540
+                else
2541
+                    logg ("*%s removed\n", safedb);
2542
+            }
2543
+        }
2544
+        else if ((ret =
2545
+                  updatedb ("safebrowsing", hostname, ipaddr, &signo, opts,
2546
+                            dnsreply, localip, outdated, &mdat, logerr, 0,
2547
+                            attempt)) > 50)
2548
+        {
2549
+            if (dnsreply)
2550
+                free (dnsreply);
2551
+            if (newver)
2552
+                free (newver);
2553
+            mirman_write ("mirrors.dat", dbdir, &mdat);
2554
+            mirman_free (&mdat);
2555
+            cli_rmdirs (updtmpdir);
2556
+            return ret;
2557
+        }
2558
+        else if (ret == 0)
2559
+            updated = 1;
2560
+
2561
+        if (!optget (opts, "Bytecode")->enabled)
2562
+        {
2563
+            const char *dbname = NULL;
2564
+
2565
+            if (!access ("bytecode.cvd", R_OK))
2566
+                dbname = "bytecode.cvd";
2567
+            else if (!access ("bytecode.cld", R_OK))
2568
+                dbname = "bytecode.cld";
2569
+
2570
+            if (dbname)
2571
+            {
2572
+                if (unlink (dbname))
2573
+                    logg ("^Bytecode is disabled but can't remove old %s\n",
2574
+                          dbname);
2575
+                else
2576
+                    logg ("*%s removed\n", dbname);
2577
+            }
2578
+        }
2579
+        else if ((ret =
2580
+                  updatedb ("bytecode", hostname, ipaddr, &signo, opts,
2581
+                            dnsreply, localip, outdated, &mdat, logerr, 0,
2582
+                            attempt)) > 50)
2583
+        {
2584
+            if (dnsreply)
2585
+                free (dnsreply);
2586
+            if (newver)
2587
+                free (newver);
2588
+            mirman_write ("mirrors.dat", dbdir, &mdat);
2589
+            mirman_free (&mdat);
2590
+            cli_rmdirs (updtmpdir);
2591
+            return ret;
2592
+        }
2593
+        else if (ret == 0)
2594
+            updated = 1;
2595
+
2596
+        /* handle extra dbs */
2597
+        if ((opt = optget (opts, "ExtraDatabase"))->enabled)
2598
+        {
2599
+            while (opt)
2600
+            {
2601
+                if ((ret =
2602
+                     updatedb (opt->strarg, hostname, ipaddr, &signo, opts,
2603
+                               NULL, localip, outdated, &mdat, logerr, 1,
2604
+                               attempt)) > 50)
2605
+                {
2606
+                    if (dnsreply)
2607
+                        free (dnsreply);
2608
+                    if (newver)
2609
+                        free (newver);
2610
+                    mirman_write ("mirrors.dat", dbdir, &mdat);
2611
+                    mirman_free (&mdat);
2612
+                    cli_rmdirs (updtmpdir);
2613
+                    return ret;
2614
+                }
2615
+                else if (ret == 0)
2616
+                    updated = 1;
2617
+                opt = opt->nextarg;
2618
+            }
2619
+        }
2620
+    }
2621
+
2622
+    if (dnsreply)
2623
+        free (dnsreply);
2624
+
2625
+    mirman_write ("mirrors.dat", dbdir, &mdat);
2626
+    mirman_free (&mdat);
2627
+
2628
+    cli_rmdirs (updtmpdir);
2629
+
2630
+    if (checkdbdir () < 0)
2631
+    {
2632
+        if (newver)
2633
+            free (newver);
2634
+        return 54;
2635
+    }
2636
+
2637
+    if (updated)
2638
+    {
2639
+        if (optget (opts, "HTTPProxyServer")->enabled || !ipaddr[0])
2640
+        {
2641
+            logg ("Database updated (%d signatures) from %s\n", signo,
2642
+                  hostname);
2643
+        }
2644
+        else
2645
+        {
2646
+            logg ("Database updated (%d signatures) from %s (IP: %s)\n",
2647
+                  signo, hostname, ipaddr);
2648
+        }
2369 2649
 
2370 2650
 #ifdef BUILD_CLAMD
2371
-	if((opt = optget(opts, "NotifyClamd"))->active)
2372
-	    notify(opt->strarg);
2651
+        if ((opt = optget (opts, "NotifyClamd"))->active)
2652
+            notify (opt->strarg);
2373 2653
 #endif
2374 2654
 
2375
-	if((opt = optget(opts, "OnUpdateExecute"))->enabled)
2376
-	    execute("OnUpdateExecute", opt->strarg, opts);
2377
-    }
2378
-
2379
-    if(outdated) {
2380
-	if((opt = optget(opts, "OnOutdatedExecute"))->enabled) {
2381
-		char *cmd = strdup(opt->strarg);
2382
-
2383
-	    if((pt = newver)) {
2384
-		while(*pt) {
2385
-		    if(!strchr("0123456789.", *pt)) {
2386
-			logg("!downloadmanager: OnOutdatedExecute: Incorrect version number string\n");
2387
-			free(newver);
2388
-			newver = NULL;
2389
-			break;
2390
-		    }
2391
-		    pt++;
2392
-		}
2393
-	    }
2394
-
2395
-	    if(newver && (pt = strstr(cmd, "%v"))) {
2396
-		    char *buffer = (char *) malloc(strlen(cmd) + strlen(newver) + 10);
2397
-
2398
-		if(!buffer) {
2399
-		    logg("!downloadmanager: Can't allocate memory for buffer\n");
2400
-		    free(cmd);
2401
-		    if(newver)
2402
-			free(newver);
2403
-		    return 75;
2404
-		}
2405
-
2406
-		*pt = 0; pt += 2;
2407
-		strcpy(buffer, cmd);
2408
-		strcat(buffer, newver);
2409
-		strcat(buffer, pt);
2410
-		free(cmd);
2411
-		cmd = strdup(buffer);
2412
-		free(buffer);
2413
-	    }
2414
-
2415
-	    if(newver)
2416
-		execute("OnOutdatedExecute", cmd, opts);
2417
-
2418
-	    free(cmd);
2419
-	}
2420
-    }
2421
-
2422
-    if(newver)
2423
-	free(newver);
2655
+        if ((opt = optget (opts, "OnUpdateExecute"))->enabled)
2656
+            execute ("OnUpdateExecute", opt->strarg, opts);
2657
+    }
2658
+
2659
+    if (outdated)
2660
+    {
2661
+        if ((opt = optget (opts, "OnOutdatedExecute"))->enabled)
2662
+        {
2663
+            char *cmd = strdup (opt->strarg);
2664
+
2665
+            if ((pt = newver))
2666
+            {
2667
+                while (*pt)
2668
+                {
2669
+                    if (!strchr ("0123456789.", *pt))
2670
+                    {
2671
+                        logg ("!downloadmanager: OnOutdatedExecute: Incorrect version number string\n");
2672
+                        free (newver);
2673
+                        newver = NULL;
2674
+                        break;
2675
+                    }
2676
+                    pt++;
2677
+                }
2678
+            }
2679
+
2680
+            if (newver && (pt = strstr (cmd, "%v")))
2681
+            {
2682
+                char *buffer =
2683
+                    (char *) malloc (strlen (cmd) + strlen (newver) + 10);
2684
+
2685
+                if (!buffer)
2686
+                {
2687
+                    logg ("!downloadmanager: Can't allocate memory for buffer\n");
2688
+                    free (cmd);
2689
+                    if (newver)
2690
+                        free (newver);
2691
+                    return 75;
2692
+                }
2693
+
2694
+                *pt = 0;
2695
+                pt += 2;
2696
+                strcpy (buffer, cmd);
2697
+                strcat (buffer, newver);
2698
+                strcat (buffer, pt);
2699
+                free (cmd);
2700
+                cmd = strdup (buffer);
2701
+                free (buffer);
2702
+            }
2703
+
2704
+            if (newver)
2705
+                execute ("OnOutdatedExecute", cmd, opts);
2706
+
2707
+            free (cmd);
2708
+        }
2709
+    }
2710
+
2711
+    if (newver)
2712
+        free (newver);
2424 2713
 
2425 2714
     return 0;
2426 2715
 }
... ...
@@ -25,8 +25,9 @@
25 25
 
26 26
 #include "shared/optparser.h"
27 27
 
28
-int downloadmanager(const struct optstruct *opts, const char *hostname, unsigned int attempt);
28
+int downloadmanager (const struct optstruct *opts, const char *hostname,
29
+                     unsigned int attempt);
29 30
 
30
-int submitstats(const char *clamdcfg, const struct optstruct *opts);
31
+int submitstats (const char *clamdcfg, const struct optstruct *opts);
31 32
 
32 33
 #endif
... ...
@@ -46,262 +46,326 @@
46 46
 
47 47
 #ifndef HAVE_GETADDRINFO
48 48
 #ifndef AF_INET6
49
-#define AF_INET6    0xbeef  /* foo */
49
+#define AF_INET6    0xbeef      /* foo */
50 50
 #endif
51 51
 #endif
52 52
 
53 53
 #define IGNORE_LONG	3 * 86400
54 54
 #define IGNORE_SHORT	6 * 3600
55 55
 
56
-void mirman_free(struct mirdat *mdat)
56
+void
57
+mirman_free (struct mirdat *mdat)
57 58
 {
58
-    if(mdat && mdat->num) {
59
-	free(mdat->mirtab);
60
-	mdat->num = 0;
59
+    if (mdat && mdat->num)
60
+    {
61
+        free (mdat->mirtab);
62
+        mdat->num = 0;
61 63
     }
62 64
 }
63 65
 
64
-int mirman_read(const char *file, struct mirdat *mdat, uint8_t active)
66
+int
67
+mirman_read (const char *file, struct mirdat *mdat, uint8_t active)
65 68
 {
66
-	struct mirdat_ip mip;
67
-	int fd, bread;
68
-
69
-
70
-    memset(mdat, 0, sizeof(struct mirdat));
71
-
72
-    if(!(mdat->active = active))
73
-	return 0;
74
-
75
-    if((fd = open(file, O_RDONLY|O_BINARY)) == -1)
76
-	return -1;
77
-
78
-    while((bread = read(fd, &mip, sizeof(mip))) == sizeof(mip)) {
79
-	mdat->mirtab = (struct mirdat_ip *) realloc(mdat->mirtab, (mdat->num + 1) * sizeof(mip));
80
-	if(!mdat->mirtab) {
81
-	    logg("!Can't allocate memory for mdat->mirtab\n");
82
-	    mirman_free(mdat);
83
-	    close(fd);
84
-	    return -1;
85
-	}
86
-	memcpy(&mdat->mirtab[mdat->num], &mip, sizeof(mip));
87
-	mdat->num++;
69
+    struct mirdat_ip mip;
70
+    int fd, bread;
71
+
72
+
73
+    memset (mdat, 0, sizeof (struct mirdat));
74
+
75
+    if (!(mdat->active = active))
76
+        return 0;
77
+
78
+    if ((fd = open (file, O_RDONLY | O_BINARY)) == -1)
79
+        return -1;
80
+
81
+    while ((bread = read (fd, &mip, sizeof (mip))) == sizeof (mip))
82
+    {
83
+        mdat->mirtab =
84
+            (struct mirdat_ip *) realloc (mdat->mirtab,
85
+                                          (mdat->num + 1) * sizeof (mip));
86
+        if (!mdat->mirtab)
87
+        {
88
+            logg ("!Can't allocate memory for mdat->mirtab\n");
89
+            mirman_free (mdat);
90
+            close (fd);
91
+            return -1;
92
+        }
93
+        memcpy (&mdat->mirtab[mdat->num], &mip, sizeof (mip));
94
+        mdat->num++;
88 95
     }
89 96
 
90
-    close(fd);
97
+    close (fd);
91 98
 
92
-    if(bread) {
93
-	logg("^Removing broken %s file.\n", file);
94
-	unlink(file);
95
-	mirman_free(mdat);
96
-	return -1;
99
+    if (bread)
100
+    {
101
+        logg ("^Removing broken %s file.\n", file);
102
+        unlink (file);
103
+        mirman_free (mdat);
104
+        return -1;
97 105
     }
98 106
 
99 107
     return 0;
100 108
 }
101 109
 
102
-int mirman_check(uint32_t *ip, int af, struct mirdat *mdat, struct mirdat_ip **md)
110
+int
111
+mirman_check (uint32_t * ip, int af, struct mirdat *mdat,
112
+              struct mirdat_ip **md)
103 113
 {
104
-	unsigned int i, flevel = cl_retflevel();
105
-
106
-
107
-    if(md)
108
-	*md = NULL;
109
-
110
-    if(!mdat->active)
111
-	return 0;
112
-
113
-    for(i = 0; i < mdat->num; i++) {
114
-
115
-	if((af == AF_INET && mdat->mirtab[i].ip4 == *ip) || (af == AF_INET6 && !memcmp(mdat->mirtab[i].ip6, ip, 4 * sizeof(uint32_t)))) {
116
-
117
-	    if(!mdat->mirtab[i].atime && !mdat->mirtab[i].ignore) {
118
-		if(md)
119
-		    *md = &mdat->mirtab[i];
120
-		return 0;
121
-	    }
122
-
123
-	    if(mdat->dbflevel && (mdat->dbflevel > flevel) && (mdat->dbflevel - flevel > 3))
124
-		if(time(NULL) - mdat->mirtab[i].atime < (mdat->dbflevel - flevel) * 3600)
125
-		    return 2;
126
-
127
-	    if(mdat->mirtab[i].ignore) {
128
-		if(!mdat->mirtab[i].atime)
129
-		    return 1;
130
-
131
-		if(time(NULL) - mdat->mirtab[i].atime > IGNORE_LONG) {
132
-		    mdat->mirtab[i].ignore = 0;
133
-		    if(md)
134
-			*md = &mdat->mirtab[i];
135
-		    return 0;
136
-		} else {
137
-		    if(mdat->mirtab[i].ignore == 2 && (time(NULL) - mdat->mirtab[i].atime > IGNORE_SHORT)) {
138
-			if(md)
139
-			    *md = &mdat->mirtab[i];
140
-			return 0;
141
-		    }
142
-		    return 1;
143
-		}
144
-	    }
145
-
146
-	    if(md)
147
-		*md = &mdat->mirtab[i];
148
-	    return 0;
149
-	}
114
+    unsigned int i, flevel = cl_retflevel ();
115
+
116
+
117
+    if (md)
118
+        *md = NULL;
119
+
120
+    if (!mdat->active)
121
+        return 0;
122
+
123
+    for (i = 0; i < mdat->num; i++)
124
+    {
125
+
126
+        if ((af == AF_INET && mdat->mirtab[i].ip4 == *ip)
127
+            || (af == AF_INET6
128
+                && !memcmp (mdat->mirtab[i].ip6, ip, 4 * sizeof (uint32_t))))
129
+        {
130
+
131
+            if (!mdat->mirtab[i].atime && !mdat->mirtab[i].ignore)
132
+            {
133
+                if (md)
134
+                    *md = &mdat->mirtab[i];
135
+                return 0;
136
+            }
137
+
138
+            if (mdat->dbflevel && (mdat->dbflevel > flevel)
139
+                && (mdat->dbflevel - flevel > 3))
140
+                if (time (NULL) - mdat->mirtab[i].atime <
141
+                    (mdat->dbflevel - flevel) * 3600)
142
+                    return 2;
143
+
144
+            if (mdat->mirtab[i].ignore)
145
+            {
146
+                if (!mdat->mirtab[i].atime)
147
+                    return 1;
148
+
149
+                if (time (NULL) - mdat->mirtab[i].atime > IGNORE_LONG)
150
+                {
151
+                    mdat->mirtab[i].ignore = 0;
152
+                    if (md)
153
+                        *md = &mdat->mirtab[i];
154
+                    return 0;
155
+                }
156
+                else
157
+                {
158
+                    if (mdat->mirtab[i].ignore == 2
159
+                        && (time (NULL) - mdat->mirtab[i].atime >
160
+                            IGNORE_SHORT))
161
+                    {
162
+                        if (md)
163
+                            *md = &mdat->mirtab[i];
164
+                        return 0;
165
+                    }
166
+                    return 1;
167
+                }
168
+            }
169
+
170
+            if (md)
171
+                *md = &mdat->mirtab[i];
172
+            return 0;
173
+        }
150 174
     }
151 175
 
152 176
     return 0;
153 177
 }
154 178
 
155
-static int mirman_update_int(uint32_t *ip, int af, struct mirdat *mdat, uint8_t broken, int succ, int fail)
179
+static int
180
+mirman_update_int (uint32_t * ip, int af, struct mirdat *mdat, uint8_t broken,
181
+                   int succ, int fail)
156 182
 {
157
-	unsigned int i, found = 0;
183
+    unsigned int i, found = 0;
158 184
 
159 185
 
160
-    if(!mdat->active)
161
-	return 0;
186
+    if (!mdat->active)
187
+        return 0;
162 188
 
163
-    for(i = 0; i < mdat->num; i++) {
164
-	if((af == AF_INET && mdat->mirtab[i].ip4 == *ip) || (af == AF_INET6 && !memcmp(mdat->mirtab[i].ip6, ip, 4 * sizeof(uint32_t)))) {
165
-	    found = 1;
166
-	    break;
167
-	}
189
+    for (i = 0; i < mdat->num; i++)
190
+    {
191
+        if ((af == AF_INET && mdat->mirtab[i].ip4 == *ip)
192
+            || (af == AF_INET6
193
+                && !memcmp (mdat->mirtab[i].ip6, ip, 4 * sizeof (uint32_t))))
194
+        {
195
+            found = 1;
196
+            break;
197
+        }
168 198
     }
169 199
 
170
-    if(found) {
171
-	mdat->mirtab[i].atime = 0; /* will be updated in mirman_write() */
172
-	if(succ || fail) {
173
-	    if((int) mdat->mirtab[i].fail + fail < 0)
174
-		mdat->mirtab[i].fail = 0;
175
-	    else
176
-		mdat->mirtab[i].fail += fail;
177
-
178
-	    if((int) mdat->mirtab[i].succ + succ < 0)
179
-		mdat->mirtab[i].succ = 0;
180
-	    else
181
-		mdat->mirtab[i].succ += succ;
182
-	} else {
183
-	    if(broken)
184
-		mdat->mirtab[i].fail++;
185
-	    else
186
-		mdat->mirtab[i].succ++;
187
-
188
-	    if(broken == 2) {
189
-		mdat->mirtab[i].ignore = 2;
190
-	    } else {
191
-		/*
192
-		 * If the total number of failures is less than 3 then never
193
-		 * mark a permanent failure, in other case use the real status.
194
-		 */
195
-		if(mdat->mirtab[i].fail < 3)
196
-		    mdat->mirtab[i].ignore = 0;
197
-		else
198
-		    mdat->mirtab[i].ignore = broken;
199
-	    }
200
-	}
201
-    } else {
202
-	mdat->mirtab = (struct mirdat_ip *) realloc(mdat->mirtab, (mdat->num + 1) * sizeof(struct mirdat_ip));
203
-	if(!mdat->mirtab) {
204
-	    logg("!Can't allocate memory for new element in mdat->mirtab\n");
205
-	    return -1;
206
-	}
207
-	if(af == AF_INET) {
208
-	    mdat->mirtab[mdat->num].ip4 = *ip;
209
-	} else {
210
-	    mdat->mirtab[mdat->num].ip4 = 0;
211
-	    memcpy(mdat->mirtab[mdat->num].ip6, ip, 4 * sizeof(uint32_t));
212
-	}
213
-	mdat->mirtab[mdat->num].atime = 0;
214
-	mdat->mirtab[mdat->num].succ = (succ > 0) ? succ : 0;
215
-	mdat->mirtab[mdat->num].fail = (fail > 0) ? fail : 0;
216
-	mdat->mirtab[mdat->num].ignore = (broken == 2) ? 2 : 0;
217
-	memset(&mdat->mirtab[mdat->num].res, 0xff, sizeof(mdat->mirtab[mdat->num].res));
218
-	if(!succ && !fail) {
219
-	    if(broken)
220
-		mdat->mirtab[mdat->num].fail++;
221
-	    else
222
-		mdat->mirtab[mdat->num].succ++;
223
-	}
224
-	mdat->num++;
200
+    if (found)
201
+    {
202
+        mdat->mirtab[i].atime = 0;  /* will be updated in mirman_write() */
203
+        if (succ || fail)
204
+        {
205
+            if ((int) mdat->mirtab[i].fail + fail < 0)
206
+                mdat->mirtab[i].fail = 0;
207
+            else
208
+                mdat->mirtab[i].fail += fail;
209
+
210
+            if ((int) mdat->mirtab[i].succ + succ < 0)
211
+                mdat->mirtab[i].succ = 0;
212
+            else
213
+                mdat->mirtab[i].succ += succ;
214
+        }
215
+        else
216
+        {
217
+            if (broken)
218
+                mdat->mirtab[i].fail++;
219
+            else
220
+                mdat->mirtab[i].succ++;
221
+
222
+            if (broken == 2)
223
+            {
224
+                mdat->mirtab[i].ignore = 2;
225
+            }
226
+            else
227
+            {
228
+                /*
229
+                 * If the total number of failures is less than 3 then never
230
+                 * mark a permanent failure, in other case use the real status.
231
+                 */
232
+                if (mdat->mirtab[i].fail < 3)
233
+                    mdat->mirtab[i].ignore = 0;
234
+                else
235
+                    mdat->mirtab[i].ignore = broken;
236
+            }
237
+        }
238
+    }
239
+    else
240
+    {
241
+        mdat->mirtab =
242
+            (struct mirdat_ip *) realloc (mdat->mirtab,
243
+                                          (mdat->num +
244
+                                           1) * sizeof (struct mirdat_ip));
245
+        if (!mdat->mirtab)
246
+        {
247
+            logg ("!Can't allocate memory for new element in mdat->mirtab\n");
248
+            return -1;
249
+        }
250
+        if (af == AF_INET)
251
+        {
252
+            mdat->mirtab[mdat->num].ip4 = *ip;
253
+        }
254
+        else
255
+        {
256
+            mdat->mirtab[mdat->num].ip4 = 0;
257
+            memcpy (mdat->mirtab[mdat->num].ip6, ip, 4 * sizeof (uint32_t));
258
+        }
259
+        mdat->mirtab[mdat->num].atime = 0;
260
+        mdat->mirtab[mdat->num].succ = (succ > 0) ? succ : 0;
261
+        mdat->mirtab[mdat->num].fail = (fail > 0) ? fail : 0;
262
+        mdat->mirtab[mdat->num].ignore = (broken == 2) ? 2 : 0;
263
+        memset (&mdat->mirtab[mdat->num].res, 0xff,
264
+                sizeof (mdat->mirtab[mdat->num].res));
265
+        if (!succ && !fail)
266
+        {
267
+            if (broken)
268
+                mdat->mirtab[mdat->num].fail++;
269
+            else
270
+                mdat->mirtab[mdat->num].succ++;
271
+        }
272
+        mdat->num++;
225 273
     }
226 274
 
227 275
     return 0;
228 276
 }
229 277
 
230
-int mirman_update(uint32_t *ip, int af, struct mirdat *mdat, uint8_t broken)
278
+int
279
+mirman_update (uint32_t * ip, int af, struct mirdat *mdat, uint8_t broken)
231 280
 {
232
-    return mirman_update_int(ip, af, mdat, broken, 0, 0);
281
+    return mirman_update_int (ip, af, mdat, broken, 0, 0);
233 282
 }
234 283
 
235
-int mirman_update_sf(uint32_t *ip, int af, struct mirdat *mdat, int succ, int fail)
284
+int
285
+mirman_update_sf (uint32_t * ip, int af, struct mirdat *mdat, int succ,
286
+                  int fail)
236 287
 {
237
-    return mirman_update_int(ip, af, mdat, 0, succ, fail);
288
+    return mirman_update_int (ip, af, mdat, 0, succ, fail);
238 289
 }
239 290
 
240
-void mirman_list(const struct mirdat *mdat)
291
+void
292
+mirman_list (const struct mirdat *mdat)
241 293
 {
242
-	unsigned int i;
243
-	time_t tm;
244
-	char ip[46];
294
+    unsigned int i;
295
+    time_t tm;
296
+    char ip[46];
245 297
 
246 298
 
247
-    for(i = 0; i < mdat->num; i++) {
248
-	printf("Mirror #%u\n", i + 1);
299
+    for (i = 0; i < mdat->num; i++)
300
+    {
301
+        printf ("Mirror #%u\n", i + 1);
249 302
 #ifdef HAVE_GETADDRINFO
250
-	if(mdat->mirtab[i].ip4)
251
-	    printf("IP: %s\n", inet_ntop(AF_INET, &mdat->mirtab[i].ip4, ip, sizeof(ip)));
252
-	else
253
-	    printf("IP: %s\n", inet_ntop(AF_INET6, mdat->mirtab[i].ip6, ip, sizeof(ip)));
303
+        if (mdat->mirtab[i].ip4)
304
+            printf ("IP: %s\n",
305
+                    inet_ntop (AF_INET, &mdat->mirtab[i].ip4, ip,
306
+                               sizeof (ip)));
307
+        else
308
+            printf ("IP: %s\n",
309
+                    inet_ntop (AF_INET6, mdat->mirtab[i].ip6, ip,
310
+                               sizeof (ip)));
254 311
 #else
255
-	if(mdat->mirtab[i].ip4)
256
-	    printf("IP: %s\n", inet_ntoa(*(struct in_addr*) &mdat->mirtab[i].ip4));
312
+        if (mdat->mirtab[i].ip4)
313
+            printf ("IP: %s\n",
314
+                    inet_ntoa (*(struct in_addr *) &mdat->mirtab[i].ip4));
257 315
 #endif
258
-	printf("Successes: %u\n", mdat->mirtab[i].succ);
259
-	printf("Failures: %u\n", mdat->mirtab[i].fail);
260
-	tm = mdat->mirtab[i].atime;
261
-	printf("Last access: %s", ctime((const time_t *)&tm));
262
-	printf("Ignore: %s\n", mdat->mirtab[i].ignore ? "Yes" : "No");
263
-	if(i != mdat->num - 1)
264
-	    printf("-------------------------------------\n");
316
+        printf ("Successes: %u\n", mdat->mirtab[i].succ);
317
+        printf ("Failures: %u\n", mdat->mirtab[i].fail);
318
+        tm = mdat->mirtab[i].atime;
319
+        printf ("Last access: %s", ctime ((const time_t *) &tm));
320
+        printf ("Ignore: %s\n", mdat->mirtab[i].ignore ? "Yes" : "No");
321
+        if (i != mdat->num - 1)
322
+            printf ("-------------------------------------\n");
265 323
     }
266 324
 }
267 325
 
268
-void mirman_whitelist(struct mirdat *mdat, unsigned int mode)
326
+void
327
+mirman_whitelist (struct mirdat *mdat, unsigned int mode)
269 328
 {
270
-	unsigned int i;
329
+    unsigned int i;
271 330
 
272
-    logg("*Whitelisting %s blacklisted mirrors\n", mode == 1 ? "all" : "short-term");
273
-    for(i = 0; i < mdat->num; i++)
274
-	if(mode == 1 || (mode == 2 && mdat->mirtab[i].ignore == 2))
275
-	    mdat->mirtab[i].ignore = 0;
331
+    logg ("*Whitelisting %s blacklisted mirrors\n",
332
+          mode == 1 ? "all" : "short-term");
333
+    for (i = 0; i < mdat->num; i++)
334
+        if (mode == 1 || (mode == 2 && mdat->mirtab[i].ignore == 2))
335
+            mdat->mirtab[i].ignore = 0;
276 336
 }
277 337
 
278
-int mirman_write(const char *file, const char *dir, struct mirdat *mdat)
338
+int
339
+mirman_write (const char *file, const char *dir, struct mirdat *mdat)
279 340
 {
280
-	int fd;
281
-	unsigned int i;
282
-	char path[512];
341
+    int fd;
342
+    unsigned int i;
343
+    char path[512];
283 344
 
284
-    snprintf(path, sizeof(path), "%s/%s", dir, file);
285
-    path[sizeof(path) - 1] = 0;
345
+    snprintf (path, sizeof (path), "%s/%s", dir, file);
346
+    path[sizeof (path) - 1] = 0;
286 347
 
287
-    if(!mdat->num)
288
-	return 0;
348
+    if (!mdat->num)
349
+        return 0;
289 350
 
290
-    if((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600)) == -1) {
291
-	logg("!Can't open %s for writing\n", path);
292
-	return -1;
351
+    if ((fd =
352
+         open (path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600)) == -1)
353
+    {
354
+        logg ("!Can't open %s for writing\n", path);
355
+        return -1;
293 356
     }
294 357
 
295
-    for(i = 0; i < mdat->num; i++)
296
-	if(!mdat->mirtab[i].atime)
297
-	    mdat->mirtab[i].atime = (uint32_t) time(NULL);
358
+    for (i = 0; i < mdat->num; i++)
359
+        if (!mdat->mirtab[i].atime)
360
+            mdat->mirtab[i].atime = (uint32_t) time (NULL);
298 361
 
299
-    if(write(fd, mdat->mirtab, mdat->num * sizeof(struct mirdat_ip)) == -1) {
300
-	logg("!Can't write to %s\n", path);
301
-	close(fd);
302
-	return -1;
362
+    if (write (fd, mdat->mirtab, mdat->num * sizeof (struct mirdat_ip)) == -1)
363
+    {
364
+        logg ("!Can't write to %s\n", path);
365
+        close (fd);
366
+        return -1;
303 367
     }
304 368
 
305
-    close(fd);
369
+    close (fd);
306 370
     return 0;
307 371
 }
... ...
@@ -21,17 +21,19 @@
21 21
 
22 22
 #include "libclamav/cltypes.h"
23 23
 
24
-struct mirdat_ip {
25
-    uint32_t ip4;	    /* IPv4 address */
26
-    uint32_t atime;	    /* last access time */
27
-    uint32_t succ;	    /* number of successful downloads from this ip */
28
-    uint32_t fail;	    /* number of failures */
29
-    uint8_t ignore;	    /* ignore flag */
30
-    uint32_t ip6[4];	    /* IPv6 address */
31
-    char res[16];	    /* reserved */
24
+struct mirdat_ip
25
+{
26
+    uint32_t ip4;               /* IPv4 address */
27
+    uint32_t atime;             /* last access time */
28
+    uint32_t succ;              /* number of successful downloads from this ip */
29
+    uint32_t fail;              /* number of failures */
30
+    uint8_t ignore;             /* ignore flag */
31
+    uint32_t ip6[4];            /* IPv6 address */
32
+    char res[16];               /* reserved */
32 33
 };
33 34
 
34
-struct mirdat {
35
+struct mirdat
36
+{
35 37
     uint8_t active;
36 38
     unsigned int num;
37 39
     uint32_t currip[4];
... ...
@@ -40,13 +42,16 @@ struct mirdat {
40 40
     struct mirdat_ip *mirtab;
41 41
 };
42 42
 
43
-int mirman_read(const char *file, struct mirdat *mdat, uint8_t active);
44
-int mirman_check(uint32_t *ip, int af, struct mirdat *mdat, struct mirdat_ip **md);
45
-int mirman_update(uint32_t *ip, int af, struct mirdat *mdat, uint8_t broken);
46
-int mirman_update_sf(uint32_t *ip, int af, struct mirdat *mdat, int succ, int fail);
47
-void mirman_list(const struct mirdat *mdat);
48
-void mirman_whitelist(struct mirdat *mdat, unsigned int mode);
49
-int mirman_write(const char *file, const char *dir, struct mirdat *mdat);
50
-void mirman_free(struct mirdat *mdat);
43
+int mirman_read (const char *file, struct mirdat *mdat, uint8_t active);
44
+int mirman_check (uint32_t * ip, int af, struct mirdat *mdat,
45
+                  struct mirdat_ip **md);
46
+int mirman_update (uint32_t * ip, int af, struct mirdat *mdat,
47
+                   uint8_t broken);
48
+int mirman_update_sf (uint32_t * ip, int af, struct mirdat *mdat, int succ,
49
+                      int fail);
50
+void mirman_list (const struct mirdat *mdat);
51
+void mirman_whitelist (struct mirdat *mdat, unsigned int mode);
52
+int mirman_write (const char *file, const char *dir, struct mirdat *mdat);
53
+void mirman_free (struct mirdat *mdat);
51 54
 
52 55
 #endif
... ...
@@ -47,14 +47,14 @@
47 47
 #ifdef SO_ERROR
48 48
 
49 49
 #ifndef timercmp
50
-# define timercmp(a, b, cmp)          \
50
+#define timercmp(a, b, cmp)          \
51 51
   (((a)->tv_sec == (b)->tv_sec) ?     \
52 52
    ((a)->tv_usec cmp (b)->tv_usec) :  \
53 53
    ((a)->tv_sec cmp (b)->tv_sec))
54 54
 #endif /* timercmp */
55 55
 
56 56
 #ifndef timersub
57
-# define timersub(a, b, result)                       \
57
+#define timersub(a, b, result)                       \
58 58
   do {                                                \
59 59
     (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;     \
60 60
     (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;  \
... ...
@@ -69,238 +69,262 @@
69 69
 #define NONBLOCK_MAX_BOGUS_LOOPS     10
70 70
 #undef  NONBLOCK_DEBUG
71 71
 
72
-static int connect_error(int sock)
72
+static int
73
+connect_error (int sock)
73 74
 {
74
-	int optval;
75
-	socklen_t optlen;
75
+    int optval;
76
+    socklen_t optlen;
76 77
 
77
-	optlen = sizeof(optval);
78
-	getsockopt(sock, SOL_SOCKET, SO_ERROR, &optval, &optlen);
78
+    optlen = sizeof (optval);
79
+    getsockopt (sock, SOL_SOCKET, SO_ERROR, &optval, &optlen);
79 80
 
80
-	if (optval) {
81
-		logg("connect_error: getsockopt(SO_ERROR): fd=%d error=%d: %s\n",
82
-		     sock, optval, strerror(optval));
83
-	}
81
+    if (optval)
82
+    {
83
+        logg ("connect_error: getsockopt(SO_ERROR): fd=%d error=%d: %s\n",
84
+              sock, optval, strerror (optval));
85
+    }
84 86
 
85
-	return optval ? -1 : 0;
87
+    return optval ? -1 : 0;
86 88
 }
87 89
 
88
-static int nonblock_connect(int sock, const struct sockaddr *addr, socklen_t addrlen, int secs)
90
+static int
91
+nonblock_connect (int sock, const struct sockaddr *addr, socklen_t addrlen,
92
+                  int secs)
89 93
 {
90
-	/* Max. of unexpected select() failures */
91
-	int select_failures = NONBLOCK_SELECT_MAX_FAILURES;
92
-	/* Max. of useless loops */
93
-	int bogus_loops = NONBLOCK_MAX_BOGUS_LOOPS;
94
-	struct timeval timeout;  /* When we should time out */
95
-	int numfd;               /* Highest fdset fd plus 1 */
96
-
97
-	/* Calculate into 'timeout' when we should time out */
98
-	gettimeofday(&timeout, 0);
99
-	timeout.tv_sec += secs;
100
-
101
-	/* Launch (possibly) non-blocking connect() request */
102
-	if (connect(sock, addr, addrlen)) {
103
-		int e = errno;
94
+    /* Max. of unexpected select() failures */
95
+    int select_failures = NONBLOCK_SELECT_MAX_FAILURES;
96
+    /* Max. of useless loops */
97
+    int bogus_loops = NONBLOCK_MAX_BOGUS_LOOPS;
98
+    struct timeval timeout;     /* When we should time out */
99
+    int numfd;                  /* Highest fdset fd plus 1 */
100
+
101
+    /* Calculate into 'timeout' when we should time out */
102
+    gettimeofday (&timeout, 0);
103
+    timeout.tv_sec += secs;
104
+
105
+    /* Launch (possibly) non-blocking connect() request */
106
+    if (connect (sock, addr, addrlen))
107
+    {
108
+        int e = errno;
104 109
 #ifdef NONBLOCK_DEBUG
105
-		logg("DEBUG nonblock_connect: connect(): fd=%d errno=%d: %s\n",
106
-		     sock, e, strerror(e));
110
+        logg ("DEBUG nonblock_connect: connect(): fd=%d errno=%d: %s\n",
111
+              sock, e, strerror (e));
107 112
 #endif
108
-		switch (e) {
109
-		case EALREADY:
110
-		case EINPROGRESS:
111
-		case EAGAIN:
112
-			break; /* wait for connection */
113
-		case EISCONN:
114
-			return 0; /* connected */
115
-		default:
116
-			logg("nonblock_connect: connect(): fd=%d errno=%d: %s\n",
117
-			     sock, e, strerror(e));
118
-			return -1; /* failed */
119
-		}
120
-	}
121
-	else {
122
-		return connect_error(sock);
123
-	}
124
-
125
-	numfd = sock + 1; /* Highest fdset fd plus 1 */
126
-
127
-	for (;;) {
128
-		fd_set fds;
129
-		struct timeval now;
130
-		struct timeval wait;
131
-		int n;
132
-
133
-		/* Force timeout if we ran out of time */
134
-		gettimeofday(&now, 0);
135
-		if (timercmp(&now, &timeout, >)) {
136
-			logg("nonblock_connect: connect timing out (%d secs)\n",
137
-			     secs);
138
-			break; /* failed */
139
-		}
140
-
141
-		/* Calculate into 'wait' how long to wait */
142
-		timersub(&timeout, &now, &wait); /* wait = timeout - now */
143
-
144
-		/* Init fds with 'sock' as the only fd */
145
-		FD_ZERO(&fds);
146
-		FD_SET(sock, &fds);
147
-
148
-		n = select(numfd, 0, &fds, 0, &wait);
149
-		if (n < 0) {
150
-			logg("nonblock_connect: select() failure %d: errno=%d: %s\n",
151
-			     select_failures, errno, strerror(errno));
152
-			if (--select_failures >= 0)
153
-				continue; /* keep waiting */
154
-			break; /* failed */
155
-		}
113
+        switch (e)
114
+        {
115
+        case EALREADY:
116
+        case EINPROGRESS:
117
+        case EAGAIN:
118
+            break;              /* wait for connection */
119
+        case EISCONN:
120
+            return 0;           /* connected */
121
+        default:
122
+            logg ("nonblock_connect: connect(): fd=%d errno=%d: %s\n",
123
+                  sock, e, strerror (e));
124
+            return -1;          /* failed */
125
+        }
126
+    }
127
+    else
128
+    {
129
+        return connect_error (sock);
130
+    }
131
+
132
+    numfd = sock + 1;           /* Highest fdset fd plus 1 */
133
+
134
+    for (;;)
135
+    {
136
+        fd_set fds;
137
+        struct timeval now;
138
+        struct timeval wait;
139
+        int n;
140
+
141
+        /* Force timeout if we ran out of time */
142
+        gettimeofday (&now, 0);
143
+        if (timercmp (&now, &timeout, >))
144
+        {
145
+            logg ("nonblock_connect: connect timing out (%d secs)\n", secs);
146
+            break;              /* failed */
147
+        }
148
+
149
+        /* Calculate into 'wait' how long to wait */
150
+        timersub (&timeout, &now, &wait);   /* wait = timeout - now */
151
+
152
+        /* Init fds with 'sock' as the only fd */
153
+        FD_ZERO (&fds);
154
+        FD_SET (sock, &fds);
155
+
156
+        n = select (numfd, 0, &fds, 0, &wait);
157
+        if (n < 0)
158
+        {
159
+            logg ("nonblock_connect: select() failure %d: errno=%d: %s\n",
160
+                  select_failures, errno, strerror (errno));
161
+            if (--select_failures >= 0)
162
+                continue;       /* keep waiting */
163
+            break;              /* failed */
164
+        }
156 165
 
157 166
 #ifdef NONBLOCK_DEBUG
158
-		logg("DEBUG nonblock_connect: select = %d\n", n);
167
+        logg ("DEBUG nonblock_connect: select = %d\n", n);
159 168
 #endif
160 169
 
161
-		if (n) {
162
-			return connect_error(sock);
163
-		}
170
+        if (n)
171
+        {
172
+            return connect_error (sock);
173
+        }
164 174
 
165
-		/* Select returned, but there is no work to do... */
166
-		if (--bogus_loops < 0) {
167
-			logg("nonblock_connect: giving up due to excessive bogus loops\n");
168
-			break; /* failed */
169
-		}
175
+        /* Select returned, but there is no work to do... */
176
+        if (--bogus_loops < 0)
177
+        {
178
+            logg ("nonblock_connect: giving up due to excessive bogus loops\n");
179
+            break;              /* failed */
180
+        }
170 181
 
171
-	} /* for loop: keep waiting */
182
+    }                           /* for loop: keep waiting */
172 183
 
173
-	return -1; /* failed */
184
+    return -1;                  /* failed */
174 185
 }
175 186
 
176
-static ssize_t nonblock_recv(int sock, void *buf, size_t len, int flags, int secs)
187
+static ssize_t
188
+nonblock_recv (int sock, void *buf, size_t len, int flags, int secs)
177 189
 {
178
-	/* Max. of unexpected select() failures */
179
-	int select_failures = NONBLOCK_SELECT_MAX_FAILURES;
180
-	/* Max. of useless loops */
181
-	int bogus_loops = NONBLOCK_MAX_BOGUS_LOOPS;
182
-	struct timeval timeout;  /* When we should time out */
183
-	int numfd;               /* Highest fdset fd plus 1 */
184
-
185
-	/* Calculate into 'timeout' when we should time out */
186
-	gettimeofday(&timeout, 0);
187
-	timeout.tv_sec += secs;
188
-
189
-	numfd = sock + 1; /* Highest fdset fd plus 1 */
190
-
191
-	for (;;) {
192
-		fd_set fds;
193
-		struct timeval now;
194
-		struct timeval wait;
195
-		int n;
196
-
197
-		/* Force timeout if we ran out of time */
198
-		gettimeofday(&now, 0);
199
-		if (timercmp(&now, &timeout, >)) {
200
-			logg("nonblock_recv: recv timing out (%d secs)\n", secs);
201
-			break; /* failed */
202
-		}
203
-
204
-		/* Calculate into 'wait' how long to wait */
205
-		timersub(&timeout, &now, &wait); /* wait = timeout - now */
206
-
207
-		/* Init fds with 'sock' as the only fd */
208
-		FD_ZERO(&fds);
209
-		FD_SET(sock, &fds);
210
-
211
-		n = select(numfd, &fds, 0, 0, &wait);
212
-		if (n < 0) {
213
-			logg("nonblock_recv: select() failure %d: errno=%d: %s\n",
214
-			     select_failures, errno, strerror(errno));
215
-			if (--select_failures >= 0)
216
-				continue; /* keep waiting */
217
-			break; /* failed */
218
-		}
219
-
220
-		if (n) {
221
-			return recv(sock, buf, len, flags);
222
-		}
223
-
224
-		/* Select returned, but there is no work to do... */
225
-		if (--bogus_loops < 0) {
226
-			logg("nonblock_recv: giving up due to excessive bogus loops\n");
227
-			break; /* failed */
228
-		}
229
-
230
-	} /* for loop: keep waiting */
231
-
232
-	return -1; /* failed */
190
+    /* Max. of unexpected select() failures */
191
+    int select_failures = NONBLOCK_SELECT_MAX_FAILURES;
192
+    /* Max. of useless loops */
193
+    int bogus_loops = NONBLOCK_MAX_BOGUS_LOOPS;
194
+    struct timeval timeout;     /* When we should time out */
195
+    int numfd;                  /* Highest fdset fd plus 1 */
196
+
197
+    /* Calculate into 'timeout' when we should time out */
198
+    gettimeofday (&timeout, 0);
199
+    timeout.tv_sec += secs;
200
+
201
+    numfd = sock + 1;           /* Highest fdset fd plus 1 */
202
+
203
+    for (;;)
204
+    {
205
+        fd_set fds;
206
+        struct timeval now;
207
+        struct timeval wait;
208
+        int n;
209
+
210
+        /* Force timeout if we ran out of time */
211
+        gettimeofday (&now, 0);
212
+        if (timercmp (&now, &timeout, >))
213
+        {
214
+            logg ("nonblock_recv: recv timing out (%d secs)\n", secs);
215
+            break;              /* failed */
216
+        }
217
+
218
+        /* Calculate into 'wait' how long to wait */
219
+        timersub (&timeout, &now, &wait);   /* wait = timeout - now */
220
+
221
+        /* Init fds with 'sock' as the only fd */
222
+        FD_ZERO (&fds);
223
+        FD_SET (sock, &fds);
224
+
225
+        n = select (numfd, &fds, 0, 0, &wait);
226
+        if (n < 0)
227
+        {
228
+            logg ("nonblock_recv: select() failure %d: errno=%d: %s\n",
229
+                  select_failures, errno, strerror (errno));
230
+            if (--select_failures >= 0)
231
+                continue;       /* keep waiting */
232
+            break;              /* failed */
233
+        }
234
+
235
+        if (n)
236
+        {
237
+            return recv (sock, buf, len, flags);
238
+        }
239
+
240
+        /* Select returned, but there is no work to do... */
241
+        if (--bogus_loops < 0)
242
+        {
243
+            logg ("nonblock_recv: giving up due to excessive bogus loops\n");
244
+            break;              /* failed */
245
+        }
246
+
247
+    }                           /* for loop: keep waiting */
248
+
249
+    return -1;                  /* failed */
233 250
 }
234 251
 
235
-static long nonblock_fcntl(int sock)
252
+static long
253
+nonblock_fcntl (int sock)
236 254
 {
237 255
 #ifdef	F_GETFL
238
-	long fcntl_flags; /* Save fcntl() flags */
239
-
240
-	fcntl_flags = fcntl(sock, F_GETFL, 0);
241
-	if (fcntl_flags == -1) {
242
-		logg("nonblock_fcntl: saving: fcntl(%d, F_GETFL): errno=%d: %s\n",
243
-		     sock, errno, strerror(errno));
244
-	}
245
-	else if (fcntl(sock, F_SETFL, fcntl_flags | O_NONBLOCK)) {
246
-		logg("nonblock_fcntl: fcntl(%d, F_SETFL, O_NONBLOCK): errno=%d: %s\n",
247
-		     sock, errno, strerror(errno));
248
-	}
249
-
250
-	return fcntl_flags;
256
+    long fcntl_flags;           /* Save fcntl() flags */
257
+
258
+    fcntl_flags = fcntl (sock, F_GETFL, 0);
259
+    if (fcntl_flags == -1)
260
+    {
261
+        logg ("nonblock_fcntl: saving: fcntl(%d, F_GETFL): errno=%d: %s\n",
262
+              sock, errno, strerror (errno));
263
+    }
264
+    else if (fcntl (sock, F_SETFL, fcntl_flags | O_NONBLOCK))
265
+    {
266
+        logg ("nonblock_fcntl: fcntl(%d, F_SETFL, O_NONBLOCK): errno=%d: %s\n", sock, errno, strerror (errno));
267
+    }
268
+
269
+    return fcntl_flags;
251 270
 #else
252
-	return 0;
271
+    return 0;
253 272
 #endif
254 273
 }
255 274
 
256
-static void restore_fcntl(int sock, long fcntl_flags)
275
+static void
276
+restore_fcntl (int sock, long fcntl_flags)
257 277
 {
258 278
 #ifdef	F_SETFL
259
-	if (fcntl_flags != -1) {
260
-		if (fcntl(sock, F_SETFL, fcntl_flags)) {
261
-			logg("restore_fcntl: restoring: fcntl(%d, F_SETFL): errno=%d: %s\n",
262
-			     sock, errno, strerror(errno));
263
-		}
264
-	}
279
+    if (fcntl_flags != -1)
280
+    {
281
+        if (fcntl (sock, F_SETFL, fcntl_flags))
282
+        {
283
+            logg ("restore_fcntl: restoring: fcntl(%d, F_SETFL): errno=%d: %s\n", sock, errno, strerror (errno));
284
+        }
285
+    }
265 286
 #endif
266 287
 }
267 288
 
268 289
 /*
269 290
 	wait_connect(): wrapper for connect(), with explicit 'secs' timeout
270 291
 */
271
-int wait_connect(int sock, const struct sockaddr *addr, socklen_t addrlen, int secs)
292
+int
293
+wait_connect (int sock, const struct sockaddr *addr, socklen_t addrlen,
294
+              int secs)
272 295
 {
273
-	long fcntl_flags; /* Save fcntl() flags */
274
-	int ret;
296
+    long fcntl_flags;           /* Save fcntl() flags */
297
+    int ret;
275 298
 
276
-	/* Temporarily set socket to non-blocking mode */
277
-	fcntl_flags = nonblock_fcntl(sock);
299
+    /* Temporarily set socket to non-blocking mode */
300
+    fcntl_flags = nonblock_fcntl (sock);
278 301
 
279
-	ret = nonblock_connect(sock, addr, addrlen, secs);
302
+    ret = nonblock_connect (sock, addr, addrlen, secs);
280 303
 
281
-	/* Restore socket's default blocking mode */
282
-	restore_fcntl(sock, fcntl_flags);
304
+    /* Restore socket's default blocking mode */
305
+    restore_fcntl (sock, fcntl_flags);
283 306
 
284
-	return ret;
307
+    return ret;
285 308
 }
286 309
 
287 310
 /*
288 311
 	wait_recv(): wrapper for recv(), with explicit 'secs' timeout
289 312
 */
290
-ssize_t wait_recv(int sock, void *buf, size_t len, int flags, int secs)
313
+ssize_t
314
+wait_recv (int sock, void *buf, size_t len, int flags, int secs)
291 315
 {
292
-	long fcntl_flags; /* Save fcntl() flags */
293
-	int ret;
316
+    long fcntl_flags;           /* Save fcntl() flags */
317
+    int ret;
294 318
 
295
-	/* Temporarily set socket to non-blocking mode */
296
-	fcntl_flags = nonblock_fcntl(sock);
319
+    /* Temporarily set socket to non-blocking mode */
320
+    fcntl_flags = nonblock_fcntl (sock);
297 321
 
298
-	ret = nonblock_recv(sock, buf, len, flags, secs);
322
+    ret = nonblock_recv (sock, buf, len, flags, secs);
299 323
 
300
-	/* Restore socket's default blocking mode */
301
-	restore_fcntl(sock, fcntl_flags);
324
+    /* Restore socket's default blocking mode */
325
+    restore_fcntl (sock, fcntl_flags);
302 326
 
303
-	return ret;
327
+    return ret;
304 328
 }
305 329
 
306 330
 #endif /* SO_ERROR */
... ...
@@ -31,11 +31,12 @@
31 31
 /*
32 32
 	wait_connect(): wrapper for connect(), with explicit 'secs' timeout
33 33
 */
34
-int wait_connect(int sock, const struct sockaddr *addr, socklen_t addrlen, int secs);
34
+int wait_connect (int sock, const struct sockaddr *addr, socklen_t addrlen,
35
+                  int secs);
35 36
 
36 37
 /*
37 38
         wait_recv(): wrapper for recv(), with explicit 'secs' timeout
38 39
 */
39
-ssize_t wait_recv(int sock, void *buf, size_t len, int flags, int secs);
40
+ssize_t wait_recv (int sock, void *buf, size_t len, int flags, int secs);
40 41
 
41 42
 #endif /* NONBLOCK_H */
... ...
@@ -43,173 +43,203 @@
43 43
 
44 44
 #include "notify.h"
45 45
 
46
-int clamd_connect(const char *cfgfile, const char *option)
46
+int
47
+clamd_connect (const char *cfgfile, const char *option)
47 48
 {
48 49
 #ifndef	_WIN32
49
-	struct sockaddr_un server;
50
+    struct sockaddr_un server;
50 51
 #endif
51 52
 #ifdef HAVE_GETADDRINFO
52
-	struct addrinfo hints, *res;
53
-	char port[6];
54
-	const char *addr;
55
-	int ret;
53
+    struct addrinfo hints, *res;
54
+    char port[6];
55
+    const char *addr;
56
+    int ret;
56 57
 #else
57
-        struct sockaddr_in server2;
58
-	struct hostent *he;
58
+    struct sockaddr_in server2;
59
+    struct hostent *he;
59 60
 #endif
60
-	struct optstruct *opts;
61
-	const struct optstruct *opt;
62
-	int sockd;
63
-	const char *socktype;
61
+    struct optstruct *opts;
62
+    const struct optstruct *opt;
63
+    int sockd;
64
+    const char *socktype;
64 65
 
65 66
 
66
-    if((opts = optparse(cfgfile, 0, NULL, 1, OPT_CLAMD, 0, NULL)) == NULL) {
67
-	logg("!%s: Can't find or parse configuration file %s\n", option, cfgfile);
68
-	return -11;
67
+    if ((opts = optparse (cfgfile, 0, NULL, 1, OPT_CLAMD, 0, NULL)) == NULL)
68
+    {
69
+        logg ("!%s: Can't find or parse configuration file %s\n", option,
70
+              cfgfile);
71
+        return -11;
69 72
     }
70 73
 
71 74
 #ifndef	_WIN32
72
-    if((opt = optget(opts, "LocalSocket"))->enabled) {
73
-	socktype = "UNIX";
74
-	server.sun_family = AF_UNIX;
75
-	strncpy(server.sun_path, opt->strarg, sizeof(server.sun_path));
76
-	server.sun_path[sizeof(server.sun_path)-1]='\0';
77
-
78
-	if((sockd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
79
-	    logg("^Clamd was NOT notified: Can't create socket endpoint for %s\n", opt->strarg);
80
-	    perror("socket()");
81
-	    optfree(opts);
82
-	    return -1;
83
-	}
84
-
85
-	if(connect(sockd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
86
-	    closesocket(sockd);
87
-	    logg("^Clamd was NOT notified: Can't connect to clamd through %s\n", opt->strarg);
88
-	    perror("connect()");
89
-	    optfree(opts);
90
-	    return -11;
91
-	}
92
-
93
-    } else
75
+    if ((opt = optget (opts, "LocalSocket"))->enabled)
76
+    {
77
+        socktype = "UNIX";
78
+        server.sun_family = AF_UNIX;
79
+        strncpy (server.sun_path, opt->strarg, sizeof (server.sun_path));
80
+        server.sun_path[sizeof (server.sun_path) - 1] = '\0';
81
+
82
+        if ((sockd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
83
+        {
84
+            logg ("^Clamd was NOT notified: Can't create socket endpoint for %s\n", opt->strarg);
85
+            perror ("socket()");
86
+            optfree (opts);
87
+            return -1;
88
+        }
89
+
90
+        if (connect
91
+            (sockd, (struct sockaddr *) &server,
92
+             sizeof (struct sockaddr_un)) < 0)
93
+        {
94
+            closesocket (sockd);
95
+            logg ("^Clamd was NOT notified: Can't connect to clamd through %s\n", opt->strarg);
96
+            perror ("connect()");
97
+            optfree (opts);
98
+            return -11;
99
+        }
100
+
101
+    }
102
+    else
94 103
 #endif
95
-    if((opt = optget(opts, "TCPSocket"))->enabled) {
96
-	socktype = "TCP";
104
+    if ((opt = optget (opts, "TCPSocket"))->enabled)
105
+    {
106
+        socktype = "TCP";
97 107
 
98 108
 #ifdef HAVE_GETADDRINFO
99
-	memset(&hints, 0, sizeof(hints));
109
+        memset (&hints, 0, sizeof (hints));
100 110
 /*
101 111
 #ifdef SUPPORT_IPv6
102 112
 	hints.ai_family = AF_UNSPEC;
103 113
 #else
104 114
 */
105
-	hints.ai_family = AF_INET;
106
-	hints.ai_socktype = SOCK_STREAM;
107
-	snprintf(port, sizeof(port), "%u", (unsigned int) opt->numarg);
108
-	port[5] = 0;
109
-
110
-	if((opt = optget(opts, "TCPAddr"))->enabled)
111
-	    addr = opt->strarg;
112
-	else
113
-	    addr = NULL;
114
-
115
-	ret = getaddrinfo(addr, port, &hints, &res);
116
-
117
-	if(ret) {
118
-	    logg("!%s: Can't resolve hostname %s (%s)\n", option, addr ? addr : "", (ret == EAI_SYSTEM) ? strerror(errno) : gai_strerror(ret));
119
-	    optfree(opts);
120
-	    return -1;
121
-	}
122
-
123
-	if((sockd = socket(res->ai_family, SOCK_STREAM, 0)) < 0) {
124
-	    perror("socket()");
125
-	    logg("!%s: Can't create TCP socket\n", option);
126
-	    optfree(opts);
127
-	    freeaddrinfo(res);
128
-	    return -1;
129
-	}
130
-
131
-	if(connect(sockd, res->ai_addr, res->ai_addrlen) == -1) {
132
-	    perror("connect()");
133
-	    closesocket(sockd);
134
-	    logg("!%s: Can't connect to clamd on %s:%s\n", option, addr ? addr : "localhost", port);
135
-	    optfree(opts);
136
-	    freeaddrinfo(res);
137
-	    return -1;
138
-	}
139
-	freeaddrinfo(res);
115
+        hints.ai_family = AF_INET;
116
+        hints.ai_socktype = SOCK_STREAM;
117
+        snprintf (port, sizeof (port), "%u", (unsigned int) opt->numarg);
118
+        port[5] = 0;
119
+
120
+        if ((opt = optget (opts, "TCPAddr"))->enabled)
121
+            addr = opt->strarg;
122
+        else
123
+            addr = NULL;
124
+
125
+        ret = getaddrinfo (addr, port, &hints, &res);
126
+
127
+        if (ret)
128
+        {
129
+            logg ("!%s: Can't resolve hostname %s (%s)\n", option,
130
+                  addr ? addr : "",
131
+                  (ret ==
132
+                   EAI_SYSTEM) ? strerror (errno) : gai_strerror (ret));
133
+            optfree (opts);
134
+            return -1;
135
+        }
136
+
137
+        if ((sockd = socket (res->ai_family, SOCK_STREAM, 0)) < 0)
138
+        {
139
+            perror ("socket()");
140
+            logg ("!%s: Can't create TCP socket\n", option);
141
+            optfree (opts);
142
+            freeaddrinfo (res);
143
+            return -1;
144
+        }
145
+
146
+        if (connect (sockd, res->ai_addr, res->ai_addrlen) == -1)
147
+        {
148
+            perror ("connect()");
149
+            closesocket (sockd);
150
+            logg ("!%s: Can't connect to clamd on %s:%s\n", option,
151
+                  addr ? addr : "localhost", port);
152
+            optfree (opts);
153
+            freeaddrinfo (res);
154
+            return -1;
155
+        }
156
+        freeaddrinfo (res);
140 157
 
141 158
 #else /* IPv4 */
142 159
 
143
-	if((sockd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
144
-	    logg("!%s: Can't create TCP socket\n", option);
145
-	    perror("socket()");
146
-	    optfree(opts);
147
-	    return -1;
148
-	}
149
-
150
-	server2.sin_family = AF_INET;
151
-	server2.sin_port = htons(opt->numarg);
152
-
153
-	if((opt = optget(opts, "TCPAddr"))->enabled) {
154
-	    if((he = gethostbyname(opt->strarg)) == 0) {
155
-		perror("gethostbyname()");
156
-		logg("^Clamd was NOT notified: Can't resolve hostname '%s'\n", opt->strarg);
157
-		optfree(opts);
158
-		closesocket(sockd);
159
-		return -1;
160
-	    }
161
-	    server2.sin_addr = *(struct in_addr *) he->h_addr_list[0];
162
-	} else
163
-	    server2.sin_addr.s_addr = inet_addr("127.0.0.1");
164
-
165
-
166
-	if(connect(sockd, (struct sockaddr *) &server2, sizeof(struct sockaddr_in)) < 0) {
167
-	    closesocket(sockd);
168
-	    logg("^Clamd was NOT notified: Can't connect to clamd on %s:%d\n",
169
-		    inet_ntoa(server2.sin_addr), ntohs(server2.sin_port));
170
-	    perror("connect()");
171
-	    optfree(opts);
172
-	    return -1;
173
-	}
160
+        if ((sockd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
161
+        {
162
+            logg ("!%s: Can't create TCP socket\n", option);
163
+            perror ("socket()");
164
+            optfree (opts);
165
+            return -1;
166
+        }
167
+
168
+        server2.sin_family = AF_INET;
169
+        server2.sin_port = htons (opt->numarg);
170
+
171
+        if ((opt = optget (opts, "TCPAddr"))->enabled)
172
+        {
173
+            if ((he = gethostbyname (opt->strarg)) == 0)
174
+            {
175
+                perror ("gethostbyname()");
176
+                logg ("^Clamd was NOT notified: Can't resolve hostname '%s'\n", opt->strarg);
177
+                optfree (opts);
178
+                closesocket (sockd);
179
+                return -1;
180
+            }
181
+            server2.sin_addr = *(struct in_addr *) he->h_addr_list[0];
182
+        }
183
+        else
184
+            server2.sin_addr.s_addr = inet_addr ("127.0.0.1");
185
+
186
+
187
+        if (connect
188
+            (sockd, (struct sockaddr *) &server2,
189
+             sizeof (struct sockaddr_in)) < 0)
190
+        {
191
+            closesocket (sockd);
192
+            logg ("^Clamd was NOT notified: Can't connect to clamd on %s:%d\n", inet_ntoa (server2.sin_addr), ntohs (server2.sin_port));
193
+            perror ("connect()");
194
+            optfree (opts);
195
+            return -1;
196
+        }
174 197
 
175 198
 #endif
176 199
 
177
-    } else {
178
-	logg("!%s: No communication socket specified in %s\n", option, cfgfile);
179
-	optfree(opts);
180
-	return 1;
200
+    }
201
+    else
202
+    {
203
+        logg ("!%s: No communication socket specified in %s\n", option,
204
+              cfgfile);
205
+        optfree (opts);
206
+        return 1;
181 207
     }
182 208
 
183
-    optfree(opts);
209
+    optfree (opts);
184 210
     return sockd;
185 211
 }
186 212
 
187
-int notify(const char *cfgfile)
213
+int
214
+notify (const char *cfgfile)
188 215
 {
189
-	char buff[20];
190
-	int sockd, bread;
191
-
192
-    if((sockd = clamd_connect(cfgfile, "NotifyClamd")) < 0)
193
-	return 1;
194
-
195
-    if(sendln(sockd, "RELOAD", 7) < 0) {
196
-	logg("!NotifyClamd: Could not write to clamd socket\n");
197
-	perror("send()");
198
-	closesocket(sockd);
199
-	return 1;
216
+    char buff[20];
217
+    int sockd, bread;
218
+
219
+    if ((sockd = clamd_connect (cfgfile, "NotifyClamd")) < 0)
220
+        return 1;
221
+
222
+    if (sendln (sockd, "RELOAD", 7) < 0)
223
+    {
224
+        logg ("!NotifyClamd: Could not write to clamd socket\n");
225
+        perror ("send()");
226
+        closesocket (sockd);
227
+        return 1;
200 228
     }
201 229
 
202
-    memset(buff, 0, sizeof(buff));
203
-    if((bread = recv(sockd, buff, sizeof(buff), 0)) > 0) {
204
-	if(!strstr(buff, "RELOADING")) {
205
-	    logg("!NotifyClamd: Unknown answer from clamd: '%s'\n", buff);
206
-	    closesocket(sockd);
207
-	    return 1;
208
-	}
230
+    memset (buff, 0, sizeof (buff));
231
+    if ((bread = recv (sockd, buff, sizeof (buff), 0)) > 0)
232
+    {
233
+        if (!strstr (buff, "RELOADING"))
234
+        {
235
+            logg ("!NotifyClamd: Unknown answer from clamd: '%s'\n", buff);
236
+            closesocket (sockd);
237
+            return 1;
238
+        }
209 239
     }
210 240
 
211
-    closesocket(sockd);
212
-    logg("Clamd successfully notified about the update.\n");
241
+    closesocket (sockd);
242
+    logg ("Clamd successfully notified about the update.\n");
213 243
     return 0;
214 244
 }
215 245
 #endif
... ...
@@ -19,7 +19,7 @@
19 19
 #ifndef __NOTIFY_H
20 20
 #define __NOTIFY_H
21 21
 
22
-int notify(const char *cfgfile);
23
-int clamd_connect(const char *cfgfile, const char *option);
22
+int notify (const char *cfgfile);
23
+int clamd_connect (const char *cfgfile, const char *option);
24 24
 
25 25
 #endif