-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathonion-print.c
285 lines (249 loc) · 7.41 KB
/
onion-print.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
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
/* File onion-print.c from https://github.com/bstarynk/misc-basile/
*
* A small web server application to print files using lp
© Copyright Basile Starynkevitch 2020
program released under GNU general public license
this is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
this 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.
*/
/*** This uses libonion from github.com/davidmoreno/onion/ ****/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <argp.h>
#include <ctype.h>
#include <onion/onion.h>
#include <onion/request.h>
#include <onion/response.h>
#include <onion/handler.h>
#include <onion/log.h>
#include <onion/shortcuts.h>
#include <onion/exportlocal.h>
#include <onion/path.h>
#include <onion/static.h>
////////////////////////////////////////////////////////////////
///// parsing program options
enum rps_progoption_en
{
OP_PROGOPT__NONE = 0,
OP_PROGOPT_WEB_SERVER_HOST = 'H',
OP_PROGOPT_WEB_SERVER_PORT = 'P',
OP_PROGOPT_PRINT_COMMAND = 1000,
OP_PROGOPT_PRINT_MEGABYTE_LIMIT,
OP_PROGOPT_VERSION,
};
onion *OP_onion = NULL;
const char *OP_webhost;
const char *OP_dirname;
const char *OP_port;
struct argp_option OP_progoptions[] = {
/* ======= web server host ======= */
{ /*name: */ "web-host", ///
/*key: */ OP_PROGOPT_WEB_SERVER_HOST,
///
/*arg: */ "WEBHOST",
///
/*flags: */ 0,
///
/*doc: */ "Run web server host name",
//
/*group: */ 0
///
},
/* ======= web server host ======= */
{ /*name: */ "web-port", ///
/*key: */ OP_PROGOPT_WEB_SERVER_PORT,
///
/*arg: */ "WEBPORT",
///
/*flags: */ 0,
///
/*doc: */ "Run web server port name",
//
/*group: */ 0
///
},
/* ======= printing command ======= */
{ /*name: */ "print-command", ///
/*key: */ OP_PROGOPT_PRINT_COMMAND,
///
/*arg: */ "PRINTCOMMAND",
///
/*flags: */ 0,
///
/*doc: */ "The printing command",
//
/*group: */ 0
///
},
/* ======= printed file size limit ======= */
{ /*name: */ "print-limit", ///
/*key: */ OP_PROGOPT_PRINT_COMMAND,
///
/*arg: */ "SIZELIMIT",
///
/*flags: */ 0,
///
/*doc: */ "The printed file size limit, in megabytes",
//
/*group: */ 0
///
},
/* ======= terminating empty option ======= */
{ /*name: */ (const char *) 0, ///
/*key: */ 0,
///
/*arg: */ (const char *) 0,
///
/*flags: */ 0,
///
/*doc: */ (const char *) 0,
///
/*group: */ 0
///
}
}; /* end OP_progoptions */
// Parse a single program option, skipping side effects when state is empty.
error_t
OP_parse1opt (int key, char *arg, struct argp_state *state)
{
bool side_effect = state != NULL;
switch (key)
{
/// --web-host=WEBHOST
case OP_PROGOPT_WEB_SERVER_HOST:
OP_webhost = arg;
return 0;
}
return ARGP_ERR_UNKNOWN;
}
void
OP_parse_program_arguments (int argc, char **restrict argv)
{
errno = 0;
struct argp_state argstate;
memset (&argstate, 0, sizeof (argstate));
static struct argp argparser;
argparser.options = OP_progoptions;
argparser.parser = OP_parse1opt;
argparser.args_doc = " ; # ";
argparser.doc =
"onion-printer: A tiny web server to redirect printing of PDF files\n."
"GPLv3+ licenses. You should have received a copy of the GNU General Public License\n"
"along with this program. If not, see https://www.gnu.org/licenses\n"
"**NO WARRANTY, not even for FITNESS FOR A PARTICULAR PURPOSE**\n"
"+++ use at your own risk +++\n" "\n Accepted program options are:\n";
argparser.children = NULL;
argparser.help_filter = NULL;
argparser.argp_domain = NULL;
if (argp_parse (&argparser, argc, argv, 0, NULL, NULL))
{
fprintf (stderr,
"%s: failed to parse program arguments. Try %s --help\n",
argv[0], argv[0]);
exit (EXIT_FAILURE);
}
} /* end OP_parse_program_arguments */
void
OP_free_onion (int unused)
{
onion_free (OP_onion);
exit (0);
}
typedef struct
{
const char *abspath;
} upload_file_data;
onion_connection_status
upload_file (upload_file_data *data, onion_request *req, onion_response *res)
{
if (onion_request_get_flags (req) & OR_POST)
{
const char *name = onion_request_get_post (req, "file");
const char *filename = onion_request_get_file (req, "file");
if (name && filename)
{
char finalname[1024];
snprintf (finalname, sizeof (finalname), "%s/%s", data->abspath,
name);
ONION_DEBUG ("Copying from %s to %s", filename, finalname);
onion_shortcut_rename (filename, finalname);
}
}
return OCS_NOT_PROCESSED; // I just ignore the request, but process over the FILE data
}
/// Footer that allows to upload files.
void
upload_file_footer (onion_response *res, const char *dirname)
{
onion_response_write0 (res,
"<p><table><tr><th>Upload a file: <form method=\"POST\" enctype=\"multipart/form-data\">"
"<input type=\"file\" name=\"file\"><input type=\"submit\"><form></th></tr></table>");
onion_response_write0 (res,
"<h2>Onion minimal fileserver. (C) 2010 <a href=\"http://www.coralbits.com\">CoralBits</a>. "
"Under <a href=\"http://www.gnu.org/licenses/agpl-3.0.html\">AGPL 3.0.</a> License.</h2>\n");
}
int
show_help ()
{
printf ("Onion basic fileserver. (C) 2010 Coralbits S.L.\n\n"
"Usage: fileserver [options] [directory to serve]\n\n"
"Options:\n"
" --pem pemfile Uses that certificate and key file. Both must be on the same file. Default is at current dir cert.pem.\n"
" --pam pamname Uses that pam name to allow access. Default login.\n"
" --port N\n"
" -p N Listens at given port. Default 8080\n"
" --listen ADDRESS\n"
" -l ADDRESS Listen to that address. It can be a IPv4 or IPv6 IP, or a local host name. Default: 0.0.0.0\n"
" --help\n"
" -h Shows this help\n"
"\n"
"fileserver serves an HTML with easy access to current files, and allows to upload new files.\n"
"It have SSL encrypting and PAM authentication.\n" "\n");
return 0;
}
int
main (int argc, char **restrict argv)
{
OP_parse_program_arguments (argc, argv);
upload_file_data data = {
OP_dirname
};
onion_handler *root =
onion_handler_new ((void *) upload_file, (void *) &data, NULL);
onion_handler *dir =
onion_handler_export_local_new (argc == 2 ? argv[1] : ".");
onion_handler_export_local_set_footer (dir, upload_file_footer);
onion_handler_add (dir,
onion_handler_static ("<h1>404 - File not found.</h1>",
404));
onion_handler_add (root, dir);
OP_onion = onion_new (O_THREADED);
if (OP_port)
onion_set_port (OP_onion, OP_port);
if (OP_webhost)
onion_set_hostname (OP_onion, OP_webhost);
signal (SIGINT, OP_free_onion);
int error = onion_listen (OP_onion);
if (error)
{
perror ("Cant create the server");
}
onion_free (OP_onion);
return 0;
}
/****************
** for Emacs...
** Local Variables: ;;
** compile-command: "gcc -o onion-print -Wall -Wextra -I/usr/local/include -O -g onion-print.c -L/usr/local/lib -Bstatic -lonion" ;;
** End: ;;
****************/