-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbeans.c
195 lines (172 loc) · 3.79 KB
/
beans.c
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
/* beans is a simple pastebin server */
#include <errno.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include "arg.h"
#define BACKLOG 32
#define MAXSIZE 32000 /* in bytes */
/* function declarations */
void die(const char *errstr, ...);
int bindon(char *port);
void *ecalloc(size_t nmemb, size_t size);
char *readall(int sd, int *len, int limit);
void run(void);
void serve(int sd);
void sout(int sd, char *fmt, ...);
/* variables */
char *argv0;
char port[8] = "2023";
char base[256] = "";
char path[256] = "/tmp";
char mode[8] = "0600";
int sockd;
/* function implementations */
void
die(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(1);
}
int
bindon(char *port) {
struct addrinfo hints, *res;
int sd = 0, e;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if((e = getaddrinfo(NULL, port, &hints, &res)))
die("getaddrinfo(): %s\n", gai_strerror(e));
if((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1)
die("socket(): %s\n", strerror(errno));
if(bind(sd, res->ai_addr, res->ai_addrlen) == -1)
die("bind(): %s\n", strerror(errno));
if(listen(sd, BACKLOG) == -1)
die("listen(): %s\n", strerror(errno));
return sd;
}
void *
ecalloc(size_t nmemb, size_t size) {
void *p;
if(!(p = calloc(nmemb, size)))
die("Cannot allocate memory.\n");
return p;
}
char *
readall(int sd, int *len, int limit) {
char *buf;
int sz = 512, r, l = 0;
if(limit && sz > limit)
sz = limit;
buf = ecalloc(1, sz);
while((r = read(sd, &buf[l], sz - l)) != -1) {
if(!r)
break;
l += r;
if(limit && l == limit)
break;
if(l == sz) {
sz *= 2;
if(limit && sz > limit)
sz = limit;
if(!(buf = realloc(buf, sz)))
die("realloc()\n");
}
}
if(r == -1) {
free(buf);
return NULL;
}
if(len)
*len = l;
buf[l] = '\0';
return buf;
}
void
run(void) {
struct sockaddr_storage conn;
socklen_t size;
int csd;
pid_t pid;
while(1) {
size = sizeof conn;
csd = accept(sockd, (struct sockaddr *)&conn, &size);
if(csd == -1) {
fprintf(stderr, "accept(): %s", strerror(errno));
continue;
}
pid = fork();
if(pid) {
if(pid == -1)
fprintf(stderr, "fork(): %s\n", strerror(errno));
close(csd);
continue;
}
serve(csd);
close(csd);
break;
}
}
void
serve(int sd) {
int len, tmpsd;
char *buf, *code;
char tmpfn[320] = {0};
buf = readall(sd, &len, MAXSIZE);
if(!(buf && len)) {
sout(sd, "Nothing pasted.\n");
return;
}
snprintf(tmpfn, sizeof tmpfn, "%s/beans.XXXXXX", path);
tmpsd = mkstemp(tmpfn);
if(tmpsd == -1) {
fprintf(stderr, "mkstemp()\n");
free(buf);
return;
}
if(write(tmpsd, buf, len) == -1)
fprintf(stderr, "write(): %s\n", strerror(errno));
code = strchr(tmpfn, '.')+1;
if(*base)
sout(sd, "%s%s\n", base, code);
else
sout(sd, "%s\n", code);
fchmod(tmpsd, strtol(mode, 0, 8));
close(tmpsd);
free(buf);
}
void
sout(int sd, char *fmt, ...) {
va_list ap;
char buf[4096];
int sz;
va_start(ap, fmt);
sz = vsnprintf(buf, sizeof buf, fmt, ap);
va_end(ap);
send(sd, buf, sz, 0);
}
int
main(int argc, char *argv[]) {
ARGBEGIN {
case 'b': strncpy(base, EARGF(die("%s: missing base URL\n", argv0)), sizeof base); break;
case 'd': strncpy(path, EARGF(die("%s: missing path\n", argv0)), sizeof path); break;
case 'm': strncpy(mode, EARGF(die("%s: missing mode\n", argv0)), sizeof mode); break;
case 'p': strncpy(port, EARGF(die("%s: missing port\n", argv0)), sizeof port); break;
case 'v': die("beans-"VERSION"\n");
} ARGEND
sockd = bindon(port);
signal(SIGCHLD, SIG_IGN); /* cleanup zombies */
run();
close(sockd);
return 0;
}