1995-12-02 17:30:23 +00:00
|
|
|
/*
|
2002-02-17 21:58:34 +00:00
|
|
|
* Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
|
2000-08-12 22:19:16 +00:00
|
|
|
* All rights reserved.
|
1998-08-03 05:56:20 +00:00
|
|
|
* Copyright (c) 1992 Eric P. Allman. All rights reserved.
|
1995-12-02 17:30:23 +00:00
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
1998-08-03 05:56:20 +00:00
|
|
|
* By using this file, you agree to the terms and conditions set
|
|
|
|
* forth in the LICENSE file which can be found at the top level of
|
|
|
|
* the sendmail distribution.
|
1995-12-02 17:30:23 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#include <sm/gen.h>
|
|
|
|
|
|
|
|
SM_IDSTR(copyright,
|
|
|
|
"@(#) Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.\n\
|
2000-08-12 22:19:16 +00:00
|
|
|
All rights reserved.\n\
|
|
|
|
Copyright (c) 1992 Eric P. Allman. All rights reserved.\n\
|
|
|
|
Copyright (c) 1992, 1993\n\
|
2002-02-17 21:58:34 +00:00
|
|
|
The Regents of the University of California. All rights reserved.\n")
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2002-06-11 21:16:51 +00:00
|
|
|
SM_IDSTR(id, "@(#)$Id: makemap.c,v 8.175 2001/12/28 22:44:01 ca Exp $")
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2003-02-23 21:58:16 +00:00
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#ifndef ISC_UNIX
|
|
|
|
# include <sys/file.h>
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* ! ISC_UNIX */
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef EX_OK
|
|
|
|
# undef EX_OK /* unistd.h may have another use for this */
|
|
|
|
#endif /* EX_OK */
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <sendmail/sendmail.h>
|
|
|
|
#include <sendmail/pathnames.h>
|
|
|
|
#include <libsmdb/smdb.h>
|
1995-12-02 17:30:23 +00:00
|
|
|
|
1997-06-27 14:53:01 +00:00
|
|
|
uid_t RealUid;
|
|
|
|
gid_t RealGid;
|
|
|
|
char *RealUserName;
|
|
|
|
uid_t RunAsUid;
|
|
|
|
uid_t RunAsGid;
|
|
|
|
char *RunAsUserName;
|
|
|
|
int Verbose = 2;
|
2002-02-17 21:58:34 +00:00
|
|
|
bool DontInitGroups = false;
|
1999-01-12 12:38:06 +00:00
|
|
|
uid_t TrustedUid = 0;
|
2000-08-12 22:19:16 +00:00
|
|
|
BITMAP256 DontBlameSendmail;
|
1997-06-27 14:53:01 +00:00
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
#define BUFSIZE 1024
|
2002-02-17 21:58:34 +00:00
|
|
|
#define ISSEP(c) (sep == '\0' ? isascii(c) && isspace(c) : (c) == sep)
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(progname)
|
|
|
|
char *progname;
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
/* XXX break the usage output into multiple lines? it's too long */
|
|
|
|
sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"Usage: %s [-C cffile] [-N] [-c cachesize] [-d] [-e] [-f] [-l] [-o] [-r] [-s] [-t delimiter] [-u] [-v] type mapname\n",
|
|
|
|
progname);
|
|
|
|
#if _FFR_COMMENT_CHAR
|
|
|
|
/* add -D comment-char */
|
|
|
|
#endif /* _FFR_COMMENT_CHAR */
|
2000-08-12 22:19:16 +00:00
|
|
|
exit(EX_USAGE);
|
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
int
|
1995-12-02 17:30:23 +00:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
char *progname;
|
1998-08-03 05:56:20 +00:00
|
|
|
char *cfile;
|
2002-02-17 21:58:34 +00:00
|
|
|
bool inclnull = false;
|
|
|
|
bool notrunc = false;
|
|
|
|
bool allowreplace = false;
|
|
|
|
bool allowempty = false;
|
|
|
|
bool verbose = false;
|
|
|
|
bool foldcase = true;
|
|
|
|
bool unmake = false;
|
2000-08-12 22:19:16 +00:00
|
|
|
char sep = '\0';
|
2002-02-17 21:58:34 +00:00
|
|
|
char comment = '#';
|
1995-12-02 17:30:23 +00:00
|
|
|
int exitstat;
|
|
|
|
int opt;
|
1999-01-12 12:38:06 +00:00
|
|
|
char *typename = NULL;
|
|
|
|
char *mapname = NULL;
|
2002-02-17 21:58:34 +00:00
|
|
|
unsigned int lineno;
|
1995-12-02 17:30:23 +00:00
|
|
|
int st;
|
|
|
|
int mode;
|
2000-08-12 22:19:16 +00:00
|
|
|
int smode;
|
1999-01-12 12:38:06 +00:00
|
|
|
int putflags = 0;
|
2000-08-12 22:19:16 +00:00
|
|
|
long sff = SFF_ROOTOK|SFF_REGONLY;
|
1998-08-03 05:56:20 +00:00
|
|
|
struct passwd *pw;
|
2000-08-12 22:19:16 +00:00
|
|
|
SMDB_DATABASE *database;
|
|
|
|
SMDB_CURSOR *cursor;
|
|
|
|
SMDB_DBENT db_key, db_val;
|
|
|
|
SMDB_DBPARAMS params;
|
|
|
|
SMDB_USER_INFO user_info;
|
1995-12-02 17:30:23 +00:00
|
|
|
char ibuf[BUFSIZE];
|
2000-08-12 22:19:16 +00:00
|
|
|
#if HASFCHOWN
|
2002-02-17 21:58:34 +00:00
|
|
|
SM_FILE_T *cfp;
|
1998-08-03 05:56:20 +00:00
|
|
|
char buf[MAXLINE];
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* HASFCHOWN */
|
1997-06-27 14:53:01 +00:00
|
|
|
static char rnamebuf[MAXNAME]; /* holds RealUserName */
|
1995-12-02 17:30:23 +00:00
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
memset(¶ms, '\0', sizeof params);
|
|
|
|
params.smdbp_cache_size = 1024 * 1024;
|
|
|
|
|
|
|
|
progname = strrchr(argv[0], '/');
|
|
|
|
if (progname != NULL)
|
|
|
|
progname++;
|
|
|
|
else
|
|
|
|
progname = argv[0];
|
2002-02-17 21:58:34 +00:00
|
|
|
cfile = getcfname(0, 0, SM_GET_SENDMAIL_CF, NULL);
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
clrbitmap(DontBlameSendmail);
|
1997-06-27 14:53:01 +00:00
|
|
|
RunAsUid = RealUid = getuid();
|
|
|
|
RunAsGid = RealGid = getgid();
|
|
|
|
pw = getpwuid(RealUid);
|
1997-08-04 05:00:07 +00:00
|
|
|
if (pw != NULL)
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_strlcpy(rnamebuf, pw->pw_name, sizeof rnamebuf);
|
1997-06-27 14:53:01 +00:00
|
|
|
else
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_snprintf(rnamebuf, sizeof rnamebuf,
|
|
|
|
"Unknown UID %d", (int) RealUid);
|
1997-06-27 14:53:01 +00:00
|
|
|
RunAsUserName = RealUserName = rnamebuf;
|
2000-08-12 22:19:16 +00:00
|
|
|
user_info.smdbu_id = RunAsUid;
|
|
|
|
user_info.smdbu_group_id = RunAsGid;
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_strlcpy(user_info.smdbu_name, RunAsUserName,
|
2000-08-12 22:19:16 +00:00
|
|
|
SMDB_MAX_USER_NAME_LEN);
|
1997-06-27 14:53:01 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#define OPTIONS "C:D:Nc:deflorst:uv"
|
1998-08-04 12:53:30 +00:00
|
|
|
while ((opt = getopt(argc, argv, OPTIONS)) != -1)
|
1995-12-02 17:30:23 +00:00
|
|
|
{
|
|
|
|
switch (opt)
|
|
|
|
{
|
1998-08-03 05:56:20 +00:00
|
|
|
case 'C':
|
|
|
|
cfile = optarg;
|
|
|
|
break;
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
case 'N':
|
2002-02-17 21:58:34 +00:00
|
|
|
inclnull = true;
|
1995-12-02 17:30:23 +00:00
|
|
|
break;
|
|
|
|
|
1996-11-18 02:26:51 +00:00
|
|
|
case 'c':
|
2000-08-12 22:19:16 +00:00
|
|
|
params.smdbp_cache_size = atol(optarg);
|
1996-11-18 02:26:51 +00:00
|
|
|
break;
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
case 'd':
|
2002-02-17 21:58:34 +00:00
|
|
|
params.smdbp_allow_dup = true;
|
2000-08-12 22:19:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
2002-02-17 21:58:34 +00:00
|
|
|
allowempty = true;
|
1995-12-02 17:30:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
2002-02-17 21:58:34 +00:00
|
|
|
foldcase = false;
|
1995-12-02 17:30:23 +00:00
|
|
|
break;
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#if _FFR_COMMENT_CHAR
|
|
|
|
case 'D':
|
|
|
|
comment = *optarg;
|
|
|
|
break;
|
|
|
|
#endif /* _FFR_COMMENT_CHAR */
|
|
|
|
|
1999-01-12 12:38:06 +00:00
|
|
|
case 'l':
|
2000-08-12 22:19:16 +00:00
|
|
|
smdb_print_available_types();
|
1999-01-12 12:38:06 +00:00
|
|
|
exit(EX_OK);
|
|
|
|
break;
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
case 'o':
|
2002-02-17 21:58:34 +00:00
|
|
|
notrunc = true;
|
1995-12-02 17:30:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
2002-02-17 21:58:34 +00:00
|
|
|
allowreplace = true;
|
1995-12-02 17:30:23 +00:00
|
|
|
break;
|
|
|
|
|
1997-06-27 14:53:01 +00:00
|
|
|
case 's':
|
2000-08-12 22:19:16 +00:00
|
|
|
setbitn(DBS_MAPINUNSAFEDIRPATH, DontBlameSendmail);
|
|
|
|
setbitn(DBS_WRITEMAPTOHARDLINK, DontBlameSendmail);
|
|
|
|
setbitn(DBS_WRITEMAPTOSYMLINK, DontBlameSendmail);
|
|
|
|
setbitn(DBS_LINKEDMAPINWRITABLEDIR, DontBlameSendmail);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
if (optarg == NULL || *optarg == '\0')
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"Invalid separator\n");
|
2000-08-12 22:19:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
sep = *optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
2002-02-17 21:58:34 +00:00
|
|
|
unmake = true;
|
1997-06-27 14:53:01 +00:00
|
|
|
break;
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
case 'v':
|
2002-02-17 21:58:34 +00:00
|
|
|
verbose = true;
|
1995-12-02 17:30:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2000-08-12 22:19:16 +00:00
|
|
|
usage(progname);
|
|
|
|
/* NOTREACHED */
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (!bitnset(DBS_WRITEMAPTOSYMLINK, DontBlameSendmail))
|
1998-08-03 05:56:20 +00:00
|
|
|
sff |= SFF_NOSLINK;
|
2000-08-12 22:19:16 +00:00
|
|
|
if (!bitnset(DBS_WRITEMAPTOHARDLINK, DontBlameSendmail))
|
1998-08-03 05:56:20 +00:00
|
|
|
sff |= SFF_NOHLINK;
|
2000-08-12 22:19:16 +00:00
|
|
|
if (!bitnset(DBS_LINKEDMAPINWRITABLEDIR, DontBlameSendmail))
|
1998-08-03 05:56:20 +00:00
|
|
|
sff |= SFF_NOWLINK;
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc != 2)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
usage(progname);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
typename = argv[0];
|
|
|
|
mapname = argv[1];
|
|
|
|
}
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
#if HASFCHOWN
|
|
|
|
/* Find TrustedUser value in sendmail.cf */
|
2002-02-17 21:58:34 +00:00
|
|
|
if ((cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, cfile, SM_IO_RDONLY,
|
|
|
|
NULL)) == NULL)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "makemap: %s: %s",
|
|
|
|
cfile, sm_errstring(errno));
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_NOINPUT);
|
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
|
|
|
register char *b;
|
|
|
|
|
|
|
|
if ((b = strchr(buf, '\n')) != NULL)
|
|
|
|
*b = '\0';
|
|
|
|
|
|
|
|
b = buf;
|
|
|
|
switch (*b++)
|
|
|
|
{
|
|
|
|
case 'O': /* option */
|
1999-01-12 12:38:06 +00:00
|
|
|
if (strncasecmp(b, " TrustedUser", 12) == 0 &&
|
|
|
|
!(isascii(b[12]) && isalnum(b[12])))
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
|
|
|
b = strchr(b, '=');
|
|
|
|
if (b == NULL)
|
|
|
|
continue;
|
|
|
|
while (isascii(*++b) && isspace(*b))
|
|
|
|
continue;
|
|
|
|
if (isascii(*b) && isdigit(*b))
|
1999-01-12 12:38:06 +00:00
|
|
|
TrustedUid = atoi(b);
|
1998-08-03 05:56:20 +00:00
|
|
|
else
|
|
|
|
{
|
1999-01-12 12:38:06 +00:00
|
|
|
TrustedUid = 0;
|
1998-08-03 05:56:20 +00:00
|
|
|
pw = getpwnam(b);
|
|
|
|
if (pw == NULL)
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr,
|
|
|
|
SM_TIME_DEFAULT,
|
|
|
|
"TrustedUser: unknown user %s\n", b);
|
1998-08-03 05:56:20 +00:00
|
|
|
else
|
1999-01-12 12:38:06 +00:00
|
|
|
TrustedUid = pw->pw_uid;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
# ifdef UID_MAX
|
1999-01-12 12:38:06 +00:00
|
|
|
if (TrustedUid > UID_MAX)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr,
|
|
|
|
SM_TIME_DEFAULT,
|
|
|
|
"TrustedUser: uid value (%ld) > UID_MAX (%ld)",
|
2000-08-12 22:19:16 +00:00
|
|
|
(long) TrustedUid,
|
|
|
|
(long) UID_MAX);
|
1999-01-12 12:38:06 +00:00
|
|
|
TrustedUid = 0;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
# endif /* UID_MAX */
|
1998-08-03 05:56:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_close(cfp, SM_TIME_DEFAULT);
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* HASFCHOWN */
|
|
|
|
|
|
|
|
if (!params.smdbp_allow_dup && !allowreplace)
|
|
|
|
putflags = SMDBF_NO_OVERWRITE;
|
|
|
|
|
|
|
|
if (unmake)
|
1995-12-02 17:30:23 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
mode = O_RDONLY;
|
|
|
|
smode = S_IRUSR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mode = O_RDWR;
|
|
|
|
if (!notrunc)
|
1995-12-02 17:30:23 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
mode |= O_CREAT|O_TRUNC;
|
|
|
|
sff |= SFF_CREAT;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
smode = S_IWUSR;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
params.smdbp_num_elements = 4096;
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
errno = smdb_open_database(&database, mapname, mode, smode, sff,
|
|
|
|
typename, &user_info, ¶ms);
|
|
|
|
if (errno != SMDBE_OK)
|
1995-12-02 17:30:23 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
char *hint;
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (errno == SMDBE_UNSUPPORTED_DB_TYPE &&
|
|
|
|
(hint = smdb_db_definition(typename)) != NULL)
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"%s: Need to recompile with -D%s for %s support\n",
|
|
|
|
progname, hint, typename);
|
2000-08-12 22:19:16 +00:00
|
|
|
else
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"%s: error opening type %s map %s: %s\n",
|
|
|
|
progname, typename, mapname,
|
|
|
|
sm_errstring(errno));
|
2000-08-12 22:19:16 +00:00
|
|
|
exit(EX_CANTCREAT);
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) database->smdb_sync(database, 0);
|
|
|
|
|
2000-10-01 02:03:50 +00:00
|
|
|
if (!unmake && geteuid() == 0 && TrustedUid != 0)
|
1997-06-27 14:53:01 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
errno = database->smdb_set_owner(database, TrustedUid, -1);
|
|
|
|
if (errno != SMDBE_OK)
|
1997-06-27 14:53:01 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"WARNING: ownership change on %s failed %s",
|
|
|
|
mapname, sm_errstring(errno));
|
1997-06-27 14:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-12 22:19:16 +00:00
|
|
|
** Copy the data
|
1995-12-02 17:30:23 +00:00
|
|
|
*/
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
exitstat = EX_OK;
|
|
|
|
if (unmake)
|
1995-12-02 17:30:23 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
errno = database->smdb_cursor(database, &cursor, 0);
|
|
|
|
if (errno != SMDBE_OK)
|
1997-06-27 14:53:01 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"%s: cannot make cursor for type %s map %s\n",
|
|
|
|
progname, typename, mapname);
|
2000-08-12 22:19:16 +00:00
|
|
|
exit(EX_SOFTWARE);
|
1997-06-27 14:53:01 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
memset(&db_key, '\0', sizeof db_key);
|
|
|
|
memset(&db_val, '\0', sizeof db_val);
|
|
|
|
|
2001-01-21 22:21:43 +00:00
|
|
|
for (lineno = 0; ; lineno++)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
errno = cursor->smdbc_get(cursor, &db_key, &db_val,
|
|
|
|
SMDB_CURSOR_GET_NEXT);
|
|
|
|
if (errno != SMDBE_OK)
|
2001-01-21 22:21:43 +00:00
|
|
|
break;
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
"%.*s\t%.*s\n",
|
|
|
|
(int) db_key.size,
|
|
|
|
(char *) db_key.data,
|
|
|
|
(int) db_val.size,
|
|
|
|
(char *)db_val.data);
|
2000-08-12 22:19:16 +00:00
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) cursor->smdbc_close(cursor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lineno = 0;
|
2002-02-17 21:58:34 +00:00
|
|
|
while (sm_io_fgets(smioin, SM_TIME_DEFAULT, ibuf, sizeof ibuf)
|
|
|
|
!= NULL)
|
1995-12-02 17:30:23 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
register char *p;
|
|
|
|
|
|
|
|
lineno++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Parse the line.
|
|
|
|
*/
|
|
|
|
|
|
|
|
p = strchr(ibuf, '\n');
|
|
|
|
if (p != NULL)
|
|
|
|
*p = '\0';
|
2002-02-17 21:58:34 +00:00
|
|
|
else if (!sm_io_eof(smioin))
|
1997-06-27 14:53:01 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"%s: %s: line %u: line too long (%ld bytes max)\n",
|
|
|
|
progname, mapname, lineno,
|
|
|
|
(long) sizeof ibuf);
|
2000-08-12 22:19:16 +00:00
|
|
|
exitstat = EX_DATAERR;
|
|
|
|
continue;
|
1997-06-27 14:53:01 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
if (ibuf[0] == '\0' || ibuf[0] == comment)
|
2000-08-12 22:19:16 +00:00
|
|
|
continue;
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sep == '\0' && isascii(ibuf[0]) && isspace(ibuf[0]))
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"%s: %s: line %u: syntax error (leading space)\n",
|
|
|
|
progname, mapname, lineno);
|
2000-08-12 22:19:16 +00:00
|
|
|
exitstat = EX_DATAERR;
|
|
|
|
continue;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
memset(&db_key, '\0', sizeof db_key);
|
|
|
|
memset(&db_val, '\0', sizeof db_val);
|
2001-01-21 22:21:43 +00:00
|
|
|
db_key.data = ibuf;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
for (p = ibuf; *p != '\0' && !(ISSEP(*p)); p++)
|
1997-06-27 14:53:01 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
if (foldcase && isascii(*p) && isupper(*p))
|
|
|
|
*p = tolower(*p);
|
1997-06-27 14:53:01 +00:00
|
|
|
}
|
2001-01-21 22:21:43 +00:00
|
|
|
db_key.size = p - ibuf;
|
2000-08-12 22:19:16 +00:00
|
|
|
if (inclnull)
|
2001-01-21 22:21:43 +00:00
|
|
|
db_key.size++;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
if (*p != '\0')
|
|
|
|
*p++ = '\0';
|
2002-02-17 21:58:34 +00:00
|
|
|
while (*p != '\0' && ISSEP(*p))
|
2000-08-12 22:19:16 +00:00
|
|
|
p++;
|
|
|
|
if (!allowempty && *p == '\0')
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"%s: %s: line %u: no RHS for LHS %s\n",
|
|
|
|
progname, mapname, lineno,
|
|
|
|
(char *) db_key.data);
|
2000-08-12 22:19:16 +00:00
|
|
|
exitstat = EX_DATAERR;
|
|
|
|
continue;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2001-01-21 22:21:43 +00:00
|
|
|
db_val.data = p;
|
|
|
|
db_val.size = strlen(p);
|
2000-08-12 22:19:16 +00:00
|
|
|
if (inclnull)
|
2001-01-21 22:21:43 +00:00
|
|
|
db_val.size++;
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
/*
|
|
|
|
** Do the database insert.
|
|
|
|
*/
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (verbose)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
"key=`%s', val=`%s'\n",
|
|
|
|
(char *) db_key.data,
|
|
|
|
(char *) db_val.data);
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
errno = database->smdb_put(database, &db_key, &db_val,
|
|
|
|
putflags);
|
1998-08-03 05:56:20 +00:00
|
|
|
switch (errno)
|
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
case SMDBE_KEY_EXIST:
|
1998-08-03 05:56:20 +00:00
|
|
|
st = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
st = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
st = -1;
|
|
|
|
break;
|
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (st < 0)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"%s: %s: line %u: key %s: put error: %s\n",
|
|
|
|
progname, mapname, lineno,
|
|
|
|
(char *) db_key.data,
|
|
|
|
sm_errstring(errno));
|
2000-08-12 22:19:16 +00:00
|
|
|
exitstat = EX_IOERR;
|
|
|
|
}
|
|
|
|
else if (st > 0)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"%s: %s: line %u: key %s: duplicate key\n",
|
|
|
|
progname, mapname,
|
|
|
|
lineno,
|
|
|
|
(char *) db_key.data);
|
2000-08-12 22:19:16 +00:00
|
|
|
exitstat = EX_DATAERR;
|
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Now close the database.
|
|
|
|
*/
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
errno = database->smdb_close(database);
|
|
|
|
if (errno != SMDBE_OK)
|
1995-12-02 17:30:23 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"%s: close(%s): %s\n",
|
|
|
|
progname, mapname, sm_errstring(errno));
|
2000-08-12 22:19:16 +00:00
|
|
|
exitstat = EX_IOERR;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
smdb_free_database(database);
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
exit(exitstat);
|
2002-02-17 21:58:34 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
return exitstat;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|