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 processarchive sql #202

Merged
merged 4 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.27
2.7.28
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.27';
my $VERSION = '2.7.28';

# 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.27"
Set-Variable version -option Constant -value "2.7.28"
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.28-1) buster; urgency=low

* Fixed a bug in the server code

-- Øyvind Hagberg <[email protected]>

nivlheim (2.7.27-1) buster; urgency=low

* Fixed a bug when parsing files in zip archive that are less than 2 bytes
Expand Down
40 changes: 29 additions & 11 deletions server/service/processarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func processArchive(url string, db *sql.DB) (err error) {
// process each file, do this in a transaction in case of errors during processing
err = utility.RunInTransaction(db, func(tx *sql.Tx) error {
err = filepath.WalkDir(tempDir, func(path string, entry fs.DirEntry, err error) error {
return processFile(&unchangedFiles, metaData, curFiles, hostInfoExists, db, path, tempDir, entry, err)
return processFile(&unchangedFiles, metaData, curFiles, hostInfoExists, db, path, tempDir, entry)
})
if err != nil {
log.Printf("Error in processFile: %s", err)
Expand All @@ -170,7 +170,7 @@ func processArchive(url string, db *sql.DB) (err error) {
return nil
}

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 {
func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[string]int64, hostinfo int64, db *sql.DB, path string, tempdir string, de fs.DirEntry) error {
if de.IsDir() {
return nil
}
Expand Down Expand Up @@ -232,22 +232,34 @@ func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[s

if oldCrc.Valid && crc == oldCrc.Int32 {
if hostinfo > 0 {
_, _ = db.Exec("UPDATE hostinfo SET lastseen = $1, clientversion = $2 "+
_, err = db.Exec("UPDATE hostinfo SET lastseen = $1, clientversion = $2 "+
" WHERE certfp = $3 AND lastseen < $4", metadata["iso_received"], metadata["clientversion"],
metadata["certfp"], metadata["iso_received"])
_, _ = db.Exec("UPDATE hostinfo SET ipaddr = $1, os_hostname= $2, dnsttl = null "+
" WHERE (ipaddr != $3 || os_hostname != $4) AND certfp = $5", metadata["ip"],
if err != nil {
return err
}
_, err = db.Exec("UPDATE hostinfo SET ipaddr = $1, os_hostname= $2, dnsttl = null "+
" WHERE (ipaddr != $3 OR os_hostname != $4) AND certfp = $5", metadata["ip"],
metadata["os_hostname"], metadata["ip"], metadata["os_hostname"], metadata["certfp"])
if err != nil {
return err
}
*unchangedFiles++
} else {
/* There is NO hostinfo record.
/ It looks like the machine was archived and just now came back.
/ Set parsed=false so the file will be parsed again,
/ because the hostinfo values must be re-populated. */
_, _ = db.Exec("UPDATE files SET parsed = false WHERE fileid=$1", fileId)
_, err = db.Exec("UPDATE files SET parsed = false WHERE fileid=$1", fileId)
if err != nil {
return err
}
}
_, _ = db.Exec("UPDATE files SET current=true, received=NOW() WHERE fileid = $1 "+
_, err = db.Exec("UPDATE files SET current=true, received=NOW() WHERE fileid = $1 "+
"AND NOT current", fileId)
if err != nil {
return err
}
// sql set current flag for this file
delete(curfiles, fileName)
return nil
Expand All @@ -257,8 +269,11 @@ 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 {
_, _ = db.Exec("UPDATE files SET current=false WHERE fileid = $1 AND current",
_, err = db.Exec("UPDATE files SET current=false WHERE fileid = $1 AND current",
curfiles[fileName])
if err != nil {
return err
}
delete(curfiles, fileName)
}

Expand All @@ -279,7 +294,10 @@ 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
for _, fileId := range curfiles {
_, _ = db.Exec("UPDATE files SET current=false WHERE fileid = $1 AND current", fileId)
_, err = db.Exec("UPDATE files SET current=false WHERE fileid = $1 AND current", fileId)
if err != nil {
return err
}
notCurrent = append(notCurrent, fileId)
}

Expand Down Expand Up @@ -456,14 +474,14 @@ func unZip(dst string, fn string) error {
}

if f.UncompressedSize64 < 2 {
// file is too small to have a BOM, just copy
// file is too small to have a BOM, just copy
_, err = io.Copy(dstFile, fileInArchive)
if err != nil {
return err
}
dstFile.Close()
} else {
// this file might be UTF-16, check for BOM
// this file might be UTF-16, check for BOM
var isUTF16 bytes.Buffer
_, err = io.CopyN(&isUTF16, fileInArchive, 2)
if err != nil {
Expand Down
Loading