/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2009,
2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/select.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <mailutils/address.h>
#include <mailutils/assoc.h>
#include <mailutils/argcv.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/iterator.h>
#include <mailutils/mutil.h>
#include <mailutils/parse822.h>
#include <mailutils/mu_auth.h>
#include <mailutils/header.h>
#include <mailutils/message.h>
#include <mailutils/envelope.h>
#include <mailutils/nls.h>
#include <mailutils/stream.h>
#include <mailutils/filter.h>
#include <mailutils/sql.h>
#include <mailutils/url.h>
#include <mailutils/io.h>
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
#include <mailutils/sys/registrar.h>
/* convert a sequence of hex characters into an integer */
unsigned long
mu_hex2ul (char hex)
{
if (hex >= '0' && hex <= '9')
return hex - '0';
if (hex >= 'a' && hex <= 'z')
return hex - 'a' + 10;
if (hex >= 'A' && hex <= 'Z')
return hex - 'A' + 10;
return -1;
}
size_t
mu_hexstr2ul (unsigned long *ul, const char *hex, size_t len)
{
size_t r;
*ul = 0;
for (r = 0; r < len; r++)
{
unsigned long v = mu_hex2ul (hex[r]);
if (v == (unsigned long)-1)
return r;
*ul = *ul * 16 + v;
}
return r;
}
char *
mu_get_homedir (void)
{
char *homedir = getenv ("HOME");
if (homedir)
homedir = strdup (homedir);
else
{
struct mu_auth_data *auth = mu_get_auth_by_uid (geteuid ());
if (!auth)
return NULL;
homedir = strdup (auth->dir);
mu_auth_data_free (auth);
}
return homedir;
}
char *
mu_getcwd ()
{
char *ret;
unsigned path_max;
char buf[128];
errno = 0;
ret = getcwd (buf, sizeof (buf));
if (ret != NULL)
return strdup (buf);
if (errno != ERANGE)
return NULL;
path_max = 128;
path_max += 2; /* The getcwd docs say to do this. */
for (;;)
{
char *cwd = (char *) malloc (path_max);
errno = 0;
ret = getcwd (cwd, path_max);
if (ret != NULL)
return ret;
if (errno != ERANGE)
{
int save_errno = errno;
free (cwd);
errno = save_errno;
return NULL;
}
free (cwd);
path_max += path_max / 16;
path_max += 32;
}
/* oops? */
return NULL;
}
char *
mu_get_full_path (const char *file)
{
char *p = NULL;
if (!file)
p = mu_getcwd ();
else if (*file != '/')
{
char *cwd = mu_getcwd ();
if (cwd)
{
p = calloc (strlen (cwd) + 1 + strlen (file) + 1, 1);
if (p)
sprintf (p, "%s/%s", cwd, file);
free (cwd);
}
}
if (!p)
p = strdup (file);
return p;
}
/* NOTE: Allocates Memory. */
/* Expand: ~ --> /home/user and to ~guest --> /home/guest. */
char *
mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
{
char *base = strdup (ref);
char *home = NULL;
char *proto = NULL;
size_t proto_len = 0;
char *p;
for (p = base; *p && isascii (*p) && isalnum (*p); p++)
;
if (*p == ':')
{
p++;
proto_len = p - base;
proto = malloc (proto_len + 1);
if (!proto)
return NULL;
memcpy (proto, base, proto_len);
proto[proto_len] = 0;
/* Allow for extra pair of slashes after the protocol specifier */
if (*p == delim[0])
p++;
if (*p == delim[0])
p++;
}
else
p = base;
if (*p == '~')
{
p++;
if (*p == delim[0] || *p == '\0')
{
char *s;
if (!homedir)
{
home = mu_get_homedir ();
if (!home)
return base;
homedir = home;
}
s = calloc (proto_len + strlen (homedir) + strlen (p) + 1, 1);
if (proto_len)
strcpy (s, proto);
else
s[0] = 0;
strcat (s, homedir);
strcat (s, p);
free (base);
base = s;
}
else
{
struct mu_auth_data *auth;
char *s = p;
char *name;
while (*s && *s != delim[0])
s++;
name = calloc (s - p + 1, 1);
memcpy (name, p, s - p);
name[s - p] = '\0';
auth = mu_get_auth_by_name (name);
free (name);
if (auth)
{
char *buf = calloc (proto_len + strlen (auth->dir)
+ strlen (s) + 1, 1);
if (proto_len)
strcpy (buf, proto);
else
buf[0] = 0;
strcat (buf, auth->dir);
strcat (buf, s);
free (base);
base = buf;
mu_auth_data_free (auth);
}
}
}
if (home)
free (home);
return base;
}
/* Smart strncpy that always add the null and returns the number of bytes
written. */
size_t
mu_cpystr (char *dst, const char *src, size_t size)
{
size_t len = src ? strlen (src) : 0 ;
if (dst == NULL || size == 0)
return len;
if (len >= size)
len = size - 1;
memcpy (dst, src, len);
dst[len] = '\0';
return len;
}
#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 64
#endif
int
mu_get_host_name (char **host)
{
char *hostname = NULL;
size_t size = 0;
char *p;
while (1)
{
if (size == 0)
{
size = MAXHOSTNAMELEN;
p = malloc (size);
}
else
{
size_t ns = size * 2;
if (ns < size)
{
free (hostname);
return ENOMEM;
}
size = ns;
p = realloc (hostname, size);
}
if (!p)
{
free (hostname);
return ENOMEM;
}
hostname = p;
hostname[size - 1] = 0;
if (gethostname (hostname, size - 1) == 0)
{
if (!hostname[size - 1])
break;
}
else if (errno != 0 && errno != ENAMETOOLONG && errno != EINVAL
&& errno != ENOMEM)
{
int rc = errno;
free (hostname);
return rc;
}
}
/* Try to return fully qualified host name */
if (!strchr (hostname, '.'))
{
struct hostent *hp = gethostbyname (hostname);
if (hp)
{
size_t len = strlen (hp->h_name);
if (size < len + 1)
{
p = realloc (hostname, len + 1);
if (!p)
{
free (hostname);
return ENOMEM;
}
hostname = p;
}
strcpy (hostname, hp->h_name);
}
}
*host = hostname;
return 0;
}
/*
* Functions used to convert unix mailbox/user names into RFC822 addr-specs.
*/
static char *mu_user_email = 0;
int
mu_set_user_email
|