xref: /onnv-gate/usr/src/cmd/sendmail/aux/editmap.c (revision 0:68f95e015346)
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 #ifndef lint
18*0Sstevel@tonic-gate SM_UNUSED(static char copyright[]) =
19*0Sstevel@tonic-gate "@(#) Copyright (c) 1998-2001 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 #endif /* ! lint */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #ifndef lint
27*0Sstevel@tonic-gate SM_UNUSED(static char id[]) = "@(#)$Id: editmap.c,v 1.24 2004/08/03 18:40:10 ca Exp $";
28*0Sstevel@tonic-gate #endif /* ! lint */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate #ifndef ISC_UNIX
33*0Sstevel@tonic-gate # include <sys/file.h>
34*0Sstevel@tonic-gate #endif /* ! ISC_UNIX */
35*0Sstevel@tonic-gate #include <ctype.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <unistd.h>
38*0Sstevel@tonic-gate #ifdef EX_OK
39*0Sstevel@tonic-gate # undef EX_OK		/* unistd.h may have another use for this */
40*0Sstevel@tonic-gate #endif /* EX_OK */
41*0Sstevel@tonic-gate #include <sysexits.h>
42*0Sstevel@tonic-gate #include <assert.h>
43*0Sstevel@tonic-gate #include <sendmail/sendmail.h>
44*0Sstevel@tonic-gate #include <sendmail/pathnames.h>
45*0Sstevel@tonic-gate #include <libsmdb/smdb.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate uid_t	RealUid;
48*0Sstevel@tonic-gate gid_t	RealGid;
49*0Sstevel@tonic-gate char	*RealUserName;
50*0Sstevel@tonic-gate uid_t	RunAsUid;
51*0Sstevel@tonic-gate uid_t	RunAsGid;
52*0Sstevel@tonic-gate char	*RunAsUserName;
53*0Sstevel@tonic-gate int	Verbose = 2;
54*0Sstevel@tonic-gate bool	DontInitGroups = false;
55*0Sstevel@tonic-gate uid_t	TrustedUid = 0;
56*0Sstevel@tonic-gate BITMAP256 DontBlameSendmail;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #define BUFSIZE		1024
59*0Sstevel@tonic-gate #define ISSEP(c) (isascii(c) && isspace(c))
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate static void usage __P((char *));
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static void
65*0Sstevel@tonic-gate usage(progname)
66*0Sstevel@tonic-gate 	char *progname;
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	fprintf(stderr,
69*0Sstevel@tonic-gate 		"Usage: %s [-C cffile] [-N] [-f] [-q|-u|-x] maptype mapname key [ \"value ...\" ]\n",
70*0Sstevel@tonic-gate 		progname);
71*0Sstevel@tonic-gate 	exit(EX_USAGE);
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate int
75*0Sstevel@tonic-gate main(argc, argv)
76*0Sstevel@tonic-gate 	int argc;
77*0Sstevel@tonic-gate 	char **argv;
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate 	char *progname;
80*0Sstevel@tonic-gate 	char *cfile;
81*0Sstevel@tonic-gate 	bool verbose = false;
82*0Sstevel@tonic-gate 	bool query = false;
83*0Sstevel@tonic-gate 	bool update = false;
84*0Sstevel@tonic-gate 	bool remove = false;
85*0Sstevel@tonic-gate 	bool inclnull = false;
86*0Sstevel@tonic-gate 	bool foldcase = true;
87*0Sstevel@tonic-gate 	unsigned int nops = 0;
88*0Sstevel@tonic-gate 	int exitstat;
89*0Sstevel@tonic-gate 	int opt;
90*0Sstevel@tonic-gate 	char *typename = NULL;
91*0Sstevel@tonic-gate 	char *mapname = NULL;
92*0Sstevel@tonic-gate 	char *keyname = NULL;
93*0Sstevel@tonic-gate 	char *value = NULL;
94*0Sstevel@tonic-gate 	int mode;
95*0Sstevel@tonic-gate 	int smode;
96*0Sstevel@tonic-gate 	int putflags = 0;
97*0Sstevel@tonic-gate 	long sff = SFF_ROOTOK|SFF_REGONLY;
98*0Sstevel@tonic-gate 	struct passwd *pw;
99*0Sstevel@tonic-gate 	SMDB_DATABASE *database;
100*0Sstevel@tonic-gate 	SMDB_DBENT db_key, db_val;
101*0Sstevel@tonic-gate 	SMDB_DBPARAMS params;
102*0Sstevel@tonic-gate 	SMDB_USER_INFO user_info;
103*0Sstevel@tonic-gate #if HASFCHOWN
104*0Sstevel@tonic-gate 	FILE *cfp;
105*0Sstevel@tonic-gate 	char buf[MAXLINE];
106*0Sstevel@tonic-gate #endif /* HASFCHOWN */
107*0Sstevel@tonic-gate 	static char rnamebuf[MAXNAME];	/* holds RealUserName */
108*0Sstevel@tonic-gate 	extern char *optarg;
109*0Sstevel@tonic-gate 	extern int optind;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	memset(&params, '\0', sizeof params);
112*0Sstevel@tonic-gate 	params.smdbp_cache_size = 1024 * 1024;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	progname = strrchr(argv[0], '/');
115*0Sstevel@tonic-gate 	if (progname != NULL)
116*0Sstevel@tonic-gate 		progname++;
117*0Sstevel@tonic-gate 	else
118*0Sstevel@tonic-gate 		progname = argv[0];
119*0Sstevel@tonic-gate 	cfile = _PATH_SENDMAILCF;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	clrbitmap(DontBlameSendmail);
122*0Sstevel@tonic-gate 	RunAsUid = RealUid = getuid();
123*0Sstevel@tonic-gate 	RunAsGid = RealGid = getgid();
124*0Sstevel@tonic-gate 	pw = getpwuid(RealUid);
125*0Sstevel@tonic-gate 	if (pw != NULL)
126*0Sstevel@tonic-gate 		(void) sm_strlcpy(rnamebuf, pw->pw_name, sizeof rnamebuf);
127*0Sstevel@tonic-gate 	else
128*0Sstevel@tonic-gate 		(void) sm_snprintf(rnamebuf, sizeof rnamebuf,
129*0Sstevel@tonic-gate 				   "Unknown UID %d", (int) RealUid);
130*0Sstevel@tonic-gate 	RunAsUserName = RealUserName = rnamebuf;
131*0Sstevel@tonic-gate 	user_info.smdbu_id = RunAsUid;
132*0Sstevel@tonic-gate 	user_info.smdbu_group_id = RunAsGid;
133*0Sstevel@tonic-gate 	(void) sm_strlcpy(user_info.smdbu_name, RunAsUserName,
134*0Sstevel@tonic-gate 			  SMDB_MAX_USER_NAME_LEN);
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate #define OPTIONS		"C:fquxvN"
137*0Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, OPTIONS)) != -1)
138*0Sstevel@tonic-gate 	{
139*0Sstevel@tonic-gate 		switch (opt)
140*0Sstevel@tonic-gate 		{
141*0Sstevel@tonic-gate 		  case 'C':
142*0Sstevel@tonic-gate 			cfile = optarg;
143*0Sstevel@tonic-gate 			break;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 		  case 'f':
146*0Sstevel@tonic-gate 			foldcase = false;
147*0Sstevel@tonic-gate 			break;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		  case 'q':
150*0Sstevel@tonic-gate 			query = true;
151*0Sstevel@tonic-gate 			nops++;
152*0Sstevel@tonic-gate 			break;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 		  case 'u':
155*0Sstevel@tonic-gate 			update = true;
156*0Sstevel@tonic-gate 			nops++;
157*0Sstevel@tonic-gate 			break;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 		  case 'x':
160*0Sstevel@tonic-gate 			remove = true;
161*0Sstevel@tonic-gate 			nops++;
162*0Sstevel@tonic-gate 			break;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		  case 'v':
165*0Sstevel@tonic-gate 			verbose = true;
166*0Sstevel@tonic-gate 			break;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 		  case 'N':
169*0Sstevel@tonic-gate 			inclnull = true;
170*0Sstevel@tonic-gate 			break;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 		  default:
173*0Sstevel@tonic-gate 			usage(progname);
174*0Sstevel@tonic-gate 			assert(0);  /* NOTREACHED */
175*0Sstevel@tonic-gate 		}
176*0Sstevel@tonic-gate 	}
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	if (!bitnset(DBS_WRITEMAPTOSYMLINK, DontBlameSendmail))
179*0Sstevel@tonic-gate 		sff |= SFF_NOSLINK;
180*0Sstevel@tonic-gate 	if (!bitnset(DBS_WRITEMAPTOHARDLINK, DontBlameSendmail))
181*0Sstevel@tonic-gate 		sff |= SFF_NOHLINK;
182*0Sstevel@tonic-gate 	if (!bitnset(DBS_LINKEDMAPINWRITABLEDIR, DontBlameSendmail))
183*0Sstevel@tonic-gate 		sff |= SFF_NOWLINK;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	argc -= optind;
186*0Sstevel@tonic-gate 	argv += optind;
187*0Sstevel@tonic-gate 	if ((nops != 1) ||
188*0Sstevel@tonic-gate 	    (query && argc != 3) ||
189*0Sstevel@tonic-gate 	    (remove && argc != 3) ||
190*0Sstevel@tonic-gate 	    (update && argc <= 3))
191*0Sstevel@tonic-gate 	{
192*0Sstevel@tonic-gate 		usage(progname);
193*0Sstevel@tonic-gate 		assert(0);  /* NOTREACHED */
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	typename = argv[0];
197*0Sstevel@tonic-gate 	mapname = argv[1];
198*0Sstevel@tonic-gate 	keyname = argv[2];
199*0Sstevel@tonic-gate 	if (update)
200*0Sstevel@tonic-gate 		value = argv[3];
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	if (foldcase)
203*0Sstevel@tonic-gate 	{
204*0Sstevel@tonic-gate 		char *p;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 		for (p = keyname; *p != '\0'; p++)
207*0Sstevel@tonic-gate 		{
208*0Sstevel@tonic-gate 			if (isascii(*p) && isupper(*p))
209*0Sstevel@tonic-gate 				*p = tolower(*p);
210*0Sstevel@tonic-gate 		}
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate #if HASFCHOWN
215*0Sstevel@tonic-gate 	/* Find TrustedUser value in sendmail.cf */
216*0Sstevel@tonic-gate 	if ((cfp = fopen(cfile, "r")) == NULL)
217*0Sstevel@tonic-gate 	{
218*0Sstevel@tonic-gate 		fprintf(stderr, "%s: %s: %s\n", progname,
219*0Sstevel@tonic-gate 			cfile, sm_errstring(errno));
220*0Sstevel@tonic-gate 		exit(EX_NOINPUT);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 	while (fgets(buf, sizeof(buf), cfp) != NULL)
223*0Sstevel@tonic-gate 	{
224*0Sstevel@tonic-gate 		register char *b;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 		if ((b = strchr(buf, '\n')) != NULL)
227*0Sstevel@tonic-gate 			*b = '\0';
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 		b = buf;
230*0Sstevel@tonic-gate 		switch (*b++)
231*0Sstevel@tonic-gate 		{
232*0Sstevel@tonic-gate 		  case 'O':		/* option */
233*0Sstevel@tonic-gate 			if (strncasecmp(b, " TrustedUser", 12) == 0 &&
234*0Sstevel@tonic-gate 			    !(isascii(b[12]) && isalnum(b[12])))
235*0Sstevel@tonic-gate 			{
236*0Sstevel@tonic-gate 				b = strchr(b, '=');
237*0Sstevel@tonic-gate 				if (b == NULL)
238*0Sstevel@tonic-gate 					continue;
239*0Sstevel@tonic-gate 				while (isascii(*++b) && isspace(*b))
240*0Sstevel@tonic-gate 					continue;
241*0Sstevel@tonic-gate 				if (isascii(*b) && isdigit(*b))
242*0Sstevel@tonic-gate 					TrustedUid = atoi(b);
243*0Sstevel@tonic-gate 				else
244*0Sstevel@tonic-gate 				{
245*0Sstevel@tonic-gate 					TrustedUid = 0;
246*0Sstevel@tonic-gate 					pw = getpwnam(b);
247*0Sstevel@tonic-gate 					if (pw == NULL)
248*0Sstevel@tonic-gate 						fprintf(stderr,
249*0Sstevel@tonic-gate 							"TrustedUser: unknown user %s\n", b);
250*0Sstevel@tonic-gate 					else
251*0Sstevel@tonic-gate 						TrustedUid = pw->pw_uid;
252*0Sstevel@tonic-gate 				}
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate # ifdef UID_MAX
255*0Sstevel@tonic-gate 				if (TrustedUid > UID_MAX)
256*0Sstevel@tonic-gate 				{
257*0Sstevel@tonic-gate 					fprintf(stderr,
258*0Sstevel@tonic-gate 						"TrustedUser: uid value (%ld) > UID_MAX (%ld)",
259*0Sstevel@tonic-gate 						(long) TrustedUid,
260*0Sstevel@tonic-gate 						(long) UID_MAX);
261*0Sstevel@tonic-gate 					TrustedUid = 0;
262*0Sstevel@tonic-gate 				}
263*0Sstevel@tonic-gate # endif /* UID_MAX */
264*0Sstevel@tonic-gate 				break;
265*0Sstevel@tonic-gate 			}
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 		  default:
269*0Sstevel@tonic-gate 			continue;
270*0Sstevel@tonic-gate 		}
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate 	(void) fclose(cfp);
273*0Sstevel@tonic-gate #endif /* HASFCHOWN */
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if (query)
276*0Sstevel@tonic-gate 	{
277*0Sstevel@tonic-gate 		mode = O_RDONLY;
278*0Sstevel@tonic-gate 		smode = S_IRUSR;
279*0Sstevel@tonic-gate 	}
280*0Sstevel@tonic-gate 	else
281*0Sstevel@tonic-gate 	{
282*0Sstevel@tonic-gate 		mode = O_RDWR | O_CREAT;
283*0Sstevel@tonic-gate 		sff |= SFF_CREAT|SFF_NOTEXCL;
284*0Sstevel@tonic-gate 		smode = S_IWUSR;
285*0Sstevel@tonic-gate 	}
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	params.smdbp_num_elements = 4096;
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	errno = smdb_open_database(&database, mapname, mode, smode, sff,
290*0Sstevel@tonic-gate 				   typename, &user_info, &params);
291*0Sstevel@tonic-gate 	if (errno != SMDBE_OK)
292*0Sstevel@tonic-gate 	{
293*0Sstevel@tonic-gate 		char *hint;
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 		if (errno == SMDBE_UNSUPPORTED_DB_TYPE &&
296*0Sstevel@tonic-gate 		    (hint = smdb_db_definition(typename)) != NULL)
297*0Sstevel@tonic-gate 			fprintf(stderr,
298*0Sstevel@tonic-gate 				"%s: Need to recompile with -D%s for %s support\n",
299*0Sstevel@tonic-gate 				progname, hint, typename);
300*0Sstevel@tonic-gate 		else
301*0Sstevel@tonic-gate 			fprintf(stderr,
302*0Sstevel@tonic-gate 				"%s: error opening type %s map %s: %s\n",
303*0Sstevel@tonic-gate 				progname, typename, mapname,
304*0Sstevel@tonic-gate 				sm_errstring(errno));
305*0Sstevel@tonic-gate 		exit(EX_CANTCREAT);
306*0Sstevel@tonic-gate 	}
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	(void) database->smdb_sync(database, 0);
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	if (geteuid() == 0 && TrustedUid != 0)
311*0Sstevel@tonic-gate 	{
312*0Sstevel@tonic-gate 		errno = database->smdb_set_owner(database, TrustedUid, -1);
313*0Sstevel@tonic-gate 		if (errno != SMDBE_OK)
314*0Sstevel@tonic-gate 		{
315*0Sstevel@tonic-gate 			fprintf(stderr,
316*0Sstevel@tonic-gate 				"WARNING: ownership change on %s failed %s",
317*0Sstevel@tonic-gate 				mapname, sm_errstring(errno));
318*0Sstevel@tonic-gate 		}
319*0Sstevel@tonic-gate 	}
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 	exitstat = EX_OK;
322*0Sstevel@tonic-gate 	if (query)
323*0Sstevel@tonic-gate 	{
324*0Sstevel@tonic-gate 		memset(&db_key, '\0', sizeof db_key);
325*0Sstevel@tonic-gate 		memset(&db_val, '\0', sizeof db_val);
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 		db_key.data = keyname;
328*0Sstevel@tonic-gate 		db_key.size = strlen(keyname);
329*0Sstevel@tonic-gate 		if (inclnull)
330*0Sstevel@tonic-gate 			db_key.size++;
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 		errno = database->smdb_get(database, &db_key, &db_val, 0);
333*0Sstevel@tonic-gate 		if (errno != SMDBE_OK)
334*0Sstevel@tonic-gate 		{
335*0Sstevel@tonic-gate 			/* XXX - Need to distinguish between not found */
336*0Sstevel@tonic-gate 			fprintf(stderr,
337*0Sstevel@tonic-gate 				"%s: couldn't find key %s in map %s\n",
338*0Sstevel@tonic-gate 				progname, keyname, mapname);
339*0Sstevel@tonic-gate 			exitstat = EX_UNAVAILABLE;
340*0Sstevel@tonic-gate 		}
341*0Sstevel@tonic-gate 		else
342*0Sstevel@tonic-gate 		{
343*0Sstevel@tonic-gate 			printf("%.*s\n", (int) db_val.size,
344*0Sstevel@tonic-gate 			       (char *) db_val.data);
345*0Sstevel@tonic-gate 		}
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate 	else if (update)
348*0Sstevel@tonic-gate 	{
349*0Sstevel@tonic-gate 		memset(&db_key, '\0', sizeof db_key);
350*0Sstevel@tonic-gate 		memset(&db_val, '\0', sizeof db_val);
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 		db_key.data = keyname;
353*0Sstevel@tonic-gate 		db_key.size = strlen(keyname);
354*0Sstevel@tonic-gate 		if (inclnull)
355*0Sstevel@tonic-gate 			db_key.size++;
356*0Sstevel@tonic-gate 		db_val.data = value;
357*0Sstevel@tonic-gate 		db_val.size = strlen(value);
358*0Sstevel@tonic-gate 		if (inclnull)
359*0Sstevel@tonic-gate 			db_val.size++;
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 		errno = database->smdb_put(database, &db_key, &db_val,
362*0Sstevel@tonic-gate 					   putflags);
363*0Sstevel@tonic-gate 		if (errno != SMDBE_OK)
364*0Sstevel@tonic-gate 		{
365*0Sstevel@tonic-gate 			fprintf(stderr,
366*0Sstevel@tonic-gate 				"%s: error updating (%s, %s) in map %s: %s\n",
367*0Sstevel@tonic-gate 				progname, keyname, value, mapname,
368*0Sstevel@tonic-gate 				sm_errstring(errno));
369*0Sstevel@tonic-gate 			exitstat = EX_IOERR;
370*0Sstevel@tonic-gate 		}
371*0Sstevel@tonic-gate 	}
372*0Sstevel@tonic-gate 	else if (remove)
373*0Sstevel@tonic-gate 	{
374*0Sstevel@tonic-gate 		memset(&db_key, '\0', sizeof db_key);
375*0Sstevel@tonic-gate 		memset(&db_val, '\0', sizeof db_val);
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 		db_key.data = keyname;
378*0Sstevel@tonic-gate 		db_key.size = strlen(keyname);
379*0Sstevel@tonic-gate 		if (inclnull)
380*0Sstevel@tonic-gate 			db_key.size++;
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 		errno = database->smdb_del(database, &db_key, 0);
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 		switch (errno)
385*0Sstevel@tonic-gate 		{
386*0Sstevel@tonic-gate 		case SMDBE_NOT_FOUND:
387*0Sstevel@tonic-gate 			fprintf(stderr,
388*0Sstevel@tonic-gate 				"%s: key %s doesn't exist in map %s\n",
389*0Sstevel@tonic-gate 				progname, keyname, mapname);
390*0Sstevel@tonic-gate 			/* Don't set exitstat */
391*0Sstevel@tonic-gate 			break;
392*0Sstevel@tonic-gate 		case SMDBE_OK:
393*0Sstevel@tonic-gate 			/* All's well */
394*0Sstevel@tonic-gate 			break;
395*0Sstevel@tonic-gate 		default:
396*0Sstevel@tonic-gate 			fprintf(stderr,
397*0Sstevel@tonic-gate 				"%s: couldn't remove key %s in map %s (error)\n",
398*0Sstevel@tonic-gate 				progname, keyname, mapname);
399*0Sstevel@tonic-gate 			exitstat = EX_IOERR;
400*0Sstevel@tonic-gate 			break;
401*0Sstevel@tonic-gate 		}
402*0Sstevel@tonic-gate 	}
403*0Sstevel@tonic-gate 	else
404*0Sstevel@tonic-gate 	{
405*0Sstevel@tonic-gate 		assert(0);  /* NOT REACHED */
406*0Sstevel@tonic-gate 	}
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	/*
409*0Sstevel@tonic-gate 	**  Now close the database.
410*0Sstevel@tonic-gate 	*/
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 	errno = database->smdb_close(database);
413*0Sstevel@tonic-gate 	if (errno != SMDBE_OK)
414*0Sstevel@tonic-gate 	{
415*0Sstevel@tonic-gate 		fprintf(stderr, "%s: close(%s): %s\n",
416*0Sstevel@tonic-gate 			progname, mapname, sm_errstring(errno));
417*0Sstevel@tonic-gate 		exitstat = EX_IOERR;
418*0Sstevel@tonic-gate 	}
419*0Sstevel@tonic-gate 	smdb_free_database(database);
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	exit(exitstat);
422*0Sstevel@tonic-gate 	/* NOTREACHED */
423*0Sstevel@tonic-gate 	return exitstat;
424*0Sstevel@tonic-gate }
425