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