new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / monitord / sign_log.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11 #include "os_crypto/md5/md5_op.h"
12 #include "os_crypto/sha1/sha1_op.h"
13 #include "monitord.h"
14
15
16 /* Sign a log file */
17 void OS_SignLog(const char *logfile, const char *logfile_old, int log_missing)
18 {
19     os_md5 mf_sum;
20     os_md5 mf_sum_old;
21
22     os_sha1 sf_sum;
23     os_sha1 sf_sum_old;
24
25     char logfilesum[OS_FLSIZE + 1];
26     char logfilesum_old[OS_FLSIZE + 1];
27
28     FILE *fp;
29
30     /* Clear the memory */
31     memset(logfilesum, '\0', OS_FLSIZE + 1);
32     memset(logfilesum_old, '\0', OS_FLSIZE + 1);
33
34     /* Set umask */
35     umask(0027);
36
37     /* Create the checksum file names */
38     snprintf(logfilesum, OS_FLSIZE, "%s.sum", logfile);
39     snprintf(logfilesum_old, OS_FLSIZE, "%s.sum", logfile_old);
40
41     /* Generate MD5 of the old file */
42     if (OS_MD5_File(logfilesum_old, mf_sum_old, OS_TEXT) < 0) {
43         merror("%s: No previous md5 checksum found: '%s'. "
44                "Starting over.", ARGV0, logfilesum_old);
45         strncpy(mf_sum_old, "none", 6);
46     }
47
48     /* Generate SHA-1 of the old file  */
49     if (OS_SHA1_File(logfilesum_old, sf_sum_old, OS_TEXT) < 0) {
50         merror("%s: No previous sha1 checksum found: '%s'. "
51                "Starting over.", ARGV0, logfilesum_old);
52         strncpy(sf_sum_old, "none", 6);
53     }
54
55     /* Generate MD5 of the current file */
56     if (OS_MD5_File(logfile, mf_sum, OS_TEXT) < 0) {
57         if (log_missing) {
58             merror("%s: File '%s' not found. MD5 checksum skipped.",
59                    ARGV0, logfile);
60         }
61         strncpy(mf_sum, "none", 6);
62     }
63
64     /* Generate SHA-1 of the current file */
65     if (OS_SHA1_File(logfile, sf_sum, OS_TEXT) < 0) {
66         if (log_missing) {
67             merror("%s: File '%s' not found. SHA1 checksum skipped.",
68                    ARGV0, logfile);
69         }
70         strncpy(sf_sum, "none", 6);
71     }
72
73     fp = fopen(logfilesum, "w");
74     if (!fp) {
75         merror(FOPEN_ERROR, ARGV0, logfilesum, errno, strerror(errno));
76         return;
77     }
78
79     fprintf(fp, "Current checksum:\n");
80     fprintf(fp, "MD5  (%s) = %s\n", logfile, mf_sum);
81     fprintf(fp, "SHA1 (%s) = %s\n\n", logfile, sf_sum);
82
83     fprintf(fp, "Chained checksum:\n");
84     fprintf(fp, "MD5  (%s) = %s\n", logfilesum_old, mf_sum_old);
85     fprintf(fp, "SHA1 (%s) = %s\n\n", logfilesum_old, sf_sum_old);
86     fclose(fp);
87
88     return;
89 }
90