/[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 598 - (show annotations)
Fri Feb 6 10:32:13 2004 UTC (20 years, 4 months ago) by n-ki
File MIME type: text/plain
File size: 4217 byte(s)
fix mem free bug with printercache - from anders flick

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 *data = NULL;
83
84 home = getenv("HOME");
85 if (home == NULL)
86 return 0;
87
88 path = (char *) xmalloc(strlen(home) + sizeof("/.rdesktop/rdpdr/") + strlen(printer_name) +
89 sizeof("/AutoPrinterCacheData"));
90 sprintf(path, "%s/.rdesktop/rdpdr/%s/AutoPrinterCacheData", home, printer_name);
91
92 fd = open(path, O_RDONLY);
93 if (fd == -1)
94 return 0;
95
96 if (fstat(fd, &st))
97 return 0;
98
99 *data = (uint8 *) xmalloc(st.st_size);
100 length = read(fd, *data, st.st_size);
101 close(fd);
102 xfree(path);
103 return length;
104 }
105
106 void
107 printercache_save_blob(char *printer_name, uint8 * data, uint32 length)
108 {
109 char *home, *path;
110 int fd;
111
112 if (printer_name == NULL)
113 return;
114
115 home = getenv("HOME");
116 if (home == NULL)
117 return;
118
119 if (!printercache_mkdir(home, printer_name))
120 return;
121
122 path = (char *) xmalloc(strlen(home) + sizeof("/.rdesktop/rdpdr/") + strlen(printer_name) +
123 sizeof("/AutoPrinterCacheData"));
124 sprintf(path, "%s/.rdesktop/rdpdr/%s/AutoPrinterCacheData", home, printer_name);
125
126 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
127 if (fd == -1)
128 {
129 perror(path);
130 return;
131 }
132
133 if (write(fd, data, length) != length)
134 {
135 perror(path);
136 unlink(path);
137 }
138
139 close(fd);
140 xfree(path);
141 }
142
143 void
144 printercache_process(STREAM s)
145 {
146 uint32 type, printer_length, driver_length, printer_unicode_length, blob_length;
147 char device_name[9], printer[256], driver[256];
148
149 in_uint32_le(s, type);
150 switch (type)
151 {
152 /*case 4: renaming of item old name and then new name */
153 /*case 3: delete item name */
154 case 2:
155 in_uint32_le(s, printer_unicode_length);
156 in_uint32_le(s, blob_length);
157
158 if (printer_unicode_length < 2 * 255)
159 {
160 rdp_in_unistr(s, printer, printer_unicode_length);
161 printercache_save_blob(printer, s->p, blob_length);
162 }
163 break;
164
165 /*case 1: */
166 // TODO: I think this one just tells us what printer is on LPT? but why?
167
168 //
169 // your name and the "users choice" of printer driver
170 // my guess is that you can store it and automagically reconnect
171 // the printer with correct driver next time.
172 default:
173
174 unimpl("RDPDR Printer Cache Packet Type: %d\n", type);
175 break;
176 }
177 }

  ViewVC Help
Powered by ViewVC 1.1.26