aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-lock-helper.c
blob: 9e5dc012a3c99b201a927f88619191841a7e686e (plain) (blame)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2001 Ximian Inc.
 *
 * Authors: Michael Zucchi <notzed@ximian.com>
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of version 2 of the GNU General Public 
 * License as published by the Free Software Foundation.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

/* lock helper process */

#include <stdio.h>
#include <stdlib.h>

#include <errno.h>

#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <signal.h>

#include <unistd.h>
#include <fcntl.h>
#include <utime.h>

#include <time.h>

#include <string.h>

#define SETEUID_SAVES (1)

/* we try and include as little as possible */

#include "camel-lock-helper.h"
#include "camel-lock.h"

#define d(x)

/* keeps track of open locks */
struct _lock_info {
    struct _lock_info *next;
    uid_t uid;
    int id;
    int depth;
    time_t stamp;       /* when last updated */
    char path[1];
};

static int lock_id = 0;
static struct _lock_info *lock_info_list;
static uid_t lock_root_uid = -1;
static uid_t lock_real_uid = -1;

/* utility functions */

static int read_n(int fd, void *buffer, int inlen)
{
    char *p = buffer;
    int len, left = inlen;

    do {
        len = read(fd, p, left);
        if (len == -1) {
            if (errno != EINTR)
                return -1;
        } else {
            left -= len;
            p += len;
        }
    } while (left > 0 && len != 0);

    return inlen - left;
}

static int write_n(int fd, void *buffer, int inlen)
{
    char *p = buffer;
    int len, left = inlen;

    do {
        len = write(fd, p, left);
        if (len == -1) {
            if (errno != EINTR)
                return -1;
        } else {
            left -= len;
            p += len;
        }
    } while (left > 0);

    return inlen;
}

void
camel_exception_setv (CamelException *ex, ExceptionId id, const char *format, ...)
{
    ;
}

void
camel_exception_clear (CamelException *exception)
{
    ;
}

char *gettext (const char *msgid);

char *
gettext (const char *msgid)
{
    return NULL;
}

static int lock_path(const char *path, guint32 *lockid)
{
    struct _lock_info *info = NULL;
    int res = CAMEL_LOCK_HELPER_STATUS_OK;
    struct stat st;

    d(fprintf(stderr, "locking path '%s' id = %d\n", path, lock_id));

    /* check to see if we have it locked already, make the lock 'recursive' */
    /* we could also error i suppose, but why bother */
    info = lock_info_list;
    while (info) {
        if (!strcmp(info->path, path)) {
            info->depth++;
            return CAMEL_LOCK_HELPER_STATUS_OK;
        }
        info = info->next;
    }

    /* check we are allowed to lock it, we must own it, be able to write to it, and it has to exist */
    if (stat(path, &st) == -1
        || st.st_uid != getuid()
        || !S_ISREG(st.st_mode)
        || (st.st_mode & 0400) == 0) {
        return CAMEL_LOCK_HELPER_STATUS_INVALID;
    }

    info = malloc(sizeof(*info) + strlen(path));
    if (info == NULL) {
        res = CAMEL_LOCK_HELPER_STATUS_NOMEM;
        goto fail;
    }

    /* we try the real uid first, and if that fails, try the 'root id' */
    if (camel_lock_dot(path, NULL) == -1) {
#ifdef SETEUID_SAVES
        if (lock_real_uid != lock_root_uid) {
            if (seteuid(lock_root_uid) != -1) {
                if (camel_lock_dot(path, NULL) == -1) {
                    seteuid(lock_real_uid);
                    res = CAMEL_LOCK_HELPER_STATUS_SYSTEM;
                    goto fail;
                }
                seteuid(lock_real_uid);
            } else {
                res = CAMEL_LOCK_HELPER_STATUS_SYSTEM;
                goto fail;
            }
        } else {
            res = CAMEL_LOCK_HELPER_STATUS_SYSTEM;
            goto fail;
        }
#else
        res = CAMEL_LOCK_HELPER_STATUS_SYSTEM;
        goto fail;
#endif
    } else {
        info->uid = lock_real_uid;
    }

    strcpy(info->path, path);
    info->id = lock_id;
    info->depth = 1;
    info->next = lock_info_list;
    info->stamp = time(0);
    lock_info_list = info;

    if (lockid)
        *lockid = lock_id;

    lock_id++;

    d(fprintf(stderr, "lock ok\n"));

    return res;
fail:
    d(fprintf(stderr, "lock failed\n"));

    if (info)
        free(info);

    return res;
}

static int unlock_id(guint32 lockid)
{
    struct _lock_info *info, *p;

    d(fprintf(stderr, "unlocking id '%d'\n", lockid));

    p = (struct _lock_info *)&lock_info_list;
    info = p->next;
    while (info) {
        if (info->id == lockid) {
            d(fprintf(stderr, "found id %d path '%s'\n", lockid, info->path));
            info->depth--;
            if (info->depth <= 0) {
#ifdef SETEUID_SAVES
                if (info->uid != lock_real_uid) {
                    seteuid(lock_root_uid);
                    camel_unlock_dot(info->path);
                    seteuid(lock_real_uid);
                } else
#endif
                    camel_unlock_dot(info->path);

                p->next = info->next;
                free(info);
            }

            return CAMEL_LOCK_HELPER_STATUS_OK;
        }
        p = info;
        info = info->next;
    }

    d(fprintf(stderr, "unknown id asked to be unlocked %d\n", lockid));
    return CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
}

static void lock_touch(const char *path)
{
    char *name;

    /* we could also check that we haven't had our lock stolen from us here */

    name = alloca(strlen(path) + 10);
    sprintf(name, "%s.lock", path);

    d(fprintf(stderr, "Updating lock %s\n", name));
    utime(name, NULL);
}

static void setup_process(void)
{
    struct sigaction sa;
    sigset_t sigset;
    
    /* ignore sigint/sigio */
    sa.sa_handler = SIG_IGN;
    sigemptyset (&sa.sa_mask);
    sa.sa_flags = 0;
    
    sigemptyset(&sigset);
    sigaddset(&sigset, SIGIO);
    sigaddset(&sigset, SIGINT);
    sigprocmask(SIG_UNBLOCK, &sigset, NULL);
    
    sigaction (SIGIO, &sa, NULL);
    sigaction (SIGINT, &sa, NULL);
    
    /* FIXME: add more sanity checks/setup here */

#ifdef SETEUID_SAVES
    /* here we change to the real user id, this is probably not particularly
       portable so may need configure checks */
    lock_real_uid = getuid();
    lock_root_uid = geteuid();
    if (lock_real_uid != lock_root_uid)
        seteuid(lock_real_uid);
#endif
}

int main(int argc, char **argv)
{
    struct _CamelLockHelperMsg msg;
    int len;
    int res;
    char *path;
    fd_set rset;
    struct timeval tv;
    struct _lock_info *info;

    setup_process();

    do {
        /* do a poll/etc, so we can refresh the .locks as required ... */
        FD_ZERO(&rset);
        FD_SET(STDIN_FILENO, &rset);

        /* check the minimum timeout we need to refresh the next oldest lock */
        if (lock_info_list) {
            time_t now = time(0);
            time_t left;
            time_t delay = CAMEL_DOT_LOCK_REFRESH;

            info = lock_info_list;
            while (info) {
                left = CAMEL_DOT_LOCK_REFRESH - (now - info->stamp);
                left = MAX(left, 0);
                delay = MIN(left, delay);
                info = info->next;
            }

            tv.tv_sec = delay;
            tv.tv_usec = 0;
        }
        
        d(fprintf(stderr, "lock helper waiting for input\n"));
        if (select(STDIN_FILENO+1, &rset, NULL, NULL, lock_info_list?&tv:NULL) == -1) {
            if (errno == EINTR)
                break;

            continue;
        }

        /* did we get a timeout?  scan for any locks that need updating */
        if (!FD_ISSET(STDIN_FILENO, &rset)) {
            time_t now = time(0);
            time_t left;

            d(fprintf(stderr, "Got a timeout, checking locks\n"));

            info = lock_info_list;
            while (info) {
                left = (now - info->stamp);
                if (left >= CAMEL_DOT_LOCK_REFRESH) {
                    lock_touch(info->path);
                    info->stamp = now;
                }
                info = info->next;
            }
            
            continue;
        }


        len = read_n(STDIN_FILENO, &msg, sizeof(msg));
        if (len == 0)
            break;

        res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
        if (len == sizeof(msg) && msg.magic == CAMEL_LOCK_HELPER_MAGIC) {
            switch(msg.id) {
            case CAMEL_LOCK_HELPER_LOCK:
                res = CAMEL_LOCK_HELPER_STATUS_NOMEM;
                path = malloc(msg.data+1);
                if (path != NULL) {
                    res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
                    len = read_n(STDIN_FILENO, path, msg.data);
                    if (len == msg.data) {
                        path[len] = 0;
                        res = lock_path(path, &msg.data);
                    }
                    free(path);
                }
                break;
            case CAMEL_LOCK_HELPER_UNLOCK:
                res = unlock_id(msg.data);
                break;
            }
        }
        d(fprintf(stderr, "returning result %d\n", res));
        msg.id = res;
        msg.magic = CAMEL_LOCK_HELPER_RETURN_MAGIC;
        write_n(STDOUT_FILENO, &msg, sizeof(msg));
    } while (1);

    d(fprintf(stderr, "parent exited, clsoing down remaining id's\n"));
    while (lock_info_list)
        unlock_id(lock_info_list->id);

    return 0;
}