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