/* IDSESSION handler * Returns non zero for serious errors, zero otherwise */ int parallel_client_scan(char *file, int scantype, int *infected, int *err, int maxlevel, int flags) { struct cli_ftw_cbdata data; struct client_parallel_data cdata; int ftw; if((cdata.sockd = dconnect()) < 0) return 1; if(sendln(cdata.sockd, "zIDSESSION", 11)) { closesocket(cdata.sockd); return 1; } cdata.infected = 0; cdata.files = 0; cdata.errors = 0; cdata.scantype = scantype; cdata.lastid = 0; cdata.ids = NULL; cdata.printok = printinfected^1; data.data = &cdata; ftw = cli_ftw(file, flags, maxlevel ? maxlevel : INT_MAX, parallel_callback, &data, ftw_chkpath); if(ftw != CL_SUCCESS) { *err += cdata.errors; *infected += cdata.infected; closesocket(cdata.sockd); return 1; } sendln(cdata.sockd, "zEND", 5); while(cdata.ids && !dspresult(&cdata)); closesocket(cdata.sockd); *infected += cdata.infected; *err += cdata.errors; if(cdata.ids) { logg("!Clamd closed the connection before scanning all files.\n"); return 1; } if(cdata.errors) return 1; if(!cdata.files) return 0; if(cdata.printok) logg("~%s: OK\n", file); return 0; }
int get_clamd_version(const struct optstruct *opts) { char *buff; int len, sockd; struct RCVLN rcv; isremote(opts); if((sockd = dconnect()) < 0) return 2; recvlninit(&rcv, sockd); if(sendln(sockd, "zVERSION", 9)) { closesocket(sockd); return 2; } while((len = recvln(&rcv, &buff, NULL))) { if(len == -1) { logg("!Error occoured while receiving version information.\n"); break; } printf("%s\n", buff); } closesocket(sockd); return 0; }
int notify (const char *cfgfile) { char buff[20]; int sockd, bread; if ((sockd = clamd_connect (cfgfile, "NotifyClamd")) < 0) return 1; if (sendln (sockd, "RELOAD", 7) < 0) { logg ("!NotifyClamd: Could not write to clamd socket\n"); perror ("send()"); closesocket (sockd); return 1; } memset (buff, 0, sizeof (buff)); if ((bread = recv (sockd, buff, sizeof (buff), 0)) > 0) { if (!strstr (buff, "RELOADING")) { logg ("!NotifyClamd: Unknown answer from clamd: '%s'\n", buff); closesocket (sockd); return 1; } } closesocket (sockd); logg ("Clamd successfully notified about the update.\n"); return 0; }
/* Issues an INSTREAM command to clamd and streams the given file * Returns >0 on success, 0 soft fail, -1 hard fail */ static int send_stream(int sockd, const char *filename) { uint32_t buf[BUFSIZ/sizeof(uint32_t)]; int fd, len; unsigned long int todo = maxstream; if(filename) { if((fd = safe_open(filename, O_RDONLY | O_BINARY))<0) { logg("~%s: Access denied. ERROR\n", filename); return 0; } } else fd = 0; if(sendln(sockd, "zINSTREAM", 10)) { close(fd); return -1; } while((len = read(fd, &buf[1], sizeof(buf) - sizeof(uint32_t))) > 0) { if((unsigned int)len > todo) len = todo; buf[0] = htonl(len); if(sendln(sockd, (const char *)buf, len+sizeof(uint32_t))) { close(fd); return -1; } todo -= len; if(!todo) { len = 0; break; } } close(fd); if(len) { logg("!Failed to read from %s.\n", filename ? filename : "STDIN"); return 0; } *buf=0; sendln(sockd, (const char *)buf, 4); return 1; }
void sendStepperReady(){ long int pos = stepperX.currentPosition(); bytepos.i= pos; byte ttt[8] = { METHOD_I2C_SLAVEMSG, 1, RETURN_DRIVER_READY, DRIVER_X, bytepos.bytes[3], // bits 0-7 bytepos.bytes[2], // bits 8-15 bytepos.bytes[1], // bits 16-23 bytepos.bytes[0] // bits 24-32 }; Serial.println("Rx" + String(pos)); Serial.flush(); sendln(ttt,8); Serial.println(); Serial.flush(); }
/* Issues a FILDES command and pass a FD to clamd * Returns >0 on success, 0 soft fail, -1 hard fail */ static int send_fdpass(int sockd, const char *filename) { struct iovec iov[1]; struct msghdr msg; struct cmsghdr *cmsg; unsigned char fdbuf[CMSG_SPACE(sizeof(int))]; char dummy[]=""; int fd; if(filename) { if((fd = open(filename, O_RDONLY))<0) { logg("~%s: Access denied. ERROR\n", filename); return 0; } } else fd = 0; if(sendln(sockd, "zFILDES", 8)) { close(fd); return -1; } iov[0].iov_base = dummy; iov[0].iov_len = 1; memset(&msg, 0, sizeof(msg)); msg.msg_control = fdbuf; msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_controllen = CMSG_LEN(sizeof(int)); cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_len = CMSG_LEN(sizeof(int)); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; *(int *)CMSG_DATA(cmsg) = fd; if(sendmsg(sockd, &msg, 0) == -1) { logg("!FD send failed: %s\n", strerror(errno)); close(fd); return -1; } close(fd); return 1; }
int reload_clamd_database(const struct optstruct *opts) { char *buff; int len, sockd; struct RCVLN rcv; isremote(opts); if((sockd = dconnect()) < 0) return 2; recvlninit(&rcv, sockd); if(sendln(sockd, "zRELOAD", 8)) { closesocket(sockd); return 2; } if(!(len = recvln(&rcv, &buff, NULL)) || len < 10 || memcmp(buff, "RELOADING", 9)) { logg("!Clamd did not reload the database\n"); closesocket(sockd); return 2; } closesocket(sockd); return 0; }
/* Sends a proper scan request to clamd and parses its replies * This is used only in non IDSESSION mode * Returns the number of infected files or -1 on error * NOTE: filename may be NULL for STREAM scantype. */ int dsresult(int sockd, int scantype, const char *filename, int *printok, int *errors) { int infected = 0, len = 0, beenthere = 0; char *bol, *eol; struct RCVLN rcv; STATBUF sb; if(filename && chkpath(filename)) return 0; recvlninit(&rcv, sockd); switch(scantype) { case MULTI: case CONT: case ALLMATCH: if (!filename) { logg("Filename cannot be NULL for MULTISCAN or CONTSCAN.\n"); return -1; } len = strlen(filename) + strlen(scancmd[scantype]) + 3; if (!(bol = malloc(len))) { logg("!Cannot allocate a command buffer: %s\n", strerror(errno)); return -1; } sprintf(bol, "z%s %s", scancmd[scantype], filename); if(sendln(sockd, bol, len)) { free(bol); return -1; } free(bol); break; case STREAM: /* NULL filename safe in send_stream() */ len = send_stream(sockd, filename); break; #ifdef HAVE_FD_PASSING case FILDES: /* NULL filename safe in send_fdpass() */ len = send_fdpass(sockd, filename); break; #endif } if(len <=0) { *printok = 0; if(errors) (*errors)++; return len; } while((len = recvln(&rcv, &bol, &eol))) { if(len == -1) return -1; beenthere = 1; if(!filename) logg("~%s\n", bol); if(len > 7) { char *colon = strrchr(bol, ':'); if(colon && colon[1] != ' ') { char *br; *colon = 0; br = strrchr(bol, '('); if(br) *br = 0; colon = strrchr(bol, ':'); } if(!colon) { char * unkco = "UNKNOWN COMMAND"; if (!strncmp(bol, unkco, sizeof(unkco) - 1)) logg("clamd replied \"UNKNOWN COMMAND\". Command was %s\n", (scantype < 0 || scantype > MAX_SCANTYPE) ? "unidentified" : scancmd[scantype]); else logg("Failed to parse reply: \"%s\"\n", bol); return -1; } else if(!memcmp(eol - 7, " FOUND", 6)) { *(eol - 7) = 0; *printok = 0; infected++; if(filename) { if(scantype >= STREAM) { logg("~%s%s FOUND\n", filename, colon); if(action) action(filename); } else { logg("~%s FOUND\n", bol); *colon = '\0'; if(action) action(bol); } } } else if(!memcmp(eol-7, " ERROR", 6)) { if(errors) (*errors)++; *printok = 0; if(filename) { if(scantype >= STREAM) logg("~%s%s\n", filename, colon); else logg("~%s\n", bol); } } } } if(!beenthere) { if (!filename) { logg("STDIN: noreply from clamd\n."); return -1; } if(CLAMSTAT(filename, &sb) == -1) { logg("~%s: stat() failed with %s, clamd may not be responding\n", filename, strerror(errno)); return -1; } if(!S_ISDIR(sb.st_mode)) { logg("~%s: no reply from clamd\n", filename); return -1; } } return infected; }
/* Sends a proper scan request to clamd and parses its replies * This is used only in non IDSESSION mode * Returns the number of infected files or -1 on error */ int dsresult(int sockd, int scantype, const char *filename, int *printok) { int infected = 0, len, beenthere = 0; char *bol, *eol; struct RCVLN rcv; recvlninit(&rcv, sockd); switch(scantype) { case MULTI: case CONT: len = strlen(filename) + strlen(scancmd[scantype]) + 3; if (!(bol = malloc(len))) { logg("!Cannot allocate a command buffer: %s\n", strerror(errno)); return -1; } sprintf(bol, "z%s %s", scancmd[scantype], filename); if(sendln(sockd, bol, len)) return -1; free(bol); break; case STREAM: len = send_stream(sockd, filename); break; #ifdef HAVE_FD_PASSING case FILDES: len = send_fdpass(sockd, filename); break; #endif } if(len <=0) { *printok = 0; return len; } while((len = recvln(&rcv, &bol, &eol))) { if(len == -1) return -1; beenthere = 1; if(!filename) logg("~%s\n", bol); if(len > 7) { char *colon = strrchr(bol, ':'); if(!colon) { logg("Failed to parse reply\n"); return -1; } else if(!memcmp(eol - 7, " FOUND", 6)) { *printok = 0; infected++; if(filename) { if(scantype >= STREAM) { logg("~%s%s\n", filename, colon); if(action) action(filename); } else { logg("~%s\n", bol); *colon = '\0'; if(action) action(bol); } } } else if(!memcmp(eol-7, " ERROR", 6)) { *printok = 0; if(filename) { if(scantype >= STREAM) logg("~%s%s\n", filename, colon); else logg("~%s\n", bol); } } } } if(!beenthere) { logg("~%s: no reply from clamd\n", filename ? filename : "STDIN"); return -1; } return infected; }
void sendYpos(){ if(pcb_type == 2 ){ // servos + magicled uint16_t pos = servos[INNER_SERVOY].last_pos; byte ttt[5] = {METHOD_I2C_SLAVEMSG, 1, METHOD_GET_Y_POS, (pos & 0xFF),(pos >>8) }; sendln(ttt,5); }else if(pcb_type == 3 ){ // actuators + magicled