util.c revision 19b4bb652f5cdc2797b66595eaf8811881aa9873
/*
Authors:
Simo Sorce <ssorce@redhat.com>
Copyright (C) 2009 Red Hat
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <netdb.h>
#include <poll.h>
#include <talloc.h>
#include <dhash.h>
#include "util/sss_utf8.h"
{
int ret;
const char *substr_end = str;
const char *substr_begin = str;
int num_strings = 0;
return EINVAL;
}
return ENOMEM;
}
do {
substr_len = 0;
/* If this is not the first substring, then move from the separator. */
}
/* Find end of the first substring */
substr_end++;
substr_len++;
}
if (trim) {
/* Trim leading whitespace */
substr_begin++;
substr_len--;
}
/* Trim trailing whitespace */
substr_end--;
substr_len--;
}
}
/* Copy the substring to the output list of strings */
if (skip_empty == false || substr_len > 0) {
goto done;
}
/* empty string is stored for substr_len == 0 */
goto done;
}
num_strings++;
}
} while (*sep_pos != '\0');
/* No allocations were done, make space for the NULL */
goto done;
}
}
if (size) {
*size = num_strings;
}
done:
return ret;
}
{
int i;
if (args) {
}
}
/* parse a string into arguments.
* arguments are separated by a space
* '\' is an escape character and can be used only to escape
* itself or the white space.
*/
char **parse_args(const char *str)
{
const char *p;
char **ret, **r;
char *tmp;
int num;
int i;
bool e, w;
num = 0;
i = 0;
e = false;
w = false;
p = str;
while (*p) {
if (*p == '\\') {
w = false;
if (e) {
/* if we were already escaping, add a '\' literal */
tmp[i] = '\\';
i++;
e = false;
} else {
/* otherwise just start escaping */
e = true;
}
} else if (isspace(*p)) {
if (e) {
/* Add escaped whitespace literally */
tmp[i] = *p;
i++;
e = false;
} else if (w == false) {
/* If previous character was non-whitespace, arg break */
tmp[i] = '\0';
i++;
w = true;
}
/* previous char was whitespace as well, skip it */
} else {
w = false;
if (e) {
/* Prepend escaped chars with a literal \ */
tmp[i] = '\\';
i++;
e = false;
}
/* Copy character from the source string */
tmp[i] = *p;
i++;
}
p++;
/* check if this was the last char */
if (*p == '\0') {
if (e) {
tmp[i] = '\\';
i++;
e = false;
}
tmp[i] = '\0';
i++;
}
/* check next char and skip multiple spaces */
continue;
}
if (!r) goto fail;
ret = r;
num++;
i = 0;
}
return ret;
fail:
return NULL;
}
{
int i = 0;
int j = 0;
char **dup_list;
if (!str_list) {
return NULL;
}
/* Find the size of the list */
while (str_list[i]) i++;
if (!dup_list) {
return NULL;
}
/* Copy the elements */
for (j = 0; j < i; j++) {
if (!dup_list[j]) {
return NULL;
}
}
/* NULL-terminate the list */
return dup_list;
}
/* Take two string lists (terminated on a NULL char*)
* and return up to three arrays of strings based on
* shared ownership.
*
* Pass NULL to any return type you don't care about
*/
char **_list1,
char **_list2,
char ***_list1_only,
char ***_list2_only,
char ***_both_lists)
{
int error;
int i;
int i2 = 0;
int i12 = 0;
char **list1_only = NULL;
char **list2_only = NULL;
char **both_lists = NULL;
unsigned long count;
if (!tmp_ctx) {
return ENOMEM;
}
if (!_list1) {
if (!list1) {
return ENOMEM;
}
}
else {
}
if (!_list2) {
if (!list2) {
return ENOMEM;
}
}
else {
}
if (error != HASH_SUCCESS) {
return EIO;
}
/* Add all entries from list 1 into a hash table */
i = 0;
while (list1[i]) {
if (error != HASH_SUCCESS) {
goto done;
}
i++;
}
/* Iterate through list 2 and remove matching items */
i = 0;
while (list2[i]) {
if (error == HASH_SUCCESS) {
if (_both_lists) {
/* String was present in both lists */
i12++;
if (!both_lists) {
goto done;
}
goto done;
}
}
}
else if (error == HASH_ERROR_KEY_NOT_FOUND) {
if (_list2_only) {
/* String was present only in list2 */
i2++;
char *, i2+1);
if (!list2_only) {
goto done;
}
goto done;
}
}
}
else {
/* An error occurred */
goto done;
}
i++;
}
/* Get the leftover entries in the hash table */
if (_list1_only) {
if (error != HASH_SUCCESS) {
goto done;
}
if (!list1_only) {
goto done;
}
for (i = 0; i < count; i++) {
if (!list1_only[i]) {
goto done;
}
}
}
if (_list2_only) {
if (list2_only) {
}
else {
if (!(*_list2_only)) {
goto done;
}
*_list2_only[0] = NULL;
}
}
if (_both_lists) {
if (both_lists) {
}
else {
if (!(*_both_lists)) {
goto done;
}
*_both_lists[0] = NULL;
}
}
done:
return ret;
}
{
}
{
}
unsigned long count,
hash_table_t **tbl,
unsigned int directory_bits,
unsigned int segment_bits,
unsigned long min_load_factor,
unsigned long max_load_factor,
void *delete_private_data)
{
int hret;
if (!internal_ctx) {
return ENOMEM;
}
switch (hret) {
case HASH_SUCCESS:
/* Steal the table pointer onto the mem_ctx,
* then make the internal_ctx a child of
* table.
*
* This way, we can clean up the values when
* we talloc_free() the table
*/
return EOK;
case HASH_ERROR_NO_MEMORY:
break;
default:
}
DEBUG(0, ("Could not create hash table: [%d][%s]\n",
return ret;
}
hash_table_t **tbl)
{
}
const char *input,
char **sanitized)
{
char *output;
size_t i = 0;
size_t j = 0;
/* Assume the worst-case. We'll resize it later, once */
if (!output) {
return ENOMEM;
}
while (input[i]) {
switch(input[i]) {
case '*':
output[j++] = '\\';
output[j++] = '2';
output[j++] = 'a';
break;
case '(':
output[j++] = '\\';
output[j++] = '2';
output[j++] = '8';
break;
case ')':
output[j++] = '\\';
output[j++] = '2';
output[j++] = '9';
break;
case '\\':
output[j++] = '\\';
output[j++] = '5';
output[j++] = 'c';
break;
default:
}
i++;
}
output[j] = '\0';
if (!*sanitized) {
return ENOMEM;
}
return EOK;
}
char *
{
}
/* out->len includes terminating '\0' */
{
} else {
}
}
/* This function only removes first and last
* character if the first character was '['.
*
* NOTE: This means, that ipv6addr must NOT be followed
* by port number.
*/
remove_ipv6_brackets(char *ipv6addr)
{
if (len < 3) {
return EINVAL;
}
}
return EOK;
}
char ***list_p)
{
size_t c;
return EINVAL;
}
/* If the input is a NULL list a new one is created with the new
* string and the terminating NULL element. */
c = 0;
} else {
/* Allocate one extra space for the new service and one for
* the terminating NULL
*/
}
return ENOMEM;
}
return ENOMEM;
}
return EOK;
}
{
size_t c;
return false;
}
return true;
}
}
return false;
}
{
while (size--) {
*p++ = 0;
}
}
{
const char *s;
char *dn;
char *p;
int l;
return EINVAL;
}
s = domain;
while ((p = strchr(s, '.'))) {
l = p - s;
if (!dn) {
return ENOMEM;
}
s = p + 1;
}
if (!dn) {
return ENOMEM;
}
for (p=dn; *p; ++p) {
*p = tolower(*p);
}
return EOK;
}
{
return true;
}
return true;
}
return false;
}
/* addr is in network order for both IPv4 and IPv6 versions */
{
char straddr[INET_ADDRSTRLEN];
("inet_ntop failed, won't log IP addresses\n"));
}
return false;
} else if ((flags & SSS_NO_LOOPBACK)
return false;
} else if ((flags & SSS_NO_LINKLOCAL)
/* 169.254.0.0/16 */
return false;
} else if ((flags & SSS_NO_BROADCAST)
return false;
}
return true;
}
{
char straddr[INET6_ADDRSTRLEN];
("inet_ntop failed, won't log IP addresses\n"));
}
return false;
return false;
return false;
}
return true;
}