1 /* 2 * SPDX-License-Identifier: ISC 3 * 4 * Copyright (c) 2013-2018 Todd C. Miller <Todd.Miller@sudo.ws> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #ifndef SUDO_UTIL_H 20 #define SUDO_UTIL_H 21 22 #include <sys/types.h> /* for dev_t, mode_t, gid_t, size_t, ssize_t, uid_t */ 23 #ifdef HAVE_STDBOOL_H 24 # include <stdbool.h> 25 #else 26 # include "compat/stdbool.h" 27 #endif /* HAVE_STDBOOL_H */ 28 29 #ifdef __TANDEM 30 # define ROOT_UID 65535 31 #else 32 # define ROOT_UID 0 33 #endif 34 #define ROOT_GID 0 35 36 #ifndef TIME_T_MAX 37 # if SIZEOF_TIME_T == 8 38 # define TIME_T_MAX LLONG_MAX 39 # else 40 # define TIME_T_MAX INT_MAX 41 # endif 42 #endif 43 44 /* 45 * Macros for operating on struct timeval. 46 */ 47 #define sudo_timevalclear(tv) ((tv)->tv_sec = (tv)->tv_usec = 0) 48 49 #define sudo_timevalisset(tv) ((tv)->tv_sec || (tv)->tv_usec) 50 51 #define sudo_timevalcmp(tv1, tv2, op) \ 52 (((tv1)->tv_sec == (tv2)->tv_sec) ? \ 53 ((tv1)->tv_usec op (tv2)->tv_usec) : \ 54 ((tv1)->tv_sec op (tv2)->tv_sec)) 55 56 #define sudo_timevaladd(tv1, tv2, tv3) \ 57 do { \ 58 (tv3)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \ 59 (tv3)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \ 60 if ((tv3)->tv_usec >= 1000000) { \ 61 (tv3)->tv_sec++; \ 62 (tv3)->tv_usec -= 1000000; \ 63 } \ 64 } while (0) 65 66 #define sudo_timevalsub(tv1, tv2, tv3) \ 67 do { \ 68 (tv3)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \ 69 (tv3)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \ 70 if ((tv3)->tv_usec < 0) { \ 71 (tv3)->tv_sec--; \ 72 (tv3)->tv_usec += 1000000; \ 73 } \ 74 } while (0) 75 76 #ifndef TIMEVAL_TO_TIMESPEC 77 # define TIMEVAL_TO_TIMESPEC(tv, ts) \ 78 do { \ 79 (ts)->tv_sec = (tv)->tv_sec; \ 80 (ts)->tv_nsec = (tv)->tv_usec * 1000; \ 81 } while (0) 82 #endif 83 84 /* 85 * Macros for operating on struct timespec. 86 */ 87 #define sudo_timespecclear(ts) ((ts)->tv_sec = (ts)->tv_nsec = 0) 88 89 #define sudo_timespecisset(ts) ((ts)->tv_sec || (ts)->tv_nsec) 90 91 #define sudo_timespeccmp(ts1, ts2, op) \ 92 (((ts1)->tv_sec == (ts2)->tv_sec) ? \ 93 ((ts1)->tv_nsec op (ts2)->tv_nsec) : \ 94 ((ts1)->tv_sec op (ts2)->tv_sec)) 95 96 #define sudo_timespecadd(ts1, ts2, ts3) \ 97 do { \ 98 (ts3)->tv_sec = (ts1)->tv_sec + (ts2)->tv_sec; \ 99 (ts3)->tv_nsec = (ts1)->tv_nsec + (ts2)->tv_nsec; \ 100 while ((ts3)->tv_nsec >= 1000000000) { \ 101 (ts3)->tv_sec++; \ 102 (ts3)->tv_nsec -= 1000000000; \ 103 } \ 104 } while (0) 105 106 #define sudo_timespecsub(ts1, ts2, ts3) \ 107 do { \ 108 (ts3)->tv_sec = (ts1)->tv_sec - (ts2)->tv_sec; \ 109 (ts3)->tv_nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \ 110 while ((ts3)->tv_nsec < 0) { \ 111 (ts3)->tv_sec--; \ 112 (ts3)->tv_nsec += 1000000000; \ 113 } \ 114 } while (0) 115 116 #ifndef TIMESPEC_TO_TIMEVAL 117 # define TIMESPEC_TO_TIMEVAL(tv, ts) \ 118 do { \ 119 (tv)->tv_sec = (ts)->tv_sec; \ 120 (tv)->tv_usec = (ts)->tv_nsec / 1000; \ 121 } while (0) 122 #endif 123 124 /* 125 * The timespec version of st_mtime may vary on different platforms. 126 */ 127 #if defined(HAVE_ST_MTIM) 128 # if defined(HAVE_ST__TIM) 129 # define SUDO_ST_MTIM st_mtim.st__tim 130 # else 131 # define SUDO_ST_MTIM st_mtim 132 # endif 133 #elif defined(HAVE_ST_MTIMESPEC) 134 # define SUDO_ST_MTIM st_mtimespec 135 #endif 136 137 /* 138 * Macro to extract mtime as timespec. 139 * If there is no way to set the timestamp using nanosecond precision, 140 * we only fetch microsecond precision. Otherwise there is a mismatch 141 * between the timestamp we read and the one we wrote. 142 */ 143 #if defined(SUDO_ST_MTIM) 144 # if defined(HAVE_FUTIMENS) && defined(HAVE_UTIMENSAT) 145 # define mtim_get(_x, _y) do { (_y).tv_sec = (_x)->SUDO_ST_MTIM.tv_sec; (_y).tv_nsec = (_x)->SUDO_ST_MTIM.tv_nsec; } while (0) 146 # else 147 # define mtim_get(_x, _y) do { (_y).tv_sec = (_x)->SUDO_ST_MTIM.tv_sec; (_y).tv_nsec = ((_x)->SUDO_ST_MTIM.tv_nsec / 1000) * 1000; } while (0) 148 # endif 149 #elif defined(HAVE_ST_NMTIME) 150 # define mtim_get(_x, _y) do { (_y).tv_sec = (_x)->st_mtime; (_y).tv_nsec = (_x)->st_nmtime; } while (0) 151 #else 152 # define mtim_get(_x, _y) do { (_y).tv_sec = (_x)->st_mtime; (_y).tv_nsec = 0; } while (0) 153 #endif /* HAVE_ST_MTIM */ 154 155 /* sizeof() that returns a signed value */ 156 #define ssizeof(_x) ((ssize_t)sizeof(_x)) 157 158 /* Bit map macros. */ 159 #define sudo_setbit(_a, _i) ((_a)[(_i) / NBBY] |= 1 << ((_i) % NBBY)) 160 #define sudo_clrbit(_a, _i) ((_a)[(_i) / NBBY] &= ~(1<<((_i) % NBBY))) 161 #define sudo_isset(_a, _i) ((_a)[(_i) / NBBY] & (1<<((_i) % NBBY))) 162 #define sudo_isclr(_a, _i) (((_a)[(_i) / NBBY] & (1<<((_i) % NBBY))) == 0) 163 164 /* sudo_parseln() flags */ 165 #define PARSELN_COMM_BOL 0x01 /* comments only at beginning of line */ 166 #define PARSELN_CONT_IGN 0x02 /* ignore line continuation char */ 167 168 /* 169 * Macros to quiet gcc's warn_unused_result attribute. 170 */ 171 #ifdef __GNUC__ 172 # define ignore_result(x) do { \ 173 __typeof__(x) y = (x); \ 174 (void)y; \ 175 } while(0) 176 #else 177 # define ignore_result(x) (void)(x) 178 #endif 179 180 /* aix.c */ 181 sudo_dso_public int aix_getauthregistry_v1(char *user, char *saved_registry); 182 #define aix_getauthregistry(_a, _b) aix_getauthregistry_v1((_a), (_b)) 183 sudo_dso_public int aix_prep_user_v1(char *user, const char *tty); 184 #define aix_prep_user(_a, _b) aix_prep_user_v1((_a), (_b)) 185 sudo_dso_public int aix_restoreauthdb_v1(void); 186 #define aix_restoreauthdb() aix_restoreauthdb_v1() 187 sudo_dso_public int aix_setauthdb_v1(char *user); 188 sudo_dso_public int aix_setauthdb_v2(char *user, char *registry); 189 #define aix_setauthdb(_a, _b) aix_setauthdb_v2((_a), (_b)) 190 191 /* basename.c */ 192 sudo_dso_public char *sudo_basename_v1(const char *filename); 193 #define sudo_basename(_a) sudo_basename_v1(_a) 194 195 /* gethostname.c */ 196 sudo_dso_public char *sudo_gethostname_v1(void); 197 #define sudo_gethostname() sudo_gethostname_v1() 198 199 /* gettime.c */ 200 sudo_dso_public int sudo_gettime_awake_v1(struct timespec *ts); 201 #define sudo_gettime_awake(_a) sudo_gettime_awake_v1((_a)) 202 sudo_dso_public int sudo_gettime_mono_v1(struct timespec *ts); 203 #define sudo_gettime_mono(_a) sudo_gettime_mono_v1((_a)) 204 sudo_dso_public int sudo_gettime_real_v1(struct timespec *ts); 205 #define sudo_gettime_real(_a) sudo_gettime_real_v1((_a)) 206 207 /* gidlist.c */ 208 sudo_dso_public int sudo_parse_gids_v1(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp); 209 #define sudo_parse_gids(_a, _b, _c) sudo_parse_gids_v1((_a), (_b), (_c)) 210 211 /* getgrouplist.c */ 212 sudo_dso_public int sudo_getgrouplist2_v1(const char *name, gid_t basegid, GETGROUPS_T **groupsp, int *ngroupsp); 213 #define sudo_getgrouplist2(_a, _b, _c, _d) sudo_getgrouplist2_v1((_a), (_b), (_c), (_d)) 214 215 /* key_val.c */ 216 sudo_dso_public char *sudo_new_key_val_v1(const char *key, const char *value); 217 #define sudo_new_key_val(_a, _b) sudo_new_key_val_v1((_a), (_b)) 218 219 /* locking.c */ 220 #define SUDO_LOCK 1 /* lock a file */ 221 #define SUDO_TLOCK 2 /* test & lock a file (non-blocking) */ 222 #define SUDO_UNLOCK 4 /* unlock a file */ 223 sudo_dso_public bool sudo_lock_file_v1(int fd, int action); 224 #define sudo_lock_file(_a, _b) sudo_lock_file_v1((_a), (_b)) 225 sudo_dso_public bool sudo_lock_region_v1(int fd, int action, off_t len); 226 #define sudo_lock_region(_a, _b, _c) sudo_lock_region_v1((_a), (_b), (_c)) 227 228 /* logfac.c */ 229 sudo_dso_public bool sudo_str2logfac_v1(const char *str, int *logfac); 230 #define sudo_str2logfac(_a, _b) sudo_str2logfac_v1((_a), (_b)) 231 sudo_dso_public const char *sudo_logfac2str_v1(int num); 232 #define sudo_logfac2str(_a) sudo_logfac2str_v1((_a)) 233 234 /* logpri.c */ 235 sudo_dso_public bool sudo_str2logpri_v1(const char *str, int *logpri); 236 #define sudo_str2logpri(_a, _b) sudo_str2logpri_v1((_a), (_b)) 237 sudo_dso_public const char *sudo_logpri2str_v1(int num); 238 #define sudo_logpri2str(_a) sudo_logpri2str_v1((_a)) 239 240 /* mkdir_parents.c */ 241 sudo_dso_public bool sudo_mkdir_parents_v1(char *path, uid_t uid, gid_t gid, mode_t mode, bool quiet); 242 #define sudo_mkdir_parents(_a, _b, _c, _d, _e) sudo_mkdir_parents_v1((_a), (_b), (_c), (_d), (_e)) 243 244 /* parseln.c */ 245 sudo_dso_public ssize_t sudo_parseln_v1(char **buf, size_t *bufsize, unsigned int *lineno, FILE *fp); 246 sudo_dso_public ssize_t sudo_parseln_v2(char **buf, size_t *bufsize, unsigned int *lineno, FILE *fp, int flags); 247 #define sudo_parseln(_a, _b, _c, _d, _e) sudo_parseln_v2((_a), (_b), (_c), (_d), (_e)) 248 249 /* progname.c */ 250 sudo_dso_public void initprogname(const char *); 251 sudo_dso_public void initprogname2(const char *, const char * const *); 252 253 /* rcstr.c */ 254 sudo_dso_public char *sudo_rcstr_dup(const char *src); 255 sudo_dso_public char *sudo_rcstr_alloc(size_t len); 256 sudo_dso_public char *sudo_rcstr_addref(const char *s); 257 sudo_dso_public void sudo_rcstr_delref(const char *s); 258 259 /* roundup.c */ 260 sudo_dso_public unsigned int sudo_pow2_roundup_v1(unsigned int len); 261 #define sudo_pow2_roundup(_a) sudo_pow2_roundup_v1((_a)) 262 263 /* secure_path.c */ 264 #define SUDO_PATH_SECURE 0 265 #define SUDO_PATH_MISSING -1 266 #define SUDO_PATH_BAD_TYPE -2 267 #define SUDO_PATH_WRONG_OWNER -3 268 #define SUDO_PATH_WORLD_WRITABLE -4 269 #define SUDO_PATH_GROUP_WRITABLE -5 270 struct stat; 271 sudo_dso_public int sudo_secure_dir_v1(const char *path, uid_t uid, gid_t gid, struct stat *sbp); 272 #define sudo_secure_dir(_a, _b, _c, _d) sudo_secure_dir_v1((_a), (_b), (_c), (_d)) 273 sudo_dso_public int sudo_secure_file_v1(const char *path, uid_t uid, gid_t gid, struct stat *sbp); 274 #define sudo_secure_file(_a, _b, _c, _d) sudo_secure_file_v1((_a), (_b), (_c), (_d)) 275 276 /* setgroups.c */ 277 sudo_dso_public int sudo_setgroups_v1(int ngids, const GETGROUPS_T *gids); 278 #define sudo_setgroups(_a, _b) sudo_setgroups_v1((_a), (_b)) 279 280 /* strsplit.c */ 281 sudo_dso_public const char *sudo_strsplit_v1(const char *str, const char *endstr, const char *sep, const char **last); 282 #define sudo_strsplit(_a, _b, _c, _d) sudo_strsplit_v1(_a, _b, _c, _d) 283 284 /* strtobool.c */ 285 sudo_dso_public int sudo_strtobool_v1(const char *str); 286 #define sudo_strtobool(_a) sudo_strtobool_v1((_a)) 287 288 /* strtonum.c */ 289 /* Not versioned for historical reasons. */ 290 sudo_dso_public long long sudo_strtonum(const char *, long long, long long, const char **); 291 292 /* strtoid.c */ 293 sudo_dso_public id_t sudo_strtoid_v1(const char *str, const char *sep, char **endp, const char **errstr); 294 sudo_dso_public id_t sudo_strtoid_v2(const char *str, const char **errstr); 295 #define sudo_strtoid(_a, _b) sudo_strtoid_v2((_a), (_b)) 296 sudo_dso_public id_t sudo_strtoidx_v1(const char *str, const char *sep, char **endp, const char **errstr); 297 #define sudo_strtoidx(_a, _b, _c, _d) sudo_strtoidx_v1((_a), (_b), (_c), (_d)) 298 299 /* strtomode.c */ 300 sudo_dso_public int sudo_strtomode_v1(const char *cp, const char **errstr); 301 #define sudo_strtomode(_a, _b) sudo_strtomode_v1((_a), (_b)) 302 303 /* sudo_printf.c */ 304 extern int (*sudo_printf)(int msg_type, const char *fmt, ...); 305 306 /* term.c */ 307 sudo_dso_public bool sudo_term_cbreak_v1(int fd); 308 #define sudo_term_cbreak(_a) sudo_term_cbreak_v1((_a)) 309 sudo_dso_public bool sudo_term_copy_v1(int src, int dst); 310 #define sudo_term_copy(_a, _b) sudo_term_copy_v1((_a), (_b)) 311 sudo_dso_public bool sudo_term_noecho_v1(int fd); 312 #define sudo_term_noecho(_a) sudo_term_noecho_v1((_a)) 313 sudo_dso_public bool sudo_term_raw_v1(int fd, int isig); 314 #define sudo_term_raw(_a, _b) sudo_term_raw_v1((_a), (_b)) 315 sudo_dso_public bool sudo_term_restore_v1(int fd, bool flush); 316 #define sudo_term_restore(_a, _b) sudo_term_restore_v1((_a), (_b)) 317 318 /* ttyname_dev.c */ 319 sudo_dso_public char *sudo_ttyname_dev_v1(dev_t tdev, char *name, size_t namelen); 320 #define sudo_ttyname_dev(_a, _b, _c) sudo_ttyname_dev_v1((_a), (_b), (_c)) 321 322 /* ttysize.c */ 323 sudo_dso_public void sudo_get_ttysize_v1(int *rowp, int *colp); 324 #define sudo_get_ttysize(_a, _b) sudo_get_ttysize_v1((_a), (_b)) 325 326 /* uuid.c */ 327 sudo_dso_public void sudo_uuid_create_v1(unsigned char uuid_out[16]); 328 #define sudo_uuid_create(_a) sudo_uuid_create_v1((_a)) 329 sudo_dso_public char *sudo_uuid_to_string_v1(unsigned char uuid[16], char *dst, size_t dstsiz); 330 #define sudo_uuid_to_string(_a, _b, _c) sudo_uuid_to_string_v1((_a), (_b), (_c)) 331 332 #endif /* SUDO_UTIL_H */ 333