~ubuntu-branches/ubuntu/oneiric/strongswan/oneiric

« back to all changes in this revision

Viewing changes to src/starter/invokepluto.c

  • Committer: Bazaar Package Importer
  • Author(s): Rene Mayrhofer
  • Date: 2009-04-18 20:28:51 UTC
  • mfrom: (1.1.9 upstream) (2.1.15 karmic)
  • Revision ID: james.westby@ubuntu.com-20090418202851-ep722qhmzvpxh6yj
Tags: 4.3.2-1
Urgency high because of security issue and FTBFS.
* New upstream release, fixes security bug.
* Fix padlock handling for i386 in debian/rules.
  Closes: #525652 (FTBFS on i386)
* Acknowledge NMUs by security team.
  Closes: #533837, #531612
* Add "Conflicts: strongswan (< 4.2.12-1)" to libstrongswan, 
  strongswan-starter, strongswan-ikev1, and strongswan-ikev2 to force
  update of the strongswan package on installation and avoid conflicts
  caused by package restructuring.
  Closes: #526037: strongswan-ikev2 and strongswan: error when trying to 
                   install together
  Closes: #526486: strongswan and libstrongswan: error when trying to 
                   install together
  Closes: #526487: strongswan-ikev1 and strongswan: error when trying to 
                   install together
  Closes: #526488: strongswan-starter and strongswan: error when trying to 
                   install together
* Debconf templates and debian/control reviewed by the debian-l10n-
  english team as part of the Smith review project. Closes: #528073
* Debconf translation updates:
  Closes: #525234: [INTL:ja] Update po-debconf template translation (ja.po) 
  Closes: #528323: [INTL:sv] po-debconf file for strongswan 
  Closes: #528370: [INTL:vi] Vietnamese debconf templates translation update 
  Closes: #529027: [INTL:pt] Updated Portuguese translation for debconf messages
  Closes: #529071: [INTL:fr] French debconf templates translation update 
  Closes: #529592: nb translation of debconf PO for strongSWAN 
  Closes: #529638: [INTL:ru] Russian debconf templates translation 
  Closes: #529661: Updated Czech translation of strongswan debconf messages 
  Closes: #529742: [INTL:eu] strongswan debconf basque translation 
  Closes: #530273: [INTL:fi] Finnish translation of the debconf templates
  Closes: #529063: [INTL:gl] strongswan 4.2.14-2 debconf translation update

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
11
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
12
 * for more details.
13
 
 *
14
 
 * RCSID $Id: invokepluto.c 5050 2009-03-27 16:14:59Z andreas $
15
13
 */
16
14
 
17
15
#include <sys/types.h>
40
38
pid_t
41
39
starter_pluto_pid(void)
42
40
{
43
 
    return _pluto_pid;
 
41
        return _pluto_pid;
44
42
}
45
43
 
46
44
void
47
45
starter_pluto_sigchild(pid_t pid)
48
46
{
49
 
    if (pid == _pluto_pid)
50
 
    {
51
 
        _pluto_pid = 0;
52
 
        if (!_stop_requested)
 
47
        if (pid == _pluto_pid)
53
48
        {
54
 
            plog("pluto has died -- restart scheduled (%dsec)"
55
 
                , PLUTO_RESTART_DELAY);
56
 
            alarm(PLUTO_RESTART_DELAY);   // restart in 5 sec
 
49
                _pluto_pid = 0;
 
50
                if (!_stop_requested)
 
51
                {
 
52
                        plog("pluto has died -- restart scheduled (%dsec)"
 
53
                                , PLUTO_RESTART_DELAY);
 
54
                        alarm(PLUTO_RESTART_DELAY);   // restart in 5 sec
 
55
                }
 
56
                unlink(PLUTO_PID_FILE);
57
57
        }
58
 
        unlink(PLUTO_PID_FILE);
59
 
    }
60
58
}
61
59
 
62
60
int
63
61
starter_stop_pluto (void)
64
62
{
65
 
    int i;
66
 
    pid_t pid = _pluto_pid;
67
 
 
68
 
    if (pid)
69
 
    {
70
 
        _stop_requested = 1;
71
 
 
72
 
        if (starter_whack_shutdown() == 0)
 
63
        int i;
 
64
        pid_t pid = _pluto_pid;
 
65
 
 
66
        if (pid)
73
67
        {
74
 
            for (i = 0; i < 400; i++)
75
 
            {
76
 
                usleep(20000); /* sleep for 20 ms */
 
68
                _stop_requested = 1;
 
69
 
 
70
                if (starter_whack_shutdown() == 0)
 
71
                {
 
72
                        for (i = 0; i < 400; i++)
 
73
                        {
 
74
                                usleep(20000); /* sleep for 20 ms */
 
75
                                if (_pluto_pid == 0)
 
76
                                {
 
77
                                        plog("pluto stopped after %d ms", 20*(i+1));
 
78
                                        return 0;
 
79
                                }
 
80
                        }
 
81
                }
 
82
                /* be more and more aggressive */
 
83
                for (i = 0; i < 20 && (pid = _pluto_pid) != 0; i++)
 
84
                {
 
85
                        
 
86
                        if (i < 10)
 
87
                        {
 
88
                                kill(pid, SIGTERM);
 
89
                        }
 
90
                        if (i == 10)
 
91
                        {
 
92
                                kill(pid, SIGKILL);
 
93
                                plog("starter_stop_pluto(): pluto does not respond, sending KILL");
 
94
                        }           
 
95
                        else
 
96
                        {
 
97
                                kill(pid, SIGKILL);
 
98
                        }
 
99
                        usleep(100000); /* sleep for 100 ms */
 
100
                }
77
101
                if (_pluto_pid == 0)
78
102
                {
79
 
                    plog("pluto stopped after %d ms", 20*(i+1));
80
 
                    return 0;
 
103
                        plog("pluto stopped after %d ms", 8000 + 100*i);
 
104
                        return 0;
81
105
                }
82
 
            }
83
 
        }
84
 
        /* be more and more aggressive */
85
 
        for (i = 0; i < 20 && (pid = _pluto_pid) != 0; i++)
86
 
        {
87
 
            
88
 
            if (i < 10)
89
 
            {
90
 
                kill(pid, SIGTERM);
91
 
            }
92
 
            if (i == 10)
93
 
            {
94
 
                kill(pid, SIGKILL);
95
 
                plog("starter_stop_pluto(): pluto does not respond, sending KILL");
96
 
            }           
97
 
            else
98
 
            {
99
 
                kill(pid, SIGKILL);
100
 
            }
101
 
            usleep(100000); /* sleep for 100 ms */
102
 
        }
103
 
        if (_pluto_pid == 0)
104
 
        {
105
 
            plog("pluto stopped after %d ms", 8000 + 100*i);
106
 
            return 0;
107
 
        }
108
 
        plog("starter_stop_pluto(): can't stop pluto !!!");
 
106
                plog("starter_stop_pluto(): can't stop pluto !!!");
 
107
                return -1;
 
108
        }
 
109
        else
 
110
        {
 
111
                plog("stater_stop_pluto(): pluto is not started...");
 
112
        }
109
113
        return -1;
110
 
    }
111
 
    else
112
 
    {
113
 
        plog("stater_stop_pluto(): pluto is not started...");
114
 
    }
115
 
    return -1;
116
114
}
117
115
 
118
116
#define ADD_DEBUG(v) { \
119
 
        for (l = cfg->setup.plutodebug; l && *l; l++) if (streq(*l, v)) \
120
 
                arg[argc++] = "--debug-" v; \
121
 
        }
 
117
                for (l = cfg->setup.plutodebug; l && *l; l++) if (streq(*l, v)) \
 
118
                                arg[argc++] = "--debug-" v; \
 
119
                }
122
120
 
123
121
int
124
 
starter_start_pluto (starter_config_t *cfg, bool no_fork)
 
122
starter_start_pluto (starter_config_t *cfg, bool no_fork, bool attach_gdb)
125
123
{
126
 
    struct stat stb;
127
 
    int i;
128
 
    pid_t pid;
129
 
    char **l;
130
 
    int argc = 2;
131
 
    char *arg[] = {
132
 
              PLUTO_CMD, "--nofork"
133
 
            , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
134
 
            , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
135
 
            , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
136
 
            , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
137
 
        };
138
 
 
139
 
    printf ("starter_start_pluto entered\n");
140
 
 
141
 
    if (cfg->setup.plutostderrlog || no_fork)
142
 
    {
143
 
        arg[argc++] = "--stderrlog";
144
 
    }
145
 
    if (cfg->setup.uniqueids)
146
 
    {
147
 
        arg[argc++] = "--uniqueids";
148
 
    }
149
 
    ADD_DEBUG("none")
150
 
    ADD_DEBUG("all")
151
 
    ADD_DEBUG("raw")
152
 
    ADD_DEBUG("crypt")
153
 
    ADD_DEBUG("parsing")
154
 
    ADD_DEBUG("emitting")
155
 
    ADD_DEBUG("control")
156
 
    ADD_DEBUG("lifecycle")
157
 
    ADD_DEBUG("klips")
158
 
    ADD_DEBUG("dns")
159
 
    ADD_DEBUG("natt")
160
 
    ADD_DEBUG("oppo")
161
 
    ADD_DEBUG("controlmore")
162
 
    ADD_DEBUG("private")
163
 
    if (cfg->setup.crlcheckinterval > 0)
164
 
    {
165
 
        static char buf1[15];
166
 
 
167
 
        arg[argc++] = "--crlcheckinterval";
168
 
        snprintf(buf1, sizeof(buf1), "%u", cfg->setup.crlcheckinterval);
169
 
        arg[argc++] = buf1;
170
 
    }
171
 
    if (cfg->setup.cachecrls)
172
 
    {
173
 
        arg[argc++] = "--cachecrls";
174
 
    }
175
 
    if (cfg->setup.strictcrlpolicy)
176
 
    {
177
 
        arg[argc++] = "--strictcrlpolicy";
178
 
    }
179
 
    if (cfg->setup.nocrsend)
180
 
    {
181
 
        arg[argc++] = "--nocrsend";
182
 
    }
183
 
    if (cfg->setup.nat_traversal)
184
 
    {
185
 
        arg[argc++] = "--nat_traversal";
186
 
    }
187
 
    if (cfg->setup.force_keepalive)
188
 
    {
189
 
        arg[argc++] = "--force_keepalive";
190
 
    }
191
 
    if (cfg->setup.keep_alive)
192
 
    {
193
 
        static char buf2[15];
194
 
 
195
 
        arg[argc++] = "--keep_alive";
196
 
        snprintf(buf2, sizeof(buf2), "%u", cfg->setup.keep_alive);
197
 
        arg[argc++] = buf2;
198
 
    }
199
 
    if (cfg->setup.virtual_private)
200
 
    {
201
 
        arg[argc++] = "--virtual_private";
202
 
        arg[argc++] = cfg->setup.virtual_private;
203
 
    }
204
 
    if (cfg->setup.pkcs11module)
205
 
    {
206
 
        arg[argc++] = "--pkcs11module";
207
 
        arg[argc++] = cfg->setup.pkcs11module;
208
 
    }
209
 
    if (cfg->setup.pkcs11initargs)
210
 
    {
211
 
        arg[argc++] = "--pkcs11initargs";
212
 
        arg[argc++] = cfg->setup.pkcs11initargs;
213
 
    }
214
 
    if (cfg->setup.pkcs11keepstate)
215
 
    {
216
 
        arg[argc++] = "--pkcs11keepstate";
217
 
    }
218
 
    if (cfg->setup.pkcs11proxy)
219
 
    {
220
 
        arg[argc++] = "--pkcs11proxy";
221
 
    }
222
 
 
223
 
    if (_pluto_pid)
224
 
    {
225
 
        plog("starter_start_pluto(): pluto already started...");
 
124
        struct stat stb;
 
125
        int i;
 
126
        pid_t pid;
 
127
        char **l;
 
128
        int argc = 2;
 
129
        char *arg[] = {
 
130
                          PLUTO_CMD, "--nofork"
 
131
                        , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
 
132
                        , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
 
133
                        , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
 
134
                        , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
 
135
                };
 
136
 
 
137
        printf ("starter_start_pluto entered\n");
 
138
        
 
139
        if (attach_gdb)
 
140
        {
 
141
                argc = 0;
 
142
                arg[argc++] = "/usr/bin/gdb";
 
143
                arg[argc++] = "--args";
 
144
                arg[argc++] = PLUTO_CMD;
 
145
                arg[argc++] = "--nofork";
 
146
                }
 
147
        if (cfg->setup.plutostderrlog || no_fork)
 
148
        {
 
149
                arg[argc++] = "--stderrlog";
 
150
        }
 
151
        if (cfg->setup.uniqueids)
 
152
        {
 
153
                arg[argc++] = "--uniqueids";
 
154
        }
 
155
        ADD_DEBUG("none")
 
156
        ADD_DEBUG("all")
 
157
        ADD_DEBUG("raw")
 
158
        ADD_DEBUG("crypt")
 
159
        ADD_DEBUG("parsing")
 
160
        ADD_DEBUG("emitting")
 
161
        ADD_DEBUG("control")
 
162
        ADD_DEBUG("lifecycle")
 
163
        ADD_DEBUG("klips")
 
164
        ADD_DEBUG("dns")
 
165
        ADD_DEBUG("natt")
 
166
        ADD_DEBUG("oppo")
 
167
        ADD_DEBUG("controlmore")
 
168
        ADD_DEBUG("private")
 
169
        if (cfg->setup.crlcheckinterval > 0)
 
170
        {
 
171
                static char buf1[15];
 
172
 
 
173
                arg[argc++] = "--crlcheckinterval";
 
174
                snprintf(buf1, sizeof(buf1), "%u", cfg->setup.crlcheckinterval);
 
175
                arg[argc++] = buf1;
 
176
        }
 
177
        if (cfg->setup.cachecrls)
 
178
        {
 
179
                arg[argc++] = "--cachecrls";
 
180
        }
 
181
        if (cfg->setup.strictcrlpolicy)
 
182
        {
 
183
                arg[argc++] = "--strictcrlpolicy";
 
184
        }
 
185
        if (cfg->setup.nocrsend)
 
186
        {
 
187
                arg[argc++] = "--nocrsend";
 
188
        }
 
189
        if (cfg->setup.nat_traversal)
 
190
        {
 
191
                arg[argc++] = "--nat_traversal";
 
192
        }
 
193
        if (cfg->setup.force_keepalive)
 
194
        {
 
195
                arg[argc++] = "--force_keepalive";
 
196
        }
 
197
        if (cfg->setup.keep_alive)
 
198
        {
 
199
                static char buf2[15];
 
200
 
 
201
                arg[argc++] = "--keep_alive";
 
202
                snprintf(buf2, sizeof(buf2), "%u", cfg->setup.keep_alive);
 
203
                arg[argc++] = buf2;
 
204
        }
 
205
        if (cfg->setup.virtual_private)
 
206
        {
 
207
                arg[argc++] = "--virtual_private";
 
208
                arg[argc++] = cfg->setup.virtual_private;
 
209
        }
 
210
        if (cfg->setup.pkcs11module)
 
211
        {
 
212
                arg[argc++] = "--pkcs11module";
 
213
                arg[argc++] = cfg->setup.pkcs11module;
 
214
        }
 
215
        if (cfg->setup.pkcs11initargs)
 
216
        {
 
217
                arg[argc++] = "--pkcs11initargs";
 
218
                arg[argc++] = cfg->setup.pkcs11initargs;
 
219
        }
 
220
        if (cfg->setup.pkcs11keepstate)
 
221
        {
 
222
                arg[argc++] = "--pkcs11keepstate";
 
223
        }
 
224
        if (cfg->setup.pkcs11proxy)
 
225
        {
 
226
                arg[argc++] = "--pkcs11proxy";
 
227
        }
 
228
 
 
229
        if (_pluto_pid)
 
230
        {
 
231
                plog("starter_start_pluto(): pluto already started...");
 
232
                return -1;
 
233
        }
 
234
        else
 
235
        {
 
236
                unlink(PLUTO_CTL_FILE);
 
237
                _stop_requested = 0;
 
238
 
 
239
                if (cfg->setup.prepluto)
 
240
                        ignore_result(system(cfg->setup.prepluto));
 
241
 
 
242
                pid = fork();
 
243
                switch (pid)
 
244
                {
 
245
                case -1:
 
246
                        plog("can't fork(): %s", strerror(errno));
 
247
                        return -1;
 
248
                case 0:
 
249
                        /* child */
 
250
                        if (cfg->setup.plutostderrlog)
 
251
                        {
 
252
                                int f = creat(cfg->setup.plutostderrlog, 00644);
 
253
 
 
254
                                /* redirect stderr to file */
 
255
                                if (f < 0)
 
256
                                {
 
257
                                        plog("couldn't open stderr redirection file '%s'",
 
258
                                                  cfg->setup.plutostderrlog);
 
259
                                }
 
260
                                else
 
261
                                {
 
262
                                        dup2(f, 2);
 
263
                                }
 
264
                        }
 
265
                        setsid();
 
266
                        sigprocmask(SIG_SETMASK, 0, NULL);
 
267
                        /* disable glibc's malloc checker, conflicts with leak detective */
 
268
                        setenv("MALLOC_CHECK_", "0", 1);
 
269
                        execv(arg[0], arg);
 
270
                        plog("can't execv(%s,...): %s", arg[0], strerror(errno));
 
271
                        exit(1);
 
272
                default:
 
273
                        /* father */
 
274
                        _pluto_pid = pid;
 
275
                        for (i = 0; i < 500 && _pluto_pid; i++)
 
276
                        {
 
277
                                /* wait for pluto for a maximum of 500 x 20 ms = 10 s */
 
278
                                usleep(20000);
 
279
                                if (stat(PLUTO_CTL_FILE, &stb) == 0)
 
280
                                {
 
281
                                        plog("pluto (%d) started after %d ms", _pluto_pid, 20*(i+1));
 
282
                                        if (cfg->setup.postpluto)
 
283
                                        {
 
284
                                                ignore_result(system(cfg->setup.postpluto));
 
285
                                        }
 
286
                                        return 0;
 
287
                                }
 
288
                        }
 
289
                        if (_pluto_pid)
 
290
                        {
 
291
                                /* If pluto is started but with no ctl file, stop it */
 
292
                                plog("pluto too long to start... - kill kill");
 
293
                                for (i = 0; i < 20 && (pid = _pluto_pid) != 0; i++)
 
294
                                {
 
295
                                        if (i < 10)
 
296
                                        {
 
297
                                                kill(pid, SIGTERM);
 
298
                                        }
 
299
                                        else
 
300
                                        {
 
301
                                                kill(pid, SIGKILL);
 
302
                                        }
 
303
                                        usleep(20000); /* sleep for 20 ms */
 
304
                                }
 
305
                        }
 
306
                        else
 
307
                        {
 
308
                                plog("pluto refused to be started");
 
309
                        }
 
310
                        return -1;
 
311
                }
 
312
        }
226
313
        return -1;
227
 
    }
228
 
    else
229
 
    {
230
 
        unlink(PLUTO_CTL_FILE);
231
 
        _stop_requested = 0;
232
 
 
233
 
        if (cfg->setup.prepluto)
234
 
            ignore_result(system(cfg->setup.prepluto));
235
 
 
236
 
        pid = fork();
237
 
        switch (pid)
238
 
        {
239
 
        case -1:
240
 
            plog("can't fork(): %s", strerror(errno));
241
 
            return -1;
242
 
        case 0:
243
 
            /* child */
244
 
            if (cfg->setup.plutostderrlog)
245
 
            {
246
 
                int f = creat(cfg->setup.plutostderrlog, 00644);
247
 
 
248
 
                /* redirect stderr to file */
249
 
                if (f < 0)
250
 
                {
251
 
                    plog("couldn't open stderr redirection file '%s'",
252
 
                          cfg->setup.plutostderrlog);
253
 
                }
254
 
                else
255
 
                {
256
 
                    dup2(f, 2);
257
 
                }
258
 
            }
259
 
            setsid();
260
 
            sigprocmask(SIG_SETMASK, 0, NULL);
261
 
            execv(arg[0], arg);
262
 
            plog("can't execv(%s,...): %s", arg[0], strerror(errno));
263
 
            exit(1);
264
 
        default:
265
 
            /* father */
266
 
            _pluto_pid = pid;
267
 
            for (i = 0; i < 500 && _pluto_pid; i++)
268
 
            {
269
 
                /* wait for pluto for a maximum of 500 x 20 ms = 10 s */
270
 
                usleep(20000);
271
 
                if (stat(PLUTO_CTL_FILE, &stb) == 0)
272
 
                {
273
 
                    plog("pluto (%d) started after %d ms", _pluto_pid, 20*(i+1));
274
 
                    if (cfg->setup.postpluto)
275
 
                    {
276
 
                        ignore_result(system(cfg->setup.postpluto));
277
 
                    }
278
 
                    return 0;
279
 
                }
280
 
            }
281
 
            if (_pluto_pid)
282
 
            {
283
 
                /* If pluto is started but with no ctl file, stop it */
284
 
                plog("pluto too long to start... - kill kill");
285
 
                for (i = 0; i < 20 && (pid = _pluto_pid) != 0; i++)
286
 
                {
287
 
                    if (i < 10)
288
 
                    {
289
 
                        kill(pid, SIGTERM);
290
 
                    }
291
 
                    else
292
 
                    {
293
 
                        kill(pid, SIGKILL);
294
 
                    }
295
 
                    usleep(20000); /* sleep for 20 ms */
296
 
                }
297
 
            }
298
 
            else
299
 
            {
300
 
                plog("pluto refused to be started");
301
 
            }
302
 
            return -1;
303
 
        }
304
 
    }
305
 
    return -1;
306
314
}