Blame library/adutil.c

Packit Service 6d40f9
/*
Packit Service 6d40f9
 * adcli
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * Copyright (C) 2012 Red Hat Inc.
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * This program is free software; you can redistribute it and/or modify
Packit Service 6d40f9
 * it under the terms of the GNU Lesser General Public License as
Packit Service 6d40f9
 * published by the Free Software Foundation; either version 2.1 of
Packit Service 6d40f9
 * the License, or (at your option) any later version.
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * This program is distributed in the hope that it will be useful, but
Packit Service 6d40f9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 6d40f9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 6d40f9
 * Lesser General Public License for more details.
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * You should have received a copy of the GNU Lesser General Public
Packit Service 6d40f9
 * License along with this program; if not, write to the Free Software
Packit Service 6d40f9
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
Packit Service 6d40f9
 * MA 02110-1301 USA
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * Author: Stef Walter <stefw@gnome.org>
Packit Service 6d40f9
 */
Packit Service 6d40f9
Packit Service 6d40f9
#include "config.h"
Packit Service 6d40f9
Packit Service 6d40f9
#include "adcli.h"
Packit Service 6d40f9
#include "adprivate.h"
Packit Service 6d40f9
#include "seq.h"
Packit Service 6d40f9
Packit Service 6d40f9
#include <assert.h>
Packit Service 6d40f9
#include <ctype.h>
Packit Service 6d40f9
#include <errno.h>
Packit Service 6d40f9
#include <stdio.h>
Packit Service 6d40f9
#include <stdlib.h>
Packit Service 6d40f9
#include <string.h>
Packit Service 6d40f9
#include <unistd.h>
Packit Service 6d40f9
#include <stdint.h>
Packit Service 6d40f9
#include <time.h>
Packit Service 637682
#include <sys/wait.h>
Packit Service 6d40f9
Packit Service 6d40f9
static adcli_message_func message_func = NULL;
Packit Service 6d40f9
static char last_error[2048] = { 0, };
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
_adcli_precond_failed (const char *message,
Packit Service 6d40f9
                       ...)
Packit Service 6d40f9
{
Packit Service 6d40f9
	va_list va;
Packit Service 6d40f9
	const char *env;
Packit Service 6d40f9
Packit Service 6d40f9
	va_start (va, message);
Packit Service 6d40f9
	vfprintf (stderr, message, va);
Packit Service 6d40f9
	va_end (va);
Packit Service 6d40f9
Packit Service 6d40f9
	env = getenv ("ADCLI_STRICT");
Packit Service 6d40f9
	if (env != NULL && env[0] != '\0')
Packit Service 6d40f9
		abort ();
Packit Service 6d40f9
Packit Service 6d40f9
	/* Let coverity know we're not supposed to return from here */
Packit Service 6d40f9
#ifdef __COVERITY__
Packit Service 6d40f9
	abort();
Packit Service 6d40f9
#endif
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
const char *
Packit Service 6d40f9
adcli_result_to_string (adcli_result res)
Packit Service 6d40f9
{
Packit Service 6d40f9
	switch (res) {
Packit Service 6d40f9
	case ADCLI_SUCCESS:
Packit Service 6d40f9
		return "Success";
Packit Service 6d40f9
	case ADCLI_ERR_UNEXPECTED:
Packit Service 6d40f9
		return "Unexpected or internal system error";
Packit Service 6d40f9
	case ADCLI_ERR_DIRECTORY:
Packit Service 6d40f9
		return "Problem with the Active Directory or connecting to it";
Packit Service 6d40f9
	case ADCLI_ERR_CREDENTIALS:
Packit Service 6d40f9
		return "The administrative credentials are invalid or access is denied";
Packit Service 6d40f9
	case ADCLI_ERR_CONFIG:
Packit Service 6d40f9
		return "The local system has an invalid configuration";
Packit Service 6d40f9
	case ADCLI_ERR_FAIL:
Packit Service 6d40f9
		return "Generic failure";
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return_val_if_reached ("Unknown error");
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
messagev (adcli_message_type type,
Packit Service 6d40f9
          const char *format,
Packit Service 6d40f9
          va_list va)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char buffer[sizeof (last_error)];
Packit Service 6d40f9
	char *where = buffer;
Packit Service 6d40f9
	int ret;
Packit Service 6d40f9
Packit Service 6d40f9
	if (type == ADCLI_MESSAGE_ERROR)
Packit Service 6d40f9
		where = last_error;
Packit Service 6d40f9
	else if (message_func == NULL)
Packit Service 6d40f9
		return;
Packit Service 6d40f9
Packit Service 6d40f9
	ret = vsnprintf (where, sizeof (buffer), format, va);
Packit Service 6d40f9
	return_if_fail (ret >= 0);
Packit Service 6d40f9
Packit Service 6d40f9
	if (message_func != NULL)
Packit Service 6d40f9
		(message_func) (type, where);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
_adcli_err (const char *format,
Packit Service 6d40f9
            ...)
Packit Service 6d40f9
{
Packit Service 6d40f9
	va_list va;
Packit Service 6d40f9
	va_start (va, format);
Packit Service 6d40f9
	messagev (ADCLI_MESSAGE_ERROR, format, va);
Packit Service 6d40f9
	va_end (va);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
_adcli_warn (const char *format,
Packit Service 6d40f9
             ...)
Packit Service 6d40f9
{
Packit Service 6d40f9
	va_list va;
Packit Service 6d40f9
	va_start (va, format);
Packit Service 6d40f9
	messagev (ADCLI_MESSAGE_ERROR, format, va);
Packit Service 6d40f9
	va_end (va);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
_adcli_info (const char *format,
Packit Service 6d40f9
             ...)
Packit Service 6d40f9
{
Packit Service 6d40f9
	va_list va;
Packit Service 6d40f9
	va_start (va, format);
Packit Service 6d40f9
	messagev (ADCLI_MESSAGE_INFO, format, va);
Packit Service 6d40f9
	va_end (va);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
adcli_set_message_func (adcli_message_func func)
Packit Service 6d40f9
{
Packit Service 6d40f9
	message_func = func;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
const char *
Packit Service 6d40f9
adcli_get_last_error (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	return last_error[0] ? last_error : NULL;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
adcli_clear_last_error (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	last_error[0] = '\0';
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
_adcli_strv_free (char **strv)
Packit Service 6d40f9
{
Packit Service 6d40f9
	seq_free (strv, free);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
char **
Packit Service 6d40f9
_adcli_strv_dup (char **strv)
Packit Service 6d40f9
{
Packit Service 6d40f9
	int count;
Packit Service 6d40f9
Packit Service 6d40f9
	if (!strv)
Packit Service 6d40f9
		return NULL;
Packit Service 6d40f9
Packit Service 6d40f9
	count = seq_count (strv);
Packit Service 6d40f9
	return seq_dup (strv, &count, (seq_copy)strdup);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
char *
Packit Service 6d40f9
_adcli_strv_join (char **strv,
Packit Service 6d40f9
                  const char *delim)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char *result = NULL;
Packit Service 6d40f9
	int at = 0;
Packit Service 6d40f9
	int dlen;
Packit Service 6d40f9
	int slen;
Packit Service 6d40f9
	int i;
Packit Service 6d40f9
Packit Service 6d40f9
	dlen = strlen (delim);
Packit Service 6d40f9
	for (i = 0; strv && strv[i] != NULL; i++) {
Packit Service 6d40f9
		slen = strlen (strv[i]);
Packit Service 6d40f9
		result = realloc (result, at + dlen + slen + 1);
Packit Service 6d40f9
		return_val_if_fail (result != NULL, NULL);
Packit Service 6d40f9
		if (at != 0) {
Packit Service 6d40f9
			memcpy (result + at, delim, dlen);
Packit Service 6d40f9
			at += dlen;
Packit Service 6d40f9
		}
Packit Service 6d40f9
Packit Service 6d40f9
		memcpy (result + at, strv[i], slen);
Packit Service 6d40f9
		at += slen;
Packit Service 6d40f9
		result[at] = '\0';
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return result;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_strv_len (char **strv)
Packit Service 6d40f9
{
Packit Service 6d40f9
	return seq_count (strv);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
char **
Packit Service 6d40f9
_adcli_strv_add (char **strv,
Packit Service 6d40f9
                 char *string,
Packit Service 6d40f9
                 int *length)
Packit Service 6d40f9
{
Packit Service 6d40f9
	int len;
Packit Service 6d40f9
Packit Service 6d40f9
	return_val_if_fail (string != NULL, strv);
Packit Service 6d40f9
Packit Service 6d40f9
	if (!length) {
Packit Service 6d40f9
		len = seq_count (strv);
Packit Service 6d40f9
		length = &len;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return seq_push (strv, length, string);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_strv_has (char **strv,
Packit Service 6d40f9
                 const char *str)
Packit Service 6d40f9
{
Packit Service 6d40f9
	int i;
Packit Service 6d40f9
Packit Service 6d40f9
	for (i = 0; strv && strv[i] != NULL; i++) {
Packit Service 6d40f9
		if (strcmp (strv[i], str) == 0)
Packit Service 6d40f9
			return 1;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return 0;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
_adcli_str_up (char *str)
Packit Service 6d40f9
{
Packit Service 6d40f9
	while (*str != '\0') {
Packit Service 6d40f9
		*str = toupper (*str);
Packit Service 6d40f9
		str++;
Packit Service 6d40f9
	}
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_str_is_up (const char *str)
Packit Service 6d40f9
{
Packit Service 6d40f9
	while (*str != '\0') {
Packit Service 6d40f9
		if (*str != toupper (*str))
Packit Service 6d40f9
			return 0;
Packit Service 6d40f9
		str++;
Packit Service 6d40f9
	}
Packit Service 6d40f9
	return 1;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
_adcli_str_down (char *str)
Packit Service 6d40f9
{
Packit Service 6d40f9
	while (*str != '\0') {
Packit Service 6d40f9
		*str = tolower (*str);
Packit Service 6d40f9
		str++;
Packit Service 6d40f9
	}
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
_adcli_str_set (char **field,
Packit Service 6d40f9
                const char *value)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char *newval = NULL;
Packit Service 6d40f9
Packit Service 6d40f9
	if (value) {
Packit Service 6d40f9
		newval = strdup (value);
Packit Service 6d40f9
		return_if_fail (newval != NULL);
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	free (*field);
Packit Service 6d40f9
	*field = newval;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
_adcli_strv_set (char ***field,
Packit Service 6d40f9
                 const char **value)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char **newval = NULL;
Packit Service 6d40f9
Packit Service 6d40f9
	if (value) {
Packit Service 6d40f9
		newval = _adcli_strv_dup ((char **)value);
Packit Service 6d40f9
		return_if_fail (newval != NULL);
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	_adcli_strv_free (*field);
Packit Service 6d40f9
	*field = newval;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
char *
Packit Service 384833
_adcli_bin_sid_to_str (const uint8_t *data,
Packit Service 384833
                       size_t len)
Packit Service 384833
{
Packit Service 384833
	uint8_t sid_rev_num;
Packit Service 384833
	int8_t num_auths;
Packit Service 384833
	uint8_t id_auth[6];
Packit Service 384833
	uint32_t id_auth_val;
Packit Service 384833
	uint32_t sub_auths[15];
Packit Service 384833
	uint32_t val;
Packit Service 384833
	size_t p = 0;
Packit Service 384833
	size_t c;
Packit Service 384833
	int nc;
Packit Service 384833
	char *sid_buf;
Packit Service 384833
	size_t sid_buf_len;
Packit Service 384833
Packit Service 384833
	if (data == NULL || len < 8) {
Packit Service 384833
		return NULL;
Packit Service 384833
	}
Packit Service 384833
Packit Service 384833
	sid_rev_num = (uint8_t) data [p];
Packit Service 384833
	p++;
Packit Service 384833
Packit Service 384833
	num_auths = (int8_t) data[p];
Packit Service 384833
	p++;
Packit Service 384833
Packit Service 384833
	if (num_auths > 15 || len < 8 + (num_auths * sizeof (uint32_t))) {
Packit Service 384833
		return NULL;
Packit Service 384833
	}
Packit Service 384833
Packit Service 384833
	for (c = 0; c < 6; c++) {
Packit Service 384833
		id_auth[c] = (uint8_t) data[p];
Packit Service 384833
		p++;
Packit Service 384833
	}
Packit Service 384833
Packit Service 384833
	/* Only 32bits are used for the string representation */
Packit Service 384833
	id_auth_val = (id_auth[2] << 24) +
Packit Service 384833
	              (id_auth[3] << 16) +
Packit Service 384833
	              (id_auth[4] << 8) +
Packit Service 384833
	              (id_auth[5]);
Packit Service 384833
Packit Service 384833
	for (c = 0; c < num_auths; c++) {
Packit Service 384833
		memcpy (&val, data + p, sizeof (uint32_t));
Packit Service 384833
		sub_auths[c] = le32toh (val);
Packit Service 384833
Packit Service 384833
		p += sizeof (uint32_t);
Packit Service 384833
	}
Packit Service 384833
Packit Service 384833
	sid_buf_len = 17 + (num_auths * 11);
Packit Service 384833
	sid_buf = calloc (1, sid_buf_len);
Packit Service 384833
	if (sid_buf == NULL) {
Packit Service 384833
		return NULL;
Packit Service 384833
	}
Packit Service 384833
Packit Service 384833
	nc = snprintf (sid_buf, sid_buf_len, "S-%u-%lu", sid_rev_num,
Packit Service 384833
	              (unsigned long) id_auth_val);
Packit Service 384833
	if (nc < 0 || nc >= sid_buf_len) {
Packit Service 384833
		free (sid_buf);
Packit Service 384833
		return NULL;
Packit Service 384833
	}
Packit Service 384833
Packit Service 384833
	p = 0;
Packit Service 384833
	for (c = 0; c < num_auths; c++) {
Packit Service 384833
		p += nc;
Packit Service 384833
		sid_buf_len -= nc;
Packit Service 384833
Packit Service 384833
		nc = snprintf (sid_buf + p, sid_buf_len, "-%lu",
Packit Service 384833
		               (unsigned long) sub_auths[c]);
Packit Service 384833
		if (nc < 0 || nc >= sid_buf_len) {
Packit Service 384833
			free (sid_buf);
Packit Service 384833
			return NULL;
Packit Service 384833
		}
Packit Service 384833
	}
Packit Service 384833
Packit Service 384833
	return sid_buf;
Packit Service 384833
}
Packit Service 384833
Packit Service 384833
char *
Packit Service 6d40f9
_adcli_str_dupn (void *data,
Packit Service 6d40f9
                 size_t len)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char *result;
Packit Service 6d40f9
Packit Service 6d40f9
	result = malloc (len + 1);
Packit Service 6d40f9
	return_val_if_fail (result, NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	memcpy (result, data, len);
Packit Service 6d40f9
	result[len] = '\0';
Packit Service 6d40f9
	return result;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_str_has_prefix (const char *str,
Packit Service 6d40f9
                       const char *prefix)
Packit Service 6d40f9
{
Packit Service 6d40f9
	size_t len = strlen (str);
Packit Service 6d40f9
	size_t lp = strlen (prefix);
Packit Service 6d40f9
	return (len >= lp && strncmp (str, prefix, lp) == 0);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_str_has_suffix (const char *str,
Packit Service 6d40f9
                       const char *suffix)
Packit Service 6d40f9
{
Packit Service 6d40f9
	size_t len = strlen (str);
Packit Service 6d40f9
	size_t ls = strlen (suffix);
Packit Service 6d40f9
	return (len >= ls && strncmp (str + (len - ls), suffix, ls) == 0);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_password_free (char *password)
Packit Service 6d40f9
{
Packit Service 6d40f9
	int ret;
Packit Service 6d40f9
Packit Service 6d40f9
	if (password == NULL)
Packit Service 6d40f9
		return 0;
Packit Service 6d40f9
Packit Service 6d40f9
	ret = adcli_mem_clear (password, strlen (password));
Packit Service 6d40f9
	free (password);
Packit Service 6d40f9
	return ret;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
adcli_mem_clear (void *data,
Packit Service 6d40f9
                 size_t length)
Packit Service 6d40f9
{
Packit Service 6d40f9
	volatile char *vp;
Packit Service 6d40f9
	int ret = 0;
Packit Service 6d40f9
Packit Service 6d40f9
	if (data == NULL)
Packit Service 6d40f9
		return 0;
Packit Service 6d40f9
Packit Service 6d40f9
	/*
Packit Service 6d40f9
	 * Cracktastic stuff here to help compilers not
Packit Service 6d40f9
	 * optimize this away
Packit Service 6d40f9
	 */
Packit Service 6d40f9
Packit Service 6d40f9
	vp = (volatile char*)data;
Packit Service 6d40f9
	while (length) {
Packit Service 6d40f9
		*vp = 0xAA;
Packit Service 6d40f9
		ret += *vp;
Packit Service 6d40f9
		vp++;
Packit Service 6d40f9
		length--;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return ret;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_write_all (int fd,
Packit Service 6d40f9
                  const char *buf,
Packit Service 6d40f9
                  int len)
Packit Service 6d40f9
{
Packit Service 6d40f9
	int res;
Packit Service 6d40f9
Packit Service 6d40f9
	if (len == -1)
Packit Service 6d40f9
		len = strlen (buf);
Packit Service 6d40f9
Packit Service 6d40f9
	while (len > 0) {
Packit Service 6d40f9
		res = write (fd, buf, len);
Packit Service 6d40f9
		if (res <= 0) {
Packit Service 6d40f9
			if (errno == EAGAIN || errno == EINTR)
Packit Service 6d40f9
				continue;
Packit Service 6d40f9
			return -errno;
Packit Service 6d40f9
		} else  {
Packit Service 6d40f9
			len -= res;
Packit Service 6d40f9
			buf += res;
Packit Service 6d40f9
		}
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return 0;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
#define AD_TO_UNIX_TIME_CONST 11644473600LL
Packit Service 6d40f9
Packit Service 6d40f9
bool
Packit Service 6d40f9
_adcli_check_nt_time_string_lifetime (const char *nt_time_string,
Packit Service 6d40f9
                                      unsigned int lifetime)
Packit Service 6d40f9
{
Packit Service 6d40f9
	uint64_t nt_now;
Packit Service 6d40f9
	unsigned long long int pwd_last_set;
Packit Service 6d40f9
	char *endptr;
Packit Service 6d40f9
	time_t now;
Packit Service 6d40f9
Packit Service 6d40f9
	if (nt_time_string == NULL) {
Packit Service 6d40f9
		_adcli_err ("Missing NT time string, assuming it is expired");
Packit Service 6d40f9
		return false;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	if (lifetime == 0) {
Packit Service 6d40f9
		_adcli_info ("Password lifetime is 0, forcing renewal");
Packit Service 6d40f9
		return false;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	now = time (NULL);
Packit Service 6d40f9
	/* NT timestamps start at 1601-01-01 and use a 100ns base */
Packit Service 6d40f9
	nt_now = (now + AD_TO_UNIX_TIME_CONST) * 1000 * 1000 * 10;
Packit Service 6d40f9
	errno = 0;
Packit Service 6d40f9
	pwd_last_set = strtoull (nt_time_string, &endptr, 10);
Packit Service 6d40f9
	if (errno != 0 || *endptr != '\0' || endptr == nt_time_string) {
Packit Service 6d40f9
		_adcli_err ("Failed to convert NT time string, assuming it is expired");
Packit Service 6d40f9
		return false;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	if (pwd_last_set + (lifetime * 24ULL * 60 * 60 \
Packit Service 6d40f9
				* 1000 * 1000 * 10) > nt_now) {
Packit Service 6d40f9
		_adcli_info ("Password not too old, no change needed");
Packit Service 6d40f9
		return true;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return false;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 637682
adcli_result
Packit Service 637682
_adcli_call_external_program (const char *binary, char * const *argv,
Packit Service 637682
                              const char *stdin_data,
Packit Service 637682
                              uint8_t **stdout_data, size_t *stdout_data_len)
Packit Service 637682
{
Packit Service 637682
	int ret;
Packit Service 637682
	int pipefd_to_child[2] = { -1, -1};
Packit Service 637682
	int pipefd_from_child[2] = { -1, -1};
Packit Service 637682
	pid_t child_pid = 0;
Packit Service 637682
	int err;
Packit Service 637682
	size_t len;
Packit Service 637682
	ssize_t rlen;
Packit Service 637682
	pid_t wret;
Packit Service 637682
	int status;
Packit Service 637682
	uint8_t read_buf[4096];
Packit Service 637682
	uint8_t *out;
Packit Service 637682
Packit Service 637682
	errno = 0;
Packit Service 637682
	ret = access (binary, X_OK);
Packit Service 637682
	if (ret != 0) {
Packit Service 637682
		err = errno;
Packit Service 637682
		_adcli_err ("Cannot run [%s]: [%d][%s].", binary, err,
Packit Service 637682
		                                          strerror (err));
Packit Service 637682
		ret = ADCLI_ERR_FAIL;
Packit Service 637682
		goto done;
Packit Service 637682
	}
Packit Service 637682
Packit Service 637682
	ret = pipe (pipefd_from_child);
Packit Service 637682
	if (ret == -1) {
Packit Service 637682
		err = errno;
Packit Service 637682
		_adcli_err ("pipe failed [%d][%s].", err, strerror (err));
Packit Service 637682
		ret = ADCLI_ERR_FAIL;
Packit Service 637682
		goto done;
Packit Service 637682
	}
Packit Service 637682
Packit Service 637682
	ret = pipe (pipefd_to_child);
Packit Service 637682
	if (ret == -1) {
Packit Service 637682
		err = errno;
Packit Service 637682
		_adcli_err ("pipe failed [%d][%s].", err, strerror (err));
Packit Service 637682
		ret = ADCLI_ERR_FAIL;
Packit Service 637682
		goto done;
Packit Service 637682
	}
Packit Service 637682
Packit Service 637682
	child_pid = fork ();
Packit Service 637682
Packit Service 637682
	if (child_pid == 0) { /* child */
Packit Service 637682
		close (pipefd_to_child[1]);
Packit Service 637682
		ret = dup2 (pipefd_to_child[0], STDIN_FILENO);
Packit Service 637682
		if (ret == -1) {
Packit Service 637682
			err = errno;
Packit Service 637682
			_adcli_err ("dup2 failed [%d][%s].", err,
Packit Service 637682
			                                     strerror (err));
Packit Service 637682
			exit (EXIT_FAILURE);
Packit Service 637682
		}
Packit Service 637682
Packit Service 637682
		close (pipefd_from_child[0]);
Packit Service 637682
		ret = dup2 (pipefd_from_child[1], STDOUT_FILENO);
Packit Service 637682
		if (ret == -1) {
Packit Service 637682
			err = errno;
Packit Service 637682
			_adcli_err ("dup2 failed [%d][%s].", err,
Packit Service 637682
			                                     strerror (err));
Packit Service 637682
			exit (EXIT_FAILURE);
Packit Service 637682
		}
Packit Service 637682
Packit Service 637682
		execv (binary, argv);
Packit Service 637682
		_adcli_err ("Failed to run %s.", binary);
Packit Service 637682
		ret = ADCLI_ERR_FAIL;
Packit Service 637682
		goto done;
Packit Service 637682
	} else if (child_pid > 0) { /* parent */
Packit Service 637682
Packit Service 637682
		if (stdin_data != NULL) {
Packit Service 637682
			len = strlen (stdin_data);
Packit Service 637682
			ret = write (pipefd_to_child[1], stdin_data, len);
Packit Service 637682
			if (ret != len) {
Packit Service 637682
				_adcli_err ("Failed to send computer account password "
Packit Service 637682
				            "to net command.");
Packit Service 637682
				ret = ADCLI_ERR_FAIL;
Packit Service 637682
				goto done;
Packit Service 637682
			}
Packit Service 637682
		}
Packit Service 637682
Packit Service 637682
		close (pipefd_to_child[0]);
Packit Service 637682
		pipefd_to_child[0] = -1;
Packit Service 637682
		close (pipefd_to_child[1]);
Packit Service 637682
		pipefd_to_child[0] = -1;
Packit Service 637682
Packit Service 637682
		if (stdout_data != NULL || stdout_data_len != NULL) {
Packit Service 637682
			rlen = read (pipefd_from_child[0], read_buf, sizeof (read_buf));
Packit Service 637682
			if (rlen < 0) {
Packit Service 637682
				ret = errno;
Packit Service 637682
				_adcli_err ("Failed to read from child [%d][%s].\n",
Packit Service 637682
				            ret, strerror (ret));
Packit Service 637682
				ret = ADCLI_ERR_FAIL;
Packit Service 637682
				goto done;
Packit Service 637682
			}
Packit Service 637682
Packit Service 637682
			out = malloc (sizeof(uint8_t) * rlen);
Packit Service 637682
			if (out == NULL) {
Packit Service 637682
				_adcli_err ("Failed to allocate memory "
Packit Service 637682
				            "for child output.");
Packit Service 637682
				ret = ADCLI_ERR_FAIL;
Packit Service 637682
				goto done;
Packit Service 637682
			} else {
Packit Service 637682
				memcpy (out, read_buf, rlen);
Packit Service 637682
			}
Packit Service 637682
Packit Service 637682
			if (stdout_data != NULL) {
Packit Service 637682
				*stdout_data = out;
Packit Service 637682
			} else {
Packit Service 637682
				free (out);
Packit Service 637682
			}
Packit Service 637682
Packit Service 637682
			if (stdout_data_len != NULL) {
Packit Service 637682
				*stdout_data_len = rlen;
Packit Service 637682
			}
Packit Service 637682
		}
Packit Service 637682
Packit Service 637682
	} else {
Packit Service 637682
		_adcli_err ("Cannot run net command.");
Packit Service 637682
		ret = ADCLI_ERR_FAIL;
Packit Service 637682
		goto done;
Packit Service 637682
	}
Packit Service 637682
Packit Service 637682
	ret = ADCLI_SUCCESS;
Packit Service 637682
Packit Service 637682
done:
Packit Service 637682
	if (pipefd_from_child[0] != -1) {
Packit Service 637682
		close (pipefd_from_child[0]);
Packit Service 637682
	}
Packit Service 637682
	if (pipefd_from_child[1] != -1) {
Packit Service 637682
		close (pipefd_from_child[1]);
Packit Service 637682
	}
Packit Service 637682
	if (pipefd_to_child[0] != -1) {
Packit Service 637682
		close (pipefd_to_child[0]);
Packit Service 637682
	}
Packit Service 637682
	if (pipefd_to_child[1] != -1) {
Packit Service 637682
		close (pipefd_to_child[1]);
Packit Service 637682
	}
Packit Service 637682
Packit Service 637682
	if (child_pid > 0) {
Packit Service 637682
		wret = waitpid (child_pid, &status, 0);
Packit Service 637682
		if (wret == -1) {
Packit Service 637682
			_adcli_err ("No sure what happend to net command.");
Packit Service 637682
		} else {
Packit Service 637682
			if (WIFEXITED (status)) {
Packit Service 637682
				_adcli_err ("net command failed with %d.",
Packit Service 637682
				            WEXITSTATUS (status));
Packit Service 637682
			}
Packit Service 637682
		}
Packit Service 637682
	}
Packit Service 637682
Packit Service 637682
	return ret;
Packit Service 637682
}
Packit Service 637682
Packit Service 637682
Packit Service 6d40f9
#ifdef UTIL_TESTS
Packit Service 6d40f9
Packit Service 6d40f9
#include "test.h"
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
test_strv_add_free (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char **strv = NULL;
Packit Service 6d40f9
Packit Service 6d40f9
	strv = _adcli_strv_add (strv, strdup ("one"), NULL);
Packit Service 6d40f9
	strv = _adcli_strv_add (strv, strdup ("two"), NULL);
Packit Service 6d40f9
	strv = _adcli_strv_add (strv, strdup ("three"), NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	assert_str_eq (strv[0], "one");
Packit Service 6d40f9
	assert_str_eq (strv[1], "two");
Packit Service 6d40f9
	assert_str_eq (strv[2], "three");
Packit Service 6d40f9
	assert (strv[3] == NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	_adcli_strv_free (strv);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
test_strv_dup (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char *values[] = { "one", "two", "three", NULL };
Packit Service 6d40f9
	char **strv;
Packit Service 6d40f9
Packit Service 6d40f9
	strv = _adcli_strv_dup (values);
Packit Service 6d40f9
Packit Service 6d40f9
	assert_str_eq (strv[0], "one");
Packit Service 6d40f9
	assert_str_eq (strv[1], "two");
Packit Service 6d40f9
	assert_str_eq (strv[2], "three");
Packit Service 6d40f9
	assert (strv[3] == NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	_adcli_strv_free (strv);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
test_strv_count (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char *values[] = { "one", "two", "three", NULL };
Packit Service 6d40f9
	int len;
Packit Service 6d40f9
Packit Service 6d40f9
	len = _adcli_strv_len (values);
Packit Service 6d40f9
	assert_num_eq (len, 3);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
test_check_nt_time_string_lifetime (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char *time_str;
Packit Service 6d40f9
Packit Service 6d40f9
	/* Missing or invalid value */
Packit Service 6d40f9
	assert (!_adcli_check_nt_time_string_lifetime (NULL, 0));
Packit Service 6d40f9
	assert (!_adcli_check_nt_time_string_lifetime ("", 0));
Packit Service 6d40f9
	assert (!_adcli_check_nt_time_string_lifetime ("a", 0));
Packit Service 6d40f9
	assert (!_adcli_check_nt_time_string_lifetime ("1a", 0));
Packit Service 6d40f9
Packit Service 6d40f9
	/* Certainly expired*/
Packit Service 6d40f9
	assert (!_adcli_check_nt_time_string_lifetime ("0", 0));
Packit Service 6d40f9
Packit Service 6d40f9
	/* 1969-01-01T00:00:00: 116129340000000000 */
Packit Service 6d40f9
	/* Calculated with PowerShell:
Packit Service 6d40f9
	 * (Get-Date -Date "1969-01-01T00:00:00").ToFileTime() */
Packit Service 6d40f9
Packit Service 6d40f9
	assert (!_adcli_check_nt_time_string_lifetime ("130645404000000000", 1));
Packit Service 6d40f9
Packit Service 6d40f9
	/* Make sure lifetime==0 will retrun false even if pwdLastSet is in the future */
Packit Service 6d40f9
	assert (asprintf (&time_str, "%llu",
Packit Service 6d40f9
			  (time (NULL) + 10 + AD_TO_UNIX_TIME_CONST) * 1000 * 1000 *10)
Packit Service 6d40f9
		!= -1);
Packit Service 6d40f9
	assert (!_adcli_check_nt_time_string_lifetime (time_str, 0));
Packit Service 715eb7
	free (time_str);
Packit Service 6d40f9
Packit Service 6d40f9
	/* This test will fail some time after 2200AD as a reminder to reflect
Packit Service 6d40f9
	 * why adcli is still needed. */
Packit Service 6d40f9
	assert (_adcli_check_nt_time_string_lifetime ("130645404000000000", 100000));
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 384833
static void
Packit Service 384833
test_bin_sid_to_str (void)
Packit Service 384833
{
Packit Service 384833
	uint8_t sid1[] = { 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
Packit Service 384833
	                   0x15, 0x00, 0x00, 0x00, 0xF8, 0x12, 0x13, 0xDC,
Packit Service 384833
	                   0x47, 0xF3, 0x1C, 0x76, 0x47, 0x2F, 0x2E, 0xD7,
Packit Service 384833
	                   0x51, 0x04, 0x00, 0x00 };
Packit Service 384833
Packit Service 384833
	uint8_t sid2[] = { 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
Packit Service 384833
	                   0x15, 0x00, 0x00, 0x00, 0xF8, 0x12, 0x13, 0xDC,
Packit Service 384833
	                   0x47, 0xF3, 0x1C, 0x76, 0x47, 0x2F, 0x2E, 0xD7};
Packit Service 384833
Packit Service 384833
	uint8_t sid3[] = { 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
Packit Service 384833
	                   0x15, 0x00, 0x00, 0x00, 0x29, 0xC9, 0x4F, 0xD9,
Packit Service 384833
	                   0xC2, 0x3C, 0xC3, 0x78, 0x36, 0x55, 0x87, 0xF8};
Packit Service 384833
Packit Service 384833
Packit Service 384833
	char *str;
Packit Service 384833
Packit Service 384833
	str = _adcli_bin_sid_to_str (sid1, sizeof (sid1));
Packit Service 384833
	assert (str != NULL);
Packit Service 384833
	assert (strcmp (str, "S-1-5-21-3692237560-1981608775-3610128199-1105") == 0);
Packit Service 384833
	free (str);
Packit Service 384833
Packit Service 384833
	str = _adcli_bin_sid_to_str (sid2, sizeof (sid2));
Packit Service 384833
	assert (str != NULL);
Packit Service 384833
	assert (strcmp (str, "S-1-5-21-3692237560-1981608775-3610128199") == 0);
Packit Service 384833
	free (str);
Packit Service 384833
Packit Service 384833
	str = _adcli_bin_sid_to_str (sid3, sizeof (sid2));
Packit Service 384833
	assert (str != NULL);
Packit Service 384833
	assert (strcmp (str, "S-1-5-21-3645884713-2026060994-4169618742") == 0);
Packit Service 384833
	free (str);
Packit Service 384833
}
Packit Service 384833
Packit Service 637682
static void
Packit Service 637682
test_call_external_program (void)
Packit Service 637682
{
Packit Service 637682
	adcli_result res;
Packit Service 637682
	char *argv[] = { NULL, NULL, NULL };
Packit Service 637682
	uint8_t *stdout_data;
Packit Service 637682
	size_t stdout_data_len;
Packit Service 637682
Packit Service 637682
	argv[0] = "/does/not/exists";
Packit Service 637682
	res = _adcli_call_external_program (argv[0], argv, NULL, NULL, NULL);
Packit Service 637682
	assert (res == ADCLI_ERR_FAIL);
Packit Service 637682
Packit Service 637682
#ifdef BIN_CAT
Packit Service 637682
	argv[0] = BIN_CAT;
Packit Service 637682
	res = _adcli_call_external_program (argv[0], argv, "Hello",
Packit Service 637682
	                                    &stdout_data, &stdout_data_len);
Packit Service 637682
	assert (res == ADCLI_SUCCESS);
Packit Service 637682
	assert (strncmp ("Hello", (char *) stdout_data, stdout_data_len) == 0);
Packit Service 637682
	free (stdout_data);
Packit Service 637682
Packit Service 637682
	res = _adcli_call_external_program (argv[0], argv, "Hello",
Packit Service 637682
	                                    NULL, NULL);
Packit Service 637682
	assert (res == ADCLI_SUCCESS);
Packit Service 637682
#endif
Packit Service 637682
Packit Service 637682
#ifdef BIN_REV
Packit Service 637682
	argv[0] = BIN_REV;
Packit Service 637682
	res = _adcli_call_external_program (argv[0], argv, "Hello\n",
Packit Service 637682
	                                    &stdout_data, &stdout_data_len);
Packit Service 637682
	assert (res == ADCLI_SUCCESS);
Packit Service 637682
	assert (strncmp ("olleH\n", (char *) stdout_data, stdout_data_len) == 0);
Packit Service 637682
	free (stdout_data);
Packit Service 637682
#endif
Packit Service 637682
Packit Service 637682
#ifdef BIN_TAC
Packit Service 637682
	argv[0] = BIN_TAC;
Packit Service 637682
	res = _adcli_call_external_program (argv[0], argv, "Hello\nWorld\n",
Packit Service 637682
	                                    &stdout_data, &stdout_data_len);
Packit Service 637682
	assert (res == ADCLI_SUCCESS);
Packit Service 637682
	assert (strncmp ("World\nHello\n", (char *) stdout_data, stdout_data_len) == 0);
Packit Service 637682
	free (stdout_data);
Packit Service 637682
#endif
Packit Service 637682
Packit Service 637682
#ifdef BIN_ECHO
Packit Service 637682
	argv[0] = BIN_ECHO;
Packit Service 637682
	argv[1] = "Hello";
Packit Service 637682
	res = _adcli_call_external_program (argv[0], argv, NULL,
Packit Service 637682
	                                    &stdout_data, &stdout_data_len);
Packit Service 637682
	assert (res == ADCLI_SUCCESS);
Packit Service 637682
	assert (strncmp ("Hello\n", (char *) stdout_data, stdout_data_len) == 0);
Packit Service 637682
	free (stdout_data);
Packit Service 637682
#endif
Packit Service 637682
}
Packit Service 637682
Packit Service 6d40f9
int
Packit Service 6d40f9
main (int argc,
Packit Service 6d40f9
      char *argv[])
Packit Service 6d40f9
{
Packit Service 6d40f9
	test_func (test_strv_add_free, "/util/strv_add_free");
Packit Service 6d40f9
	test_func (test_strv_dup, "/util/strv_dup");
Packit Service 6d40f9
	test_func (test_strv_count, "/util/strv_count");
Packit Service 6d40f9
	test_func (test_check_nt_time_string_lifetime, "/util/check_nt_time_string_lifetime");
Packit Service 384833
	test_func (test_bin_sid_to_str, "/util/bin_sid_to_str");
Packit Service 637682
	test_func (test_call_external_program, "/util/call_external_program");
Packit Service 6d40f9
	return test_run (argc, argv);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
#endif /* UTIL_TESTS */