static void detectFileType(char *name) { FILE *fp; char *typeName; char hdr[9]; int r = -1; if(!strcmp(name, "-")) fp = stdin; else { if((fp = fopen(name, "r")) == NULL) { perror(name); goto err; } } if((r = rsgt_tlvrdHeader(fp, (uchar*)hdr)) != 0) goto err; if(!strcmp(hdr, "LOGSIG10")) typeName = "Log Signature File, Version 10"; else if(!strcmp(hdr, "GTSTAT10")) typeName = "rsyslog GuardTime Signature State File, Version 10"; else typeName = "unknown"; printf("%s: %s [%s]\n", name, hdr, typeName); if(fp != stdin) fclose(fp); return; err: fprintf(stderr, "error %d (%s) processing file %s\n", r, RSGTE2String(r), name); }
/** * Read the file header and compare it to the expected value. * The file pointer is placed right after the header. * @param[in] fp file pointer of tlv file * @param[in] excpect expected header (e.g. "LOGSIG10") * @returns 0 if ok, something else otherwise */ int rsgt_chkFileHdr(FILE *fp, char *expect) { int r; char hdr[9]; if((r = rsgt_tlvrdHeader(fp, (uchar*)hdr)) != 0) goto done; if(strcmp(hdr, expect)) r = RSGTE_INVLHDR; else r = 0; done: return r; }
static void dumpFile(char *name) { FILE *fp; char hdr[9]; void *obj; tlvrecord_t rec; int r = -1; if(!strcmp(name, "-")) fp = stdin; else { printf("Processing file %s:\n", name); if((fp = fopen(name, "r")) == NULL) { perror(name); goto err; } } if((r = rsgt_tlvrdHeader(fp, (uchar*)hdr)) != 0) goto err; if(!strcmp(hdr, "LOGSIG10")) printf("File Header: Version 10 (deprecated) - conversion needed.\n"); else if(!strcmp(hdr, "LOGSIG11")) printf("File Header: Version 11\n"); else printf("File Header: '%s'\n", hdr); while(1) { /* we will err out on EOF */ if((r = rsgt_tlvrd(fp, &rec, &obj)) != 0) { if(feof(fp)) break; else goto err; } rsgt_tlvprint(stdout, rec.tlvtype, obj, verbose); rsgt_objfree(rec.tlvtype, obj); } if(fp != stdin) fclose(fp); return; err: fprintf(stderr, "error %d (%s) processing file %s\n", r, RSGTE2String(r), name); }
static void convertFile(char *name) { FILE *oldsigfp = NULL, *newsigfp = NULL; char hdr[9]; int r = -1; char newsigfname[4096]; char oldsigfname[4096]; if(!strcmp(name, "-")) oldsigfp = stdin; else { printf("Processing file %s:\n", name); if((oldsigfp = fopen(name, "r")) == NULL) { perror(name); goto err; } } if((r = rsgt_tlvrdHeader(oldsigfp, (uchar*)hdr)) != 0) goto err; if(!strcmp(hdr, "LOGSIG10")) { printf("Found Signature File with Version 10 - starting conversion.\n"); snprintf(newsigfname, sizeof(newsigfname), "%s.LOGSIG11", name); snprintf(oldsigfname, sizeof(oldsigfname), "%s.LOGSIG10", name); if((newsigfp = fopen(newsigfname, "w")) == NULL) { perror(newsigfname); r = RSGTE_IO; goto err; } else { /* Write FileHeader first */ if ( fwrite(LOGSIGHDR, sizeof(LOGSIGHDR)-1, 1, newsigfp) != 1) goto err; } if ((r = rsgt_ConvertSigFile(oldsigfp, newsigfp, verbose)) != 0) goto err; else { /* Close FILES */ if(oldsigfp != stdin) fclose(oldsigfp); if (newsigfp != NULL) fclose(newsigfp); /* Delete OLDFILE if there is one*/ if(unlink(oldsigfname) != 0) { if(errno != ENOENT) { perror("Error removing old file"); r = RSGTE_IO; goto err; } } /* Copy main sigfile to oldfile */ if(link(name, oldsigfname) != 0) { perror("Error moving old file"); r = RSGTE_IO; goto err; } /* Delete current sigfile*/ if(unlink(name) != 0) { if(errno != ENOENT) { perror("Error removing old file"); r = RSGTE_IO; goto err; } } /* Copy new sigfile to main sigfile */ if(link(newsigfname, name) != 0) { perror("Error moving new file"); r = RSGTE_IO; goto err; } /* Delete temporary new sigfile*/ if(unlink(newsigfname) != 0) { if(errno != ENOENT) { perror("Error removing new file"); r = RSGTE_IO; goto err; } } printf("File %s was converted to Version 11.\n", name); } } else printf("File does not need to be converted, File Header is: '%s'\n", hdr); return; err: fprintf(stderr, "error %d (%s) converting file %s\n", r, RSGTE2String(r), name); }