/[rdesktop]/sourceforge.net/trunk/rdesktop/printercache.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/printercache.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 580 - (show annotations)
Fri Jan 23 08:35:52 2004 UTC (20 years, 4 months ago) by astrand
File MIME type: text/plain
File size: 3927 byte(s)
Ran indent-all.sh

1 /* -*- c-basic-offset: 8 -*-
2 * rdesktop: A Remote Desktop Protocol client.
3 * Entrypoint and utility functions
4 * Copyright (C) Matthew Chapman 1999-2003
5 * Copyright (C) Jeroen Meijer 2003
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /* According to the W2K RDP Printer Redirection WhitePaper, a data
23 * blob is sent to the client after the configuration of the printer
24 * is changed at the server.
25 *
26 * This data blob is saved to the registry. The client returns this
27 * data blob in a new session with the printer announce data.
28 * The data is not interpreted by the client.
29 */
30
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include "rdesktop.h"
38
39 BOOL
40 printercache_mkdir(char *base, char *printer)
41 {
42 char *path;
43
44 path = (char *) xmalloc(strlen(base) + sizeof("/.rdesktop/rdpdr/") + strlen(printer));
45
46 sprintf(path, "%s/.rdesktop", base);
47 if ((mkdir(path, 0700) == -1) && errno != EEXIST)
48 {
49 perror(path);
50 return False;
51 }
52
53 strcat(path, "/rdpdr");
54 if ((mkdir(path, 0700) == -1) && errno != EEXIST)
55 {
56 perror(path);
57 return False;
58 }
59
60 strcat(path, "/");
61 strcat(path, printer);
62 if ((mkdir(path, 0700) == -1) && errno != EEXIST)
63 {
64 perror(path);
65 return False;
66 }
67
68 xfree(path);
69 return True;
70 }
71
72 int
73 printercache_load_blob(char *printer_name, uint8 ** data)
74 {
75 char *home, *path;
76 struct stat st;
77 int fd, length;
78
79 if (printer_name == NULL)
80 return 0;
81
82 home = getenv("HOME");
83 if (home == NULL)
84 return 0;
85
86 path = (char *) xmalloc(strlen(home) + sizeof("/.rdesktop/rdpdr/") + strlen(printer_name) +
87 sizeof("/AutoPrinterCacheData"));
88 sprintf(path, "%s/.rdesktop/rdpdr/%s/AutoPrinterCacheData", home, printer_name);
89
90 fd = open(path, O_RDONLY);
91 if (fd == -1)
92 return 0;
93
94 if (fstat(fd, &st))
95 return 0;
96
97 *data = (uint8 *) xmalloc(st.st_size);
98 length = read(fd, *data, st.st_size);
99 close(fd);
100 xfree(path);
101 return length;
102 }
103
104 void
105 printercache_save_blob(char *printer_name, uint8 * data, uint32 length)
106 {
107 char *home, *path;
108 int fd;
109
110 if (printer_name == NULL)
111 return;
112
113 home = getenv("HOME");
114 if (home == NULL)
115 return;
116
117 if (!printercache_mkdir(home, printer_name))
118 return;
119
120 path = (char *) xmalloc(strlen(home) + sizeof("/.rdesktop/rdpdr/") + strlen(printer_name) +
121 sizeof("/AutoPrinterCacheData"));
122 sprintf(path, "%s/.rdesktop/rdpdr/%s/AutoPrinterCacheData", home, printer_name);
123
124 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
125 if (fd == -1)
126 {
127 perror(path);
128 return;
129 }
130
131 if (write(fd, data, length) != length)
132 {
133 perror(path);
134 unlink(path);
135 }
136
137 close(fd);
138 xfree(path);
139 }
140
141 void
142 printercache_process(STREAM s)
143 {
144 uint32 type, printer_length, driver_length, printer_unicode_length, blob_length;
145 char device_name[9], printer[256], driver[256];
146
147 in_uint32_le(s, type);
148 switch (type)
149 {
150 case 2:
151
152 in_uint32_le(s, printer_unicode_length);
153 in_uint32_le(s, blob_length);
154
155 if (printer_unicode_length < 2 * 255)
156 {
157 rdp_in_unistr(s, printer, printer_unicode_length);
158 printercache_save_blob(printer, s->p, blob_length);
159 }
160 break;
161
162 case 1:
163
164 // TODO: I think this one just tells us what printer is on LPT? but why?
165
166 default:
167
168 unimpl("RDPDR Printer Cache Packet Type: %d\n", type);
169 break;
170 }
171 }

  ViewVC Help
Powered by ViewVC 1.1.26