queue_null.c revision 1.3
1/*	$OpenBSD: queue_null.c,v 1.3 2013/07/19 11:14:08 eric Exp $	*/
2
3/*
4 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <sys/queue.h>
21#include <sys/tree.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24
25#include <ctype.h>
26#include <err.h>
27#include <errno.h>
28#include <event.h>
29#include <fcntl.h>
30#include <imsg.h>
31#include <inttypes.h>
32#include <libgen.h>
33#include <pwd.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <time.h>
38#include <unistd.h>
39
40#include "smtpd.h"
41#include "log.h"
42
43static int queue_null_init(struct passwd *, int);
44static int queue_null_message(enum queue_op, uint32_t *);
45static int queue_null_envelope(enum queue_op , uint64_t *, char *, size_t);
46
47struct queue_backend queue_backend_null = {
48	queue_null_init,
49	queue_null_message,
50	queue_null_envelope,
51};
52
53static int	devnull;
54
55static int
56queue_null_init(struct passwd *pw, int server)
57{
58
59	devnull = open("/dev/null", O_WRONLY, 0777);
60	if (devnull == -1) {
61		log_warn("open");
62		return (0);
63	}
64
65	return (1);
66}
67
68static int
69queue_null_message(enum queue_op qop, uint32_t *msgid)
70{
71	switch (qop) {
72	case QOP_CREATE:
73		*msgid = queue_generate_msgid();
74		return (1);
75	case QOP_DELETE:
76	case QOP_COMMIT:
77		return (1);
78	case QOP_FD_R:
79		return (-1);
80	case QOP_FD_RW:
81		return dup(devnull);
82	case QOP_CORRUPT:
83		return (0);
84	default:
85		fatalx("queue_null_message: unsupported operation.");
86	}
87
88	return (0);
89}
90
91static int
92queue_null_envelope(enum queue_op qop, uint64_t *evpid, char *buf, size_t len)
93{
94	uint32_t	msgid;
95
96	if (qop == QOP_WALK)
97		return (-1);
98
99	switch (qop) {
100	case QOP_CREATE:
101		msgid = evpid_to_msgid(*evpid);
102		*evpid = queue_generate_evpid(msgid);
103		return (1);
104	case QOP_DELETE:
105		return (1);
106	case QOP_LOAD:
107		return (0);
108	case QOP_UPDATE:
109		return (1);
110	default:
111		fatalx("queue_null_envelope: unsupported operation.");
112	}
113
114	return (0);
115}
116