1 /*
2 Copyright 2021 Northern.tech AS
3
4 This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 3.
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. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
19 To the extent this program is licensed as part of the Enterprise
20 versions of CFEngine, the applicable Commercial Open Source License
21 (COSL) may apply to this file if you as a licensee so wish it. See
22 included file COSL.txt.
23 */
24
25 /*
26 Contains any fixes which need to be made because of lack of OS support on a
27 given platform These are conditionally compiled, pending extensions or
28 developments in the OS concerned.
29
30 FIXME: move to the libcompat/ directory or to the appropriate source file.
31 */
32
33 #include <cf3.defs.h>
34
35 #include <audit.h>
36
37 #ifdef __ANDROID__
38 # include <file_lib.h> /* File_Copy() */
39 #endif
40
41 static char *cf_format_strtimestamp(struct tm *tm, char *buf);
42
43 /*********************************************************/
44
45 #ifndef HAVE_SETNETGRENT
46 #if SETNETGRENT_RETURNS_INT
47 int
48 #else
49 void
50 #endif
setnetgrent(const char * netgroup)51 setnetgrent(const char *netgroup)
52 {
53 #if SETNETGRENT_RETURNS_INT
54 return 0;
55 #endif
56 }
57
58 #endif
59
60 /**********************************************************/
61
62 #ifndef HAVE_GETNETGRENT
63
getnetgrent(char ** machinep,char ** userp,char ** domainp)64 int getnetgrent(char **machinep, char **userp, char **domainp)
65 {
66 *machinep = NULL;
67 *userp = NULL;
68 *domainp = NULL;
69 return 0;
70 }
71
72 #endif
73
74 /***********************************************************/
75
76 #ifndef HAVE_ENDNETGRENT
77 #if ENDNETGRENT_RETURNS_INT
78 int
79 #else
80 void
81 #endif
endnetgrent(void)82 endnetgrent(void)
83 {
84 #if ENDNETGRENT_RETURNS_INT
85 return 1;
86 #endif
87 }
88
89 #endif
90
91 /***********************************************************/
92
93 #ifndef HAVE_SETEGID
94
setegid(gid_t gid)95 int setegid(gid_t gid)
96 {
97 # ifdef HAVE_SETREGID
98 return setregid(-1, gid);
99 # else
100 Log(LOG_LEVEL_VERBOSE, "(This system does not have setregid (patches.c)");
101 return -1;
102 # endif
103 }
104
105 #endif
106
107 /*******************************************************************/
108
IsPrivileged()109 bool IsPrivileged()
110 {
111 #ifdef _WIN32
112 return true;
113 #else
114 return (getuid() == 0);
115 #endif
116 }
117
118 /*
119 * This function converts passed time_t value to string timestamp used
120 * throughout the system. By sheer coincidence this timestamp has the same
121 * format as ctime(3) output on most systems (but NT differs in definition of
122 * ctime format, so those are not identical there).
123 *
124 * Buffer passed should be at least 26 bytes long (including the trailing zero).
125 *
126 * Please use this function instead of (non-portable and deprecated) ctime_r or
127 * (non-threadsafe) ctime.
128 */
129
130 /*******************************************************************/
131
cf_strtimestamp_local(const time_t time,char * buf)132 char *cf_strtimestamp_local(const time_t time, char *buf)
133 {
134 struct tm tm;
135
136 if (localtime_r(&time, &tm) == NULL)
137 {
138 Log(LOG_LEVEL_VERBOSE, "Unable to parse passed timestamp. (localtime_r: %s)", GetErrorStr());
139 return NULL;
140 }
141
142 return cf_format_strtimestamp(&tm, buf);
143 }
144
145 /*******************************************************************/
146
cf_strtimestamp_utc(const time_t time,char * buf)147 char *cf_strtimestamp_utc(const time_t time, char *buf)
148 {
149 struct tm tm;
150
151 if (gmtime_r(&time, &tm) == NULL)
152 {
153 Log(LOG_LEVEL_VERBOSE, "Unable to parse passed timestamp. (gmtime_r: %s)", GetErrorStr());
154 return NULL;
155 }
156
157 return cf_format_strtimestamp(&tm, buf);
158 }
159
160 /*******************************************************************/
161
cf_format_strtimestamp(struct tm * tm,char * buf)162 static char *cf_format_strtimestamp(struct tm *tm, char *buf)
163 {
164 /* Security checks */
165 if ((tm->tm_year < -2899) || (tm->tm_year > 8099))
166 {
167 Log(LOG_LEVEL_ERR, "Unable to format timestamp: passed year is out of range: %d", tm->tm_year + 1900);
168 return NULL;
169 }
170
171 /* There is no easy way to replicate ctime output by using strftime */
172
173 if (snprintf(buf, 26, "%3.3s %3.3s %2d %02d:%02d:%02d %04d",
174 DAY_TEXT[tm->tm_wday ? (tm->tm_wday - 1) : 6], MONTH_TEXT[tm->tm_mon],
175 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_year + 1900) >= 26)
176 {
177 Log(LOG_LEVEL_ERR, "Unable to format timestamp: passed values are out of range");
178 return NULL;
179 }
180
181 return buf;
182 }
183
184 /*******************************************************************/
185
LinkOrCopy(const char * from,const char * to,int sym)186 bool LinkOrCopy(const char *from, const char *to, int sym)
187 /**
188 * Creates symlink to file on platforms supporting it, copies on
189 * others.
190 **/
191 {
192
193 #ifdef __MINGW32__ // only copy on Windows for now
194
195 if (!CopyFile(from, to, TRUE))
196 {
197 return false;
198 }
199
200 #elif __ANDROID__ /* link() not supported on ANDROID platform */
201 return File_Copy(from, to);
202 #else /* !__MINGW32__ */
203
204 if (sym)
205 {
206 if (symlink(from, to) == -1)
207 {
208 return false;
209 }
210 }
211 else // hardlink
212 {
213 if (link(from, to) == -1)
214 {
215 return false;
216 }
217 }
218
219 #endif /* !__MINGW32__ */
220
221 return true;
222 }
223