Debian Codegraph @

src/file.c

raw
1 /*
2 * stunnel TLS offloading and load-balancing proxy
3 * Copyright (C) 1998-2026 Michal Trojnara <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses>.
17 *
18 * Linking stunnel statically or dynamically with other modules is making
19 * a combined work based on stunnel. Thus, the terms and conditions of
20 * the GNU General Public License cover the whole combination.
21 *
22 * In addition, as a special exception, the copyright holder of stunnel
23 * gives you permission to combine stunnel with free software programs or
24 * libraries that are released under the GNU LGPL and with code included
25 * in the standard release of OpenSSL under the OpenSSL License (or
26 * modified versions of such code, with unchanged license). You may copy
27 * and distribute such a system following the terms of the GNU GPL for
28 * stunnel and the licenses of the other code concerned.
29 *
30 * Note that people who make modified versions of stunnel are not obligated
31 * to grant this special exception for their modified versions; it is their
32 * choice whether to do so. The GNU General Public License gives permission
33 * to release a modified version without this exception; this exception
34 * also makes it possible to release a modified version which carries
35 * forward this exception.
36 */
37
38 #include "prototypes.h"
39
40 DISK_FILE *file_fdopen(int fd, FILE_MODE file_mode) {
41 DISK_FILE *df;
42 FILE *f;
43 char *mode;
44
45 switch(fd) {
46 case 0:
47 f=stdin;
48 break;
49 case 1:
50 f=stdout;
51 break;
52 case 2:
53 f=stderr;
54 break;
55 default:
56 switch(file_mode) {
57 case FILE_MODE_READ:
58 mode="r";
59 break;
60 case FILE_MODE_APPEND:
61 mode="a";
62 break;
63 case FILE_MODE_OVERWRITE:
64 mode="w";
65 break;
66 default: /* invalid file_mode */
67 return NULL;
68 }
69 f=fdopen(fd, mode);
70 }
71 if(!f)
72 return NULL;
73 df=str_alloc(sizeof(DISK_FILE));
74 df->f=f;
75 return df;
76 }
77
78 DISK_FILE *file_open(char *name, FILE_MODE file_mode) {
79 int fd, flags;
80
81 /* open file */
82 switch(file_mode) {
83 case FILE_MODE_READ:
84 flags=O_RDONLY;
85 break;
86 case FILE_MODE_APPEND:
87 flags=O_CREAT|O_WRONLY|O_APPEND;
88 break;
89 case FILE_MODE_OVERWRITE:
90 flags=O_CREAT|O_WRONLY|O_TRUNC;
91 break;
92 default: /* invalid file_mode */
93 return NULL;
94 }
95 #ifdef O_CLOEXEC
96 flags|=O_CLOEXEC;
97 #endif /* O_CLOEXEC */
98 /* don't fopen() directly to prevent O_CLOEXEC race condition */
99 #ifdef USE_WIN32
100 fd=_open(name, flags, _S_IREAD|_S_IWRITE);
101 #else /* USE_WIN32 */
102 fd=open(name, flags, 0640);
103 #endif /* USE_WIN32 */
104 if(fd<0)
105 return NULL;
106 return file_fdopen(fd, file_mode);
107 }
108
109 void file_close(DISK_FILE *df) {
110 if(!df) /* nothing to do */
111 return;
112 if(fileno(df->f)>2) /* never close stdin/stdout/stder */
113 fclose(df->f);
114 str_free(df);
115 }
116
117 ssize_t file_getline(DISK_FILE *df, char *line, int len) {
118 ssize_t i;
119 int c;
120
121 if(!df) /* not opened */
122 return -1;
123
124 for(i=0; i<len-1; i++) {
125 c=getc(df->f);
126 if(c==EOF) {
127 if(!i) /* no previously retrieved data */
128 return -1;
129 break; /* MSDOS-style last file line */
130 }
131 line[i]=(char)c;
132 if(line[i]=='\n') /* LF */
133 break;
134 if(line[i]=='\r') /* CR */
135 --i; /* ignore - it must be the last check */
136 }
137 line[i]='\0';
138 return i;
139 }
140
141 ssize_t file_putline_nonewline(DISK_FILE *df, char *line) {
142 /* used for fatal_debug() -> no str.c functions are allowed */
143 FILE *f;
144 int num;
145
146 f=df ? df->f : stderr; /* no file -> write to stderr */
147 num=fputs(line, f); /* automatically converts LF->CRLF on Windows */
148 return (ssize_t)num;
149 }
150
151 ssize_t file_putline_newline(DISK_FILE *df, char *line) {
152 char *buff;
153 size_t len;
154 ssize_t num;
155
156 len=strlen(line);
157 buff=str_alloc(len+3); /* +2 for LF+NUL */
158 strcpy(buff, line);
159 buff[len++]='\n'; /* LF */
160 buff[len]='\0'; /* NUL */
161 num=file_putline_nonewline(df, buff);
162 str_free(buff);
163 return num;
164 }
165
166 int file_flush(DISK_FILE *df) {
167 return fflush(df ? df->f : stderr); /* no file -> flush stderr */
168 }
169
170 int file_permissions(const char *file_name) {
171 #if !defined(USE_WIN32) && !defined(USE_OS2)
172 struct stat sb; /* buffer for stat */
173
174 /* check permissions of the private key file */
175 if(stat(file_name, &sb)) {
176 ioerror(file_name);
177 return 1; /* FAILED */
178 }
179 if(sb.st_mode & 7)
180 s_log(LOG_WARNING,
181 "Insecure file permissions on %s", file_name);
182 #else
183 (void)file_name; /* squash the unused parameter warning */
184 /* not (yet) implemented */
185 #endif
186 return 0;
187 }
188
189 #ifdef USE_WIN32
190
191 LPTSTR str2tstr(LPCSTR in) {
192 LPTSTR out;
193 #ifdef UNICODE
194 int len;
195
196 len=MultiByteToWideChar(CP_UTF8, 0, in, -1, NULL, 0);
197 if(!len)
198 return str_tprintf(TEXT("MultiByteToWideChar() failed"));
199 out=str_alloc(((size_t)len+1)*sizeof(WCHAR));
200 len=MultiByteToWideChar(CP_UTF8, 0, in, -1, out, len);
201 if(!len) {
202 str_free(out);
203 return str_tprintf(TEXT("MultiByteToWideChar() failed"));
204 }
205 #else
206 /* FIXME: convert UTF-8 to native codepage */
207 out=str_dup(in);
208 #endif
209 return out;
210 }
211
212 LPSTR tstr2str(LPCTSTR in) {
213 LPSTR out;
214 #ifdef UNICODE
215 int len;
216
217 len=WideCharToMultiByte(CP_UTF8, 0, in, -1, NULL, 0, NULL, NULL);
218 if(!len)
219 return str_printf("WideCharToMultiByte() failed");
220 out=str_alloc((size_t)len+1);
221 len=WideCharToMultiByte(CP_UTF8, 0, in, -1, out, len, NULL, NULL);
222 if(!len) {
223 str_free(out);
224 return str_printf("WideCharToMultiByte() failed");
225 }
226 #else
227 /* FIXME: convert native codepage to UTF-8 */
228 out=str_dup(in);
229 #endif
230 return out;
231 }
232
233 #endif /* USE_WIN32 */
234
235 /* end of file.c */