forked from OpenPrinting/cups-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcups-brf.c
163 lines (147 loc) · 4.81 KB
/
cups-brf.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
/*
* BRF (Braille-Ready Format) virtual backend
*
* Copyright (c) 2017 by Samuel Thibault <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <cups/backend.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <pwd.h>
int main(int argc, char *argv[]) {
char *user;
char *dir;
char *title;
char *outfile;
char *c;
char buffer[4096];
ssize_t sizein, sizeout, done;
struct passwd *pw;
int ret;
int fd;
if (setuid(0)) {
/* We need to be root to be able to turn into another user. */
fprintf(stderr,"ERROR: cups-brf must be called as root\n");
return CUPS_BACKEND_FAILED;
}
if (argc == 1) {
/* This is just discovery. */
printf("file cups-brf:/ \"Virtual Braille BRF Printer\" \"CUPS-BRF\" \"MFG:Generic;MDL:CUPS-BRF Printer;DES:Generic CUPS-BRF Printer;CLS:PRINTER;CMD:BRF;\"\n");
return CUPS_BACKEND_OK;
}
if (argc < 6) {
/* Invalid number of parameters. */
fprintf(stderr, "ERROR: cups-brf jobid user name nb options [filename]\n");
return CUPS_BACKEND_FAILED;
}
if (argc == 7) {
/* Explicit file name, open it. */
char *filename = argv[6];
fd = open(filename, O_RDONLY);
if (dup2(fd, STDIN_FILENO) < 0) {
fprintf(stderr, "ERROR: opening file \"%s\"\n", filename);
return CUPS_BACKEND_FAILED;
}
}
/* Now we have everything, turn into the user */
user = argv[2];
pw = getpwnam(user);
if (pw == NULL) {
fprintf(stderr, "ERROR: getting user \"%s\" information\n", user);
return CUPS_BACKEND_FAILED;
}
if (setgid(pw->pw_gid)) {
fprintf(stderr, "ERROR: turning gid into %u\n", pw->pw_gid);
return CUPS_BACKEND_FAILED;
}
if (setuid(pw->pw_uid)) {
fprintf(stderr, "ERROR: turning uid into %u\n", pw->pw_uid);
return CUPS_BACKEND_FAILED;
}
/* Now we act as user */
umask(0077);
/* Create BRF directory in $HOME */
if (asprintf(&dir, "%s/BRF", pw->pw_dir) < 0) {
fprintf(stderr, "ERROR: could not allocate memory\n");
return CUPS_BACKEND_FAILED;
}
fprintf(stderr, "DEBUG: creating directory \"%s\n", dir);
ret = mkdir(dir, 0700);
if (ret == -1 && errno != EEXIST) {
fprintf(stderr, "ERROR: could not create directory \"%s\": %s\n", dir, strerror(errno));
return CUPS_BACKEND_FAILED;
}
/* Avoid escaping from the directory */
title = argv[3];
for (c = title; *c; c++) {
if (*c == '/')
*c = '_';
}
/* Avoid hiding the file */
while (*title == '.')
title++;
/* Avoid empty title */
if (!*title)
title = "unknown";
/* generate mask */
if (asprintf(&outfile, "%s/%s.XXXXXX.brf", dir, title) < 0) {
fprintf(stderr, "ERROR: could not allocate memory\n");
return CUPS_BACKEND_FAILED;
}
/* Create file */
fprintf(stderr, "DEBUG: creating file \"%s\n", outfile);
fd = mkstemps(outfile, 4);
if (fd < 0) {
fprintf(stderr, "ERROR: could not create file \"%s\": %s\n", outfile, strerror(errno));
return CUPS_BACKEND_FAILED;
}
/* We are all set, copy data. */
while (1) {
/* Read some. */
sizein = read(STDIN_FILENO, buffer, sizeof(buffer));
if (sizein < 0) {
fprintf(stderr, "ERROR: while reading input: %s\n", strerror(errno));
return CUPS_BACKEND_FAILED;
}
if (sizein == 0)
/* We are done! */
break;
/* Write it. */
for (done = 0; done < sizein; done += sizeout) {
sizeout = write(fd, buffer + done, sizein - done);
if (sizeout < 0) {
fprintf(stderr, "ERROR: while writing to \"%s\": %s\n", outfile, strerror(errno));
return CUPS_BACKEND_FAILED;
}
}
}
if (close(fd) < 0) {
fprintf(stderr, "ERROR: while closing \"%s\": %s\n", outfile, strerror(errno));
return CUPS_BACKEND_FAILED;
}
return CUPS_BACKEND_OK;
}