Net-Dropbear

 view release on metacpan or  search on metacpan

dropbear/dbutil.c  view on Meta::CPAN

	}
#endif

	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
		if (errno == ENODEV) {
			/* Some devices (like /dev/null redirected in)
			 * can't be set to non-blocking */
			TRACE(("ignoring ENODEV for setnonblocking"))
		} else {
		{
			dropbear_exit("Couldn't set nonblocking");
		}
		}
	}
	TRACE(("leave setnonblocking"))
}

void disallow_core() {
	struct rlimit lim = {0};
	if (getrlimit(RLIMIT_CORE, &lim) < 0) {
		TRACE(("getrlimit(RLIMIT_CORE) failed"));
	}
	lim.rlim_cur = 0;
	if (setrlimit(RLIMIT_CORE, &lim) < 0) {
		TRACE(("setrlimit(RLIMIT_CORE) failed"));
	}
}

/* Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE, with the result in *val */
int m_str_to_uint(const char* str, unsigned int *val) {
	unsigned long l;
	char *endp;

	l = strtoul(str, &endp, 10);

	if (endp == str || *endp != '\0') {
		/* parse error */
		return DROPBEAR_FAILURE;
	}

	/* The c99 spec doesn't actually seem to define EINVAL, but most platforms
	 * I've looked at mention it in their manpage */
	if ((l == 0 && errno == EINVAL)
		|| (l == ULONG_MAX && errno == ERANGE)
		|| (l > UINT_MAX)) {
		return DROPBEAR_FAILURE;
	} else {
		*val = l;
		return DROPBEAR_SUCCESS;
	}
}

/* Returns malloced path. inpath beginning with '~/' expanded,
   otherwise returned as-is */
char * expand_homedir_path(const char *inpath) {
	struct passwd *pw = NULL;
	if (strncmp(inpath, "~/", 2) == 0) {
		char *homedir = getenv("HOME");

		if (!homedir) {
			pw = getpwuid(getuid());
			if (pw) {
				homedir = pw->pw_dir;
			}
		}

		if (homedir) {
			int len = strlen(inpath)-2 + strlen(homedir) + 2;
			char *buf = m_malloc(len);
			snprintf(buf, len, "%s/%s", homedir, inpath+2);
			return buf;
		}
	}

	/* Fallback */
	return m_strdup(inpath);
}

int constant_time_memcmp(const void* a, const void *b, size_t n)
{
	const char *xa = a, *xb = b;
	uint8_t c = 0;
	size_t i;
	for (i = 0; i < n; i++)
	{
		c |= (xa[i] ^ xb[i]);
	}
	return c;
}

/* higher-resolution monotonic timestamp, falls back to gettimeofday */
void gettime_wrapper(struct timespec *now) {
	struct timeval tv;
#if DROPBEAR_FUZZ
	if (fuzz.fuzzing) {
		/* time stands still when fuzzing */
		now->tv_sec = 5;
		now->tv_nsec = 0;
	}
#endif

#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
	/* POSIX monotonic clock. Newer Linux, BSD, MacOSX >10.12 */
	if (clock_gettime(CLOCK_MONOTONIC, now) == 0) {
		return;
	}
#endif

#if defined(__linux__) && defined(SYS_clock_gettime)
	{
	/* Old linux toolchain - kernel might support it but not the build headers */
	/* Also glibc <2.17 requires -lrt which we neglect to add */
	static int linux_monotonic_failed = 0;
	if (!linux_monotonic_failed) {
		/* CLOCK_MONOTONIC isn't in some headers */
		int clock_source_monotonic = 1; 
		if (syscall(SYS_clock_gettime, clock_source_monotonic, now) == 0) {
			return;
		} else {
			/* Don't try again */
			linux_monotonic_failed = 1;



( run in 0.863 second using v1.01-cache-2.11-cpan-39bf76dae61 )