Back to index

opendkim  2.6.4
Classes | Defines | Functions | Variables
util.c File Reference
#include "build-config.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <assert.h>
#include <syslog.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <dkim-strl.h>
#include "opendkim.h"
#include "util.h"
#include "opendkim-db.h"

Go to the source code of this file.

Classes

struct  dkimf_dstring

Defines

#define _PATH_DEVNULL   "/dev/null"
#define DEFARGS   8
#define INADDR_NONE   ((uint32_t) -1)

Functions

_Bool dkimf_isblank (char *str)
void dkimf_optlist (FILE *where)
void dkimf_setmaxfd (void)
void dkimf_stripbrackets (char *addr)
void dkimf_lowercase (u_char *str)
_Bool dkimf_checkhost (DKIMF_DB db, char *host)
_Bool dkimf_checkip (DKIMF_DB db, struct sockaddr *ip)
size_t dkimf_inet_ntoa (struct in_addr a, char *buf, size_t buflen)
void dkimf_trimspaces (u_char *str)
void dkimf_stripcr (char *str)
void dkimf_mkpath (char *path, size_t pathlen, char *root, char *file)
_Bool dkimf_hostlist (char *host, char **list)
static _Bool dkimf_dstring_resize (struct dkimf_dstring *dstr, int len)
struct dkimf_dstringdkimf_dstring_new (int len, int maxlen)
void dkimf_dstring_free (struct dkimf_dstring *dstr)
_Bool dkimf_dstring_copy (struct dkimf_dstring *dstr, u_char *str)
_Bool dkimf_dstring_cat (struct dkimf_dstring *dstr, u_char *str)
_Bool dkimf_dstring_cat1 (struct dkimf_dstring *dstr, int c)
_Bool dkimf_dstring_catn (struct dkimf_dstring *dstr, unsigned char *str, size_t nbytes)
u_char * dkimf_dstring_get (struct dkimf_dstring *dstr)
int dkimf_dstring_len (struct dkimf_dstring *dstr)
void dkimf_dstring_blank (struct dkimf_dstring *dstr)
void dkimf_dstring_chop (struct dkimf_dstring *dstr, int len)
size_t dkimf_dstring_printf (struct dkimf_dstring *dstr, char *fmt,...)
int dkimf_socket_cleanup (char *sockspec)
_Bool dkimf_mkregexp (char *src, char *dst, size_t dstlen)
void dkimf_base64_encode_file (int infd, FILE *out, int lm, int rm, int initial)
_Bool dkimf_subdomain (char *d1, char *d2)
void dkimf_ipstring (char *buf, size_t buflen, struct sockaddr_storage *ss)

Variables

static char util_c_id [] = "@(#)$Id: util.c,v 1.47.2.1 2010/10/27 21:43:09 cm-msk Exp $"
static char * optlist []
static unsigned char alphabet [64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

Class Documentation

struct dkimf_dstring

Definition at line 226 of file util.c.

Class Members
int ds_alloc
u_char * ds_buf
int ds_len
int ds_max

Define Documentation

#define _PATH_DEVNULL   "/dev/null"

Definition at line 46 of file util.c.

#define DEFARGS   8

Definition at line 64 of file util.c.

#define INADDR_NONE   ((uint32_t) -1)

Definition at line 68 of file util.c.


Function Documentation

void dkimf_base64_encode_file ( int  infd,
FILE *  out,
int  lm,
int  rm,
int  initial 
)

Definition at line 1601 of file util.c.

{
       int len;
       int bits;
       int c;
       int d;
       int char_count;
       ssize_t rlen;
       char buf[MAXBUFRSZ];

       assert(infd >= 0);
       assert(out != NULL);
       assert(lm >= 0);
       assert(rm >= 0);
       assert(initial >= 0);

       bits = 0;
       char_count = 0;
       len = initial;

       (void) lseek(infd, 0, SEEK_SET);

       for (;;)
       {
              rlen = read(infd, buf, sizeof buf);
              if (rlen == -1)
                     break;

              for (c = 0; c < rlen; c++)
              {
                     bits += buf[c];
                     char_count++;
                     if (char_count == 3)
                     {
                            fputc(alphabet[bits >> 18], out);
                            fputc(alphabet[(bits >> 12) & 0x3f], out);
                            fputc(alphabet[(bits >> 6) & 0x3f], out);
                            fputc(alphabet[bits & 0x3f], out);
                            len += 4;
                            if (rm > 0 && lm > 0 && len >= rm - 4)
                            {
                                   fputc('\n', out);
                                   for (d = 0; d < lm; d++)
                                          fputc(' ', out);
                                   len = lm;
                            }
                            bits = 0;
                            char_count = 0;
                     }
                     else
                     {
                            bits <<= 8;
                     }
              }

              if (rlen < (ssize_t) sizeof buf)
                     break;
       }

       if (char_count != 0)
       {
              if (rm > 0 && lm > 0 && len >= rm - 4)
              {
                     fputc('\n', out);
                     for (d = 0; d < lm; d++)
                            fputc(' ', out);
              }
              bits <<= 16 - (8 * char_count);
              fputc(alphabet[bits >> 18], out);
              fputc(alphabet[(bits >> 12) & 0x3f], out);
              if (char_count == 1)
                     fputc('=', out);
              else
                     fputc(alphabet[(bits >> 6) & 0x3f], out);
              fputc('=', out);
       }
}

Here is the caller graph for this function:

_Bool dkimf_checkhost ( DKIMF_DB  db,
char *  host 
)

Definition at line 394 of file util.c.

{
       _Bool exists;
       int status;
       char *p;
       char buf[BUFRSZ + 1];

       assert(host != NULL);

       /* short circuit */
       if (db == NULL)
              return FALSE;

       /* iterate over the possibilities */
       for (p = host;
            p != NULL;
            p = (p == host ? strchr(host, '.') : strchr(p + 1, '.')))
       {
              /* try the negative case */
              snprintf(buf, sizeof buf, "!%s", p);
              exists = FALSE;
              status = dkimf_db_get(db, buf, 0, NULL, 0, &exists);
              if (status != 0)
                     return FALSE;
              else if (exists)
                     return FALSE;

              /* ...and now the positive case */
              exists = FALSE;
              status = dkimf_db_get(db, &buf[1], 0, NULL, 0, &exists);
              if (status != 0)
                     return FALSE;
              if (exists)
                     return TRUE;
       }

       return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

_Bool dkimf_checkip ( DKIMF_DB  db,
struct sockaddr ip 
)

Definition at line 446 of file util.c.

{
       _Bool exists;
       char ipbuf[DKIM_MAXHOSTNAMELEN + 1];

       assert(ip != NULL);

       /* short circuit */
       if (db == NULL)
              return FALSE;

#if AF_INET6
       if (ip->sa_family == AF_INET6)
       {
              int status;
              int bits;
              size_t dst_len;
              char *dst;
              struct sockaddr_in6 sin6;
              struct in6_addr addr;

              memcpy(&sin6, ip, sizeof sin6);

              memcpy(&addr, &sin6.sin6_addr, sizeof addr);

              memset(ipbuf, '\0', sizeof ipbuf);
              ipbuf[0] = '!';

              dst = &ipbuf[1];
              dst_len = sizeof ipbuf - 1;

              inet_ntop(AF_INET6, &addr, dst, dst_len);
              dkimf_lowercase((u_char *) dst);

              exists = FALSE;

              status = dkimf_db_get(db, ipbuf, 0, NULL, 0, &exists);
              if (status != 0)
                     return FALSE;
              if (exists)
                     return FALSE;

              status = dkimf_db_get(db, &ipbuf[1], 0, NULL, 0,
                                    &exists);
              if (status != 0)
                     return FALSE;
              if (exists)
                     return TRUE;

              /* iterate over possible bitwise expressions */
              for (bits = 0; bits <= 128; bits++)
              {
                     size_t sz;

                     /* try this one */
                     memset(ipbuf, '\0', sizeof ipbuf);
                     ipbuf[0] = '!';

                     dst = &ipbuf[1];
                     dst_len = sizeof ipbuf - 1;

                     inet_ntop(AF_INET6, &addr, dst, dst_len);
                     dkimf_lowercase((u_char *) dst);

                     sz = strlcat(ipbuf, "/", sizeof ipbuf);
                     if (sz >= sizeof ipbuf)
                            return FALSE;

                     dst = &ipbuf[sz];
                     dst_len = sizeof ipbuf - sz;

                     sz = snprintf(dst, dst_len, "%d", 128 - bits);
                     if (sz >= sizeof ipbuf)
                            return FALSE;

                     exists = FALSE;

                     status = dkimf_db_get(db, ipbuf, 0, NULL, 0, &exists);
                     if (status != 0)
                            return FALSE;
                     else if (exists)
                            return FALSE;

                     status = dkimf_db_get(db, &ipbuf[1], 0, NULL, 0,
                                           &exists);
                     if (status != 0)
                            return FALSE;
                     else if (exists)
                            return TRUE;

                     /* flip off a bit */
                     if (bits != 128)
                     {
                            int idx;
                            int bit;

                            idx = 15 - (bits / 8);
                            bit = bits % 8;
                            addr.s6_addr[idx] &= ~(1 << bit);
                     }
              }
       }
#endif /* AF_INET6 */

       if (ip->sa_family == AF_INET)
       {
              _Bool exists;
              int c;
              int status;
              int bits;
              struct in_addr addr;
              struct in_addr mask;
              struct sockaddr_in sin;

              memcpy(&sin, ip, sizeof sin);
              memcpy(&addr.s_addr, &sin.sin_addr, sizeof addr.s_addr);

              /* try the IP address directly */
              exists = FALSE;

              ipbuf[0] = '!';
              (void) dkimf_inet_ntoa(addr, &ipbuf[1], sizeof ipbuf - 1);
              status = dkimf_db_get(db, ipbuf, 0, NULL, 0, &exists);
              if (status != 0)
                     return FALSE;
              if (exists)
                     return FALSE;

              status = dkimf_db_get(db, &ipbuf[1], 0, NULL, 0, &exists);
              if (status != 0)
                     return FALSE;
              if (exists)
                     return TRUE;

              /* iterate over possible bitwise expressions */
              for (bits = 32; bits >= 0; bits--)
              {
                     if (bits == 32)
                     {
                            mask.s_addr = 0xffffffff;
                     }
                     else
                     {
                            mask.s_addr = 0;
                            for (c = 0; c < bits; c++)
                                   mask.s_addr |= htonl(1 << (31 - c));
                     }

                     addr.s_addr = addr.s_addr & mask.s_addr;

                     memset(ipbuf, '\0', sizeof ipbuf);
                     ipbuf[0] = '!';
                     (void) dkimf_inet_ntoa(addr, &ipbuf[1],
                                            sizeof ipbuf - 1);
                     c = strlen(ipbuf);
                     ipbuf[c] = '/';
                     c++;

                     snprintf(&ipbuf[c], sizeof ipbuf - c, "%d", bits);

                     exists = FALSE;
                     status = dkimf_db_get(db, ipbuf, 0, NULL, 0, &exists);
                     if (status != 0)
                            return FALSE;
                     if (exists)
                            return FALSE;

                     status = dkimf_db_get(db, &ipbuf[1], 0, NULL, 0,
                                           &exists);
                     if (status != 0)
                            return FALSE;
                     if (exists)
                            return TRUE;

                     (void) dkimf_inet_ntoa(mask, &ipbuf[c],
                                            sizeof ipbuf - c);
              
                     exists = FALSE;
                     status = dkimf_db_get(db, ipbuf, 0, NULL, 0,
                                           &exists);
                     if (status != 0)
                            return FALSE;
                     if (exists)
                            return FALSE;

                     status = dkimf_db_get(db, &ipbuf[1], 0, NULL, 0,
                                           &exists);
                     if (status != 0)
                            return FALSE;
                     if (exists)
                            return TRUE;
              }
       }

       return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void dkimf_dstring_blank ( struct dkimf_dstring dstr)

Definition at line 1353 of file util.c.

{
       assert(dstr != NULL);

       dstr->ds_len = 0;
       dstr->ds_buf[0] = '\0';
}

Here is the caller graph for this function:

_Bool dkimf_dstring_cat ( struct dkimf_dstring dstr,
u_char *  str 
)

Definition at line 1190 of file util.c.

{
       int len;

       assert(dstr != NULL);
       assert(str != NULL);

       len = strlen((char *) str) + dstr->ds_len;

       /* too big? */
       if (dstr->ds_max > 0 && len >= dstr->ds_max)
              return FALSE;

       /* fits now? */
       if (dstr->ds_alloc <= len)
       {
              /* nope; try to resize */
              if (!dkimf_dstring_resize(dstr, len + 1))
                     return FALSE;
       }

       /* append */
       dstr->ds_len = strlcat((char *) dstr->ds_buf, (char *) str,
                              dstr->ds_alloc);

       return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

_Bool dkimf_dstring_cat1 ( struct dkimf_dstring dstr,
int  c 
)

Definition at line 1233 of file util.c.

{
       int len;

       assert(dstr != NULL);

       len = dstr->ds_len + 1;

       /* too big? */
       if (dstr->ds_max > 0 && len >= dstr->ds_max)
              return FALSE;

       /* fits now? */
       if (dstr->ds_alloc <= len)
       {
              /* nope; try to resize */
              if (!dkimf_dstring_resize(dstr, len + 1))
                     return FALSE;
       }

       /* append */
       dstr->ds_buf[dstr->ds_len++] = c;
       dstr->ds_buf[dstr->ds_len] = '\0';

       return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

_Bool dkimf_dstring_catn ( struct dkimf_dstring dstr,
unsigned char *  str,
size_t  nbytes 
)

Definition at line 1276 of file util.c.

{
       size_t needed;

       assert(dstr != NULL);
       assert(str != NULL);

       needed = dstr->ds_len + nbytes;

       /* too big? */
       if (dstr->ds_max > 0 && needed >= dstr->ds_max)
              return FALSE;

       /* fits now? */
       if (dstr->ds_alloc <= needed)
       {
              /* nope; try to resize */
              if (!dkimf_dstring_resize(dstr, needed + 1))
                     return FALSE;
       }

       /* append */
       memcpy(dstr->ds_buf + dstr->ds_len, str, nbytes);
       dstr->ds_len += nbytes;
       dstr->ds_buf[dstr->ds_len] = '\0';

       return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void dkimf_dstring_chop ( struct dkimf_dstring dstr,
int  len 
)

Definition at line 1373 of file util.c.

{
       assert(dstr != NULL);

       if (len < dstr->ds_len)
       {
              dstr->ds_len = len;
              dstr->ds_buf[len] = '\0';
       }
}

Here is the caller graph for this function:

_Bool dkimf_dstring_copy ( struct dkimf_dstring dstr,
u_char *  str 
)

Definition at line 1147 of file util.c.

{
       int len;

       assert(dstr != NULL);
       assert(str != NULL);

       len = strlen((char *) str);

       /* too big? */
       if (dstr->ds_max > 0 && len >= dstr->ds_max)
              return FALSE;

       /* fits now? */
       if (dstr->ds_alloc <= len)
       {
              /* nope; try to resize */
              if (!dkimf_dstring_resize(dstr, len + 1))
                     return FALSE;
       }

       /* copy */
       dstr->ds_len = strlcpy((char *) dstr->ds_buf, (char *) str,
                              dstr->ds_alloc);

       return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void dkimf_dstring_free ( struct dkimf_dstring dstr)

Definition at line 1124 of file util.c.

{
       assert(dstr != NULL);

       free(dstr->ds_buf);
       free(dstr);
}

Here is the caller graph for this function:

u_char* dkimf_dstring_get ( struct dkimf_dstring dstr)

Definition at line 1317 of file util.c.

{
       assert(dstr != NULL);

       return dstr->ds_buf;
}

Here is the caller graph for this function:

int dkimf_dstring_len ( struct dkimf_dstring dstr)

Definition at line 1335 of file util.c.

{
       assert(dstr != NULL);

       return dstr->ds_len;
}

Here is the caller graph for this function:

struct dkimf_dstring* dkimf_dstring_new ( int  len,
int  maxlen 
) [read]

Definition at line 1083 of file util.c.

{
       struct dkimf_dstring *new;

       /* fail on invalid parameters */
       if ((maxlen > 0 && len > maxlen) || len == 0)
              return NULL;

       if (len < BUFRSZ)
              len = BUFRSZ;

       new = malloc(sizeof(struct dkimf_dstring));
       if (new == NULL)
              return NULL;

       new->ds_buf = malloc(len);
       if (new->ds_buf == NULL)
       {
              free(new);
              return NULL;
       }

       memset(new->ds_buf, '\0', len);
       new->ds_alloc = len;
       new->ds_len = 0;
       new->ds_max = maxlen;

       return new;
}

Here is the caller graph for this function:

size_t dkimf_dstring_printf ( struct dkimf_dstring dstr,
char *  fmt,
  ... 
)

Definition at line 1397 of file util.c.

{
       size_t len;
       va_list ap;
       va_list ap2;

       assert(dstr != NULL);
       assert(fmt != NULL);

       va_start(ap, fmt);
       va_copy(ap2, ap);
       len = vsnprintf((char *) dstr->ds_buf + dstr->ds_len, dstr->ds_alloc,
                       fmt, ap);
       va_end(ap);

       if (len > dstr->ds_len)
       {
              if (!dkimf_dstring_resize(dstr, len + 1))
              {
                     va_end(ap2);
                     return (size_t) -1;
              }

              len = vsnprintf((char *) dstr->ds_buf + dstr->ds_len,
                              dstr->ds_alloc, fmt, ap2);
       }

       va_end(ap2);

       dstr->ds_len += len;

       return dstr->ds_len;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static _Bool dkimf_dstring_resize ( struct dkimf_dstring dstr,
int  len 
) [static]

Definition at line 1020 of file util.c.

{
       int newsz;
       u_char *new;

       assert(dstr != NULL);
       assert(len > 0);

       if (dstr->ds_alloc >= len)
              return TRUE;

       /* must resize */
       for (newsz = dstr->ds_alloc * 2;
            newsz < len;
            newsz *= 2)
       {
              /* impose ds_max limit, if specified */
              if (dstr->ds_max > 0 && newsz > dstr->ds_max)
              {
                     if (len <= dstr->ds_max)
                     {
                            newsz = len;
                            break;
                     }

                     return FALSE;
              }

              /* check for overflow */
              if (newsz > INT_MAX / 2)
              {
                     /* next iteration will overflow "newsz" */
                     return FALSE;
              }
       }

       new = malloc(newsz);
       if (new == NULL)
              return FALSE;

       memcpy(new, dstr->ds_buf, dstr->ds_alloc);

       free(dstr->ds_buf);

       dstr->ds_alloc = newsz;
       dstr->ds_buf = new;

       return TRUE;
}

Here is the caller graph for this function:

_Bool dkimf_hostlist ( char *  host,
char **  list 
)

Definition at line 976 of file util.c.

{
       int c;
       char *p;

       assert(host != NULL);
       assert(list != NULL);

       /* walk the entire list */
       for (c = 0; list[c] != NULL; c++)
       {
              /* first try a full hostname match */
              if (strcasecmp(host, list[c]) == 0)
                     return TRUE;

              /* try each domain */
              for (p = strchr(host, '.'); p != NULL; p = strchr(p + 1, '.'))
              {
                     if (strcasecmp(p, list[c]) == 0)
                            return TRUE;
              }
       }

       /* not found */
       return FALSE;
}
size_t dkimf_inet_ntoa ( struct in_addr  a,
char *  buf,
size_t  buflen 
)

Definition at line 830 of file util.c.

{
       in_addr_t addr;

       assert(buf != NULL);

       addr = ntohl(a.s_addr);

       return snprintf(buf, buflen, "%d.%d.%d.%d",
                       (addr >> 24), (addr >> 16) & 0xff,
                       (addr >> 8) & 0xff, addr & 0xff);
}

Here is the caller graph for this function:

void dkimf_ipstring ( char *  buf,
size_t  buflen,
struct sockaddr_storage *  ss 
)

Definition at line 1731 of file util.c.

{
       assert(buf != NULL);
       assert(ss != NULL);

       switch (ss->ss_family)
       {
         case AF_INET:
         {
              struct sockaddr_in *sa;

              sa = (struct sockaddr_in *) ss;

              (void) inet_ntop(ss->ss_family, &sa->sin_addr, buf, buflen);

              break;
         }

#ifdef AF_INET6
         case AF_INET6:
         {
              struct sockaddr_in6 *sa;

              sa = (struct sockaddr_in6 *) ss;

              (void) inet_ntop(ss->ss_family, &sa->sin6_addr, buf, buflen);

              break;
         }
#endif /* AF_INET6 */

         default:
              break;
       }
}

Here is the caller graph for this function:

_Bool dkimf_isblank ( char *  str)

Definition at line 248 of file util.c.

{
       char *p;

       for (p = str; *p != '\0'; p++)
       {
              if (isascii(*p) && isspace(*p))
                     continue;

              return FALSE;
       }

       return TRUE;
}

Here is the caller graph for this function:

void dkimf_lowercase ( u_char *  str)

Definition at line 369 of file util.c.

{
       u_char *p;

       assert(str != NULL);

       for (p = str; *p != '\0'; p++)
       {
              if (isascii(*p) && isupper(*p))
                     *p = tolower(*p);
       }
}

Here is the caller graph for this function:

void dkimf_mkpath ( char *  path,
size_t  pathlen,
char *  root,
char *  file 
)

Definition at line 938 of file util.c.

{
       assert(path != NULL);
       assert(root != NULL);
       assert(file != NULL);

       if (file[0] == '/')                       /* explicit path */
       {
              strlcpy(path, file, pathlen);
       }
       else if (root[0] == '\0')                 /* no root, use cwd */
       {
              (void) getcwd(path, pathlen);
              strlcat(path, "/", pathlen);
              strlcat(path, file, pathlen);
       }
       else                                      /* use root */
       {
              strlcpy(path, root, pathlen);
              if (root[strlen(root) - 1] != '/')
                     strlcat(path, "/", pathlen);
              strlcat(path, file, pathlen);
       }
}
_Bool dkimf_mkregexp ( char *  src,
char *  dst,
size_t  dstlen 
)

Definition at line 1537 of file util.c.

{
       char *p;
       char *q;
       char *end;

       assert(src != NULL);
       assert(dst != NULL);

       if (dstlen == 0)
              return FALSE;

       dst[0] = '^';

       end = dst + dstlen;

       for (p = src, q = dst + 1; *p != '\0' && q < end; p++)
       {
              switch (*p)
              {
                case '*':
                     *q = '.';
                     q++;
                     *q = '*';
                     q++;
                     break;

                case '.':
                     *q = '\\';
                     q++;
                     *q = '.';
                     q++;
                     break;

                default:
                     *q = *p;
                     q++;
                     break;
              }
       }

       *q++ = '$';

       if (q >= end)
              return FALSE;
       else
              return TRUE;
}

Here is the caller graph for this function:

void dkimf_optlist ( FILE *  where)

Definition at line 274 of file util.c.

{
       _Bool first = TRUE;
       int c;

       assert(where != NULL);

       for (c = 0; optlist[c] != NULL; c++)
       {
              if (first)
              {
                     fprintf(where, "\tActive code options:\n");
                     first = FALSE;
              }

              fprintf(where, "\t\t%s\n", optlist[c]);
       }
        fprintf(where, "\t%s\n", LIBOPENDKIM_FEATURE_STRING);
}

Here is the caller graph for this function:

void dkimf_setmaxfd ( void  )

Definition at line 305 of file util.c.

{
       struct rlimit rlp;

       if (getrlimit(RLIMIT_NOFILE, &rlp) != 0)
       {
              syslog(LOG_WARNING, "getrlimit(): %s", strerror(errno));
       }
       else
       {
              rlp.rlim_cur = rlp.rlim_max;
              if (setrlimit(RLIMIT_NOFILE, &rlp) != 0)
              {
                     syslog(LOG_WARNING, "setrlimit(): %s",
                            strerror(errno));
              }
       }
}

Here is the caller graph for this function:

int dkimf_socket_cleanup ( char *  sockspec)

Definition at line 1443 of file util.c.

{
       int s;
       char *colon;
       struct sockaddr_un sock;

       assert(sockspec != NULL);

       /* we only care about "local" or "unix" sockets */
       colon = strchr(sockspec, ':');
       if (colon != NULL)
       {
              if (strncasecmp(sockspec, "local:", 6) != 0 &&
                  strncasecmp(sockspec, "unix:", 5) != 0)
                     return 0;
       }

       /* find the filename */
       if (colon == NULL)
       {
              colon = sockspec;
       }
       else
       {
              if (*(colon + 1) == '\0')
                     return EINVAL;
       }

       /* get a socket */
       s = socket(PF_UNIX, SOCK_STREAM, 0);
       if (s == -1)
              return errno;

       /* set up a connection */
       memset(&sock, '\0', sizeof sock);
#ifdef BSD
       sock.sun_len = sizeof sock;
#endif /* BSD */
       sock.sun_family = PF_UNIX;
       strlcpy(sock.sun_path, colon + 1, sizeof sock.sun_path);

       /* try to connect */
       if (connect(s, (struct sockaddr *) &sock, (socklen_t) sizeof sock) != 0)
       {
              /* if ECONNREFUSED, try to unlink */
              if (errno == ECONNREFUSED)
              {
                     close(s);

                     if (unlink(sock.sun_path) == 0)
                            return 0;
                     else
                            return errno;
              }

              /* if ENOENT, the socket's not there */
              else if (errno == ENOENT)
              {
                     close(s);

                     return 0;
              }

              /* something else happened */
              else
              {
                     int saveerr;

                     saveerr = errno;

                     close(s);

                     return saveerr;
              }
       }

       /* connection apparently succeeded */
       close(s);
       return EADDRINUSE;
}

Here is the caller graph for this function:

void dkimf_stripbrackets ( char *  addr)

Definition at line 335 of file util.c.

{
       char *p, *q;

       assert(addr != NULL);

       p = addr;
       q = addr + strlen(addr) - 1;

       while (*p == '<' && *q == '>')
       {
              p++;
              *q-- = '\0';
       }

       if (p != addr)
       {
              for (q = addr; *p != '\0'; p++, q++)
                     *q = *p;
              *q = '\0';
       }
}

Here is the caller graph for this function:

void dkimf_stripcr ( char *  str)

Definition at line 903 of file util.c.

{
       char *p;
       char *q;

       assert(str != NULL);

       for (p = str, q = str; *p != '\0'; p++)
       {
              if (*p == '\r')
                     continue;

              if (q != p)
                     *q = *p;
              q++;
       }

       if (q != p)
              *q = *p;
}

Here is the caller graph for this function:

_Bool dkimf_subdomain ( char *  d1,
char *  d2 
)

Definition at line 1697 of file util.c.

{
       char *p;

       assert(d1 != NULL);
       assert(d2 != NULL);

#if 0
       if (strcasecmp(d1, d2) == 0)
              return TRUE;
#endif /* 0 */

       for (p = strchr(d1, '.'); p != NULL; p = strchr(p + 1, '.'))
       {
              if (strcasecmp(d2, p + 1) == 0)
                     return TRUE;
       }

       return FALSE;
}
void dkimf_trimspaces ( u_char *  str)

Definition at line 854 of file util.c.

{
       size_t len = 0;
       u_char *p;
       u_char *last;
       u_char *firsttext = NULL;

       assert(str != NULL);

       last = NULL;

       for (p = str; *p != '\0'; p++)
       {
              len++;

              if (isascii(*p) && isspace(*p))
              {
                     if (last == NULL)
                     {
                            last = p;
                            continue;
                     }
              }
              else
              {
                     last = NULL;
                     if (firsttext == NULL)
                            firsttext = p;
              }
       }

       if (last != NULL)
              *last = '\0';

       if (firsttext != NULL && firsttext != str)
              memmove(str, firsttext, len - (firsttext - str) + 1);
}

Here is the caller graph for this function:


Variable Documentation

unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" [static]

Definition at line 235 of file util.c.

char* optlist[] [static]

Definition at line 76 of file util.c.

char util_c_id[] = "@(#)$Id: util.c,v 1.47.2.1 2010/10/27 21:43:09 cm-msk Exp $" [static]

Definition at line 11 of file util.c.