]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cupsaddsmb.c
d4876a089126e55bc1445ebb6bba9edaadc1f27f
[thirdparty/cups.git] / systemv / cupsaddsmb.c
1 /*
2 * "$Id: cupsaddsmb.c,v 1.3 2001/12/18 21:08:21 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 *subcmd) /* I - Sub-command */
126 {
127 int status; /* Status of command */
128 char temp[4096]; /* 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 -c \'%s\'",
136 command, cupsUser(), p, args, subcmd);
137 else
138 snprintf(temp, sizeof(temp), "%s -N -U\'%s\' %s -c \'%s\'",
139 command, cupsUser(), args, subcmd);
140
141 if (Verbosity)
142 printf("Running command: %s\n", temp);
143 else
144 {
145 strncat(temp, " </dev/null >/dev/null 2>/dev/null", sizeof(temp) - 1);
146 temp[sizeof(temp) - 1] = '\0';
147 }
148
149 if (Verbosity)
150 printf("Running the following command:\n\n %s\n", temp);
151 else
152 {
153 strncat(temp, " >/dev/null 2>/dev/null", sizeof(temp) - 1);
154 temp[sizeof(temp) - 1] = '\0';
155 }
156
157 if ((status = system(temp)) != 0)
158 {
159 if (Verbosity)
160 puts("");
161
162 snprintf(temp, sizeof(temp),
163 "Password for %s required to access %s via SAMBA: ",
164 cupsUser(), cupsServer());
165
166 if ((p = cupsGetPassword(temp)) == NULL)
167 break;
168 }
169 else
170 {
171 if (Verbosity)
172 puts("");
173
174 break;
175 }
176 }
177
178 return (status);
179 }
180
181
182 /*
183 * 'export_dest()' - Export a destination to SAMBA.
184 */
185
186 int /* O - 0 on success, non-zero on error */
187 export_dest(const char *dest) /* I - Destination to export */
188 {
189 int status; /* Status of smbclient/rpcclient commands */
190 const char *ppdfile; /* PPD file for printer drivers */
191 char command[1024], /* Command to run */
192 subcmd[1024]; /* Sub-command */
193 const char *datadir; /* CUPS_DATADIR */
194
195
196 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
197 datadir = CUPS_DATADIR;
198
199 /* Get the PPD file... */
200 if ((ppdfile = cupsGetPPD(dest)) == NULL)
201 {
202 fprintf(stderr, "Warning: No PPD file for printer \"%s\"!\n", dest);
203 return (1);
204 }
205
206 /* Do the smbclient commands needed for the Windows drivers... */
207 snprintf(command, sizeof(command), "smbclient //%s/print\\$", cupsServer());
208
209 snprintf(subcmd, sizeof(subcmd),
210 "mkdir W32X86;"
211 "put %s W32X86/%s.PPD;"
212 "put %s/drivers/ADOBEPS5.DLL W32X86/ADOBEPS5.DLL;"
213 "put %s/drivers/ADOBEPSU.DLL W32X86/ADOBEPSU.DLL;"
214 "put %s/drivers/ADOBEPSU.HLP W32X86/ADOBEPSU.HLP",
215 ppdfile, dest, datadir, datadir, datadir);
216
217 if ((status = do_samba_command(command, "", subcmd)) != 0)
218 {
219 fprintf(stderr, "ERROR: Unable to copy Windows printer driver files (%d)!\n",
220 status);
221 unlink(ppdfile);
222 return (3);
223 }
224
225 snprintf(subcmd, sizeof(subcmd),
226 "mkdir WIN40;"
227 "put %s WIN40/%s.PPD;"
228 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
229 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
230 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
231 "put %s/drivers/DEFPRTR2.PPD WIN40/DEFPRTR2.PPD;"
232 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
233 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
234 ppdfile, dest, datadir, datadir, datadir,
235 datadir, datadir, datadir);
236
237 if ((status = do_samba_command(command, "", subcmd)) != 0)
238 {
239 fprintf(stderr, "ERROR: Unable to copy Windows printer driver files (%d)!\n",
240 status);
241 unlink(ppdfile);
242 return (3);
243 }
244
245 unlink(ppdfile);
246
247 /* Do the rpcclient commands needed for the Windows drivers... */
248 snprintf(subcmd, sizeof(subcmd),
249 "adddriver \"Windows NT x86\" \"%s:ADOBEPS5.DLL:%s.PPD:ADOBEPSU.DLL:ADOBEPSU.HLP:NULL:RAW:NULL\"",
250 dest, dest);
251
252 if ((status = do_samba_command("rpcclient", cupsServer(), subcmd)) != 0)
253 {
254 fprintf(stderr, "ERROR: Unable to install Windows printer driver files (%d)!\n",
255 status);
256 return (5);
257 }
258
259 snprintf(subcmd, sizeof(subcmd), "addprinter %s %s \"%s\" \"\"",
260 dest, dest, dest);
261
262 if ((status = do_samba_command("rpcclient", cupsServer(), subcmd)) != 0)
263 {
264 fprintf(stderr, "ERROR: Unable to install Windows printer driver files (%d)!\n",
265 status);
266 return (5);
267 }
268
269 snprintf(subcmd, sizeof(subcmd),
270 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:ADOBEPS4.HLP:PSMON.DLL:RAW:ADFONTS.MFM,DEFPRTR2.PPD,ICONLIB.DLL\"",
271 dest, dest);
272
273 if ((status = do_samba_command("rpcclient", cupsServer(), subcmd)) != 0)
274 {
275 fprintf(stderr, "ERROR: Unable to install Windows printer driver files (%d)!\n",
276 status);
277 return (5);
278 }
279
280 return (0);
281 }
282
283
284 /*
285 * End of "$Id: cupsaddsmb.c,v 1.3 2001/12/18 21:08:21 mike Exp $".
286 */