Mercurial > masqmail
annotate src/local.c @ 351:4c046561582a
Minor refactoring
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Thu, 14 Jul 2011 14:12:32 +0200 (2011-07-14) |
parents | 63efd381e27b |
children | 41958685480d |
rev | line source |
---|---|
0 | 1 /* MasqMail |
2 Copyright (C) 1999-2001 Oliver Kurth | |
224 | 3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de> |
0 | 4 |
5 This program is free software; you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation; either version 2 of the License, or | |
8 (at your option) any later version. | |
9 | |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 */ | |
19 | |
15 | 20 #include <sys/wait.h> |
169 | 21 #include <sys/stat.h> |
15 | 22 |
0 | 23 #include "masqmail.h" |
24 #include "peopen.h" | |
25 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
26 static void |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
27 message_stream(FILE * out, message * msg, GList * hdr_list, guint flags) |
0 | 28 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
29 time_t now = time(NULL); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
30 GList *node; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
31 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
32 if (flags & MSGSTR_FROMLINE) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
33 fprintf(out, "From <%s@%s> %s", msg->return_path->local_part, msg->return_path->domain, ctime(&now)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
34 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
35 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
36 foreach(hdr_list, node) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
37 header *hdr = (header *) (node->data); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
38 fputs(hdr->header, out); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
39 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
40 putc('\n', out); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
41 foreach(msg->data_list, node) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
42 /* From hack: */ |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
43 if (flags & MSGSTR_FROMHACK) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
44 if (strncmp(node->data, "From ", 5) == 0) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
45 putc('>', out); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
46 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
47 fputs(node->data, out); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
48 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
49 putc('\n', out); |
0 | 50 } |
51 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
52 gboolean |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
53 append_file(message * msg, GList * hdr_list, gchar * user) |
0 | 54 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
55 struct passwd *pw; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
56 gboolean ok = FALSE; |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
57 uid_t saved_uid = geteuid(); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
58 gid_t saved_gid = getegid(); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
59 gboolean uid_ok = TRUE, gid_ok = TRUE; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
60 gchar *filename; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
61 FILE *out; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
62 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
63 /* headers may be special for a local delivery */ |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
64 if (!hdr_list) |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
65 hdr_list = msg->hdr_list; |
0 | 66 |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
67 if (!(pw = getpwnam(user))) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
68 logwrite(LOG_ALERT, "could not find password entry for user %s\n", user); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
69 errno = ENOENT; /* getpwnam does not set errno correctly */ |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
70 return FALSE; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
71 } |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
72 |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
73 if (!conf.run_as_user) { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
74 uid_ok = (seteuid(0) == 0); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
75 if (uid_ok) { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
76 gid_ok = (setegid(conf.mail_gid) == 0); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
77 uid_ok = (seteuid(pw->pw_uid) == 0); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
78 } |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
79 if (!uid_ok || !gid_ok) { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
80 logwrite(LOG_ALERT, "could not set uid or gid for local delivery, uid = %d: %s\n", pw->pw_uid, strerror(errno)); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
81 return FALSE; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
82 } |
0 | 83 } |
84 | |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
85 DEBUG(5) debugf("running as euid %d, egid %d\n", geteuid(), getegid()); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
86 |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
87 filename = g_strdup_printf("%s/%s", conf.mail_dir, user); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
88 if (!(out = fopen(filename, "a"))) { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
89 logwrite(LOG_ALERT, "could not open file %s: %s\n", filename, strerror(errno)); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
90 } else { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
91 #ifdef USE_LIBLOCKFILE |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
92 gint err; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
93 /* lock file using liblockfile */ |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
94 err = maillock(user, 3); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
95 if (err == 0) { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
96 #else |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
97 /* lock file: */ |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
98 struct flock lock; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
99 lock.l_type = F_WRLCK; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
100 lock.l_whence = SEEK_END; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
101 lock.l_start = lock.l_len = 0; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
102 if (fcntl(fileno(out), F_SETLK, &lock) != -1) { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
103 #endif |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
104 fchmod(fileno(out), 0600); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
105 message_stream(out, msg, hdr_list, MSGSTR_FROMLINE | MSGSTR_FROMHACK); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
106 ok = TRUE; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
107 |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
108 /* close when still user */ |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
109 fclose(out); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
110 #ifdef USE_LIBLOCKFILE |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
111 mailunlock(); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
112 #endif |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
113 } else { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
114 fclose(out); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
115 #ifdef USE_LIBLOCKFILE |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
116 DEBUG(3) debugf("could not lock file %s: error %d\n", filename, err); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
117 } /* XEmacs indenting convenience... */ |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
118 #else |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
119 DEBUG(3) debugf("could not lock file %s: %s\n", filename, strerror(errno)); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
120 } |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
121 #endif |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
122 } |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
123 g_free(filename); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
124 |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
125 if (!conf.run_as_user) { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
126 uid_ok = (seteuid(0) == 0); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
127 if (uid_ok) { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
128 gid_ok = (setegid(saved_gid) == 0); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
129 uid_ok = (seteuid(saved_uid) == 0); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
130 } |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
131 } |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
132 |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
133 if (!uid_ok || !gid_ok) { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
134 /* FIXME: if this fails we HAVE to exit, because we shall not run |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
135 with some users id. But we do not return, and so this message |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
136 will not be finished, so the user will get the message again |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
137 next time a delivery is attempted... */ |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
138 logwrite(LOG_ALERT, "could not set back uid or gid after local delivery: %s\n", strerror(errno)); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
139 logwrite(LOG_ALERT, "uid=%d, gid=%d, euid=%d, egid=%d, want = %d, %d\n", |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
140 getuid(), getgid(), geteuid(), getegid(), saved_uid, saved_gid); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
141 logwrite(LOG_ALERT, "In case of trouble, see local.c:append_file() for details.\n", strerror(errno)); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
142 exit(1); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
143 } |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
144 return ok; |
0 | 145 } |
146 | |
147 gboolean | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
148 pipe_out(message * msg, GList * hdr_list, address * rcpt, gchar * cmd, guint flags) |
0 | 149 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
150 gchar *envp[40]; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
151 FILE *out; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
152 uid_t saved_uid = geteuid(); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
153 gid_t saved_gid = getegid(); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
154 gboolean ok = FALSE; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
155 gint i, n; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
156 pid_t pid; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
157 void (*old_signal) (int); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
158 int status; |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
159 address *ancestor = addr_find_ancestor(rcpt); |
0 | 160 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
161 /* set uid and gid to the mail ids */ |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
162 if (!conf.run_as_user) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
163 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
164 } |
0 | 165 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
166 /* set environment */ |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
167 n = 0; |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
168 envp[n++] = g_strdup_printf("SENDER=%s@%s", msg->return_path->local_part, msg->return_path->domain); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
169 envp[n++] = g_strdup_printf("SENDER_DOMAIN=%s", msg->return_path->domain); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
170 envp[n++] = g_strdup_printf("SENDER_LOCAL=%s", msg->return_path->local_part); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
171 envp[n++] = g_strdup_printf("RECEIVED_HOST=%s", msg->received_host ? msg->received_host : ""); |
0 | 172 |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
173 envp[n++] = g_strdup_printf("RETURN_PATH=%s@%s", msg->return_path->local_part, msg->return_path->domain); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
174 envp[n++] = g_strdup_printf("DOMAIN=%s", ancestor->domain); |
0 | 175 |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
176 envp[n++] = g_strdup_printf("LOCAL_PART=%s", ancestor->local_part); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
177 envp[n++] = g_strdup_printf("USER=%s", ancestor->local_part); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
178 envp[n++] = g_strdup_printf("LOGNAME=%s", ancestor->local_part); |
0 | 179 |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
180 envp[n++] = g_strdup_printf("MESSAGE_ID=%s", msg->uid); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
181 envp[n++] = g_strdup_printf("QUALIFY_DOMAIN=%s", conf.host_name); |
0 | 182 |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
183 envp[n] = NULL; |
0 | 184 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
185 old_signal = signal(SIGCHLD, SIG_DFL); |
0 | 186 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
187 out = peidopen(cmd, "w", envp, &pid, conf.mail_uid, conf.mail_gid); |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
188 if (!out) { |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
189 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", cmd, strerror(errno)); |
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
190 } else { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
191 message_stream(out, msg, hdr_list, flags); |
0 | 192 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
193 fclose(out); |
0 | 194 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
195 waitpid(pid, &status, 0); |
0 | 196 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
197 if (WEXITSTATUS(status) != 0) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
198 int exstat = WEXITSTATUS(status); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
199 logwrite(LOG_ALERT, "process returned %d (%s)\n", exstat, ext_strerror(1024 + exstat)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
200 errno = 1024 + exstat; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
201 } else if (WIFSIGNALED(status)) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
202 logwrite(LOG_ALERT, "process got signal %d\n", WTERMSIG(status)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
203 } else |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
204 ok = TRUE; |
0 | 205 |
332
63efd381e27b
refactoring, partly also related to set_euidgid()
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
206 } |
0 | 207 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
208 signal(SIGCHLD, old_signal); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
209 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
210 /* free environment */ |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
211 for (i = 0; i < n; i++) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
212 g_free(envp[i]); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
213 } |
0 | 214 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
215 /* set uid and gid back */ |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
216 if (!conf.run_as_user) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
217 set_euidgid(saved_uid, saved_gid, NULL, NULL); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
218 } |
0 | 219 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
220 return ok; |
0 | 221 } |