Debian Codegraph @

src/be.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 1999-2003, 2005-2006, 2010-2011, 2013, 2019-2023 Rami Lehti,
5 * Pablo Virolainen, Richard van den Berg, Hannes von Haugwitz
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "config.h"cxx . . $ `<file>/include/config.h`/
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #ifdef HAVE_FCNTLcxx . . $ `./include/config.h:47:9`!
28 #include <fcntl.h>
29 #endif
30 #include <unistd.h>
31 #include <errno.h>
32 #ifdef WITH_ZLIBcxx . . $ `./include/config.h:266:9`!
33 #include <zlib.h>
34 #endif
35 #include "log.h"cxx . . $ `<file>/include/log.h`/
36 #include "util.h"cxx . . $ `<file>/include/util.h`/
37 #include "errorcodes.h"cxx . . $ `<file>/include/errorcodes.h`/
38 #ifdef WITH_CURL
39 #include "fopen.h"
40 #endif
41 #include "be.h"cxx . . $ `<file>/include/be.h`/
42 #include "url.h"cxx . . $ `<file>/include/url.h`/
43
44 /*for locale support*/
45 #include "locale-aide.h"cxx . . $ `<file>/include/locale-aide.h`/
46 /*for locale support*/
47
48
49 void* be_initcxx . . $ be_init(d7a38dc26e275cb4).(url_tcxx . . $ url_t#* u, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! readonly, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! iszipped, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! append, int linenumber, char* filename, char* linebuf, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! *created) {
50 FILEcxx . . $ FILE#* fh=NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
51 long a=0;
52 char* err=NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
53 int fd;
54 #if HAVE_FCNTLcxx . . $ `include/config.h:47:9`! && HAVE_FTRUNCATEcxx . . $ `include/config.h:56:9`!
55 struct flockcxx . . $ flock# fl;
56 #endif
57
58 switch (u->typecxx . . $ url_t#type.) {
59 case url_filecxx . . $ url_file. : {
60 u->valuecxx . . $ url_t#value. = expand_tildecxx . . $ expand_tilde(7a9722d75b371431).(u->valuecxx . . $ url_t#value.);
61 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., "open (%s, gzip: %s, append: %s) file '%s'", readonly?"read-only":"read/write", btoacxx . . $ btoa(626eec357d49c77e).(iszipped), btoacxx . . $ btoa(626eec357d49c77e).(append), u->valuecxx . . $ url_t#value.);
62 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! new_file = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
63 if (readonly) {
64 fd = opencxx . . $ open(e156e3aa1e5c4ad1).(u->valuecxx . . $ url_t#value., O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!, 0);
65 } else {
66 int flag_truncate;
67 #if HAVE_FCNTLcxx . . $ `include/config.h:47:9`! && HAVE_FTRUNCATEcxx . . $ `include/config.h:56:9`!
68 flag_truncate = 0;
69 #else
70 flag_truncate = O_TRUNC;
71 #endif
72 fd = opencxx . . $ open(e156e3aa1e5c4ad1).(u->valuecxx . . $ url_t#value., O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!|O_EXCLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:50:10`!|O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!, 0666);
73 new_file = fd >= 0;
74 if (fd == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EEXISTcxx . . $ `<external>/8987895939074593727/errno-base.h:21:9`!) {
75 fd = opencxx . . $ open(e156e3aa1e5c4ad1).(u->valuecxx . . $ url_t#value., O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!|(append?O_APPENDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:59:10`!:flag_truncate), 0666);
76 }
77 }
78 if(fd==-1) {
79 LOG_CONFIG_FORMAT_LINEcxx . . $ `include/log.h:59:9`!(LOG_LEVEL_ERROR, "open (%s) failed for file '%s': %s", readonly?"read-only":"read/write", u->value, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
80 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
81 } else {
82 if (created) { *created = new_file; }
83 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., "%s file '%s' with fd=%i",new_file?"created":"opened existing", u->valuecxx . . $ url_t#value.,fd);
84 }
85 #if HAVE_FCNTLcxx . . $ `include/config.h:47:9`! && HAVE_FTRUNCATEcxx . . $ `include/config.h:56:9`!
86 if(!readonly) {
87 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(u->valuecxx . . $ url_t#value., "/dev/null")) {
88 fl.l_typecxx . . $ flock#l_type. = F_WRLCKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:227:10`!;
89 fl.l_whencecxx . . $ flock#l_whence. = SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!;
90 fl.l_startcxx . . $ flock#l_start. = 0;
91 fl.l_lencxx . . $ flock#l_len. = 0;
92 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., "try to get lock for file '%s'", u->valuecxx . . $ url_t#value.);
93 if (fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(fd, F_SETLKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:108:11`!, &fl) == -1) {
94 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "cannot get lock for file '%s': %s", u->valuecxx . . $ url_t#value., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
95 exitcxx . . $ exit(3ca292c338df9bb3).(LOCK_ERRORcxx . . $ `include/errorcodes.h:32:9`!);
96 } else {
97 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., "successfully got lock for file '%s'", u->valuecxx . . $ url_t#value.);
98 }
99 if (!append) {
100 if(ftruncatecxx . . $ ftruncate(bba9d6e9fb34fad7).(fd,0)==-1) {
101 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.,_cxx . . $ `include/locale-aide.h:33:9`!("ftruncate failed for file %s: %s"),u->valuecxx . . $ url_t#value., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
102 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
103 } else {
104 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., "successfully truncated file '%s' to size 0", u->valuecxx . . $ url_t#value.);
105 }
106 }
107 } else {
108 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., "skip lock for '/dev/null'");
109 }
110 }
111 #endif
112 #ifdef WITH_ZLIBcxx . . $ `include/config.h:266:9`!
113 if(iszipped && !readonly){
114 gzFilecxx . . $ gzFile# gzfh = gzdopencxx . . $ gzdopen(22355c0c2644e5c0).(fd,"wb9");
115 if(gzfh==NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!){
116 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., _cxx . . $ `include/locale-aide.h:33:9`!("gzdopen (%s) failed for file %s"), readonly?"read-only":"read/write", u->valuecxx . . $ url_t#value.);
117 }
118 return gzfh;
119 }
120 else{
121 #endif
122 fh=fdopencxx . . $ fdopen(8f4e2d7350ebd077).(fd,readonly?"r":"w+");
123 if(fh==NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!){
124 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., _cxx . . $ `include/locale-aide.h:33:9`!("fdopen (%s) failed for file '%s': %s"), readonly?"read-only":"read/write", u->valuecxx . . $ url_t#value., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
125 }
126 return fh;
127 #ifdef WITH_ZLIBcxx . . $ `include/config.h:266:9`!
128 }
129 #endif
130 }
131 case url_stdoutcxx . . $ url_stdout. : {
132 #ifdef WITH_ZLIBcxx . . $ `include/config.h:266:9`!
133 if(iszipped){
134 return gzdopencxx . . $ gzdopen(22355c0c2644e5c0).(filenocxx . . $ fileno(89bc1efcdac0ca22).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!),"wb");
135 }
136 else{
137 #endif
138 return stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!;
139 #ifdef WITH_ZLIBcxx . . $ `include/config.h:266:9`!
140 }
141 #endif
142 }
143 case url_stdincxx . . $ url_stdin. : {
144 #ifdef WITH_ZLIBcxx . . $ `include/config.h:266:9`!
145 if(iszipped){
146 return gzdopencxx . . $ gzdopen(22355c0c2644e5c0).(filenocxx . . $ fileno(89bc1efcdac0ca22).(stdincxx . . $ `<external>/232824719582829767/stdio.h:157:9`!),"r");
147 }
148 else{
149 #endif
150 return stdincxx . . $ `<external>/232824719582829767/stdio.h:157:9`!;
151 #ifdef WITH_ZLIBcxx . . $ `include/config.h:266:9`!
152 }
153 #endif
154 }
155 case url_stderrcxx . . $ url_stderr. : {
156 #ifdef WITH_ZLIBcxx . . $ `include/config.h:266:9`!
157 if(iszipped){
158 return gzdopencxx . . $ gzdopen(22355c0c2644e5c0).(filenocxx . . $ fileno(89bc1efcdac0ca22).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!),"wb");
159 }
160 else{
161 #endif
162 return stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!;
163 #ifdef WITH_ZLIBcxx . . $ `include/config.h:266:9`!
164 }
165 #endif
166 }
167 case url_fdcxx . . $ url_fd. : {
168 a=strtolcxx . . $ strtol(44bbb87609b212fc).(u->valuecxx . . $ url_t#value.,&err,10);
169 if(*err!='\0'||errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!==ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!){
170 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.,"illegal file descriptor value:%s",u->valuecxx . . $ url_t#value.);
171 }
172 #ifdef WITH_ZLIBcxx . . $ `include/config.h:266:9`!
173 if(iszipped && !readonly){
174 gzFilecxx . . $ gzFile# gzfh = gzdopencxx . . $ gzdopen(22355c0c2644e5c0).(a,"w");
175 if(gzfh==NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!){
176 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.,"couldn't reopen file descriptor %li",a);
177 }
178 return gzfh;
179 }
180 else{
181 #endif
182 fh=fdopencxx . . $ fdopen(8f4e2d7350ebd077).(a,readonly?"r":"w");
183 if(fh==NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!){
184 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.,"couldn't reopen file descriptor %li",a);
185 }
186 return fh;
187 #ifdef WITH_ZLIBcxx . . $ `include/config.h:266:9`!
188 }
189 #endif
190 }
191 #ifdef WITH_CURL
192 case url_http:
193 case url_https:
194 case url_ftp:
195 {
196 log_msg(LOG_LEVEL_DEBUG,_("opening curl '%s' for %s"),u->value,readonly?"r":"w+");
197 if (iszipped) {
198 return NULL;
199 }
200 return url_fopen(u->value,readonly?"r":"w+");
201 }
202 #endif /* WITH CURL */
203 default:{
204 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "unsupported backend: %i", u->typecxx . . $ url_t#type.);
205 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
206 }
207 }
208 /* Not reached */
209 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
210
211 }