Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file parsing when file in zip archive is less than 2 bytes. Less … #201

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.26
2.7.27
2 changes: 1 addition & 1 deletion client/nivlheim_client
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ my $NAME = 'nivlheim_client';
my $AUTHOR = 'Øyvind Hagberg';
my $CONTACT = '[email protected]';
my $RIGHTS = 'USIT/IT-DRIFT/GD/GID, University of Oslo, Norway';
my $VERSION = '2.7.26';
my $VERSION = '2.7.27';

# Usage text
my $USAGE = <<"END_USAGE";
Expand Down
2 changes: 1 addition & 1 deletion client/windows/nivlheim_client.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ param(
[bool]$nosleep = $false
)

Set-Variable version -option Constant -value "2.7.26"
Set-Variable version -option Constant -value "2.7.27"
Set-Variable useragent -option Constant -value "NivlheimPowershellClient/$version"
Set-PSDebug -strict
Set-StrictMode -version "Latest" # http://technet.microsoft.com/en-us/library/hh849692.aspx
Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
nivlheim (2.7.27-1) buster; urgency=low

* Fixed a bug when parsing files in zip archive that are less than 2 bytes

-- Marius Pedersen <[email protected]>

nivlheim (2.7.26-1) buster; urgency=low

* Fixed a bug when clients post files from a IPv6 address
Expand Down
1 change: 0 additions & 1 deletion server/service/postArchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ func (vars *apiMethodPostArchive) ServeHTTP(w http.ResponseWriter, req *http.Req
var size uint64 = 0

if filetype == "application/zip" {
log.Println("The archive is in Zip format")
match = regexp.MustCompile(`\.tgz$`)
newFile := match.ReplaceAll([]byte(archiveFile), []byte(".zip"))
os.Rename(archiveFile, string(newFile))
Expand Down
48 changes: 25 additions & 23 deletions server/service/processarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func processArchive(url string, db *sql.DB) (err error) {
log.Printf("Processing archive %s", url)

file := config.QueueDir + "/" + url
log.Println(file)

if !fileExists(file) {
log.Printf("File %s does not exist", file)
Expand Down Expand Up @@ -76,7 +75,6 @@ func processArchive(url string, db *sql.DB) (err error) {
log.Println(err)
}
for _, f := range files {
log.Println("Removing ", f)
err = os.Remove(f)
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -174,13 +172,11 @@ func processArchive(url string, db *sql.DB) (err error) {

func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[string]int64, hostinfo int64, db *sql.DB, path string, tempdir string, de fs.DirEntry, err error) error {
if de.IsDir() {
log.Printf("Skipping directory %s", path)
return nil
}

// only process files under the "files" and "commands" directories
if !(strings.Contains(path, "/files/") || strings.Contains(path, "/commands/")) {
log.Printf("Skipping file %s", path)
return nil
}

Expand Down Expand Up @@ -235,7 +231,6 @@ func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[s
}

if oldCrc.Valid && crc == oldCrc.Int32 {
log.Printf("Skipping file %s, no changes", fileName)
if hostinfo > 0 {
_, _ = db.Exec("UPDATE hostinfo SET lastseen = $1, clientversion = $2 "+
" WHERE certfp = $3 AND lastseen < $4", metadata["iso_received"], metadata["clientversion"],
Expand All @@ -262,7 +257,6 @@ func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[s

// Set current to false for the previous version of this file
if _, ok := curfiles[fileName]; ok {
log.Printf("Setting current to false for %s", fileName)
_, _ = db.Exec("UPDATE files SET current=false WHERE fileid = $1 AND current",
curfiles[fileName])
delete(curfiles, fileName)
Expand All @@ -284,7 +278,6 @@ func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[s

// clear the "current" flag for files that weren't in this package
var notCurrent []int64
log.Println("Clear the current flag for deleted files")
for _, fileId := range curfiles {
_, _ = db.Exec("UPDATE files SET current=false WHERE fileid = $1 AND current", fileId)
notCurrent = append(notCurrent, fileId)
Expand All @@ -294,7 +287,6 @@ func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[s
// their "current" flag cleared, and can be removed from the
// in-memory search cache.
for _, id := range notCurrent {
log.Printf("id: %d", id)
removeFileFromFastSearch(id)
}

Expand Down Expand Up @@ -463,26 +455,36 @@ func unZip(dst string, fn string) error {
return err
}

var isUTF16 bytes.Buffer
_, err = io.CopyN(&isUTF16, fileInArchive, 2)
if err != nil {
return err
}
bom := isUTF16.Bytes()

pr := bytes.NewReader(bom)

if len(bom) == 2 && bom[0] == 0xFF && bom[1] == 0xFE { //UTF-16LE
scanner := bufio.NewScanner(transform.NewReader(io.MultiReader(pr, fileInArchive), unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()))
for scanner.Scan() {
dstFile.WriteString(scanner.Text() + "\r\n")
if f.UncompressedSize64 < 2 {
// file is too small to have a BOM, just copy
_, err = io.Copy(dstFile, fileInArchive)
if err != nil {
return err
}
dstFile.Close()
} else {
if _, err := io.Copy(dstFile, io.MultiReader(pr, fileInArchive)); err != nil {
// this file might be UTF-16, check for BOM
var isUTF16 bytes.Buffer
_, err = io.CopyN(&isUTF16, fileInArchive, 2)
if err != nil {
return err
}
bom := isUTF16.Bytes()

pr := bytes.NewReader(bom)

if len(bom) == 2 && bom[0] == 0xFF && bom[1] == 0xFE { //UTF-16LE
scanner := bufio.NewScanner(transform.NewReader(io.MultiReader(pr, fileInArchive), unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()))
for scanner.Scan() {
dstFile.WriteString(scanner.Text() + "\r\n")
}
} else {
if _, err := io.Copy(dstFile, io.MultiReader(pr, fileInArchive)); err != nil {
return err
}
}
dstFile.Close()
}
dstFile.Close()
err = os.Chtimes(filePath, f.Modified, f.Modified)
if err != nil {
log.Printf("Error setting mod time on %s: %s", filePath, err)
Expand Down
Loading