190792Sgshapiro/*
2261370Sgshapiro * Copyright (c) 2001, 2003 Proofpoint, Inc. and its suppliers.
390792Sgshapiro *	All rights reserved.
490792Sgshapiro * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
590792Sgshapiro * Copyright (c) 1988, 1993
690792Sgshapiro *	The Regents of the University of California.  All rights reserved.
790792Sgshapiro *
890792Sgshapiro * By using this file, you agree to the terms and conditions set
990792Sgshapiro * forth in the LICENSE file which can be found at the top level of
1090792Sgshapiro * the sendmail distribution.
1190792Sgshapiro */
1290792Sgshapiro
1390792Sgshapiro#include <sm/gen.h>
14266711SgshapiroSM_RCSID("@(#)$Id: errstring.c,v 1.20 2013-11-22 20:51:42 ca Exp $")
1590792Sgshapiro
1690792Sgshapiro#include <errno.h>
1790792Sgshapiro#include <stdio.h>	/* sys_errlist, on some platforms */
1890792Sgshapiro
1990792Sgshapiro#include <sm/io.h>	/* sm_snprintf */
2090792Sgshapiro#include <sm/string.h>
2190792Sgshapiro#include <sm/errstring.h>
2290792Sgshapiro
2390792Sgshapiro#if NAMED_BIND
2490792Sgshapiro# include <netdb.h>
2590792Sgshapiro#endif
2690792Sgshapiro
2790792Sgshapiro#if LDAPMAP
2890792Sgshapiro# include <lber.h>
2990792Sgshapiro# include <ldap.h>			/* for LDAP error codes */
3090792Sgshapiro#endif /* LDAPMAP */
3190792Sgshapiro
3290792Sgshapiro/*
3390792Sgshapiro**  Notice: this file is used by libmilter. Please try to avoid
3490792Sgshapiro**	using libsm specific functions.
3590792Sgshapiro*/
3690792Sgshapiro
3790792Sgshapiro/*
3890792Sgshapiro**  SM_ERRSTRING -- return string description of error code
3990792Sgshapiro**
4090792Sgshapiro**	Parameters:
4190792Sgshapiro**		errnum -- the error number to translate
4290792Sgshapiro**
4390792Sgshapiro**	Returns:
4490792Sgshapiro**		A string description of errnum.
45120256Sgshapiro**
46120256Sgshapiro**	Note: this may point to a local (static) buffer.
4790792Sgshapiro*/
4890792Sgshapiro
4990792Sgshapiroconst char *
5090792Sgshapirosm_errstring(errnum)
5190792Sgshapiro	int errnum;
5290792Sgshapiro{
5390792Sgshapiro	char *ret;
5490792Sgshapiro
55120256Sgshapiro
5690792Sgshapiro	switch (errnum)
5790792Sgshapiro	{
5890792Sgshapiro	  case EPERM:
5990792Sgshapiro		/* SunOS gives "Not owner" -- this is the POSIX message */
6090792Sgshapiro		return "Operation not permitted";
6190792Sgshapiro
6290792Sgshapiro	/*
6390792Sgshapiro	**  Error messages used internally in sendmail.
6490792Sgshapiro	*/
6590792Sgshapiro
6690792Sgshapiro	  case E_SM_OPENTIMEOUT:
6790792Sgshapiro		return "Timeout on file open";
6890792Sgshapiro
6990792Sgshapiro	  case E_SM_NOSLINK:
7090792Sgshapiro		return "Symbolic links not allowed";
7190792Sgshapiro
7290792Sgshapiro	  case E_SM_NOHLINK:
7390792Sgshapiro		return "Hard links not allowed";
7490792Sgshapiro
7590792Sgshapiro	  case E_SM_REGONLY:
7690792Sgshapiro		return "Regular files only";
7790792Sgshapiro
7890792Sgshapiro	  case E_SM_ISEXEC:
7990792Sgshapiro		return "Executable files not allowed";
8090792Sgshapiro
8190792Sgshapiro	  case E_SM_WWDIR:
8290792Sgshapiro		return "World writable directory";
8390792Sgshapiro
8490792Sgshapiro	  case E_SM_GWDIR:
8590792Sgshapiro		return "Group writable directory";
8690792Sgshapiro
8790792Sgshapiro	  case E_SM_FILECHANGE:
8890792Sgshapiro		return "File changed after open";
8990792Sgshapiro
9090792Sgshapiro	  case E_SM_WWFILE:
9190792Sgshapiro		return "World writable file";
9290792Sgshapiro
9390792Sgshapiro	  case E_SM_GWFILE:
9490792Sgshapiro		return "Group writable file";
9590792Sgshapiro
9690792Sgshapiro	  case E_SM_GRFILE:
9790792Sgshapiro		return "Group readable file";
9890792Sgshapiro
9990792Sgshapiro	  case E_SM_WRFILE:
10090792Sgshapiro		return "World readable file";
10190792Sgshapiro
10290792Sgshapiro	/*
10390792Sgshapiro	**  DNS error messages.
10490792Sgshapiro	*/
10590792Sgshapiro
10690792Sgshapiro#if NAMED_BIND
10790792Sgshapiro	  case HOST_NOT_FOUND + E_DNSBASE:
10890792Sgshapiro		return "Name server: host not found";
10990792Sgshapiro
11090792Sgshapiro	  case TRY_AGAIN + E_DNSBASE:
11190792Sgshapiro		return "Name server: host name lookup failure";
11290792Sgshapiro
11390792Sgshapiro	  case NO_RECOVERY + E_DNSBASE:
11490792Sgshapiro		return "Name server: non-recoverable error";
11590792Sgshapiro
11690792Sgshapiro	  case NO_DATA + E_DNSBASE:
11790792Sgshapiro		return "Name server: no data known";
11890792Sgshapiro#endif /* NAMED_BIND */
11990792Sgshapiro
12090792Sgshapiro	/*
12190792Sgshapiro	**  libsmdb error messages.
12290792Sgshapiro	*/
12390792Sgshapiro
12490792Sgshapiro	  case SMDBE_MALLOC:
12590792Sgshapiro		return "Memory allocation failed";
12690792Sgshapiro
12790792Sgshapiro	  case SMDBE_GDBM_IS_BAD:
12890792Sgshapiro		return "GDBM is not supported";
12990792Sgshapiro
13090792Sgshapiro	  case SMDBE_UNSUPPORTED:
13190792Sgshapiro		return "Unsupported action";
13290792Sgshapiro
13390792Sgshapiro	  case SMDBE_DUPLICATE:
13490792Sgshapiro		return "Key already exists";
13590792Sgshapiro
13690792Sgshapiro	  case SMDBE_BAD_OPEN:
13790792Sgshapiro		return "Database open failed";
13890792Sgshapiro
13990792Sgshapiro	  case SMDBE_NOT_FOUND:
14090792Sgshapiro		return "Key not found";
14190792Sgshapiro
14290792Sgshapiro	  case SMDBE_UNKNOWN_DB_TYPE:
14390792Sgshapiro		return "Unknown database type";
14490792Sgshapiro
14590792Sgshapiro	  case SMDBE_UNSUPPORTED_DB_TYPE:
14690792Sgshapiro		return "Support for database type not compiled into this program";
14790792Sgshapiro
14890792Sgshapiro	  case SMDBE_INCOMPLETE:
14990792Sgshapiro		return "DB sync did not finish";
15090792Sgshapiro
15190792Sgshapiro	  case SMDBE_KEY_EMPTY:
15290792Sgshapiro		return "Key is empty";
15390792Sgshapiro
15490792Sgshapiro	  case SMDBE_KEY_EXIST:
15590792Sgshapiro		return "Key already exists";
15690792Sgshapiro
15790792Sgshapiro	  case SMDBE_LOCK_DEADLOCK:
15890792Sgshapiro		return "Locker killed to resolve deadlock";
15990792Sgshapiro
16090792Sgshapiro	  case SMDBE_LOCK_NOT_GRANTED:
16190792Sgshapiro		return "Lock unavailable";
16290792Sgshapiro
16390792Sgshapiro	  case SMDBE_LOCK_NOT_HELD:
16490792Sgshapiro		return "Lock not held by locker";
16590792Sgshapiro
16690792Sgshapiro	  case SMDBE_RUN_RECOVERY:
16790792Sgshapiro		return "Database panic, run recovery";
16890792Sgshapiro
16990792Sgshapiro	  case SMDBE_IO_ERROR:
17090792Sgshapiro		return "I/O error";
17190792Sgshapiro
17290792Sgshapiro	  case SMDBE_READ_ONLY:
17390792Sgshapiro		return "Database opened read-only";
17490792Sgshapiro
17590792Sgshapiro	  case SMDBE_DB_NAME_TOO_LONG:
17690792Sgshapiro		return "Name too long";
17790792Sgshapiro
17890792Sgshapiro	  case SMDBE_INVALID_PARAMETER:
17990792Sgshapiro		return "Invalid parameter";
18090792Sgshapiro
18190792Sgshapiro	  case SMDBE_ONLY_SUPPORTS_ONE_CURSOR:
18290792Sgshapiro		return "Only one cursor allowed";
18390792Sgshapiro
18490792Sgshapiro	  case SMDBE_NOT_A_VALID_CURSOR:
18590792Sgshapiro		return "Invalid cursor";
18690792Sgshapiro
18790792Sgshapiro	  case SMDBE_OLD_VERSION:
18890792Sgshapiro		return "Berkeley DB file is an old version, recreate it";
189120256Sgshapiro
190120256Sgshapiro	  case SMDBE_VERSION_MISMATCH:
191120256Sgshapiro		return "Berkeley DB version mismatch between include file and library";
192132943Sgshapiro
193132943Sgshapiro#if LDAPMAP
194132943Sgshapiro
195132943Sgshapiro	/*
196132943Sgshapiro	**  LDAP URL error messages.
197132943Sgshapiro	*/
198132943Sgshapiro
199132943Sgshapiro	/* OpenLDAP errors */
200132943Sgshapiro# ifdef LDAP_URL_ERR_MEM
201132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_MEM:
202132943Sgshapiro		return "LDAP URL can't allocate memory space";
203132943Sgshapiro# endif /* LDAP_URL_ERR_MEM */
204132943Sgshapiro
205132943Sgshapiro# ifdef LDAP_URL_ERR_PARAM
206132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_PARAM:
207132943Sgshapiro		return "LDAP URL parameter is bad";
208132943Sgshapiro# endif /* LDAP_URL_ERR_PARAM */
209132943Sgshapiro
210132943Sgshapiro# ifdef LDAP_URL_ERR_BADSCHEME
211132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_BADSCHEME:
212132943Sgshapiro		return "LDAP URL doesn't begin with \"ldap[si]://\"";
213132943Sgshapiro# endif /* LDAP_URL_ERR_BADSCHEME */
214132943Sgshapiro
215132943Sgshapiro# ifdef LDAP_URL_ERR_BADENCLOSURE
216132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_BADENCLOSURE:
217132943Sgshapiro		return "LDAP URL is missing trailing \">\"";
218132943Sgshapiro# endif /* LDAP_URL_ERR_BADENCLOSURE */
219132943Sgshapiro
220132943Sgshapiro# ifdef LDAP_URL_ERR_BADURL
221132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_BADURL:
222132943Sgshapiro		return "LDAP URL is bad";
223132943Sgshapiro# endif /* LDAP_URL_ERR_BADURL */
224132943Sgshapiro
225132943Sgshapiro# ifdef LDAP_URL_ERR_BADHOST
226132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_BADHOST:
227132943Sgshapiro		return "LDAP URL host port is bad";
228132943Sgshapiro# endif /* LDAP_URL_ERR_BADHOST */
229132943Sgshapiro
230132943Sgshapiro# ifdef LDAP_URL_ERR_BADATTRS
231132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_BADATTRS:
232132943Sgshapiro		return "LDAP URL bad (or missing) attributes";
233132943Sgshapiro# endif /* LDAP_URL_ERR_BADATTRS */
234132943Sgshapiro
235132943Sgshapiro# ifdef LDAP_URL_ERR_BADSCOPE
236132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_BADSCOPE:
237132943Sgshapiro		return "LDAP URL scope string is invalid (or missing)";
238132943Sgshapiro# endif /* LDAP_URL_ERR_BADSCOPE */
239132943Sgshapiro
240132943Sgshapiro# ifdef LDAP_URL_ERR_BADFILTER
241132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_BADFILTER:
242132943Sgshapiro		return "LDAP URL bad or missing filter";
243132943Sgshapiro# endif /* LDAP_URL_ERR_BADFILTER */
244132943Sgshapiro
245132943Sgshapiro# ifdef LDAP_URL_ERR_BADEXTS
246132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_BADEXTS:
247132943Sgshapiro		return "LDAP URL bad or missing extensions";
248132943Sgshapiro# endif /* LDAP_URL_ERR_BADEXTS */
249132943Sgshapiro
250132943Sgshapiro	/* Sun LDAP errors */
251132943Sgshapiro# ifdef LDAP_URL_ERR_NOTLDAP
252132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_NOTLDAP:
253132943Sgshapiro		return "LDAP URL doesn't begin with \"ldap://\"";
254132943Sgshapiro# endif /* LDAP_URL_ERR_NOTLDAP */
255132943Sgshapiro
256132943Sgshapiro# ifdef LDAP_URL_ERR_NODN
257132943Sgshapiro	  case E_LDAPURLBASE + LDAP_URL_ERR_NODN:
258132943Sgshapiro		return "LDAP URL has no DN (required)";
259132943Sgshapiro# endif /* LDAP_URL_ERR_NODN */
260132943Sgshapiro
261132943Sgshapiro#endif /* LDAPMAP */
26290792Sgshapiro	}
26390792Sgshapiro
264132943Sgshapiro#if LDAPMAP
265132943Sgshapiro
26690792Sgshapiro	/*
26790792Sgshapiro	**  LDAP error messages.
26890792Sgshapiro	*/
26990792Sgshapiro
27090792Sgshapiro	if (errnum >= E_LDAPBASE)
27190792Sgshapiro		return ldap_err2string(errnum - E_LDAPBASE);
27290792Sgshapiro#endif /* LDAPMAP */
27390792Sgshapiro
27490792Sgshapiro	ret = strerror(errnum);
27590792Sgshapiro	if (ret == NULL)
27690792Sgshapiro	{
27790792Sgshapiro		static char buf[30];
27890792Sgshapiro
27990792Sgshapiro		(void) sm_snprintf(buf, sizeof buf, "Error %d", errnum);
28090792Sgshapiro		return buf;
28190792Sgshapiro	}
28290792Sgshapiro	return ret;
28390792Sgshapiro}
284