]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cupsaddsmb.c
Fix SAMBA driver export - the Win95 export commands weren't right,
[thirdparty/cups.git] / systemv / cupsaddsmb.c
1 /*
2 * "$Id: cupsaddsmb.c,v 1.2 2001/11/09 17:19:44 mike Exp $"
3 *
4 * "cupsaddsmb" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2001 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Export printers on the command-line.
27 * do_samba_command() - Do a SAMBA command, asking for a password as needed.
28 * export_dest() - Export a destination to SAMBA.
29 */
30
31 /*
32 * Include necessary headers...
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <cups/cups.h>
38 #include <cups/string.h>
39 #include <errno.h>
40
41
42 /*
43 * Local globals...
44 */
45
46 int Verbosity = 0;
47
48
49 /*
50 * Local functions...
51 */
52
53 int do_samba_command(const char *, const char *, const char *);
54 int export_dest(const char *);
55
56
57 /*
58 * 'main()' - Export printers on the command-line.
59 */
60
61 int /* O - Exit status */
62 main(int argc, /* I - Number of command-line arguments */
63 char *argv[]) /* I - Command-line arguments */
64 {
65 int i, j; /* Looping vars */
66 int status; /* Status from export_dest() */
67 int num_printers; /* Number of printers */
68 char **printers; /* Printers */
69
70
71 for (i = 1; i < argc; i ++)
72 if (strcmp(argv[i], "-a") == 0)
73 {
74 num_printers = cupsGetPrinters(&printers);
75
76 for (j = 0, status = 0; j < num_printers; j ++)
77 if ((status = export_dest(printers[j])) != 0)
78 break;
79
80 for (j = 0; j < num_printers; j ++)
81 free(printers[j]);
82
83 if (num_printers)
84 free(printers);
85
86 if (status)
87 return (status);
88 }
89 else if (strcmp(argv[i], "-U") == 0)
90 {
91 i ++;
92 if (i >= argc)
93 {
94 puts("Usage: cupsaddsmb [-a] [-U user] [-v] [printer1 ... printerN]");
95 return (1);
96 }
97
98 cupsSetUser(argv[i]);
99 }
100 else if (strcmp(argv[i], "-v") == 0)
101 Verbosity = 1;
102 else if (argv[i][0] != '-')
103 {
104 if ((status = export_dest(argv[i])) != 0)
105 return (status);
106 }
107 else
108 {
109 puts("Usage: cupsaddsmb [-a] [-U user] [-v] [printer1 ... printerN]");
110 return (1);
111 }
112
113 return (0);
114 }
115
116
117 /*
118 * 'do_samba_command()' - Do a SAMBA command, asking for
119 * a password as needed.
120 */
121
122 int /* O - Status of command */
123 do_samba_command(const char *command, /* I - Command to run */
124 const char *args, /* I - Argument(s) for command */
125 const char *filename) /* I - File to use as input */
126 {
127 int status; /* Status of command */
128 char temp[1024]; /* Command/prompt string */
129 static const char *p = NULL; /* Password data */
130
131
132 for (status = 1;;)
133 {
134 if (p)
135 snprintf(temp, sizeof(temp), "%s -N -U \'%s%%%s\' %s <%s",
136 command, cupsUser(), p, args, filename ? filename : "/dev/null");
137 else
138 snprintf(temp, sizeof(temp), "%s -N -U \'%s\' %s <%s",
139 command, cupsUser(), args, filename ? filename : "/dev/null");
140
141 if (Verbosity)
142 {
143 printf("Running the following command:\n\n %s\n", temp);
144
145 if (filename)
146 {
147 char cat[1024];
148
149
150 puts("\nwith the following input:\n");
151
152 snprintf(cat, sizeof(cat), "cat %s", filename);
153 system(cat);
154 }
155 }
156 else
157 {
158 strncat(temp, " >/dev/null 2>/dev/null", sizeof(temp) - 1);
159 temp[sizeof(temp) - 1] = '\0';
160 }
161
162 if ((status = system(temp)) != 0)
163 {
164 if (Verbosity)
165 puts("");
166
167 snprintf(temp, sizeof(temp),
168 "Password for %s required to access %s via SAMBA: ",
169 cupsUser(), cupsServer());
170
171 if ((p = cupsGetPassword(temp)) == NULL)
172 break;
173 }
174 else
175 {
176 if (Verbosity)
177 puts("");
178
179 break;
180 }
181 }
182
183 return (status);
184 }
185
186
187 /*
188 * 'export_dest()' - Export a destination to SAMBA.
189 */
190
191 int /* O - 0 on success, non-zero on error */
192 export_dest(const char *dest) /* I - Destination to export */
193 {
194 int status; /* Status of smbclient/rpcclient commands */
195 FILE *fp; /* File pointer for temp file */
196 char tempfile[1024]; /* Temporary file for print commands */
197 const char *ppdfile; /* PPD file for printer drivers */
198 char command[1024]; /* Command to run */
199 const char *datadir; /* CUPS_DATADIR */
200
201
202 /* Get a temporary file for our smbclient and rpcclient commands... */
203 cupsTempFile(tempfile, sizeof(tempfile));
204
205 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
206 datadir = CUPS_DATADIR;
207
208 /* Get the PPD file... */
209 if ((ppdfile = cupsGetPPD(dest)) == NULL)
210 {
211 fprintf(stderr, "Warning: No PPD file for printer \"%s\"!\n", dest);
212 return (1);
213 }
214
215 /* Write the smbclient commands needed for the Windows drivers... */
216 if ((fp = fopen(tempfile, "w")) == NULL)
217 {
218 fprintf(stderr, "ERROR: Unable to create temporary file \"%s\" for export - %s\n",
219 tempfile, strerror(errno));
220 unlink(ppdfile);
221 return (2);
222 }
223
224 fputs("mkdir W32X86\n", fp);
225 fprintf(fp, "put %s W32X86/%s.PPD\n", ppdfile, dest);
226 fprintf(fp, "put %s/drivers/ADOBEPS5.DLL W32X86/ADOBEPS5.DLL\n",
227 datadir);
228 fprintf(fp, "put %s/drivers/ADOBEPSU.DLL W32X86/ADOBEPSU.DLL\n",
229 datadir);
230 fprintf(fp, "put %s/drivers/ADOBEPSU.HLP W32X86/ADOBEPSU.HLP\n",
231 datadir);
232 fputs("mkdir WIN40\n", fp);
233 fprintf(fp, "put %s WIN40/%s.PPD\n", ppdfile, dest);
234 fprintf(fp, "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM\n",
235 datadir);
236 fprintf(fp, "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV\n",
237 datadir);
238 fprintf(fp, "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP\n",
239 datadir);
240 fprintf(fp, "put %s/drivers/DEFPRTR2.PPD WIN40/DEFPRTR2.PPD\n",
241 datadir);
242 fprintf(fp, "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL\n",
243 datadir);
244 fprintf(fp, "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL\n",
245 datadir);
246 fputs("quit\n", fp);
247
248 fclose(fp);
249
250 /* Run the smbclient command to copy the Windows drivers... */
251 snprintf(command, sizeof(command), "smbclient //%s/print\\$", cupsServer());
252
253 if ((status = do_samba_command(command, "", tempfile)) != 0)
254 {
255 fprintf(stderr, "ERROR: Unable to copy Windows printer driver files (%d)!\n",
256 status);
257 unlink(ppdfile);
258 unlink(tempfile);
259 return (3);
260 }
261
262 unlink(ppdfile);
263
264 /* Write the rpcclient commands needed for the Windows drivers... */
265 if ((fp = fopen(tempfile, "w")) == NULL)
266 {
267 fprintf(stderr, "ERROR: Unable to create temporary file \"%s\" for export - %s\n",
268 tempfile, strerror(errno));
269 unlink(tempfile);
270 return (4);
271 }
272
273 fprintf(fp, "adddriver \"Windows NT x86\" "
274 "\"%s:ADOBEPS5.DLL:%s.PPD:ADOBEPSU.DLL:ADOBEPSU.HLP:"
275 "NULL:RAW:NULL\"\n",
276 dest, dest);
277 fprintf(fp, "addprinter %s %s \"%s\" \"\"\n", dest, dest, dest);
278
279 /*
280 * MRS: For some reason, SAMBA doesn't like to install Win9x drivers
281 * with aux files. They are currently commented out but further
282 * investigation is required...
283 */
284
285 fprintf(fp, "adddriver \"Windows 4.0\" "
286 "\"%s:ADOBEPS4.DRV:%s.PPD:NULL:ADOBEPS4.HLP:"
287 "PSMON.DLL:RAW:NULL\"\n",
288 /*"PSMON.DLL:RAW:ADFONTS.MFM,DEFPRTR2.PPD,ICONLIB.DLL\"\n",*/
289 dest, dest);
290 fputs("quit\n", fp);
291
292 fclose(fp);
293
294 /* Run the rpcclient command to install the Windows drivers... */
295 if ((status = do_samba_command("rpcclient", cupsServer(), tempfile)) != 0)
296 {
297 fprintf(stderr, "ERROR: Unable to install Windows printer driver files (%d)!\n",
298 status);
299 unlink(tempfile);
300 return (5);
301 }
302
303 unlink(tempfile);
304
305 return (0);
306 }
307
308
309 /*
310 * End of "$Id: cupsaddsmb.c,v 1.2 2001/11/09 17:19:44 mike Exp $".
311 */