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