1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright (c) 1998-2002, 2004 Sendmail, Inc. and its suppliers. 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate * Copyright (c) 1992 Eric P. Allman. All rights reserved. 5*0Sstevel@tonic-gate * Copyright (c) 1992, 1993 6*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 7*0Sstevel@tonic-gate * 8*0Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set 9*0Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of 10*0Sstevel@tonic-gate * the sendmail distribution. 11*0Sstevel@tonic-gate * 12*0Sstevel@tonic-gate */ 13*0Sstevel@tonic-gate 14*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 15*0Sstevel@tonic-gate 16*0Sstevel@tonic-gate #include <sm/gen.h> 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gate SM_IDSTR(copyright, 19*0Sstevel@tonic-gate "@(#) Copyright (c) 1998-2002, 2004 Sendmail, Inc. and its suppliers.\n\ 20*0Sstevel@tonic-gate All rights reserved.\n\ 21*0Sstevel@tonic-gate Copyright (c) 1992 Eric P. Allman. All rights reserved.\n\ 22*0Sstevel@tonic-gate Copyright (c) 1992, 1993\n\ 23*0Sstevel@tonic-gate The Regents of the University of California. All rights reserved.\n") 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate SM_IDSTR(id, "@(#)$Id: makemap.c,v 8.177 2004/08/03 23:57:24 ca Exp $") 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate #include <sys/types.h> 29*0Sstevel@tonic-gate #ifndef ISC_UNIX 30*0Sstevel@tonic-gate # include <sys/file.h> 31*0Sstevel@tonic-gate #endif /* ! ISC_UNIX */ 32*0Sstevel@tonic-gate #include <ctype.h> 33*0Sstevel@tonic-gate #include <stdlib.h> 34*0Sstevel@tonic-gate #include <unistd.h> 35*0Sstevel@tonic-gate #ifdef EX_OK 36*0Sstevel@tonic-gate # undef EX_OK /* unistd.h may have another use for this */ 37*0Sstevel@tonic-gate #endif /* EX_OK */ 38*0Sstevel@tonic-gate #include <sysexits.h> 39*0Sstevel@tonic-gate #include <sendmail/sendmail.h> 40*0Sstevel@tonic-gate #include <sendmail/pathnames.h> 41*0Sstevel@tonic-gate #include <libsmdb/smdb.h> 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate uid_t RealUid; 44*0Sstevel@tonic-gate gid_t RealGid; 45*0Sstevel@tonic-gate char *RealUserName; 46*0Sstevel@tonic-gate uid_t RunAsUid; 47*0Sstevel@tonic-gate uid_t RunAsGid; 48*0Sstevel@tonic-gate char *RunAsUserName; 49*0Sstevel@tonic-gate int Verbose = 2; 50*0Sstevel@tonic-gate bool DontInitGroups = false; 51*0Sstevel@tonic-gate uid_t TrustedUid = 0; 52*0Sstevel@tonic-gate BITMAP256 DontBlameSendmail; 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate #define BUFSIZE 1024 55*0Sstevel@tonic-gate #define ISSEP(c) (sep == '\0' ? isascii(c) && isspace(c) : (c) == sep) 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate static void usage __P((char *)); 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate static void 60*0Sstevel@tonic-gate usage(progname) 61*0Sstevel@tonic-gate char *progname; 62*0Sstevel@tonic-gate { 63*0Sstevel@tonic-gate sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 64*0Sstevel@tonic-gate "Usage: %s [-C cffile] [-N] [-c cachesize] [-D commentchar]\n", 65*0Sstevel@tonic-gate progname); 66*0Sstevel@tonic-gate sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 67*0Sstevel@tonic-gate " %*s [-d] [-e] [-f] [-l] [-o] [-r] [-s] [-t delimiter]\n", 68*0Sstevel@tonic-gate (int) strlen(progname), ""); 69*0Sstevel@tonic-gate sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 70*0Sstevel@tonic-gate " %*s [-u] [-v] type mapname\n", 71*0Sstevel@tonic-gate (int) strlen(progname), ""); 72*0Sstevel@tonic-gate exit(EX_USAGE); 73*0Sstevel@tonic-gate } 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate int 76*0Sstevel@tonic-gate main(argc, argv) 77*0Sstevel@tonic-gate int argc; 78*0Sstevel@tonic-gate char **argv; 79*0Sstevel@tonic-gate { 80*0Sstevel@tonic-gate char *progname; 81*0Sstevel@tonic-gate char *cfile; 82*0Sstevel@tonic-gate bool inclnull = false; 83*0Sstevel@tonic-gate bool notrunc = false; 84*0Sstevel@tonic-gate bool allowreplace = false; 85*0Sstevel@tonic-gate bool allowempty = false; 86*0Sstevel@tonic-gate bool verbose = false; 87*0Sstevel@tonic-gate bool foldcase = true; 88*0Sstevel@tonic-gate bool unmake = false; 89*0Sstevel@tonic-gate char sep = '\0'; 90*0Sstevel@tonic-gate char comment = '#'; 91*0Sstevel@tonic-gate int exitstat; 92*0Sstevel@tonic-gate int opt; 93*0Sstevel@tonic-gate char *typename = NULL; 94*0Sstevel@tonic-gate char *mapname = NULL; 95*0Sstevel@tonic-gate unsigned int lineno; 96*0Sstevel@tonic-gate int st; 97*0Sstevel@tonic-gate int mode; 98*0Sstevel@tonic-gate int smode; 99*0Sstevel@tonic-gate int putflags = 0; 100*0Sstevel@tonic-gate long sff = SFF_ROOTOK|SFF_REGONLY; 101*0Sstevel@tonic-gate struct passwd *pw; 102*0Sstevel@tonic-gate SMDB_DATABASE *database; 103*0Sstevel@tonic-gate SMDB_CURSOR *cursor; 104*0Sstevel@tonic-gate SMDB_DBENT db_key, db_val; 105*0Sstevel@tonic-gate SMDB_DBPARAMS params; 106*0Sstevel@tonic-gate SMDB_USER_INFO user_info; 107*0Sstevel@tonic-gate char ibuf[BUFSIZE]; 108*0Sstevel@tonic-gate #if HASFCHOWN 109*0Sstevel@tonic-gate SM_FILE_T *cfp; 110*0Sstevel@tonic-gate char buf[MAXLINE]; 111*0Sstevel@tonic-gate #endif /* HASFCHOWN */ 112*0Sstevel@tonic-gate static char rnamebuf[MAXNAME]; /* holds RealUserName */ 113*0Sstevel@tonic-gate extern char *optarg; 114*0Sstevel@tonic-gate extern int optind; 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate memset(¶ms, '\0', sizeof params); 117*0Sstevel@tonic-gate params.smdbp_cache_size = 1024 * 1024; 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate progname = strrchr(argv[0], '/'); 120*0Sstevel@tonic-gate if (progname != NULL) 121*0Sstevel@tonic-gate progname++; 122*0Sstevel@tonic-gate else 123*0Sstevel@tonic-gate progname = argv[0]; 124*0Sstevel@tonic-gate cfile = getcfname(0, 0, SM_GET_SENDMAIL_CF, NULL); 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate clrbitmap(DontBlameSendmail); 127*0Sstevel@tonic-gate RunAsUid = RealUid = getuid(); 128*0Sstevel@tonic-gate RunAsGid = RealGid = getgid(); 129*0Sstevel@tonic-gate pw = getpwuid(RealUid); 130*0Sstevel@tonic-gate if (pw != NULL) 131*0Sstevel@tonic-gate (void) sm_strlcpy(rnamebuf, pw->pw_name, sizeof rnamebuf); 132*0Sstevel@tonic-gate else 133*0Sstevel@tonic-gate (void) sm_snprintf(rnamebuf, sizeof rnamebuf, 134*0Sstevel@tonic-gate "Unknown UID %d", (int) RealUid); 135*0Sstevel@tonic-gate RunAsUserName = RealUserName = rnamebuf; 136*0Sstevel@tonic-gate user_info.smdbu_id = RunAsUid; 137*0Sstevel@tonic-gate user_info.smdbu_group_id = RunAsGid; 138*0Sstevel@tonic-gate (void) sm_strlcpy(user_info.smdbu_name, RunAsUserName, 139*0Sstevel@tonic-gate SMDB_MAX_USER_NAME_LEN); 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate #define OPTIONS "C:D:Nc:deflorst:uv" 142*0Sstevel@tonic-gate while ((opt = getopt(argc, argv, OPTIONS)) != -1) 143*0Sstevel@tonic-gate { 144*0Sstevel@tonic-gate switch (opt) 145*0Sstevel@tonic-gate { 146*0Sstevel@tonic-gate case 'C': 147*0Sstevel@tonic-gate cfile = optarg; 148*0Sstevel@tonic-gate break; 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate case 'N': 151*0Sstevel@tonic-gate inclnull = true; 152*0Sstevel@tonic-gate break; 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate case 'c': 155*0Sstevel@tonic-gate params.smdbp_cache_size = atol(optarg); 156*0Sstevel@tonic-gate break; 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate case 'd': 159*0Sstevel@tonic-gate params.smdbp_allow_dup = true; 160*0Sstevel@tonic-gate break; 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate case 'e': 163*0Sstevel@tonic-gate allowempty = true; 164*0Sstevel@tonic-gate break; 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate case 'f': 167*0Sstevel@tonic-gate foldcase = false; 168*0Sstevel@tonic-gate break; 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate case 'D': 171*0Sstevel@tonic-gate comment = *optarg; 172*0Sstevel@tonic-gate break; 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate case 'l': 175*0Sstevel@tonic-gate smdb_print_available_types(); 176*0Sstevel@tonic-gate exit(EX_OK); 177*0Sstevel@tonic-gate break; 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate case 'o': 180*0Sstevel@tonic-gate notrunc = true; 181*0Sstevel@tonic-gate break; 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate case 'r': 184*0Sstevel@tonic-gate allowreplace = true; 185*0Sstevel@tonic-gate break; 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gate case 's': 188*0Sstevel@tonic-gate setbitn(DBS_MAPINUNSAFEDIRPATH, DontBlameSendmail); 189*0Sstevel@tonic-gate setbitn(DBS_WRITEMAPTOHARDLINK, DontBlameSendmail); 190*0Sstevel@tonic-gate setbitn(DBS_WRITEMAPTOSYMLINK, DontBlameSendmail); 191*0Sstevel@tonic-gate setbitn(DBS_LINKEDMAPINWRITABLEDIR, DontBlameSendmail); 192*0Sstevel@tonic-gate break; 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate case 't': 195*0Sstevel@tonic-gate if (optarg == NULL || *optarg == '\0') 196*0Sstevel@tonic-gate { 197*0Sstevel@tonic-gate sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 198*0Sstevel@tonic-gate "Invalid separator\n"); 199*0Sstevel@tonic-gate break; 200*0Sstevel@tonic-gate } 201*0Sstevel@tonic-gate sep = *optarg; 202*0Sstevel@tonic-gate break; 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate case 'u': 205*0Sstevel@tonic-gate unmake = true; 206*0Sstevel@tonic-gate break; 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate case 'v': 209*0Sstevel@tonic-gate verbose = true; 210*0Sstevel@tonic-gate break; 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate default: 213*0Sstevel@tonic-gate usage(progname); 214*0Sstevel@tonic-gate /* NOTREACHED */ 215*0Sstevel@tonic-gate } 216*0Sstevel@tonic-gate } 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate if (!bitnset(DBS_WRITEMAPTOSYMLINK, DontBlameSendmail)) 219*0Sstevel@tonic-gate sff |= SFF_NOSLINK; 220*0Sstevel@tonic-gate if (!bitnset(DBS_WRITEMAPTOHARDLINK, DontBlameSendmail)) 221*0Sstevel@tonic-gate sff |= SFF_NOHLINK; 222*0Sstevel@tonic-gate if (!bitnset(DBS_LINKEDMAPINWRITABLEDIR, DontBlameSendmail)) 223*0Sstevel@tonic-gate sff |= SFF_NOWLINK; 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate argc -= optind; 226*0Sstevel@tonic-gate argv += optind; 227*0Sstevel@tonic-gate if (argc != 2) 228*0Sstevel@tonic-gate { 229*0Sstevel@tonic-gate usage(progname); 230*0Sstevel@tonic-gate /* NOTREACHED */ 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate else 233*0Sstevel@tonic-gate { 234*0Sstevel@tonic-gate typename = argv[0]; 235*0Sstevel@tonic-gate mapname = argv[1]; 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate #if HASFCHOWN 239*0Sstevel@tonic-gate /* Find TrustedUser value in sendmail.cf */ 240*0Sstevel@tonic-gate if ((cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, cfile, SM_IO_RDONLY, 241*0Sstevel@tonic-gate NULL)) == NULL) 242*0Sstevel@tonic-gate { 243*0Sstevel@tonic-gate sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "makemap: %s: %s", 244*0Sstevel@tonic-gate cfile, sm_errstring(errno)); 245*0Sstevel@tonic-gate exit(EX_NOINPUT); 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL) 248*0Sstevel@tonic-gate { 249*0Sstevel@tonic-gate register char *b; 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate if ((b = strchr(buf, '\n')) != NULL) 252*0Sstevel@tonic-gate *b = '\0'; 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate b = buf; 255*0Sstevel@tonic-gate switch (*b++) 256*0Sstevel@tonic-gate { 257*0Sstevel@tonic-gate case 'O': /* option */ 258*0Sstevel@tonic-gate if (strncasecmp(b, " TrustedUser", 12) == 0 && 259*0Sstevel@tonic-gate !(isascii(b[12]) && isalnum(b[12]))) 260*0Sstevel@tonic-gate { 261*0Sstevel@tonic-gate b = strchr(b, '='); 262*0Sstevel@tonic-gate if (b == NULL) 263*0Sstevel@tonic-gate continue; 264*0Sstevel@tonic-gate while (isascii(*++b) && isspace(*b)) 265*0Sstevel@tonic-gate continue; 266*0Sstevel@tonic-gate if (isascii(*b) && isdigit(*b)) 267*0Sstevel@tonic-gate TrustedUid = atoi(b); 268*0Sstevel@tonic-gate else 269*0Sstevel@tonic-gate { 270*0Sstevel@tonic-gate TrustedUid = 0; 271*0Sstevel@tonic-gate pw = getpwnam(b); 272*0Sstevel@tonic-gate if (pw == NULL) 273*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, 274*0Sstevel@tonic-gate SM_TIME_DEFAULT, 275*0Sstevel@tonic-gate "TrustedUser: unknown user %s\n", b); 276*0Sstevel@tonic-gate else 277*0Sstevel@tonic-gate TrustedUid = pw->pw_uid; 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate # ifdef UID_MAX 281*0Sstevel@tonic-gate if (TrustedUid > UID_MAX) 282*0Sstevel@tonic-gate { 283*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, 284*0Sstevel@tonic-gate SM_TIME_DEFAULT, 285*0Sstevel@tonic-gate "TrustedUser: uid value (%ld) > UID_MAX (%ld)", 286*0Sstevel@tonic-gate (long) TrustedUid, 287*0Sstevel@tonic-gate (long) UID_MAX); 288*0Sstevel@tonic-gate TrustedUid = 0; 289*0Sstevel@tonic-gate } 290*0Sstevel@tonic-gate # endif /* UID_MAX */ 291*0Sstevel@tonic-gate break; 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate default: 296*0Sstevel@tonic-gate continue; 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate } 299*0Sstevel@tonic-gate (void) sm_io_close(cfp, SM_TIME_DEFAULT); 300*0Sstevel@tonic-gate #endif /* HASFCHOWN */ 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gate if (!params.smdbp_allow_dup && !allowreplace) 303*0Sstevel@tonic-gate putflags = SMDBF_NO_OVERWRITE; 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate if (unmake) 306*0Sstevel@tonic-gate { 307*0Sstevel@tonic-gate mode = O_RDONLY; 308*0Sstevel@tonic-gate smode = S_IRUSR; 309*0Sstevel@tonic-gate } 310*0Sstevel@tonic-gate else 311*0Sstevel@tonic-gate { 312*0Sstevel@tonic-gate mode = O_RDWR; 313*0Sstevel@tonic-gate if (!notrunc) 314*0Sstevel@tonic-gate { 315*0Sstevel@tonic-gate mode |= O_CREAT|O_TRUNC; 316*0Sstevel@tonic-gate sff |= SFF_CREAT; 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate smode = S_IWUSR; 319*0Sstevel@tonic-gate } 320*0Sstevel@tonic-gate 321*0Sstevel@tonic-gate params.smdbp_num_elements = 4096; 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate errno = smdb_open_database(&database, mapname, mode, smode, sff, 324*0Sstevel@tonic-gate typename, &user_info, ¶ms); 325*0Sstevel@tonic-gate if (errno != SMDBE_OK) 326*0Sstevel@tonic-gate { 327*0Sstevel@tonic-gate char *hint; 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate if (errno == SMDBE_UNSUPPORTED_DB_TYPE && 330*0Sstevel@tonic-gate (hint = smdb_db_definition(typename)) != NULL) 331*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 332*0Sstevel@tonic-gate "%s: Need to recompile with -D%s for %s support\n", 333*0Sstevel@tonic-gate progname, hint, typename); 334*0Sstevel@tonic-gate else 335*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 336*0Sstevel@tonic-gate "%s: error opening type %s map %s: %s\n", 337*0Sstevel@tonic-gate progname, typename, mapname, 338*0Sstevel@tonic-gate sm_errstring(errno)); 339*0Sstevel@tonic-gate exit(EX_CANTCREAT); 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate (void) database->smdb_sync(database, 0); 343*0Sstevel@tonic-gate 344*0Sstevel@tonic-gate if (!unmake && geteuid() == 0 && TrustedUid != 0) 345*0Sstevel@tonic-gate { 346*0Sstevel@tonic-gate errno = database->smdb_set_owner(database, TrustedUid, -1); 347*0Sstevel@tonic-gate if (errno != SMDBE_OK) 348*0Sstevel@tonic-gate { 349*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 350*0Sstevel@tonic-gate "WARNING: ownership change on %s failed %s", 351*0Sstevel@tonic-gate mapname, sm_errstring(errno)); 352*0Sstevel@tonic-gate } 353*0Sstevel@tonic-gate } 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate /* 356*0Sstevel@tonic-gate ** Copy the data 357*0Sstevel@tonic-gate */ 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate exitstat = EX_OK; 360*0Sstevel@tonic-gate if (unmake) 361*0Sstevel@tonic-gate { 362*0Sstevel@tonic-gate errno = database->smdb_cursor(database, &cursor, 0); 363*0Sstevel@tonic-gate if (errno != SMDBE_OK) 364*0Sstevel@tonic-gate { 365*0Sstevel@tonic-gate 366*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 367*0Sstevel@tonic-gate "%s: cannot make cursor for type %s map %s\n", 368*0Sstevel@tonic-gate progname, typename, mapname); 369*0Sstevel@tonic-gate exit(EX_SOFTWARE); 370*0Sstevel@tonic-gate } 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate memset(&db_key, '\0', sizeof db_key); 373*0Sstevel@tonic-gate memset(&db_val, '\0', sizeof db_val); 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gate for (lineno = 0; ; lineno++) 376*0Sstevel@tonic-gate { 377*0Sstevel@tonic-gate errno = cursor->smdbc_get(cursor, &db_key, &db_val, 378*0Sstevel@tonic-gate SMDB_CURSOR_GET_NEXT); 379*0Sstevel@tonic-gate if (errno != SMDBE_OK) 380*0Sstevel@tonic-gate break; 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 383*0Sstevel@tonic-gate "%.*s\t%.*s\n", 384*0Sstevel@tonic-gate (int) db_key.size, 385*0Sstevel@tonic-gate (char *) db_key.data, 386*0Sstevel@tonic-gate (int) db_val.size, 387*0Sstevel@tonic-gate (char *)db_val.data); 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gate } 390*0Sstevel@tonic-gate (void) cursor->smdbc_close(cursor); 391*0Sstevel@tonic-gate } 392*0Sstevel@tonic-gate else 393*0Sstevel@tonic-gate { 394*0Sstevel@tonic-gate lineno = 0; 395*0Sstevel@tonic-gate while (sm_io_fgets(smioin, SM_TIME_DEFAULT, ibuf, sizeof ibuf) 396*0Sstevel@tonic-gate != NULL) 397*0Sstevel@tonic-gate { 398*0Sstevel@tonic-gate register char *p; 399*0Sstevel@tonic-gate 400*0Sstevel@tonic-gate lineno++; 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate /* 403*0Sstevel@tonic-gate ** Parse the line. 404*0Sstevel@tonic-gate */ 405*0Sstevel@tonic-gate 406*0Sstevel@tonic-gate p = strchr(ibuf, '\n'); 407*0Sstevel@tonic-gate if (p != NULL) 408*0Sstevel@tonic-gate *p = '\0'; 409*0Sstevel@tonic-gate else if (!sm_io_eof(smioin)) 410*0Sstevel@tonic-gate { 411*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 412*0Sstevel@tonic-gate "%s: %s: line %u: line too long (%ld bytes max)\n", 413*0Sstevel@tonic-gate progname, mapname, lineno, 414*0Sstevel@tonic-gate (long) sizeof ibuf); 415*0Sstevel@tonic-gate exitstat = EX_DATAERR; 416*0Sstevel@tonic-gate continue; 417*0Sstevel@tonic-gate } 418*0Sstevel@tonic-gate 419*0Sstevel@tonic-gate if (ibuf[0] == '\0' || ibuf[0] == comment) 420*0Sstevel@tonic-gate continue; 421*0Sstevel@tonic-gate if (sep == '\0' && isascii(ibuf[0]) && isspace(ibuf[0])) 422*0Sstevel@tonic-gate { 423*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 424*0Sstevel@tonic-gate "%s: %s: line %u: syntax error (leading space)\n", 425*0Sstevel@tonic-gate progname, mapname, lineno); 426*0Sstevel@tonic-gate exitstat = EX_DATAERR; 427*0Sstevel@tonic-gate continue; 428*0Sstevel@tonic-gate } 429*0Sstevel@tonic-gate 430*0Sstevel@tonic-gate memset(&db_key, '\0', sizeof db_key); 431*0Sstevel@tonic-gate memset(&db_val, '\0', sizeof db_val); 432*0Sstevel@tonic-gate db_key.data = ibuf; 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gate for (p = ibuf; *p != '\0' && !(ISSEP(*p)); p++) 435*0Sstevel@tonic-gate { 436*0Sstevel@tonic-gate if (foldcase && isascii(*p) && isupper(*p)) 437*0Sstevel@tonic-gate *p = tolower(*p); 438*0Sstevel@tonic-gate } 439*0Sstevel@tonic-gate db_key.size = p - ibuf; 440*0Sstevel@tonic-gate if (inclnull) 441*0Sstevel@tonic-gate db_key.size++; 442*0Sstevel@tonic-gate 443*0Sstevel@tonic-gate if (*p != '\0') 444*0Sstevel@tonic-gate *p++ = '\0'; 445*0Sstevel@tonic-gate while (*p != '\0' && ISSEP(*p)) 446*0Sstevel@tonic-gate p++; 447*0Sstevel@tonic-gate if (!allowempty && *p == '\0') 448*0Sstevel@tonic-gate { 449*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 450*0Sstevel@tonic-gate "%s: %s: line %u: no RHS for LHS %s\n", 451*0Sstevel@tonic-gate progname, mapname, lineno, 452*0Sstevel@tonic-gate (char *) db_key.data); 453*0Sstevel@tonic-gate exitstat = EX_DATAERR; 454*0Sstevel@tonic-gate continue; 455*0Sstevel@tonic-gate } 456*0Sstevel@tonic-gate 457*0Sstevel@tonic-gate db_val.data = p; 458*0Sstevel@tonic-gate db_val.size = strlen(p); 459*0Sstevel@tonic-gate if (inclnull) 460*0Sstevel@tonic-gate db_val.size++; 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate /* 463*0Sstevel@tonic-gate ** Do the database insert. 464*0Sstevel@tonic-gate */ 465*0Sstevel@tonic-gate 466*0Sstevel@tonic-gate if (verbose) 467*0Sstevel@tonic-gate { 468*0Sstevel@tonic-gate (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 469*0Sstevel@tonic-gate "key=`%s', val=`%s'\n", 470*0Sstevel@tonic-gate (char *) db_key.data, 471*0Sstevel@tonic-gate (char *) db_val.data); 472*0Sstevel@tonic-gate } 473*0Sstevel@tonic-gate 474*0Sstevel@tonic-gate errno = database->smdb_put(database, &db_key, &db_val, 475*0Sstevel@tonic-gate putflags); 476*0Sstevel@tonic-gate switch (errno) 477*0Sstevel@tonic-gate { 478*0Sstevel@tonic-gate case SMDBE_KEY_EXIST: 479*0Sstevel@tonic-gate st = 1; 480*0Sstevel@tonic-gate break; 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate case 0: 483*0Sstevel@tonic-gate st = 0; 484*0Sstevel@tonic-gate break; 485*0Sstevel@tonic-gate 486*0Sstevel@tonic-gate default: 487*0Sstevel@tonic-gate st = -1; 488*0Sstevel@tonic-gate break; 489*0Sstevel@tonic-gate } 490*0Sstevel@tonic-gate 491*0Sstevel@tonic-gate if (st < 0) 492*0Sstevel@tonic-gate { 493*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 494*0Sstevel@tonic-gate "%s: %s: line %u: key %s: put error: %s\n", 495*0Sstevel@tonic-gate progname, mapname, lineno, 496*0Sstevel@tonic-gate (char *) db_key.data, 497*0Sstevel@tonic-gate sm_errstring(errno)); 498*0Sstevel@tonic-gate exitstat = EX_IOERR; 499*0Sstevel@tonic-gate } 500*0Sstevel@tonic-gate else if (st > 0) 501*0Sstevel@tonic-gate { 502*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 503*0Sstevel@tonic-gate "%s: %s: line %u: key %s: duplicate key\n", 504*0Sstevel@tonic-gate progname, mapname, 505*0Sstevel@tonic-gate lineno, 506*0Sstevel@tonic-gate (char *) db_key.data); 507*0Sstevel@tonic-gate exitstat = EX_DATAERR; 508*0Sstevel@tonic-gate } 509*0Sstevel@tonic-gate } 510*0Sstevel@tonic-gate } 511*0Sstevel@tonic-gate 512*0Sstevel@tonic-gate /* 513*0Sstevel@tonic-gate ** Now close the database. 514*0Sstevel@tonic-gate */ 515*0Sstevel@tonic-gate 516*0Sstevel@tonic-gate errno = database->smdb_close(database); 517*0Sstevel@tonic-gate if (errno != SMDBE_OK) 518*0Sstevel@tonic-gate { 519*0Sstevel@tonic-gate (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, 520*0Sstevel@tonic-gate "%s: close(%s): %s\n", 521*0Sstevel@tonic-gate progname, mapname, sm_errstring(errno)); 522*0Sstevel@tonic-gate exitstat = EX_IOERR; 523*0Sstevel@tonic-gate } 524*0Sstevel@tonic-gate smdb_free_database(database); 525*0Sstevel@tonic-gate 526*0Sstevel@tonic-gate exit(exitstat); 527*0Sstevel@tonic-gate 528*0Sstevel@tonic-gate /* NOTREACHED */ 529*0Sstevel@tonic-gate return exitstat; 530*0Sstevel@tonic-gate } 531