17c478bd9Sstevel@tonic-gate /*
2e9af4bc0SJohn Beck  * Copyright (c) 1998-2001, 2008 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Eric P. Allman.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 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-2001 Sendmail, Inc. and its suppliers.\n\
187c478bd9Sstevel@tonic-gate 	All rights reserved.\n\
197c478bd9Sstevel@tonic-gate      Copyright (c) 1983 Eric P. Allman.  All rights reserved.\n\
207c478bd9Sstevel@tonic-gate      Copyright (c) 1988, 1993\n\
217c478bd9Sstevel@tonic-gate 	The Regents of the University of California.  All rights reserved.\n")
227c478bd9Sstevel@tonic-gate 
23e9af4bc0SJohn Beck SM_IDSTR(id, "@(#)$Id: praliases.c,v 8.96 2008/07/10 20:13:10 ca Exp $")
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #include <ctype.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #ifdef EX_OK
307c478bd9Sstevel@tonic-gate # undef EX_OK		/* unistd.h may have another use for this */
317c478bd9Sstevel@tonic-gate #endif /* EX_OK */
327c478bd9Sstevel@tonic-gate #include <sysexits.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifndef NOT_SENDMAIL
367c478bd9Sstevel@tonic-gate # define NOT_SENDMAIL
377c478bd9Sstevel@tonic-gate #endif /* ! NOT_SENDMAIL */
387c478bd9Sstevel@tonic-gate #include <sendmail/sendmail.h>
397c478bd9Sstevel@tonic-gate #include <sendmail/pathnames.h>
407c478bd9Sstevel@tonic-gate #include <libsmdb/smdb.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static void praliases __P((char *, int, char **));
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate uid_t	RealUid;
457c478bd9Sstevel@tonic-gate gid_t	RealGid;
467c478bd9Sstevel@tonic-gate char	*RealUserName;
477c478bd9Sstevel@tonic-gate uid_t	RunAsUid;
487800901eSjbeck gid_t	RunAsGid;
497c478bd9Sstevel@tonic-gate char	*RunAsUserName;
507c478bd9Sstevel@tonic-gate int	Verbose = 2;
517c478bd9Sstevel@tonic-gate bool	DontInitGroups = false;
527c478bd9Sstevel@tonic-gate uid_t	TrustedUid = 0;
537c478bd9Sstevel@tonic-gate BITMAP256 DontBlameSendmail;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate # define DELIMITERS		" ,/"
567c478bd9Sstevel@tonic-gate # define PATH_SEPARATOR		':'
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate int
main(argc,argv)597c478bd9Sstevel@tonic-gate main(argc, argv)
607c478bd9Sstevel@tonic-gate 	int argc;
617c478bd9Sstevel@tonic-gate 	char **argv;
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	char *cfile;
647c478bd9Sstevel@tonic-gate 	char *filename = NULL;
657c478bd9Sstevel@tonic-gate 	SM_FILE_T *cfp;
667c478bd9Sstevel@tonic-gate 	int ch;
677c478bd9Sstevel@tonic-gate 	char afilebuf[MAXLINE];
687c478bd9Sstevel@tonic-gate 	char buf[MAXLINE];
697c478bd9Sstevel@tonic-gate 	struct passwd *pw;
707c478bd9Sstevel@tonic-gate 	static char rnamebuf[MAXNAME];
717c478bd9Sstevel@tonic-gate 	extern char *optarg;
727c478bd9Sstevel@tonic-gate 	extern int optind;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	clrbitmap(DontBlameSendmail);
757c478bd9Sstevel@tonic-gate 	RunAsUid = RealUid = getuid();
767c478bd9Sstevel@tonic-gate 	RunAsGid = RealGid = getgid();
777c478bd9Sstevel@tonic-gate 	pw = getpwuid(RealUid);
787c478bd9Sstevel@tonic-gate 	if (pw != NULL)
797c478bd9Sstevel@tonic-gate 	{
807c478bd9Sstevel@tonic-gate 		if (strlen(pw->pw_name) > MAXNAME - 1)
817c478bd9Sstevel@tonic-gate 			pw->pw_name[MAXNAME] = 0;
827c478bd9Sstevel@tonic-gate 		sm_snprintf(rnamebuf, sizeof rnamebuf, "%s", pw->pw_name);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 	else
857c478bd9Sstevel@tonic-gate 		(void) sm_snprintf(rnamebuf, sizeof rnamebuf,
867c478bd9Sstevel@tonic-gate 		    "Unknown UID %d", (int) RealUid);
877c478bd9Sstevel@tonic-gate 	RunAsUserName = RealUserName = rnamebuf;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	cfile = getcfname(0, 0, SM_GET_SENDMAIL_CF, NULL);
907c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "C:f:")) != -1)
917c478bd9Sstevel@tonic-gate 	{
927c478bd9Sstevel@tonic-gate 		switch ((char)ch) {
937c478bd9Sstevel@tonic-gate 		case 'C':
947c478bd9Sstevel@tonic-gate 			cfile = optarg;
957c478bd9Sstevel@tonic-gate 			break;
967c478bd9Sstevel@tonic-gate 		case 'f':
977c478bd9Sstevel@tonic-gate 			filename = optarg;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		case '?':
1007c478bd9Sstevel@tonic-gate 		default:
1017c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
102e9af4bc0SJohn Beck 			    "usage: praliases [-C cffile] [-f aliasfile]"
103e9af4bc0SJohn Beck 			    " [key ...]\n");
1047c478bd9Sstevel@tonic-gate 			exit(EX_USAGE);
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	argc -= optind;
1087c478bd9Sstevel@tonic-gate 	argv += optind;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (filename != NULL)
1117c478bd9Sstevel@tonic-gate 	{
1127c478bd9Sstevel@tonic-gate 		praliases(filename, argc, argv);
1137c478bd9Sstevel@tonic-gate 		exit(EX_OK);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if ((cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, cfile, SM_IO_RDONLY,
1177c478bd9Sstevel@tonic-gate 			      NULL)) == NULL)
1187c478bd9Sstevel@tonic-gate 	{
1197c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
1207c478bd9Sstevel@tonic-gate 				     "praliases: %s: %s\n", cfile,
1217c478bd9Sstevel@tonic-gate 				     sm_errstring(errno));
1227c478bd9Sstevel@tonic-gate 		exit(EX_NOINPUT);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
1267c478bd9Sstevel@tonic-gate 	{
1277c478bd9Sstevel@tonic-gate 		register char *b, *p;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		b = strchr(buf, '\n');
1307c478bd9Sstevel@tonic-gate 		if (b != NULL)
1317c478bd9Sstevel@tonic-gate 			*b = '\0';
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		b = buf;
1347c478bd9Sstevel@tonic-gate 		switch (*b++)
1357c478bd9Sstevel@tonic-gate 		{
1367c478bd9Sstevel@tonic-gate 		  case 'O':		/* option -- see if alias file */
1377c478bd9Sstevel@tonic-gate 			if (sm_strncasecmp(b, " AliasFile", 10) == 0 &&
1387c478bd9Sstevel@tonic-gate 			    !(isascii(b[10]) && isalnum(b[10])))
1397c478bd9Sstevel@tonic-gate 			{
1407c478bd9Sstevel@tonic-gate 				/* new form -- find value */
1417c478bd9Sstevel@tonic-gate 				b = strchr(b, '=');
1427c478bd9Sstevel@tonic-gate 				if (b == NULL)
1437c478bd9Sstevel@tonic-gate 					continue;
1447c478bd9Sstevel@tonic-gate 				while (isascii(*++b) && isspace(*b))
1457c478bd9Sstevel@tonic-gate 					continue;
1467c478bd9Sstevel@tonic-gate 			}
1477c478bd9Sstevel@tonic-gate 			else if (*b++ != 'A')
1487c478bd9Sstevel@tonic-gate 			{
1497c478bd9Sstevel@tonic-gate 				/* something else boring */
1507c478bd9Sstevel@tonic-gate 				continue;
1517c478bd9Sstevel@tonic-gate 			}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 			/* this is the A or AliasFile option -- save it */
1547c478bd9Sstevel@tonic-gate 			if (sm_strlcpy(afilebuf, b, sizeof afilebuf) >=
1557c478bd9Sstevel@tonic-gate 			    sizeof afilebuf)
1567c478bd9Sstevel@tonic-gate 			{
1577c478bd9Sstevel@tonic-gate 				(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
1587c478bd9Sstevel@tonic-gate 				    "praliases: AliasFile filename too long: %.30s\n",
1597c478bd9Sstevel@tonic-gate 					b);
1607c478bd9Sstevel@tonic-gate 				(void) sm_io_close(cfp, SM_TIME_DEFAULT);
1617c478bd9Sstevel@tonic-gate 				exit(EX_CONFIG);
1627c478bd9Sstevel@tonic-gate 			}
1637c478bd9Sstevel@tonic-gate 			b = afilebuf;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 			for (p = b; p != NULL; )
1667c478bd9Sstevel@tonic-gate 			{
1677c478bd9Sstevel@tonic-gate 				while (isascii(*p) && isspace(*p))
1687c478bd9Sstevel@tonic-gate 					p++;
1697c478bd9Sstevel@tonic-gate 				if (*p == '\0')
1707c478bd9Sstevel@tonic-gate 					break;
1717c478bd9Sstevel@tonic-gate 				b = p;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 				p = strpbrk(p, DELIMITERS);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 				/* find end of spec */
1767c478bd9Sstevel@tonic-gate 				if (p != NULL)
1777c478bd9Sstevel@tonic-gate 				{
1787c478bd9Sstevel@tonic-gate 					bool quoted = false;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 					for (; *p != '\0'; p++)
1817c478bd9Sstevel@tonic-gate 					{
1827c478bd9Sstevel@tonic-gate 						/*
1837c478bd9Sstevel@tonic-gate 						**  Don't break into a quoted
1847c478bd9Sstevel@tonic-gate 						**  string.
1857c478bd9Sstevel@tonic-gate 						*/
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 						if (*p == '"')
1887c478bd9Sstevel@tonic-gate 							quoted = !quoted;
1897c478bd9Sstevel@tonic-gate 						else if (*p == ',' && !quoted)
1907c478bd9Sstevel@tonic-gate 							break;
1917c478bd9Sstevel@tonic-gate 					}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 					/* No more alias specs follow */
1947c478bd9Sstevel@tonic-gate 					if (*p == '\0')
1957c478bd9Sstevel@tonic-gate 					{
1967c478bd9Sstevel@tonic-gate 						/* chop trailing whitespace */
1977c478bd9Sstevel@tonic-gate 						while (isascii(*p) &&
1987c478bd9Sstevel@tonic-gate 						       isspace(*p) &&
1997c478bd9Sstevel@tonic-gate 						       p > b)
2007c478bd9Sstevel@tonic-gate 							p--;
2017c478bd9Sstevel@tonic-gate 						*p = '\0';
2027c478bd9Sstevel@tonic-gate 						p = NULL;
2037c478bd9Sstevel@tonic-gate 					}
2047c478bd9Sstevel@tonic-gate 				}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 				if (p != NULL)
2077c478bd9Sstevel@tonic-gate 				{
2087c478bd9Sstevel@tonic-gate 					char *e = p - 1;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 					/* chop trailing whitespace */
2117c478bd9Sstevel@tonic-gate 					while (isascii(*e) &&
2127c478bd9Sstevel@tonic-gate 					       isspace(*e) &&
2137c478bd9Sstevel@tonic-gate 					       e > b)
2147c478bd9Sstevel@tonic-gate 						e--;
2157c478bd9Sstevel@tonic-gate 					*++e = '\0';
2167c478bd9Sstevel@tonic-gate 					*p++ = '\0';
2177c478bd9Sstevel@tonic-gate 				}
2187c478bd9Sstevel@tonic-gate 				praliases(b, argc, argv);
2197c478bd9Sstevel@tonic-gate 			}
220fec46055SToomas Soome 			/* FALLTHROUGH */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		  default:
2237c478bd9Sstevel@tonic-gate 			continue;
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	(void) sm_io_close(cfp, SM_TIME_DEFAULT);
2277c478bd9Sstevel@tonic-gate 	exit(EX_OK);
2287c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2297c478bd9Sstevel@tonic-gate 	return EX_OK;
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static void
praliases(filename,argc,argv)2337c478bd9Sstevel@tonic-gate praliases(filename, argc, argv)
2347c478bd9Sstevel@tonic-gate 	char *filename;
2357c478bd9Sstevel@tonic-gate 	int argc;
2367c478bd9Sstevel@tonic-gate 	char **argv;
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	int result;
2397c478bd9Sstevel@tonic-gate 	char *colon;
2407c478bd9Sstevel@tonic-gate 	char *db_name;
2417c478bd9Sstevel@tonic-gate 	char *db_type;
2427c478bd9Sstevel@tonic-gate 	SMDB_DATABASE *database = NULL;
2437c478bd9Sstevel@tonic-gate 	SMDB_CURSOR *cursor = NULL;
2447c478bd9Sstevel@tonic-gate 	SMDB_DBENT db_key, db_value;
2457c478bd9Sstevel@tonic-gate 	SMDB_DBPARAMS params;
2467c478bd9Sstevel@tonic-gate 	SMDB_USER_INFO user_info;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	colon = strchr(filename, PATH_SEPARATOR);
2497c478bd9Sstevel@tonic-gate 	if (colon == NULL)
2507c478bd9Sstevel@tonic-gate 	{
2517c478bd9Sstevel@tonic-gate 		db_name = filename;
2527c478bd9Sstevel@tonic-gate 		db_type = SMDB_TYPE_DEFAULT;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	else
2557c478bd9Sstevel@tonic-gate 	{
2567c478bd9Sstevel@tonic-gate 		*colon = '\0';
2577c478bd9Sstevel@tonic-gate 		db_name = colon + 1;
2587c478bd9Sstevel@tonic-gate 		db_type = filename;
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/* clean off arguments */
2627c478bd9Sstevel@tonic-gate 	for (;;)
2637c478bd9Sstevel@tonic-gate 	{
2647c478bd9Sstevel@tonic-gate 		while (isascii(*db_name) && isspace(*db_name))
2657c478bd9Sstevel@tonic-gate 			db_name++;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		if (*db_name != '-')
2687c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 		while (*db_name != '\0' &&
2707c478bd9Sstevel@tonic-gate 		       !(isascii(*db_name) && isspace(*db_name)))
2717c478bd9Sstevel@tonic-gate 			db_name++;
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/* Skip non-file based DB types */
2757c478bd9Sstevel@tonic-gate 	if (db_type != NULL && *db_type != '\0')
2767c478bd9Sstevel@tonic-gate 	{
2777c478bd9Sstevel@tonic-gate 		if (db_type != SMDB_TYPE_DEFAULT &&
2787c478bd9Sstevel@tonic-gate 		    strcmp(db_type, "hash") != 0 &&
2797c478bd9Sstevel@tonic-gate 		    strcmp(db_type, "btree") != 0 &&
2807c478bd9Sstevel@tonic-gate 		    strcmp(db_type, "dbm") != 0)
2817c478bd9Sstevel@tonic-gate 		{
2827c478bd9Sstevel@tonic-gate 			sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
2837c478bd9Sstevel@tonic-gate 				      "praliases: Skipping non-file based alias type %s\n",
2847c478bd9Sstevel@tonic-gate 				db_type);
2857c478bd9Sstevel@tonic-gate 			return;
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (*db_name == '\0' || (db_type != NULL && *db_type == '\0'))
2907c478bd9Sstevel@tonic-gate 	{
2917c478bd9Sstevel@tonic-gate 		if (colon != NULL)
2927c478bd9Sstevel@tonic-gate 			*colon = ':';
2937c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
2947c478bd9Sstevel@tonic-gate 		    "praliases: illegal alias specification: %s\n", filename);
2957c478bd9Sstevel@tonic-gate 		goto fatal;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	memset(&params, '\0', sizeof params);
2997c478bd9Sstevel@tonic-gate 	params.smdbp_cache_size = 1024 * 1024;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	user_info.smdbu_id = RunAsUid;
3027c478bd9Sstevel@tonic-gate 	user_info.smdbu_group_id = RunAsGid;
3037c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(user_info.smdbu_name, RunAsUserName,
3047c478bd9Sstevel@tonic-gate 			  SMDB_MAX_USER_NAME_LEN);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	result = smdb_open_database(&database, db_name, O_RDONLY, 0,
3077c478bd9Sstevel@tonic-gate 				    SFF_ROOTOK, db_type, &user_info, &params);
3087c478bd9Sstevel@tonic-gate 	if (result != SMDBE_OK)
3097c478bd9Sstevel@tonic-gate 	{
3107c478bd9Sstevel@tonic-gate 		sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
3117c478bd9Sstevel@tonic-gate 			      "praliases: %s: open: %s\n",
3127c478bd9Sstevel@tonic-gate 			      db_name, sm_errstring(result));
3137c478bd9Sstevel@tonic-gate 		goto fatal;
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (argc == 0)
3177c478bd9Sstevel@tonic-gate 	{
3187c478bd9Sstevel@tonic-gate 		memset(&db_key, '\0', sizeof db_key);
3197c478bd9Sstevel@tonic-gate 		memset(&db_value, '\0', sizeof db_value);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 		result = database->smdb_cursor(database, &cursor, 0);
3227c478bd9Sstevel@tonic-gate 		if (result != SMDBE_OK)
3237c478bd9Sstevel@tonic-gate 		{
3247c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
3257c478bd9Sstevel@tonic-gate 			    "praliases: %s: set cursor: %s\n", db_name,
3267c478bd9Sstevel@tonic-gate 			    sm_errstring(result));
3277c478bd9Sstevel@tonic-gate 			goto fatal;
3287c478bd9Sstevel@tonic-gate 		}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 		while ((result = cursor->smdbc_get(cursor, &db_key, &db_value,
3317c478bd9Sstevel@tonic-gate 						   SMDB_CURSOR_GET_NEXT)) ==
3327c478bd9Sstevel@tonic-gate 						   SMDBE_OK)
3337c478bd9Sstevel@tonic-gate 		{
3347c478bd9Sstevel@tonic-gate #if 0
3357c478bd9Sstevel@tonic-gate 			/* skip magic @:@ entry */
3367c478bd9Sstevel@tonic-gate 			if (db_key.size == 2 &&
3377c478bd9Sstevel@tonic-gate 			    db_key.data[0] == '@' &&
3387c478bd9Sstevel@tonic-gate 			    db_key.data[1] == '\0' &&
3397c478bd9Sstevel@tonic-gate 			    db_value.size == 2 &&
3407c478bd9Sstevel@tonic-gate 			    db_value.data[0] == '@' &&
3417c478bd9Sstevel@tonic-gate 			    db_value.data[1] == '\0')
3427c478bd9Sstevel@tonic-gate 				continue;
3437c478bd9Sstevel@tonic-gate #endif /* 0 */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3467c478bd9Sstevel@tonic-gate 					     "%.*s:%.*s\n",
3477c478bd9Sstevel@tonic-gate 					     (int) db_key.size,
3487c478bd9Sstevel@tonic-gate 					     (char *) db_key.data,
3497c478bd9Sstevel@tonic-gate 					     (int) db_value.size,
3507c478bd9Sstevel@tonic-gate 					     (char *) db_value.data);
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		if (result != SMDBE_OK && result != SMDBE_LAST_ENTRY)
3547c478bd9Sstevel@tonic-gate 		{
3557c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
3567c478bd9Sstevel@tonic-gate 				"praliases: %s: get value at cursor: %s\n",
3577c478bd9Sstevel@tonic-gate 				db_name, sm_errstring(result));
3587c478bd9Sstevel@tonic-gate 			goto fatal;
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 	else for (; *argv != NULL; ++argv)
3627c478bd9Sstevel@tonic-gate 	{
3637c478bd9Sstevel@tonic-gate 		int get_res;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		memset(&db_key, '\0', sizeof db_key);
3667c478bd9Sstevel@tonic-gate 		memset(&db_value, '\0', sizeof db_value);
3677c478bd9Sstevel@tonic-gate 		db_key.data = *argv;
3687c478bd9Sstevel@tonic-gate 		db_key.size = strlen(*argv);
3697c478bd9Sstevel@tonic-gate 		get_res = database->smdb_get(database, &db_key, &db_value, 0);
3707c478bd9Sstevel@tonic-gate 		if (get_res == SMDBE_NOT_FOUND)
3717c478bd9Sstevel@tonic-gate 		{
3727c478bd9Sstevel@tonic-gate 			db_key.size++;
3737c478bd9Sstevel@tonic-gate 			get_res = database->smdb_get(database, &db_key,
3747c478bd9Sstevel@tonic-gate 						     &db_value, 0);
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 		if (get_res == SMDBE_OK)
3777c478bd9Sstevel@tonic-gate 		{
3787c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3797c478bd9Sstevel@tonic-gate 					     "%.*s:%.*s\n",
3807c478bd9Sstevel@tonic-gate 					     (int) db_key.size,
3817c478bd9Sstevel@tonic-gate 					     (char *) db_key.data,
3827c478bd9Sstevel@tonic-gate 					     (int) db_value.size,
3837c478bd9Sstevel@tonic-gate 					     (char *) db_value.data);
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 		else
3867c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3877c478bd9Sstevel@tonic-gate 					     "%s: No such key\n",
3887c478bd9Sstevel@tonic-gate 					     (char *)db_key.data);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate  fatal:
3927c478bd9Sstevel@tonic-gate 	if (cursor != NULL)
3937c478bd9Sstevel@tonic-gate 		(void) cursor->smdbc_close(cursor);
3947c478bd9Sstevel@tonic-gate 	if (database != NULL)
3957c478bd9Sstevel@tonic-gate 		(void) database->smdb_close(database);
3967c478bd9Sstevel@tonic-gate 	if (colon != NULL)
3977c478bd9Sstevel@tonic-gate 		*colon = ':';
3987c478bd9Sstevel@tonic-gate 	return;
3997c478bd9Sstevel@tonic-gate }
400