summaryrefslogtreecommitdiffstats
path: root/tdeprint/cups/cupsdoprint.c
blob: 999b5f493a5cc742e7b3957a35607bcc49fe2dad (plain)
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
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include <config.h>

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include <cups/cups.h>
#include <cups/ipp.h>

#define BUFSIZE		1024
#define BUFSIZE2	32

#define	USE_LOG		0

/* global variables */
char	passwd[BUFSIZE2] = {0};
int	pwd_asked = 0;
#if USE_LOG
FILE	*debugF = NULL;
#endif

/* utility functions */
static void error(const char* msg)
{
	fprintf(stderr, "%s\n", msg);
#if USE_LOG
	if (debugF != NULL) fclose(debugF);
#endif
	exit(-1);
}

static void usage()
{
	error("usage: cupsdoprint [-H host[:port]][-P dest][-J name][-o opt=value[,...]][-U login[:password]] files...");
}

static char * shell_quote(const char *s)
{
   char *result;
   char *p;
   p = result = malloc(strlen(s)*4+3);
   *p++ = '\'';
   while(*s)
   {
     if (*s == '\'')
     {
        *p++ = '\'';
        *p++ = '\\';
        *p++ = '\'';
        *p++ = '\'';
	s++;
     }
     else
     {
        *p++ = *s++;
     }
   }
   *p++ = '\'';
   *p = '\0';
   return result;
}

static const char* getPasswordCB(const char* prompt)
{
	char buf[ 256 ] = {0}, *c;
	char *_user = shell_quote( cupsUser() ), *_passwd = NULL;
	FILE *output;

	snprintf( buf, sizeof( buf )-1, "dcop kded tdeprintd requestPassword %s %s %d %d",
			_user,
			cupsServer(),
			ippPort(),
			pwd_asked );
	free( _user );
	_user = NULL;
	output = popen( buf, "r" );
	if ( output != NULL )
	{
		while ( fgets( buf, sizeof( buf )-1, output ) )
		{
			_user = _passwd = NULL;
			if ( strcmp( buf, "::" ) != 0 )
			{
				c = strchr( buf, ':' );
				if ( c != NULL )
				{
					*c = '\0';
					_user = buf;
					_passwd = ++c;
					c = strchr( c, ':' );
					if ( c != NULL )
					{
						*c = '\0';
						/* retrieve password sequence number */
						pwd_asked = atoi( ++c );
						/* update CUPS with current username */
						cupsSetUser( _user );
						/* copy password to a non temporary location */
						strlcpy( passwd, _passwd, BUFSIZE2 );
						_passwd = passwd;
					}
					else
						_passwd = NULL;
				}
			}
		}
		pclose( output );
	}
	else
		return NULL;

	/* erase buffer containing unencrypted password, for security */
	memset( buf, 0, 256 );

	/* if OK, _passwd should point to global passwd variable, otherwise it should be NULL */
	return _passwd;
}

/* main function */
int main(int argc, char* argv[])
{
	int	c, port = -1;
	char	printer[BUFSIZE] = {0}, jobname[BUFSIZE] = {0}, host[BUFSIZE] = {0};
	char	login[BUFSIZE2] = {0};
	char	*a;
	cups_option_t	*options = NULL;
	int		num_options = 0;
	char*	files[100] = {0};
	int	num_files = 0;
	int	jobID = 0;

#if USE_LOG
	debugF = fopen("/tmp/cupsdoprint.debug","w");
	if (debugF == NULL)
		error("unable to open log file");
#endif

	while ((c=getopt(argc, argv, "P:J:H:o:U:?")) != -1)
	{
#if USE_LOG
		fprintf(debugF,"%c: %s\n",c,optarg);
#endif
		switch (c)
		{
			case 'P':
				strlcpy(printer, optarg, BUFSIZE);
				if ((a=strchr(printer, '/')) != NULL)
					error("This utility doesn't support printer instances");
				break;
			case 'J':
				strlcpy(jobname, optarg, BUFSIZE);
				break;
			case 'H':
				strlcpy(host, optarg, BUFSIZE);
				if ((a=strchr(host, ':')) != NULL)
				{
					*a = 0;
					port = atoi(++a);
					if (port == 0)
						error("Wrong port value");
				}
				break;
			case 'U':
				strlcpy(login, optarg, BUFSIZE2);
				if ((a=strchr(login, ':')) != NULL)
				{
					*a = 0;
					strlcpy(passwd, ++a, BUFSIZE2);
				}
				break;
			case 'o':
#if USE_LOG
				fprintf(debugF,"Parsing options (n=%d)\n",num_options);
#endif
				num_options = cupsParseOptions(optarg, num_options, &options);
#if USE_LOG
				fprintf(debugF,"Options parsed (n=%d)\n",num_options);
#endif
				break;
			case '?':
			default:
				usage();
				break;
		}
	}

	/* check the printer */
	if (!*printer)
	{
		printer[BUFSIZE-1] = '\0';
		if (getenv("PRINTER") != NULL)
			strlcpy(printer, getenv("PRINTER"), BUFSIZE-1);
		else
			error("No printer specified (and PRINTER variable is empty)");
	}

	/* CUPS settings */
	if (host[0] != 0) cupsSetServer(host);
	if (port > 0) ippSetPort(port);
	if (login[0] != 0) cupsSetUser(login);
	if (jobname[0] == 0) strcpy(jobname,"TDE Print System");
	cupsSetPasswordCB(getPasswordCB);

	/* check for files */
	if (optind < 1 || optind >= argc)
		error("This utility doesn't support printing from STDIN");
	else
		for (c=optind; c<argc; c++)
		{
			if (access(argv[c], R_OK) != 0)
			{
				fprintf(stderr, "%s: ", argv[c]);
				error("file not found or not readable");
			}
			else
				files[num_files++] = strdup(argv[c]);
		}

#if USE_LOG
	fprintf(debugF,"Processed options:\n");
	for (c=0; c<num_options; c++)
		fprintf(debugF,"%s = %s\n",options[c].name,options[c].value);
#endif
	/* print files */
	jobID = cupsPrintFiles(printer, num_files, files, jobname, num_options, options);
	/* erase unemcrypted password for security */
	memset( passwd, 0, BUFSIZE2 );
	/* check job creation status */
	if (jobID <= 0)
		error(ippErrorString(cupsLastError()));

#if USE_LOG
	fclose(debugF);
#endif
	return 0;
}