diff options
Diffstat (limited to 'contrib/notmuch-deliver/maildrop')
23 files changed, 1375 insertions, 0 deletions
diff --git a/contrib/notmuch-deliver/maildrop/maildir/maildircreate.c b/contrib/notmuch-deliver/maildrop/maildir/maildircreate.c new file mode 100644 index 0000000..74030f4 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/maildir/maildircreate.c @@ -0,0 +1,241 @@ +/* +** Copyright 1998 - 2003 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#include "maildircreate.h" +#include "maildirmisc.h" +#include <sys/types.h> +#if HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif + +#if TIME_WITH_SYS_TIME +#include <sys/time.h> +#include <time.h> +#else +#if HAVE_SYS_TIME_H +#include <sys/time.h> +#else +#include <time.h> +#endif +#endif +#if HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif + +#if HAVE_UNISTD_H +#include <unistd.h> +#endif +#include <string.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include "numlib/numlib.h" + + +static const char rcsid[]="$Id: maildircreate.c,v 1.6 2003/01/26 04:07:03 mrsam Exp $"; + +FILE *maildir_tmpcreate_fp(struct maildir_tmpcreate_info *info) +{ + int fd=maildir_tmpcreate_fd(info); + FILE *fp; + + if (fd < 0) + return NULL; + + fp=fdopen(fd, "w+"); + + if (fp == NULL) + { + close(fd); + return NULL; + } + + return fp; +} + +static int maildir_tmpcreate_fd_do(struct maildir_tmpcreate_info *info); + +#define KEEPTRYING (60 * 60) +#define SLEEPFOR 3 + +int maildir_tmpcreate_fd(struct maildir_tmpcreate_info *info) +{ + int i; + + if (!info->doordie) + return (maildir_tmpcreate_fd_do(info)); + + for (i=0; i<KEEPTRYING / SLEEPFOR; i++) + { + int fd=maildir_tmpcreate_fd_do(info); + + if (fd >= 0 || errno != EAGAIN) + return fd; + + sleep(SLEEPFOR); + } + + return -1; +} + +static int maildir_tmpcreate_fd_do(struct maildir_tmpcreate_info *info) +{ + const char *maildir=info->maildir; + const char *uniq=info->uniq; + const char *hostname=info->hostname; + + char hostname_buf[256]; + char time_buf[NUMBUFSIZE]; + char usec_buf[NUMBUFSIZE]; + char pid_buf[NUMBUFSIZE]; + char len_buf[NUMBUFSIZE+3]; + char dev_buf[NUMBUFSIZE]; + char ino_buf[NUMBUFSIZE]; + struct timeval tv; + + struct stat stat_buf; + int fd; + + if (!maildir) + maildir="."; + if (!uniq) + uniq=""; + + if (!hostname || !*hostname) + { + hostname_buf[sizeof(hostname_buf)-1]=0; + if (gethostname(hostname_buf, sizeof(hostname_buf)-1) < 0) + strcpy(hostname_buf, "localhost"); + hostname=hostname_buf; + } + + gettimeofday(&tv, NULL); + + libmail_str_time_t(tv.tv_sec, time_buf); + libmail_str_time_t(tv.tv_usec, usec_buf); + libmail_str_pid_t(getpid(), pid_buf); + len_buf[0]=0; + if (info->msgsize > 0) + { + strcpy(len_buf, ",S="); + libmail_str_size_t(info->msgsize, len_buf+3); + } + + if (info->tmpname) + free(info->tmpname); + + info->tmpname=malloc(strlen(maildir)+strlen(uniq)+ + strlen(hostname)+strlen(time_buf)+ + strlen(usec_buf)+ + strlen(pid_buf)+strlen(len_buf)+100); + + if (!info->tmpname) + { + maildir_tmpcreate_free(info); + return -1; + } + + strcpy(info->tmpname, maildir); + strcat(info->tmpname, "/tmp/"); + strcat(info->tmpname, time_buf); + strcat(info->tmpname, ".M"); + strcat(info->tmpname, usec_buf); + strcat(info->tmpname, "P"); + strcat(info->tmpname, pid_buf); + + if (*uniq) + strcat(strcat(info->tmpname, "_"), uniq); + strcat(info->tmpname, "."); + strcat(info->tmpname, hostname); + strcat(info->tmpname, len_buf); + + if (stat( info->tmpname, &stat_buf) == 0) + { + maildir_tmpcreate_free(info); + errno=EAGAIN; + return -1; + } + + if (errno != ENOENT) + { + maildir_tmpcreate_free(info); + if (errno == EAGAIN) + errno=EIO; + return -1; + } + + if ((fd=maildir_safeopen_stat(info->tmpname, O_CREAT|O_RDWR|O_TRUNC, + info->openmode, &stat_buf)) < 0) + { + maildir_tmpcreate_free(info); + return -1; + } + + libmail_strh_dev_t(stat_buf.st_dev, dev_buf); + libmail_strh_ino_t(stat_buf.st_ino, ino_buf); + + if (info->newname) + free(info->newname); + + info->newname=malloc(strlen(info->tmpname)+strlen(ino_buf)+ + strlen(dev_buf)+3); + + if (!info->newname) + { + maildir_tmpcreate_free(info); + unlink(info->tmpname); + close(fd); + if (errno == EAGAIN) + errno=EIO; + return -1; + } + + strcpy(info->newname, maildir); + strcat(info->newname, "/new/"); + strcat(info->newname, time_buf); + strcat(info->newname, ".M"); + strcat(info->newname, usec_buf); + strcat(info->newname, "P"); + strcat(info->newname, pid_buf); + strcat(info->newname, "V"); + strcat(info->newname, dev_buf); + strcat(info->newname, "I"); + strcat(info->newname, ino_buf); + if (*uniq) + strcat(strcat(info->newname, "_"), uniq); + strcat(info->newname, "."); + strcat(info->newname, hostname); + strcat(info->newname, len_buf); + + return fd; +} + +void maildir_tmpcreate_free(struct maildir_tmpcreate_info *info) +{ + if (info->tmpname) + free(info->tmpname); + info->tmpname=NULL; + + if (info->newname) + free(info->newname); + info->newname=NULL; +} + +int maildir_movetmpnew(const char *tmpname, const char *newname) +{ + if (link(tmpname, newname) == 0) + { + unlink(tmpname); + return 0; + } + + if (errno != EXDEV) + return -1; + + /* AFS? */ + + return rename(tmpname, newname); +} diff --git a/contrib/notmuch-deliver/maildrop/maildir/maildircreate.h b/contrib/notmuch-deliver/maildrop/maildir/maildircreate.h new file mode 100644 index 0000000..ea1c71a --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/maildir/maildircreate.h @@ -0,0 +1,52 @@ +#ifndef maildircreate_h +#define maildircreate_h + +/* +** Copyright 1998 - 2003 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <stdio.h> + +#ifdef __cplusplus +extern "C" { +#endif + +static const char maildircreate_h_rcsid[]="$Id: maildircreate.h,v 1.10 2006/10/29 00:03:53 mrsam Exp $"; + + /* Create messages in maildirs */ + +struct maildir_tmpcreate_info { + const char *maildir; + unsigned long msgsize; /* If known, 0 otherwise (must use requota later)*/ + const char *uniq; /* You need when creating multiple msgs */ + const char *hostname; /* If known, NULL otherwise */ + int openmode; /* Default open mode */ + int doordie; /* Loop until we get it right. */ + char *tmpname; /* On exit, filename in tmp */ + char *newname; /* On exit, filename in new */ +}; + +#define maildir_tmpcreate_init(i) \ + do \ + { \ + memset( (i), 0, sizeof(*(i))); \ + (i)->openmode=0644; \ + } while(0) + +int maildir_tmpcreate_fd(struct maildir_tmpcreate_info *); +FILE *maildir_tmpcreate_fp(struct maildir_tmpcreate_info *); +void maildir_tmpcreate_free(struct maildir_tmpcreate_info *); + + /* Move created message from tmp to new */ +int maildir_movetmpnew(const char *tmpname, const char *newname); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/contrib/notmuch-deliver/maildrop/maildir/maildirmisc.h b/contrib/notmuch-deliver/maildrop/maildir/maildirmisc.h new file mode 100644 index 0000000..545d11e --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/maildir/maildirmisc.h @@ -0,0 +1,202 @@ +#ifndef maildirmisc_h +#define maildirmisc_h + +/* +** Copyright 2000-2003 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#if HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +static const char maildirmisc_h_rcsid[]="$Id: maildirmisc.h,v 1.18 2006/07/22 02:48:15 mrsam Exp $"; + +/* +** +** Miscellaneous maildir-related code +** +*/ + +/* Some special folders */ + +#define INBOX "INBOX" +#define DRAFTS "Drafts" +#define SENT "Sent" +#define TRASH "Trash" +#define SHARED "shared" + +#define SHAREDSUBDIR "shared-folders" + +#define NEWSHAREDSP "#shared" +#define NEWSHARED "#shared." + +#define PUBLIC "public" /* SMAP */ + +int maildir_make(const char *maildir, int perm, int subdirperm, + int folder); + +int maildir_del(const char *maildir); + +int maildir_del_content(const char *maildir); + +char *maildir_name2dir(const char *maildir, /* DIR location */ + const char *foldername); /* INBOX.name */ + +char *maildir_location(const char *homedir, + const char *maildir); +/* +** Homedir is the account's home directory, "maildir" is where the account's +** default Maildir is configured to be (usually "./Maildir"). Combine the +** two to produce an absolute pathname. +*/ + + +char *maildir_folderdir(const char *, /* maildir */ + const char *); /* folder name */ + /* Returns the directory corresponding to foldername (foldername is + ** checked to make sure that it's a valid name, else we set errno + ** to EINVAL, and return (0). + */ + +char *maildir_filename(const char *, /* maildir */ + const char *, /* folder */ + const char *); /* filename */ + /* + ** Builds the filename to this message, suitable for opening. + ** If the file doesn't appear to be there, search the maildir to + ** see if someone changed the flags, and return the current filename. + */ + +int maildir_safeopen(const char *, /* filename */ + int, /* mode */ + int); /* perm */ + +/* +** Same arguments as open(). When we're accessing a shared maildir, +** prevent someone from playing cute and dumping a bunch of symlinks +** in there. This function will open the indicate file only if the +** last component is not a symlink. +** This is implemented by opening the file with O_NONBLOCK (to prevent +** a DOS attack of someone pointing the symlink to a pipe, causing +** the open to hang), clearing O_NONBLOCK, then stat-int the file +** descriptor, lstating the filename, and making sure that dev/ino +** match. +*/ + +int maildir_semisafeopen(const char *, /* filename */ + int, /* mode */ + int); /* perm */ + +/* +** Same thing, except that we allow ONE level of soft link indirection, +** because we're reading from our own maildir, which points to the +** message in the sharable maildir. +*/ + +int maildir_safeopen_stat(const char *path, int mode, int perm, + struct stat *stat1); + /* Sane as maildir_safeopen(), except that we also initialize a + ** struct stat, saving an extra syscall to the caller. + */ + +int maildir_mkdir(const char *); /* directory */ +/* +** Create maildir including all subdirectories in the path (like mkdir -p) +*/ + +void maildir_purgetmp(const char *); /* maildir */ + /* purges old stuff out of tmp */ + +void maildir_purge(const char *, /* directory */ + unsigned); /* time_t to purge */ + +void maildir_getnew(const char *, /* maildir */ + const char *, /* folder */ + void (*)(const char *, void *), /* Callback function for + ** every moved msg. + */ + void *arg); /* Passthrough callback arg */ + + /* move messages from new to cur */ + +int maildir_deletefolder(const char *, /* maildir */ + const char *); /* folder */ + /* deletes a folder */ + +void maildir_list(const char *maildir, + void (*func)(const char *, void *), + void *voidp); + +void maildir_list_sharable(const char *, /* maildir */ + void (*)(const char *, void *), /* callback function */ + void *); /* 2nd arg to callback func */ + /* list sharable folders */ + +int maildir_shared_subscribe(const char *, /* maildir */ + const char *); /* folder */ + /* subscribe to a shared folder */ + +void maildir_list_shared(const char *, /* maildir */ + void (*)(const char *, void *), /* callback function */ + void *); /* 2nd arg to the callback func */ + /* list subscribed folders */ + +int maildir_shared_unsubscribe(const char *, /* maildir */ + const char *); /* folder */ + /* unsubscribe from a shared folder */ + +char *maildir_shareddir(const char *, /* maildir */ + const char *); /* folder */ + /* + ** Validate and return a path to a shared folder. folderdir must be + ** a name of a valid shared folder. + */ + +void maildir_shared_sync(const char *); /* maildir */ + /* "sync" the shared folder */ + +int maildir_sharedisro(const char *); /* maildir */ + /* maildir is a shared read-only folder */ + +int maildir_unlinksharedmsg(const char *); /* filename */ + /* Remove a message from a shared folder */ + +/* Internal function that reads a symlink */ + +char *maildir_getlink(const char *); + + /* Determine whether the maildir filename has a certain flag */ + +int maildir_hasflag(const char *filename, char); + +#define MAILDIR_DELETED(f) maildir_hasflag((f), 'T') + + /* + ** Hierarchical maildir rename. + */ + +#define MAILDIR_RENAME_FOLDER 1 +#define MAILDIR_RENAME_SUBFOLDERS 2 + +int maildir_rename(const char *maildir, /* Path to the maildir */ + const char *oldname, /* .foldername */ + const char *newname, /* .foldername */ + int flags, /* See above */ + void (*callback_func)(const char *old_path, + const char *new_path) + ); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/contrib/notmuch-deliver/maildrop/maildir/maildirmkdir.c b/contrib/notmuch-deliver/maildrop/maildir/maildirmkdir.c new file mode 100644 index 0000000..754b2c7 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/maildir/maildirmkdir.c @@ -0,0 +1,78 @@ +/* +** Copyright 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <sys/types.h> +#include <sys/stat.h> +#include <string.h> +#include <stdlib.h> +#if HAVE_UNISTD_H +#include <unistd.h> +#endif +#include <errno.h> + +#include "maildirmisc.h" + +static const char rcsid[]="$Id: maildirmkdir.c,v 1.2 2002/03/15 03:09:21 mrsam Exp $"; + +int maildir_mkdir(const char *dir) +{ +char *buf, *p; +size_t l; + + if (dir == 0 || dir[0] == 0) + { + errno = EINVAL; + return (-1); + } + l = strlen(dir); + if ((buf = malloc(l + sizeof("/tmp"))) == 0) + { + errno = ENOMEM; + return (-1); + } + strcpy(buf, dir); + strcpy(buf+l, "/cur"); + + /* We do mkdir -p here */ + + p = buf+1; + while ((p = strchr(p, '/')) != 0) + { + *p = '\0'; + if (mkdir(buf, 0700) < 0 && errno != EEXIST) + { + free(buf); + return (-1); + } + *p++ = '/'; + } + + if (mkdir(buf, 0700) < 0 && errno != EEXIST) { + free(buf); + return (-1); + } + strcpy(buf+l, "/new"); + if (mkdir(buf, 0700) < 0 && errno != EEXIST) { + free(buf); + return (-1); + } + /* + * make /tmp last because this is the one we open first - + * the existence of this directory implies the whole + * Maildir structure is complete + */ + strcpy(buf+l, "/tmp"); + if (mkdir(buf, 0700) < 0 && errno != EEXIST) { + free(buf); + return (-1); + } + free(buf); + return (0); +} + diff --git a/contrib/notmuch-deliver/maildrop/maildir/maildiropen.c b/contrib/notmuch-deliver/maildrop/maildir/maildiropen.c new file mode 100644 index 0000000..5071df7 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/maildir/maildiropen.c @@ -0,0 +1,141 @@ +/* +** Copyright 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <sys/types.h> +#include <sys/stat.h> +#include <string.h> +#include <stdlib.h> +#include <time.h> +#if HAVE_UNISTD_H +#include <unistd.h> +#endif +#include <stdio.h> +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> + +#include "maildirmisc.h" + +static const char rcsid[]="$Id: maildiropen.c,v 1.8 2003/01/19 16:39:52 mrsam Exp $"; + +char *maildir_getlink(const char *filename) +{ +#if HAVE_READLINK +size_t bufsiz; +char *buf; + + bufsiz=0; + buf=0; + + for (;;) + { + int n; + + if (buf) free(buf); + bufsiz += 256; + if ((buf=malloc(bufsiz)) == 0) + { + perror("malloc"); + return (0); + } + if ((n=readlink(filename, buf, bufsiz)) < 0) + { + free(buf); + return (0); + } + if (n < bufsiz) + { + buf[n]=0; + break; + } + } + return (buf); +#else + return (0); +#endif +} + +int maildir_semisafeopen(const char *path, int mode, int perm) +{ + +#if HAVE_READLINK + +char *l=maildir_getlink(path); + + if (l) + { + int f; + + if (*l != '/') + { + char *q=malloc(strlen(path)+strlen(l)+2); + char *s; + + if (!q) + { + free(l); + return (-1); + } + + strcpy(q, path); + if ((s=strchr(q, '/')) != 0) + s[1]=0; + else *q=0; + strcat(q, l); + free(l); + l=q; + } + + f=maildir_safeopen(l, mode, perm); + + free(l); + return (f); + } +#endif + + return (maildir_safeopen(path, mode, perm)); +} + +int maildir_safeopen(const char *path, int mode, int perm) +{ + struct stat stat1; + + return maildir_safeopen_stat(path, mode, perm, &stat1); +} + +int maildir_safeopen_stat(const char *path, int mode, int perm, + struct stat *stat1) +{ + struct stat stat2; + + int fd=open(path, mode +#ifdef O_NONBLOCK + | O_NONBLOCK +#else + | O_NDELAY +#endif + , perm); + + if (fd < 0) return (fd); + if (fcntl(fd, F_SETFL, (mode & O_APPEND)) || fstat(fd, stat1) + || lstat(path, &stat2)) + { + close(fd); + return (-1); + } + + if (stat1->st_dev != stat2.st_dev || stat1->st_ino != stat2.st_ino) + { + close(fd); + errno=ENOENT; + return (-1); + } + + return (fd); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/Makefile.am b/contrib/notmuch-deliver/maildrop/numlib/Makefile.am new file mode 100644 index 0000000..c0f129f --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/Makefile.am @@ -0,0 +1,27 @@ +# $Id: Makefile.am,v 1.12 2007/06/30 15:40:53 mrsam Exp $ +# +# Copyright 1998 - 2004 Double Precision, Inc. See COPYING for +# distribution information. + + +CLEANFILES=$(noinst_DATA) +noinst_LTLIBRARIES=libnumlib.la + +libnumlib_la_SOURCES=\ + atotimet.c \ + atouidt.c \ + changeuidgid.c \ + numlib.h \ + strdevt.c \ + strgidt.c \ + strhdevt.c \ + strhinot.c \ + strhpidt.c \ + strhtimet.c \ + strinot.c \ + strofft.c \ + strpidt.c \ + strsize.c \ + strsizet.c \ + strtimet.c \ + struidt.c diff --git a/contrib/notmuch-deliver/maildrop/numlib/atotimet.c b/contrib/notmuch-deliver/maildrop/numlib/atotimet.c new file mode 100644 index 0000000..d494fd2 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/atotimet.c @@ -0,0 +1,14 @@ +/* +** Copyright 2003 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: atotimet.c,v 1.1 2003/08/03 03:09:19 mrsam Exp $"; + +LIBMAIL_STRIMPL(time_t, libmail_strtotime_t, libmail_atotime_t) diff --git a/contrib/notmuch-deliver/maildrop/numlib/atouidt.c b/contrib/notmuch-deliver/maildrop/numlib/atouidt.c new file mode 100644 index 0000000..3c01ecf --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/atouidt.c @@ -0,0 +1,15 @@ +/* +** Copyright 2003 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: atouidt.c,v 1.1 2004/01/11 02:47:33 mrsam Exp $"; + +LIBMAIL_STRIMPL(uid_t, libmail_strtouid_t, libmail_atouid_t) +LIBMAIL_STRIMPL(gid_t, libmail_strtogid_t, libmail_atogid_t) diff --git a/contrib/notmuch-deliver/maildrop/numlib/changeuidgid.c b/contrib/notmuch-deliver/maildrop/numlib/changeuidgid.c new file mode 100644 index 0000000..5679392 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/changeuidgid.c @@ -0,0 +1,111 @@ +/* +** Copyright 1998 - 2002 Double Precision, Inc. See COPYING for +** distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include <sys/types.h> +#if HAVE_UNISTD_H +#include <unistd.h> +#endif +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <grp.h> +#include <pwd.h> +#include <errno.h> + +#include "numlib.h" + +static const char rcsid[]="$Id: changeuidgid.c,v 1.2 2003/01/05 04:01:17 mrsam Exp $"; + +void libmail_changegroup(gid_t gid) +{ + if ( setgid(gid)) + { + perror("setgid"); + exit(1); + } + +#if HAVE_SETGROUPS + if ( getuid() == 0 && setgroups(1, &gid) ) + { + perror("setgroups"); + exit(1); + } +#endif +} + +void libmail_changeuidgid(uid_t uid, gid_t gid) +{ + libmail_changegroup(gid); + if ( setuid(uid)) + { + perror("setuid"); + exit(1); + } +} + +void libmail_changeusername(const char *uname, const gid_t *forcegrp) +{ +struct passwd *pw; +uid_t changeuid; +gid_t changegid; + +/* uname might be a pointer returned from a previous called to getpw(), +** and libc has a problem getting it back. +*/ +char *p=malloc(strlen(uname)+1); + + if (!p) + { + perror("malloc"); + exit(1); + } + strcpy(p, uname); + + errno=ENOENT; + if ((pw=getpwnam(p)) == 0) + { + free(p); + perror("getpwnam"); + exit(1); + } + free(p); + + changeuid=pw->pw_uid; + + if ( !forcegrp ) forcegrp= &pw->pw_gid; + + changegid= *forcegrp; + + if ( setgid( changegid )) + { + perror("setgid"); + exit(1); + } + +#if HAVE_INITGROUPS + if ( getuid() == 0 && initgroups(pw->pw_name, changegid) ) + { + perror("initgroups"); + exit(1); + } +#else +#if HAVE_SETGROUPS + if ( getuid() == 0 && setgroups(1, &changegid) ) + { + perror("setgroups"); + exit(1); + } +#endif +#endif + + if (setuid(changeuid)) + { + perror("setuid"); + exit(1); + } +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/numlib.h b/contrib/notmuch-deliver/maildrop/numlib/numlib.h new file mode 100644 index 0000000..d356c6d --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/numlib.h @@ -0,0 +1,97 @@ +#ifndef numlib_h +#define numlib_h + +/* +** Copyright 1998 - 2003 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#ifdef __cplusplus +extern "C" { +#endif + +static const char numlib_h_rcsid[]="$Id: numlib.h,v 1.10 2004/01/11 02:47:33 mrsam Exp $"; + +#if HAVE_CONFIG_H +#include "../numlib/config.h" /* VPATH build */ +#endif + +#include <sys/types.h> +#include <time.h> + +#define NUMBUFSIZE 60 + +/* Convert various system types to decimal */ + +char *libmail_str_time_t(time_t, char *); +char *libmail_str_off_t(off_t, char *); +char *libmail_str_pid_t(pid_t, char *); +char *libmail_str_dev_t(dev_t, char *); +char *libmail_str_ino_t(ino_t, char *); +char *libmail_str_uid_t(uid_t, char *); +char *libmail_str_gid_t(gid_t, char *); +char *libmail_str_size_t(size_t, char *); + +char *libmail_str_sizekb(unsigned long, char *); /* X Kb or X Mb */ + +/* Convert selected system types to hex */ + +char *libmail_strh_time_t(time_t, char *); +char *libmail_strh_pid_t(pid_t, char *); +char *libmail_strh_ino_t(ino_t, char *); +char *libmail_strh_dev_t(dev_t, char *); + +/* And, now let's do the reverse */ + +time_t libmail_strtotime_t(const char **); +time_t libmail_atotime_t(const char *); + +uid_t libmail_strtouid_t(const char **); +uid_t libmail_atouid_t(const char *); + +gid_t libmail_strtogid_t(const char **); +gid_t libmail_atogid_t(const char *); + + /* Common macros: */ + +#define LIBMAIL_STRIMPL(type, f1, f2) \ +\ +type f1(const char **p)\ +{\ + type n=0;\ + while ( **p >= '0' && **p <= '9') n=n*10 + (char)(*(*p)++ - '0');\ + return n;\ +}\ +\ +type f2(const char *p)\ +{\ + return f1(&p);\ +} + + +/* +** The following functions are used by root to reset its user and group id +** to the authenticated user's. Various functions are provided to handle +** various situations. +*/ + +void libmail_changegroup(gid_t); /* Set the group id only. Also clear any + ** auxiliary group ids */ + +void libmail_changeuidgid(uid_t, gid_t); + /* Set both user id and group id. Also clear + ** aux group ids */ + +void libmail_changeusername(const char *, const gid_t *); + /* + ** Set the userid to the indicate user's. If second argument is + ** not null, it points to the groupid to set. If it's null, the + ** group id is taken from the passwd file. Auxiliary IDs are set + ** to any aux IDs set for the user in the group file. If there are + ** no aux group IDs for the user, any AUX ids are cleared. + */ + +#ifdef __cplusplus +} +#endif +#endif diff --git a/contrib/notmuch-deliver/maildrop/numlib/strdevt.c b/contrib/notmuch-deliver/maildrop/numlib/strdevt.c new file mode 100644 index 0000000..2e542d5 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strdevt.c @@ -0,0 +1,26 @@ +/* +** Copyright 2003 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strdevt.c,v 1.1 2003/01/26 03:22:40 mrsam Exp $"; + +char *libmail_str_dev_t(dev_t t, char *arg) +{ + char buf[NUMBUFSIZE]; + char *p=buf+sizeof(buf)-1; + + *p=0; + do + { + *--p= '0' + (t % 10); + t=t / 10; + } while(t); + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/strgidt.c b/contrib/notmuch-deliver/maildrop/numlib/strgidt.c new file mode 100644 index 0000000..89472e3 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strgidt.c @@ -0,0 +1,26 @@ +/* +** Copyright 1998 - 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strgidt.c,v 1.4 2003/01/05 04:01:17 mrsam Exp $"; + +char *libmail_str_gid_t(gid_t t, char *arg) +{ +char buf[NUMBUFSIZE]; +char *p=buf+sizeof(buf)-1; + + *p=0; + do + { + *--p= '0' + (t % 10); + t=t / 10; + } while(t); + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/strhdevt.c b/contrib/notmuch-deliver/maildrop/numlib/strhdevt.c new file mode 100644 index 0000000..98e25e5 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strhdevt.c @@ -0,0 +1,29 @@ +/* +** Copyright 1998 - 2003 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strhdevt.c,v 1.2 2003/03/12 02:45:56 mrsam Exp $"; + +static const char xdigit[]="0123456789ABCDEF"; + +char *libmail_strh_dev_t(dev_t t, char *arg) +{ +char buf[sizeof(t)*2+1]; +char *p=buf+sizeof(buf)-1; +unsigned i; + + *p=0; + for (i=0; i<sizeof(t)*2; i++) + { + *--p= xdigit[t & 15]; + t=t / 16; + } + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/strhinot.c b/contrib/notmuch-deliver/maildrop/numlib/strhinot.c new file mode 100644 index 0000000..fa64091 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strhinot.c @@ -0,0 +1,29 @@ +/* +** Copyright 1998 - 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strhinot.c,v 1.5 2003/03/12 02:45:56 mrsam Exp $"; + +static const char xdigit[]="0123456789ABCDEF"; + +char *libmail_strh_ino_t(ino_t t, char *arg) +{ +char buf[sizeof(t)*2+1]; +char *p=buf+sizeof(buf)-1; +unsigned i; + + *p=0; + for (i=0; i<sizeof(t)*2; i++) + { + *--p= xdigit[t & 15]; + t=t / 16; + } + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/strhpidt.c b/contrib/notmuch-deliver/maildrop/numlib/strhpidt.c new file mode 100644 index 0000000..2723af0 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strhpidt.c @@ -0,0 +1,29 @@ +/* +** Copyright 1998 - 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strhpidt.c,v 1.5 2003/03/12 02:45:56 mrsam Exp $"; + +static const char xdigit[]="0123456789ABCDEF"; + +char *libmail_strh_pid_t(pid_t t, char *arg) +{ +char buf[sizeof(t)*2+1]; +char *p=buf+sizeof(buf)-1; +unsigned i; + + *p=0; + for (i=0; i<sizeof(t)*2; i++) + { + *--p= xdigit[t & 15]; + t=t / 16; + } + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/strhtimet.c b/contrib/notmuch-deliver/maildrop/numlib/strhtimet.c new file mode 100644 index 0000000..b86b05a --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strhtimet.c @@ -0,0 +1,29 @@ +/* +** Copyright 1998 - 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strhtimet.c,v 1.5 2003/03/12 02:45:56 mrsam Exp $"; + +static const char xdigit[]="0123456789ABCDEF"; + +char *libmail_strh_time_t(time_t t, char *arg) +{ +char buf[sizeof(t)*2+1]; +char *p=buf+sizeof(buf)-1; +unsigned i; + + *p=0; + for (i=0; i<sizeof(t)*2; i++) + { + *--p= xdigit[t & 15]; + t=t / 16; + } + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/strinot.c b/contrib/notmuch-deliver/maildrop/numlib/strinot.c new file mode 100644 index 0000000..eb544c3 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strinot.c @@ -0,0 +1,26 @@ +/* +** Copyright 1998 - 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strinot.c,v 1.4 2003/01/05 04:01:17 mrsam Exp $"; + +char *libmail_str_ino_t(ino_t t, char *arg) +{ +char buf[NUMBUFSIZE]; +char *p=buf+sizeof(buf)-1; + + *p=0; + do + { + *--p= '0' + (t % 10); + t=t / 10; + } while(t); + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/strofft.c b/contrib/notmuch-deliver/maildrop/numlib/strofft.c new file mode 100644 index 0000000..d93edcb --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strofft.c @@ -0,0 +1,37 @@ +/* +** Copyright 1998 - 2002 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strofft.c,v 1.5 2003/01/05 04:01:17 mrsam Exp $"; + +char *libmail_str_off_t(off_t t, char *arg) +{ + char buf[NUMBUFSIZE]; + char *p=buf+sizeof(buf)-1; + int isneg=0; + + if (t < 0) + { + t= -t; + isneg=1; + } + + *p=0; + do + { + *--p= '0' + (t % 10); + t=t / 10; + } while(t); + + if (isneg) + *--p='-'; + + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/strpidt.c b/contrib/notmuch-deliver/maildrop/numlib/strpidt.c new file mode 100644 index 0000000..12ee9ce --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strpidt.c @@ -0,0 +1,26 @@ +/* +** Copyright 1998 - 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strpidt.c,v 1.4 2003/01/05 04:01:17 mrsam Exp $"; + +char *libmail_str_pid_t(pid_t t, char *arg) +{ +char buf[NUMBUFSIZE]; +char *p=buf+sizeof(buf)-1; + + *p=0; + do + { + *--p= '0' + (t % 10); + t=t / 10; + } while(t); + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/strsize.c b/contrib/notmuch-deliver/maildrop/numlib/strsize.c new file mode 100644 index 0000000..0a7dcaa --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strsize.c @@ -0,0 +1,62 @@ +/* +** Copyright 2001 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strsize.c,v 1.2 2003/01/05 04:01:17 mrsam Exp $"; + +static void cat_n(char *buf, unsigned long n) +{ +char bb[NUMBUFSIZE+1]; +char *p=bb+sizeof(bb)-1; + + *p=0; + do + { + *--p = "0123456789"[n % 10]; + n=n/10; + } while (n); + strcat(buf, p); +} + +char *libmail_str_sizekb(unsigned long n, char *sizebuf) +{ + /* If size is less than 1K bytes, display it as 0.xK */ + + if (n < 1024) + { + strcpy(sizebuf, "0."); + cat_n(sizebuf, (int)(10 * n / 1024 )); + strcat(sizebuf, "K"); + } + /* If size is less than 1 meg, display is as xK */ + + else if (n < 1024 * 1024) + { + *sizebuf=0; + cat_n(sizebuf, (unsigned long)(n+512)/1024); + strcat(sizebuf, "K"); + } + + /* Otherwise, display in megabytes */ + + else + { + unsigned long nm=(double)n / (1024.0 * 1024.0) * 10; + + *sizebuf=0; + cat_n( sizebuf, nm / 10); + strcat(sizebuf, "."); + cat_n( sizebuf, nm % 10); + strcat(sizebuf, "M"); + } + + return (sizebuf); +} + diff --git a/contrib/notmuch-deliver/maildrop/numlib/strsizet.c b/contrib/notmuch-deliver/maildrop/numlib/strsizet.c new file mode 100644 index 0000000..d4ec92d --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strsizet.c @@ -0,0 +1,26 @@ +/* +** Copyright 1998 - 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strsizet.c,v 1.4 2003/01/05 04:01:18 mrsam Exp $"; + +char *libmail_str_size_t(size_t t, char *arg) +{ +char buf[NUMBUFSIZE]; +char *p=buf+sizeof(buf)-1; + + *p=0; + do + { + *--p= '0' + (t % 10); + t=t / 10; + } while(t); + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/strtimet.c b/contrib/notmuch-deliver/maildrop/numlib/strtimet.c new file mode 100644 index 0000000..be7e051 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/strtimet.c @@ -0,0 +1,26 @@ +/* +** Copyright 1998 - 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: strtimet.c,v 1.4 2003/01/05 04:01:18 mrsam Exp $"; + +char *libmail_str_time_t(time_t t, char *arg) +{ +char buf[NUMBUFSIZE]; +char *p=buf+sizeof(buf)-1; + + *p=0; + do + { + *--p= '0' + (t % 10); + t=t / 10; + } while(t); + return (strcpy(arg, p)); +} diff --git a/contrib/notmuch-deliver/maildrop/numlib/struidt.c b/contrib/notmuch-deliver/maildrop/numlib/struidt.c new file mode 100644 index 0000000..50f3f74 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/struidt.c @@ -0,0 +1,26 @@ +/* +** Copyright 1998 - 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include "numlib.h" +#include <string.h> + +static const char rcsid[]="$Id: struidt.c,v 1.4 2003/01/05 04:01:18 mrsam Exp $"; + +char *libmail_str_uid_t(uid_t t, char *arg) +{ +char buf[NUMBUFSIZE]; +char *p=buf+sizeof(buf)-1; + + *p=0; + do + { + *--p= '0' + (t % 10); + t=t / 10; + } while(t); + return (strcpy(arg, p)); +} |