17c478bd9Sstevel@tonic-gate /*
2d4660949Sjbeck * Copyright (c) 1998-2002, 2004, 2008 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate * All rights reserved.
47c478bd9Sstevel@tonic-gate * Copyright (c) 1992 Eric P. Allman. All rights reserved.
57c478bd9Sstevel@tonic-gate * Copyright (c) 1992, 1993
67c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set
97c478bd9Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of
107c478bd9Sstevel@tonic-gate * the sendmail distribution.
117c478bd9Sstevel@tonic-gate *
127c478bd9Sstevel@tonic-gate */
137c478bd9Sstevel@tonic-gate
147c478bd9Sstevel@tonic-gate #include <sm/gen.h>
157c478bd9Sstevel@tonic-gate
167c478bd9Sstevel@tonic-gate SM_IDSTR(copyright,
177c478bd9Sstevel@tonic-gate "@(#) Copyright (c) 1998-2002, 2004 Sendmail, Inc. and its suppliers.\n\
187c478bd9Sstevel@tonic-gate All rights reserved.\n\
197c478bd9Sstevel@tonic-gate Copyright (c) 1992 Eric P. Allman. All rights reserved.\n\
207c478bd9Sstevel@tonic-gate Copyright (c) 1992, 1993\n\
217c478bd9Sstevel@tonic-gate The Regents of the University of California. All rights reserved.\n")
227c478bd9Sstevel@tonic-gate
23d4660949Sjbeck SM_IDSTR(id, "@(#)$Id: makemap.c,v 8.179 2008/04/14 02:06:16 ca Exp $")
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #ifndef ISC_UNIX
277c478bd9Sstevel@tonic-gate # include <sys/file.h>
287c478bd9Sstevel@tonic-gate #endif /* ! ISC_UNIX */
297c478bd9Sstevel@tonic-gate #include <ctype.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #ifdef EX_OK
337c478bd9Sstevel@tonic-gate # undef EX_OK /* unistd.h may have another use for this */
347c478bd9Sstevel@tonic-gate #endif /* EX_OK */
357c478bd9Sstevel@tonic-gate #include <sysexits.h>
367c478bd9Sstevel@tonic-gate #include <sendmail/sendmail.h>
377c478bd9Sstevel@tonic-gate #include <sendmail/pathnames.h>
387c478bd9Sstevel@tonic-gate #include <libsmdb/smdb.h>
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate uid_t RealUid;
417c478bd9Sstevel@tonic-gate gid_t RealGid;
427c478bd9Sstevel@tonic-gate char *RealUserName;
437c478bd9Sstevel@tonic-gate uid_t RunAsUid;
447800901eSjbeck gid_t RunAsGid;
457c478bd9Sstevel@tonic-gate char *RunAsUserName;
467c478bd9Sstevel@tonic-gate int Verbose = 2;
477c478bd9Sstevel@tonic-gate bool DontInitGroups = false;
487c478bd9Sstevel@tonic-gate uid_t TrustedUid = 0;
497c478bd9Sstevel@tonic-gate BITMAP256 DontBlameSendmail;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #define BUFSIZE 1024
527c478bd9Sstevel@tonic-gate #define ISSEP(c) (sep == '\0' ? isascii(c) && isspace(c) : (c) == sep)
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static void usage __P((char *));
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate static void
usage(progname)577c478bd9Sstevel@tonic-gate usage(progname)
587c478bd9Sstevel@tonic-gate char *progname;
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
617c478bd9Sstevel@tonic-gate "Usage: %s [-C cffile] [-N] [-c cachesize] [-D commentchar]\n",
627c478bd9Sstevel@tonic-gate progname);
637c478bd9Sstevel@tonic-gate sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
647c478bd9Sstevel@tonic-gate " %*s [-d] [-e] [-f] [-l] [-o] [-r] [-s] [-t delimiter]\n",
657c478bd9Sstevel@tonic-gate (int) strlen(progname), "");
667c478bd9Sstevel@tonic-gate sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
677c478bd9Sstevel@tonic-gate " %*s [-u] [-v] type mapname\n",
687c478bd9Sstevel@tonic-gate (int) strlen(progname), "");
697c478bd9Sstevel@tonic-gate exit(EX_USAGE);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate int
main(argc,argv)737c478bd9Sstevel@tonic-gate main(argc, argv)
747c478bd9Sstevel@tonic-gate int argc;
757c478bd9Sstevel@tonic-gate char **argv;
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate char *progname;
787c478bd9Sstevel@tonic-gate char *cfile;
797c478bd9Sstevel@tonic-gate bool inclnull = false;
807c478bd9Sstevel@tonic-gate bool notrunc = false;
817c478bd9Sstevel@tonic-gate bool allowreplace = false;
827c478bd9Sstevel@tonic-gate bool allowempty = false;
837c478bd9Sstevel@tonic-gate bool verbose = false;
847c478bd9Sstevel@tonic-gate bool foldcase = true;
857c478bd9Sstevel@tonic-gate bool unmake = false;
867c478bd9Sstevel@tonic-gate char sep = '\0';
877c478bd9Sstevel@tonic-gate char comment = '#';
887c478bd9Sstevel@tonic-gate int exitstat;
897c478bd9Sstevel@tonic-gate int opt;
907c478bd9Sstevel@tonic-gate char *typename = NULL;
917c478bd9Sstevel@tonic-gate char *mapname = NULL;
927c478bd9Sstevel@tonic-gate unsigned int lineno;
937c478bd9Sstevel@tonic-gate int st;
947c478bd9Sstevel@tonic-gate int mode;
957c478bd9Sstevel@tonic-gate int smode;
967c478bd9Sstevel@tonic-gate int putflags = 0;
977c478bd9Sstevel@tonic-gate long sff = SFF_ROOTOK|SFF_REGONLY;
987c478bd9Sstevel@tonic-gate struct passwd *pw;
997c478bd9Sstevel@tonic-gate SMDB_DATABASE *database;
1007c478bd9Sstevel@tonic-gate SMDB_CURSOR *cursor;
1017c478bd9Sstevel@tonic-gate SMDB_DBENT db_key, db_val;
1027c478bd9Sstevel@tonic-gate SMDB_DBPARAMS params;
1037c478bd9Sstevel@tonic-gate SMDB_USER_INFO user_info;
1047c478bd9Sstevel@tonic-gate char ibuf[BUFSIZE];
1057c478bd9Sstevel@tonic-gate #if HASFCHOWN
1067c478bd9Sstevel@tonic-gate SM_FILE_T *cfp;
1077c478bd9Sstevel@tonic-gate char buf[MAXLINE];
1087c478bd9Sstevel@tonic-gate #endif /* HASFCHOWN */
1097c478bd9Sstevel@tonic-gate static char rnamebuf[MAXNAME]; /* holds RealUserName */
1107c478bd9Sstevel@tonic-gate extern char *optarg;
1117c478bd9Sstevel@tonic-gate extern int optind;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate memset(¶ms, '\0', sizeof params);
1147c478bd9Sstevel@tonic-gate params.smdbp_cache_size = 1024 * 1024;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate progname = strrchr(argv[0], '/');
1177c478bd9Sstevel@tonic-gate if (progname != NULL)
1187c478bd9Sstevel@tonic-gate progname++;
1197c478bd9Sstevel@tonic-gate else
1207c478bd9Sstevel@tonic-gate progname = argv[0];
1217c478bd9Sstevel@tonic-gate cfile = getcfname(0, 0, SM_GET_SENDMAIL_CF, NULL);
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate clrbitmap(DontBlameSendmail);
1247c478bd9Sstevel@tonic-gate RunAsUid = RealUid = getuid();
1257c478bd9Sstevel@tonic-gate RunAsGid = RealGid = getgid();
1267c478bd9Sstevel@tonic-gate pw = getpwuid(RealUid);
1277c478bd9Sstevel@tonic-gate if (pw != NULL)
1287c478bd9Sstevel@tonic-gate (void) sm_strlcpy(rnamebuf, pw->pw_name, sizeof rnamebuf);
1297c478bd9Sstevel@tonic-gate else
1307c478bd9Sstevel@tonic-gate (void) sm_snprintf(rnamebuf, sizeof rnamebuf,
1317c478bd9Sstevel@tonic-gate "Unknown UID %d", (int) RealUid);
1327c478bd9Sstevel@tonic-gate RunAsUserName = RealUserName = rnamebuf;
1337c478bd9Sstevel@tonic-gate user_info.smdbu_id = RunAsUid;
1347c478bd9Sstevel@tonic-gate user_info.smdbu_group_id = RunAsGid;
1357c478bd9Sstevel@tonic-gate (void) sm_strlcpy(user_info.smdbu_name, RunAsUserName,
1367c478bd9Sstevel@tonic-gate SMDB_MAX_USER_NAME_LEN);
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate #define OPTIONS "C:D:Nc:deflorst:uv"
1397c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, OPTIONS)) != -1)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate switch (opt)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate case 'C':
1447c478bd9Sstevel@tonic-gate cfile = optarg;
1457c478bd9Sstevel@tonic-gate break;
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate case 'N':
1487c478bd9Sstevel@tonic-gate inclnull = true;
1497c478bd9Sstevel@tonic-gate break;
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate case 'c':
1527c478bd9Sstevel@tonic-gate params.smdbp_cache_size = atol(optarg);
1537c478bd9Sstevel@tonic-gate break;
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate case 'd':
1567c478bd9Sstevel@tonic-gate params.smdbp_allow_dup = true;
1577c478bd9Sstevel@tonic-gate break;
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate case 'e':
1607c478bd9Sstevel@tonic-gate allowempty = true;
1617c478bd9Sstevel@tonic-gate break;
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate case 'f':
1647c478bd9Sstevel@tonic-gate foldcase = false;
1657c478bd9Sstevel@tonic-gate break;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate case 'D':
1687c478bd9Sstevel@tonic-gate comment = *optarg;
1697c478bd9Sstevel@tonic-gate break;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate case 'l':
1727c478bd9Sstevel@tonic-gate smdb_print_available_types();
1737c478bd9Sstevel@tonic-gate exit(EX_OK);
1747c478bd9Sstevel@tonic-gate break;
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate case 'o':
1777c478bd9Sstevel@tonic-gate notrunc = true;
1787c478bd9Sstevel@tonic-gate break;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate case 'r':
1817c478bd9Sstevel@tonic-gate allowreplace = true;
1827c478bd9Sstevel@tonic-gate break;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate case 's':
1857c478bd9Sstevel@tonic-gate setbitn(DBS_MAPINUNSAFEDIRPATH, DontBlameSendmail);
1867c478bd9Sstevel@tonic-gate setbitn(DBS_WRITEMAPTOHARDLINK, DontBlameSendmail);
1877c478bd9Sstevel@tonic-gate setbitn(DBS_WRITEMAPTOSYMLINK, DontBlameSendmail);
1887c478bd9Sstevel@tonic-gate setbitn(DBS_LINKEDMAPINWRITABLEDIR, DontBlameSendmail);
1897c478bd9Sstevel@tonic-gate break;
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate case 't':
1927c478bd9Sstevel@tonic-gate if (optarg == NULL || *optarg == '\0')
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
1957c478bd9Sstevel@tonic-gate "Invalid separator\n");
1967c478bd9Sstevel@tonic-gate break;
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate sep = *optarg;
1997c478bd9Sstevel@tonic-gate break;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate case 'u':
2027c478bd9Sstevel@tonic-gate unmake = true;
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate case 'v':
2067c478bd9Sstevel@tonic-gate verbose = true;
2077c478bd9Sstevel@tonic-gate break;
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate default:
2107c478bd9Sstevel@tonic-gate usage(progname);
2117c478bd9Sstevel@tonic-gate /* NOTREACHED */
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate if (!bitnset(DBS_WRITEMAPTOSYMLINK, DontBlameSendmail))
2167c478bd9Sstevel@tonic-gate sff |= SFF_NOSLINK;
2177c478bd9Sstevel@tonic-gate if (!bitnset(DBS_WRITEMAPTOHARDLINK, DontBlameSendmail))
2187c478bd9Sstevel@tonic-gate sff |= SFF_NOHLINK;
2197c478bd9Sstevel@tonic-gate if (!bitnset(DBS_LINKEDMAPINWRITABLEDIR, DontBlameSendmail))
2207c478bd9Sstevel@tonic-gate sff |= SFF_NOWLINK;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate argc -= optind;
2237c478bd9Sstevel@tonic-gate argv += optind;
2247c478bd9Sstevel@tonic-gate if (argc != 2)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate usage(progname);
2277c478bd9Sstevel@tonic-gate /* NOTREACHED */
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate else
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate typename = argv[0];
2327c478bd9Sstevel@tonic-gate mapname = argv[1];
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate #if HASFCHOWN
2367c478bd9Sstevel@tonic-gate /* Find TrustedUser value in sendmail.cf */
2377c478bd9Sstevel@tonic-gate if ((cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, cfile, SM_IO_RDONLY,
2387c478bd9Sstevel@tonic-gate NULL)) == NULL)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "makemap: %s: %s",
2417c478bd9Sstevel@tonic-gate cfile, sm_errstring(errno));
2427c478bd9Sstevel@tonic-gate exit(EX_NOINPUT);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate register char *b;
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate if ((b = strchr(buf, '\n')) != NULL)
2497c478bd9Sstevel@tonic-gate *b = '\0';
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate b = buf;
2527c478bd9Sstevel@tonic-gate switch (*b++)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate case 'O': /* option */
2557c478bd9Sstevel@tonic-gate if (strncasecmp(b, " TrustedUser", 12) == 0 &&
2567c478bd9Sstevel@tonic-gate !(isascii(b[12]) && isalnum(b[12])))
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate b = strchr(b, '=');
2597c478bd9Sstevel@tonic-gate if (b == NULL)
2607c478bd9Sstevel@tonic-gate continue;
2617c478bd9Sstevel@tonic-gate while (isascii(*++b) && isspace(*b))
2627c478bd9Sstevel@tonic-gate continue;
2637c478bd9Sstevel@tonic-gate if (isascii(*b) && isdigit(*b))
2647c478bd9Sstevel@tonic-gate TrustedUid = atoi(b);
2657c478bd9Sstevel@tonic-gate else
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate TrustedUid = 0;
2687c478bd9Sstevel@tonic-gate pw = getpwnam(b);
2697c478bd9Sstevel@tonic-gate if (pw == NULL)
2707c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr,
2717c478bd9Sstevel@tonic-gate SM_TIME_DEFAULT,
2727c478bd9Sstevel@tonic-gate "TrustedUser: unknown user %s\n", b);
2737c478bd9Sstevel@tonic-gate else
2747c478bd9Sstevel@tonic-gate TrustedUid = pw->pw_uid;
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate # ifdef UID_MAX
2787c478bd9Sstevel@tonic-gate if (TrustedUid > UID_MAX)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr,
2817c478bd9Sstevel@tonic-gate SM_TIME_DEFAULT,
2827c478bd9Sstevel@tonic-gate "TrustedUser: uid value (%ld) > UID_MAX (%ld)",
2837c478bd9Sstevel@tonic-gate (long) TrustedUid,
2847c478bd9Sstevel@tonic-gate (long) UID_MAX);
2857c478bd9Sstevel@tonic-gate TrustedUid = 0;
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate # endif /* UID_MAX */
2887c478bd9Sstevel@tonic-gate break;
2897c478bd9Sstevel@tonic-gate }
290fec46055SToomas Soome /* FALLTHROUGH */
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate default:
2937c478bd9Sstevel@tonic-gate continue;
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate (void) sm_io_close(cfp, SM_TIME_DEFAULT);
2977c478bd9Sstevel@tonic-gate #endif /* HASFCHOWN */
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate if (!params.smdbp_allow_dup && !allowreplace)
3007c478bd9Sstevel@tonic-gate putflags = SMDBF_NO_OVERWRITE;
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate if (unmake)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate mode = O_RDONLY;
3057c478bd9Sstevel@tonic-gate smode = S_IRUSR;
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate else
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate mode = O_RDWR;
3107c478bd9Sstevel@tonic-gate if (!notrunc)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate mode |= O_CREAT|O_TRUNC;
3137c478bd9Sstevel@tonic-gate sff |= SFF_CREAT;
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate smode = S_IWUSR;
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate params.smdbp_num_elements = 4096;
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate errno = smdb_open_database(&database, mapname, mode, smode, sff,
3217c478bd9Sstevel@tonic-gate typename, &user_info, ¶ms);
3227c478bd9Sstevel@tonic-gate if (errno != SMDBE_OK)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate char *hint;
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate if (errno == SMDBE_UNSUPPORTED_DB_TYPE &&
3277c478bd9Sstevel@tonic-gate (hint = smdb_db_definition(typename)) != NULL)
3287c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
3297c478bd9Sstevel@tonic-gate "%s: Need to recompile with -D%s for %s support\n",
3307c478bd9Sstevel@tonic-gate progname, hint, typename);
3317c478bd9Sstevel@tonic-gate else
3327c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
3337c478bd9Sstevel@tonic-gate "%s: error opening type %s map %s: %s\n",
3347c478bd9Sstevel@tonic-gate progname, typename, mapname,
3357c478bd9Sstevel@tonic-gate sm_errstring(errno));
3367c478bd9Sstevel@tonic-gate exit(EX_CANTCREAT);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate (void) database->smdb_sync(database, 0);
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate if (!unmake && geteuid() == 0 && TrustedUid != 0)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate errno = database->smdb_set_owner(database, TrustedUid, -1);
3447c478bd9Sstevel@tonic-gate if (errno != SMDBE_OK)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
3477c478bd9Sstevel@tonic-gate "WARNING: ownership change on %s failed %s",
3487c478bd9Sstevel@tonic-gate mapname, sm_errstring(errno));
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate ** Copy the data
3547c478bd9Sstevel@tonic-gate */
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate exitstat = EX_OK;
3577c478bd9Sstevel@tonic-gate if (unmake)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate errno = database->smdb_cursor(database, &cursor, 0);
3607c478bd9Sstevel@tonic-gate if (errno != SMDBE_OK)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
3647c478bd9Sstevel@tonic-gate "%s: cannot make cursor for type %s map %s\n",
3657c478bd9Sstevel@tonic-gate progname, typename, mapname);
3667c478bd9Sstevel@tonic-gate exit(EX_SOFTWARE);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate memset(&db_key, '\0', sizeof db_key);
3707c478bd9Sstevel@tonic-gate memset(&db_val, '\0', sizeof db_val);
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate for (lineno = 0; ; lineno++)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate errno = cursor->smdbc_get(cursor, &db_key, &db_val,
3757c478bd9Sstevel@tonic-gate SMDB_CURSOR_GET_NEXT);
3767c478bd9Sstevel@tonic-gate if (errno != SMDBE_OK)
3777c478bd9Sstevel@tonic-gate break;
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
380d4660949Sjbeck "%.*s%c%.*s\n",
3817c478bd9Sstevel@tonic-gate (int) db_key.size,
3827c478bd9Sstevel@tonic-gate (char *) db_key.data,
383d4660949Sjbeck (sep != '\0') ? sep : '\t',
3847c478bd9Sstevel@tonic-gate (int) db_val.size,
3857c478bd9Sstevel@tonic-gate (char *)db_val.data);
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate (void) cursor->smdbc_close(cursor);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate else
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate lineno = 0;
3937c478bd9Sstevel@tonic-gate while (sm_io_fgets(smioin, SM_TIME_DEFAULT, ibuf, sizeof ibuf)
3947c478bd9Sstevel@tonic-gate != NULL)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate register char *p;
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate lineno++;
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate ** Parse the line.
4027c478bd9Sstevel@tonic-gate */
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate p = strchr(ibuf, '\n');
4057c478bd9Sstevel@tonic-gate if (p != NULL)
4067c478bd9Sstevel@tonic-gate *p = '\0';
4077c478bd9Sstevel@tonic-gate else if (!sm_io_eof(smioin))
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
4107c478bd9Sstevel@tonic-gate "%s: %s: line %u: line too long (%ld bytes max)\n",
4117c478bd9Sstevel@tonic-gate progname, mapname, lineno,
4127c478bd9Sstevel@tonic-gate (long) sizeof ibuf);
4137c478bd9Sstevel@tonic-gate exitstat = EX_DATAERR;
4147c478bd9Sstevel@tonic-gate continue;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate if (ibuf[0] == '\0' || ibuf[0] == comment)
4187c478bd9Sstevel@tonic-gate continue;
4197c478bd9Sstevel@tonic-gate if (sep == '\0' && isascii(ibuf[0]) && isspace(ibuf[0]))
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
4227c478bd9Sstevel@tonic-gate "%s: %s: line %u: syntax error (leading space)\n",
4237c478bd9Sstevel@tonic-gate progname, mapname, lineno);
4247c478bd9Sstevel@tonic-gate exitstat = EX_DATAERR;
4257c478bd9Sstevel@tonic-gate continue;
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate memset(&db_key, '\0', sizeof db_key);
4297c478bd9Sstevel@tonic-gate memset(&db_val, '\0', sizeof db_val);
4307c478bd9Sstevel@tonic-gate db_key.data = ibuf;
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate for (p = ibuf; *p != '\0' && !(ISSEP(*p)); p++)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate if (foldcase && isascii(*p) && isupper(*p))
4357c478bd9Sstevel@tonic-gate *p = tolower(*p);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate db_key.size = p - ibuf;
4387c478bd9Sstevel@tonic-gate if (inclnull)
4397c478bd9Sstevel@tonic-gate db_key.size++;
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate if (*p != '\0')
4427c478bd9Sstevel@tonic-gate *p++ = '\0';
4437c478bd9Sstevel@tonic-gate while (*p != '\0' && ISSEP(*p))
4447c478bd9Sstevel@tonic-gate p++;
4457c478bd9Sstevel@tonic-gate if (!allowempty && *p == '\0')
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
4487c478bd9Sstevel@tonic-gate "%s: %s: line %u: no RHS for LHS %s\n",
4497c478bd9Sstevel@tonic-gate progname, mapname, lineno,
4507c478bd9Sstevel@tonic-gate (char *) db_key.data);
4517c478bd9Sstevel@tonic-gate exitstat = EX_DATAERR;
4527c478bd9Sstevel@tonic-gate continue;
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate db_val.data = p;
4567c478bd9Sstevel@tonic-gate db_val.size = strlen(p);
4577c478bd9Sstevel@tonic-gate if (inclnull)
4587c478bd9Sstevel@tonic-gate db_val.size++;
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate /*
4617c478bd9Sstevel@tonic-gate ** Do the database insert.
4627c478bd9Sstevel@tonic-gate */
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate if (verbose)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4677c478bd9Sstevel@tonic-gate "key=`%s', val=`%s'\n",
4687c478bd9Sstevel@tonic-gate (char *) db_key.data,
4697c478bd9Sstevel@tonic-gate (char *) db_val.data);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate errno = database->smdb_put(database, &db_key, &db_val,
4737c478bd9Sstevel@tonic-gate putflags);
4747c478bd9Sstevel@tonic-gate switch (errno)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate case SMDBE_KEY_EXIST:
4777c478bd9Sstevel@tonic-gate st = 1;
4787c478bd9Sstevel@tonic-gate break;
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate case 0:
4817c478bd9Sstevel@tonic-gate st = 0;
4827c478bd9Sstevel@tonic-gate break;
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate default:
4857c478bd9Sstevel@tonic-gate st = -1;
4867c478bd9Sstevel@tonic-gate break;
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate if (st < 0)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
4927c478bd9Sstevel@tonic-gate "%s: %s: line %u: key %s: put error: %s\n",
4937c478bd9Sstevel@tonic-gate progname, mapname, lineno,
4947c478bd9Sstevel@tonic-gate (char *) db_key.data,
4957c478bd9Sstevel@tonic-gate sm_errstring(errno));
4967c478bd9Sstevel@tonic-gate exitstat = EX_IOERR;
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate else if (st > 0)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5017c478bd9Sstevel@tonic-gate "%s: %s: line %u: key %s: duplicate key\n",
5027c478bd9Sstevel@tonic-gate progname, mapname,
5037c478bd9Sstevel@tonic-gate lineno,
5047c478bd9Sstevel@tonic-gate (char *) db_key.data);
5057c478bd9Sstevel@tonic-gate exitstat = EX_DATAERR;
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate /*
5117c478bd9Sstevel@tonic-gate ** Now close the database.
5127c478bd9Sstevel@tonic-gate */
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate errno = database->smdb_close(database);
5157c478bd9Sstevel@tonic-gate if (errno != SMDBE_OK)
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5187c478bd9Sstevel@tonic-gate "%s: close(%s): %s\n",
5197c478bd9Sstevel@tonic-gate progname, mapname, sm_errstring(errno));
5207c478bd9Sstevel@tonic-gate exitstat = EX_IOERR;
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate smdb_free_database(database);
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate exit(exitstat);
5257c478bd9Sstevel@tonic-gate
5267c478bd9Sstevel@tonic-gate /* NOTREACHED */
5277c478bd9Sstevel@tonic-gate return exitstat;
5287c478bd9Sstevel@tonic-gate }
529