util.c
3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2007, 2010-2012, 2014 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#include "maidag.h"
void
close_fds ()
{
int i;
long fdlimit = MAXFD;
#if defined (HAVE_SYSCONF) && defined (_SC_OPEN_MAX)
fdlimit = sysconf (_SC_OPEN_MAX);
#elif defined (HAVE_GETDTABLESIZE)
fdlimit = getdtablesize ();
#endif
for (i = 3; i < fdlimit; i++)
close (i);
}
int
switch_user_id (struct mu_auth_data *auth, int user)
{
int rc;
uid_t uid;
if (!auth || auth->change_uid == 0)
return 0;
if (user)
uid = auth->uid;
else
uid = 0;
#if defined(HAVE_SETREUID)
rc = setreuid (0, uid);
#elif defined(HAVE_SETRESUID)
rc = setresuid (-1, uid, -1);
#elif defined(HAVE_SETEUID)
rc = seteuid (uid);
#else
# error "No way to reset user privileges?"
#endif
if (rc < 0)
maidag_error ("setreuid(0, %d): %s (r=%d, e=%d)",
uid, strerror (errno), getuid (), geteuid ());
return rc;
}
void
maidag_error (const char *fmt, ...)
{
va_list ap;
guess_retval (errno);
va_start (ap, fmt);
if (!mu_log_syslog)
{
vfprintf (stderr, fmt, ap);
fputc ('\n', stderr);
}
va_end (ap);
va_start (ap, fmt);
mu_verror (fmt, ap);
va_end (ap);
}
int temp_errors[] = {
#ifdef EAGAIN
EAGAIN, /* Try again */
#endif
#ifdef EBUSY
EBUSY, /* Device or resource busy */
#endif
#ifdef EPROCLIM
EPROCLIM, /* Too many processes */
#endif
#ifdef EUSERS
EUSERS, /* Too many users */
#endif
#ifdef ECONNABORTED
ECONNABORTED, /* Software caused connection abort */
#endif
#ifdef ECONNREFUSED
ECONNREFUSED, /* Connection refused */
#endif
#ifdef ECONNRESET
ECONNRESET, /* Connection reset by peer */
#endif
#ifdef EDEADLK
EDEADLK, /* Resource deadlock would occur */
#endif
#ifdef EDEADLOCK
EDEADLOCK, /* Resource deadlock would occur */
#endif
#ifdef EFBIG
EFBIG, /* File too large */
#endif
#ifdef EHOSTDOWN
EHOSTDOWN, /* Host is down */
#endif
#ifdef EHOSTUNREACH
EHOSTUNREACH, /* No route to host */
#endif
#ifdef EMFILE
EMFILE, /* Too many open files */
#endif
#ifdef ENETDOWN
ENETDOWN, /* Network is down */
#endif
#ifdef ENETUNREACH
ENETUNREACH, /* Network is unreachable */
#endif
#ifdef ENETRESET
ENETRESET, /* Network dropped connection because of reset */
#endif
#ifdef ENFILE
ENFILE, /* File table overflow */
#endif
#ifdef ENOBUFS
ENOBUFS, /* No buffer space available */
#endif
#ifdef ENOMEM
ENOMEM, /* Out of memory */
#endif
#ifdef ENOSPC
ENOSPC, /* No space left on device */
#endif
#ifdef EROFS
EROFS, /* Read-only file system */
#endif
#ifdef ESTALE
ESTALE, /* Stale NFS file handle */
#endif
#ifdef ETIMEDOUT
ETIMEDOUT, /* Connection timed out */
#endif
#ifdef EWOULDBLOCK
EWOULDBLOCK, /* Operation would block */
#endif
};
void
guess_retval (int ec)
{
int i;
/* Temporary failures override hard errors. */
if (exit_code == EX_TEMPFAIL)
return;
#ifdef EDQUOT
if (ec == EDQUOT)
{
exit_code = EX_QUOTA();
return;
}
#endif
for (i = 0; i < sizeof (temp_errors)/sizeof (temp_errors[0]); i++)
if (temp_errors[i] == ec)
{
exit_code = EX_TEMPFAIL;
return;
}
exit_code = EX_UNAVAILABLE;
}