2 * "$Id: cupsaddsmb.c,v 1.14 2002/12/17 19:00:20 swdev Exp $"
4 * "cupsaddsmb" command for the Common UNIX Printing System (CUPS).
6 * Copyright 2001-2003 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
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 * usage() - Show program usage and exit...
33 * Include necessary headers...
38 #include <cups/cups.h>
39 #include <cups/language.h>
40 #include <cups/string.h>
49 const char *SAMBAUser
,
57 int do_samba_command(const char *, const char *);
58 int export_dest(const char *);
63 * 'main()' - Export printers on the command-line.
66 int /* O - Exit status */
67 main(int argc
, /* I - Number of command-line arguments */
68 char *argv
[]) /* I - Command-line arguments */
70 int i
, j
; /* Looping vars */
71 int status
; /* Status from export_dest() */
72 int export_all
; /* Export all printers? */
73 int num_printers
; /* Number of printers */
74 char **printers
; /* Printers */
78 * Parse command-line arguments...
83 SAMBAUser
= cupsUser();
86 for (i
= 1; i
< argc
; i
++)
87 if (strcmp(argv
[i
], "-a") == 0)
89 else if (strcmp(argv
[i
], "-U") == 0)
97 else if (strcmp(argv
[i
], "-H") == 0)
103 SAMBAServer
= argv
[i
];
105 else if (strcmp(argv
[i
], "-h") == 0)
111 cupsSetServer(argv
[i
]);
113 else if (strcmp(argv
[i
], "-v") == 0)
115 else if (argv
[i
][0] != '-')
117 if (SAMBAServer
== NULL
)
118 SAMBAServer
= cupsServer();
120 if ((status
= export_dest(argv
[i
])) != 0)
127 * See if the user specified "-a"...
133 * Export all printers...
136 if (SAMBAServer
== NULL
)
137 SAMBAServer
= cupsServer();
139 num_printers
= cupsGetPrinters(&printers
);
141 for (j
= 0, status
= 0; j
< num_printers
; j
++)
142 if ((status
= export_dest(printers
[j
])) != 0)
145 for (j
= 0; j
< num_printers
; j
++)
160 * 'do_samba_command()' - Do a SAMBA command, asking for
161 * a password as needed.
164 int /* O - Status of command */
165 do_samba_command(const char *command
, /* I - Command to run */
166 const char *subcmd
) /* I - Sub-command */
168 int status
; /* Status of command */
169 char temp
[4096]; /* Command/prompt string */
170 static const char *p
= NULL
; /* Password data */
177 snprintf(temp
, sizeof(temp
),
178 "Password for %s required to access %s via SAMBA: ",
179 SAMBAUser
, SAMBAServer
);
181 if ((p
= cupsGetPassword(temp
)) == NULL
)
185 snprintf(temp
, sizeof(temp
), "%s -N -U\'%s%%%s\' -c \'%s\'",
186 command
, SAMBAUser
, p
, subcmd
);
189 printf("Running command: %s\n", temp
);
191 strlcat(temp
, " </dev/null >/dev/null 2>/dev/null", sizeof(temp
));
193 if ((status
= system(temp
)) != 0)
217 * 'export_dest()' - Export a destination to SAMBA.
220 int /* O - 0 on success, non-zero on error */
221 export_dest(const char *dest
) /* I - Destination to export */
223 int i
; /* Looping var */
224 int status
; /* Status of smbclient/rpcclient commands */
225 const char *ppdfile
; /* PPD file for printer drivers */
226 char command
[1024], /* Command to run */
227 subcmd
[1024]; /* Sub-command */
228 const char *datadir
; /* CUPS_DATADIR */
229 FILE *fp
; /* PPD file */
230 http_t
*http
; /* Connection to server */
231 cups_lang_t
*language
; /* Default language */
232 ipp_t
*request
, /* IPP request */
233 *response
; /* IPP response */
234 ipp_attribute_t
*attr
; /* IPP attribute */
235 static const char *pattrs
[] = /* Printer attributes we want */
237 "job-sheets-supported",
242 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
243 datadir
= CUPS_DATADIR
;
246 * Get the PPD file...
249 if ((ppdfile
= cupsGetPPD(dest
)) == NULL
)
251 fprintf(stderr
, "Warning: No PPD file for printer \"%s\" - skipping!\n", dest
);
256 * Append the supported banner pages to the PPD file...
259 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption())) == NULL
)
261 fprintf(stderr
, "Unable to connect to server \"%s\" for %s - %s\n",
262 cupsServer(), dest
, strerror(errno
));
268 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
269 request
->request
.op
.request_id
= 1;
271 language
= cupsLangDefault();
273 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
274 "attributes-charset", NULL
, cupsLangEncoding(language
));
276 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
277 "attributes-natural-language", NULL
, language
->language
);
279 snprintf(command
, sizeof(command
), "ipp://localhost/printers/%s", dest
);
280 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
281 "printer-uri", NULL
, command
);
283 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
284 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
288 * Do the request and get back a response...
291 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
293 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
295 fprintf(stderr
, "ERROR: get-printer-attributes failed for %s: %s\n",
296 dest
, ippErrorString(response
->request
.status
.status_code
));
304 fprintf(stderr
, "ERROR: get-printer-attributes failed for %s: %s\n",
305 dest
, ippErrorString(cupsLastError()));
311 * Append the banner attributes to the end of the PPD file...
314 if ((fp
= fopen(ppdfile
, "a")) == NULL
)
316 fprintf(stderr
, "ERROR: Unable to append banner attributes to PPD file for %s - %s\n",
317 dest
, strerror(errno
));
323 if ((attr
= ippFindAttribute(response
, "job-sheets-supported", IPP_TAG_NAME
)) != NULL
)
325 fprintf(fp
, "*cupsJobSheetsSupported: \"%s", attr
->values
[0].string
.text
);
327 for (i
= 1; i
< attr
->num_values
; i
++)
328 fprintf(fp
, ",%s", attr
->values
[i
].string
.text
);
333 if ((attr
= ippFindAttribute(response
, "job-sheets-default", IPP_TAG_NAME
)) != NULL
)
335 fprintf(fp
, "*cupsJobSheetsDefault: \"%s", attr
->values
[0].string
.text
);
337 if (attr
->num_values
> 1)
338 fprintf(fp
, ",%s", attr
->values
[1].string
.text
);
346 cupsLangFree(language
);
350 * See which drivers are available - the new CUPS drivers or the
354 snprintf(command
, sizeof(command
), "%s/drivers/cupsdrvr.dll", datadir
);
355 if (access(command
, 0) == 0)
358 * Do the smbclient commands needed for the CUPS WinNT drivers...
361 snprintf(command
, sizeof(command
), "smbclient //%s/print\\$", SAMBAServer
);
363 snprintf(subcmd
, sizeof(subcmd
),
365 "put %s W32X86/%s.ppd;"
366 "put %s/drivers/cupsdrvr.dll W32X86/cupsdrvr.dll;"
367 "put %s/drivers/cupsui.dll W32X86/cupsui.dll;"
368 "put %s/drivers/cups.hlp W32X86/cups.hlp",
369 ppdfile
, dest
, datadir
, datadir
, datadir
);
371 if ((status
= do_samba_command(command
, subcmd
)) != 0)
373 fprintf(stderr
, "ERROR: Unable to copy Windows printer driver files (%d)!\n",
380 * Do the rpcclient commands needed for the CUPS WinNT drivers...
383 snprintf(subcmd
, sizeof(subcmd
),
384 "adddriver \"Windows NT x86\" \"%s:cupsdrvr.dll:%s.ppd:cupsui.dll:cups.hlp:NULL:RAW:NULL\"",
387 snprintf(command
, sizeof(command
), "rpcclient %s", SAMBAServer
);
389 if ((status
= do_samba_command(command
, subcmd
)) != 0)
391 fprintf(stderr
, "ERROR: Unable to install Windows printer driver files (%d)!\n",
400 * Do the smbclient commands needed for the Adobe WinNT drivers...
403 snprintf(command
, sizeof(command
), "smbclient //%s/print\\$", SAMBAServer
);
405 snprintf(subcmd
, sizeof(subcmd
),
407 "put %s W32X86/%s.PPD;"
408 "put %s/drivers/ADOBEPS5.DLL W32X86/ADOBEPS5.DLL;"
409 "put %s/drivers/ADOBEPSU.DLL W32X86/ADOBEPSU.DLL;"
410 "put %s/drivers/ADOBEPSU.HLP W32X86/ADOBEPSU.HLP",
411 ppdfile
, dest
, datadir
, datadir
, datadir
);
413 if ((status
= do_samba_command(command
, subcmd
)) != 0)
415 fprintf(stderr
, "ERROR: Unable to copy Windows printer driver files (%d)!\n",
422 * Do the rpcclient commands needed for the Adobe WinNT drivers...
425 snprintf(subcmd
, sizeof(subcmd
),
426 "adddriver \"Windows NT x86\" \"%s:ADOBEPS5.DLL:%s.PPD:ADOBEPSU.DLL:ADOBEPSU.HLP:NULL:RAW:NULL\"",
429 snprintf(command
, sizeof(command
), "rpcclient %s", SAMBAServer
);
431 if ((status
= do_samba_command(command
, subcmd
)) != 0)
433 fprintf(stderr
, "ERROR: Unable to install Windows printer driver files (%d)!\n",
440 snprintf(command
, sizeof(command
), "%s/drivers/ADOBEPS4.DRV", datadir
);
441 if (access(command
, 0) == 0)
444 * Do the smbclient commands needed for the Adobe Win9x drivers...
447 snprintf(command
, sizeof(command
), "smbclient //%s/print\\$", SAMBAServer
);
449 snprintf(subcmd
, sizeof(subcmd
),
451 "put %s WIN40/%s.PPD;"
452 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
453 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
454 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
455 "put %s/drivers/DEFPRTR2.PPD WIN40/DEFPRTR2.PPD;"
456 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
457 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
458 ppdfile
, dest
, datadir
, datadir
, datadir
,
459 datadir
, datadir
, datadir
);
461 if ((status
= do_samba_command(command
, subcmd
)) != 0)
463 fprintf(stderr
, "ERROR: Unable to copy Windows printer driver files (%d)!\n",
470 * Do the rpcclient commands needed for the Adobe Win9x drivers...
473 snprintf(command
, sizeof(command
), "rpcclient %s", SAMBAServer
);
475 snprintf(subcmd
, sizeof(subcmd
),
476 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:ADOBEPS4.HLP:PSMON.DLL:RAW:"
477 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,DEFPRTR2.PPD,ICONLIB.DLL\"",
480 if ((status
= do_samba_command(command
, subcmd
)) != 0)
482 fprintf(stderr
, "ERROR: Unable to install Windows printer driver files (%d)!\n",
492 * Finally, associate the drivers we just added with the queue...
495 snprintf(command
, sizeof(command
), "rpcclient %s", SAMBAServer
);
497 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
499 if ((status
= do_samba_command(command
, subcmd
)) != 0)
501 fprintf(stderr
, "ERROR: Unable to install Windows printer driver files (%d)!\n",
511 * 'usage()' - Show program usage and exit...
517 puts("Usage: cupsaddsmb [options] printer1 ... printerN");
518 puts(" cupsaddsmb [options] -a");
521 puts(" -H samba-server Use the named SAMBA server");
522 puts(" -U samba-user Authenticate using the named SAMBA user");
523 puts(" -a Export all printers");
524 puts(" -h cups-server Use the named CUPS server");
525 puts(" -v Be verbose (show commands)");
531 * End of "$Id: cupsaddsmb.c,v 1.14 2002/12/17 19:00:20 swdev Exp $".