1/*
2 * "$Id: adminutil.c 11645 2014-02-27 16:35:53Z msweet $"
3 *
4 * Administration utility API definitions for CUPS.
5 *
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 2001-2007 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file.  If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 */
17
18/*
19 * Include necessary headers...
20 */
21
22#include "cups-private.h"
23#include "adminutil.h"
24#include <fcntl.h>
25#include <sys/stat.h>
26#ifdef WIN32
27#else
28#  include <unistd.h>
29#  include <sys/wait.h>
30#endif /* WIN32 */
31
32
33/*
34 * Local functions...
35 */
36
37static int		do_samba_command(const char *command,
38			                 const char *address,
39			                 const char *subcommand,
40					 const char *authfile,
41					 FILE *logfile);
42static http_status_t	get_cupsd_conf(http_t *http, _cups_globals_t *cg,
43			               time_t last_update, char *name,
44				       size_t namelen, int *remote);
45static void		invalidate_cupsd_cache(_cups_globals_t *cg);
46static void		write_option(cups_file_t *dstfp, int order,
47			             const char *name, const char *text,
48				     const char *attrname,
49		        	     ipp_attribute_t *suppattr,
50				     ipp_attribute_t *defattr, int defval,
51				     int valcount);
52
53
54/*
55 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
56 *
57 * @deprecated@
58 */
59
60char *					/* O - PPD file or NULL */
61cupsAdminCreateWindowsPPD(
62    http_t     *http,			/* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
63    const char *dest,			/* I - Printer or class */
64    char       *buffer,			/* I - Filename buffer */
65    int        bufsize)			/* I - Size of filename buffer */
66{
67  const char		*src;		/* Source PPD filename */
68  cups_file_t		*srcfp,		/* Source PPD file */
69			*dstfp;		/* Destination PPD file */
70  ipp_t			*request,	/* IPP request */
71			*response;	/* IPP response */
72  ipp_attribute_t	*suppattr,	/* IPP -supported attribute */
73			*defattr;	/* IPP -default attribute */
74  cups_lang_t		*language;	/* Current language */
75  char			line[256],	/* Line from PPD file */
76			junk[256],	/* Extra junk to throw away */
77			*ptr,		/* Pointer into line */
78			uri[1024],	/* Printer URI */
79			option[41],	/* Option */
80			choice[41];	/* Choice */
81  int			jcloption,	/* In a JCL option? */
82			jclorder,	/* Next JCL order dependency */
83			linenum;	/* Current line number */
84  time_t		curtime;	/* Current time */
85  struct tm		*curdate;	/* Current date */
86  static const char * const pattrs[] =	/* Printer attributes we want */
87			{
88			  "job-hold-until-supported",
89			  "job-hold-until-default",
90			  "job-sheets-supported",
91			  "job-sheets-default",
92			  "job-priority-supported",
93			  "job-priority-default"
94			};
95
96
97 /*
98  * Range check the input...
99  */
100
101  if (buffer)
102    *buffer = '\0';
103
104  if (!http)
105    http = _cupsConnect();
106
107  if (!http || !dest || !buffer || bufsize < 2)
108    return (NULL);
109
110 /*
111  * Get the PPD file...
112  */
113
114  if ((src = cupsGetPPD2(http, dest)) == NULL)
115    return (NULL);
116
117 /*
118  * Get the supported banner pages, etc. for the printer...
119  */
120
121  request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
122
123  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
124                   "localhost", 0, "/printers/%s", dest);
125  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
126               "printer-uri", NULL, uri);
127
128  ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
129                "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
130		NULL, pattrs);
131
132 /*
133  * Do the request and get back a response...
134  */
135
136  response = cupsDoRequest(http, request, "/");
137  if (!response || cupsLastError() > IPP_STATUS_OK_CONFLICTING)
138  {
139    unlink(src);
140    return (NULL);
141  }
142
143 /*
144  * Open the original PPD file...
145  */
146
147  if ((srcfp = cupsFileOpen(src, "rb")) == NULL)
148    return (NULL);
149
150 /*
151  * Create a temporary output file using the destination buffer...
152  */
153
154  if ((dstfp = cupsTempFile2(buffer, bufsize)) == NULL)
155  {
156    cupsFileClose(srcfp);
157
158    unlink(src);
159
160    return (NULL);
161  }
162
163 /*
164  * Write a new header explaining that this isn't the original PPD...
165  */
166
167  cupsFilePuts(dstfp, "*PPD-Adobe: \"4.3\"\n");
168
169  curtime = time(NULL);
170  curdate = gmtime(&curtime);
171
172  cupsFilePrintf(dstfp, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
173                        "for CUPS Windows Driver\n",
174        	 curdate->tm_year + 1900, curdate->tm_mon + 1, curdate->tm_mday,
175        	 curdate->tm_hour, curdate->tm_min, curdate->tm_sec);
176
177 /*
178  * Read the existing PPD file, converting all PJL commands to CUPS
179  * job ticket comments...
180  */
181
182  jcloption = 0;
183  jclorder  = 0;
184  linenum   = 0;
185  language  = cupsLangDefault();
186
187  while (cupsFileGets(srcfp, line, sizeof(line)))
188  {
189    linenum ++;
190
191    if (!strncmp(line, "*PPD-Adobe:", 11))
192    {
193     /*
194      * Already wrote the PPD header...
195      */
196
197      continue;
198    }
199    else if (!strncmp(line, "*JCLBegin:", 10) ||
200             !strncmp(line, "*JCLToPSInterpreter:", 20) ||
201	     !strncmp(line, "*JCLEnd:", 8) ||
202	     !strncmp(line, "*Protocols:", 11))
203    {
204     /*
205      * Don't use existing JCL keywords; we'll create our own, below...
206      */
207
208      cupsFilePrintf(dstfp, "*%% Commented out for CUPS Windows Driver...\n"
209                            "*%%%s\n", line + 1);
210      continue;
211    }
212    else if (!strncmp(line, "*JCLOpenUI", 10))
213    {
214      jcloption = 1;
215      cupsFilePrintf(dstfp, "%s\n", line);
216    }
217    else if (!strncmp(line, "*JCLCloseUI", 11))
218    {
219      jcloption = 0;
220      cupsFilePrintf(dstfp, "%s\n", line);
221    }
222    else if (jcloption && !strncmp(line, "*OrderDependency:", 17))
223    {
224      for (ptr = line + 17; _cups_isspace(*ptr); ptr ++);
225
226      ptr = strchr(ptr, ' ');
227
228      if (ptr)
229      {
230	cupsFilePrintf(dstfp, "*OrderDependency: %d%s\n", jclorder, ptr);
231	jclorder ++;
232      }
233      else
234        cupsFilePrintf(dstfp, "%s\n", line);
235    }
236    else if (jcloption &&
237             strncmp(line, "*End", 4) &&
238             strncmp(line, "*Default", 8))
239    {
240      if ((ptr = strchr(line, ':')) == NULL)
241      {
242        snprintf(line, sizeof(line),
243	         _cupsLangString(language, _("Missing value on line %d.")),
244		 linenum);
245        _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR, line, 0);
246
247        cupsFileClose(srcfp);
248        cupsFileClose(dstfp);
249
250	unlink(src);
251	unlink(buffer);
252
253        *buffer = '\0';
254
255	return (NULL);
256      }
257
258      if ((ptr = strchr(ptr, '\"')) == NULL)
259      {
260        snprintf(line, sizeof(line),
261	         _cupsLangString(language,
262		                 _("Missing double quote on line %d.")),
263	         linenum);
264        _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR, line, 0);
265
266        cupsFileClose(srcfp);
267        cupsFileClose(dstfp);
268
269	unlink(src);
270	unlink(buffer);
271
272        *buffer = '\0';
273
274	return (NULL);
275      }
276
277      if (sscanf(line, "*%40s%*[ \t]%40[^:/]", option, choice) != 2)
278      {
279        snprintf(line, sizeof(line),
280	         _cupsLangString(language,
281		                 _("Bad option + choice on line %d.")),
282	         linenum);
283        _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR, line, 0);
284
285        cupsFileClose(srcfp);
286        cupsFileClose(dstfp);
287
288	unlink(src);
289	unlink(buffer);
290
291        *buffer = '\0';
292
293	return (NULL);
294      }
295
296      if (strchr(ptr + 1, '\"') == NULL)
297      {
298       /*
299        * Skip remaining...
300	*/
301
302	while (cupsFileGets(srcfp, junk, sizeof(junk)) != NULL)
303	{
304	  linenum ++;
305
306	  if (!strncmp(junk, "*End", 4))
307	    break;
308	}
309      }
310
311      snprintf(ptr + 1, sizeof(line) - (size_t)(ptr - line + 1),
312               "%%cupsJobTicket: %s=%s\n\"\n*End", option, choice);
313
314      cupsFilePrintf(dstfp, "*%% Changed for CUPS Windows Driver...\n%s\n",
315                     line);
316    }
317    else
318      cupsFilePrintf(dstfp, "%s\n", line);
319  }
320
321  cupsFileClose(srcfp);
322  unlink(src);
323
324  if (linenum == 0)
325  {
326    _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR, _("Empty PPD file."), 1);
327
328    cupsFileClose(dstfp);
329    unlink(buffer);
330
331    *buffer = '\0';
332
333    return (NULL);
334  }
335
336 /*
337  * Now add the CUPS-specific attributes and options...
338  */
339
340  cupsFilePuts(dstfp, "\n*% CUPS Job Ticket support and options...\n");
341  cupsFilePuts(dstfp, "*Protocols: PJL\n");
342  cupsFilePuts(dstfp, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
343  cupsFilePuts(dstfp, "*JCLToPSInterpreter: \"\"\n");
344  cupsFilePuts(dstfp, "*JCLEnd: \"\"\n");
345
346  cupsFilePuts(dstfp, "\n*OpenGroup: CUPS/CUPS Options\n\n");
347
348  if ((defattr = ippFindAttribute(response, "job-hold-until-default",
349                                  IPP_TAG_ZERO)) != NULL &&
350      (suppattr = ippFindAttribute(response, "job-hold-until-supported",
351                                   IPP_TAG_ZERO)) != NULL)
352    write_option(dstfp, jclorder ++, "cupsJobHoldUntil", "Hold Until",
353                 "job-hold-until", suppattr, defattr, 0, 1);
354
355  if ((defattr = ippFindAttribute(response, "job-priority-default",
356                                  IPP_TAG_INTEGER)) != NULL &&
357      (suppattr = ippFindAttribute(response, "job-priority-supported",
358                                   IPP_TAG_RANGE)) != NULL)
359    write_option(dstfp, jclorder ++, "cupsJobPriority", "Priority",
360                 "job-priority", suppattr, defattr, 0, 1);
361
362  if ((defattr = ippFindAttribute(response, "job-sheets-default",
363                                  IPP_TAG_ZERO)) != NULL &&
364      (suppattr = ippFindAttribute(response, "job-sheets-supported",
365                                   IPP_TAG_ZERO)) != NULL)
366  {
367    write_option(dstfp, jclorder ++, "cupsJobSheetsStart", "Start Banner",
368                 "job-sheets", suppattr, defattr, 0, 2);
369    write_option(dstfp, jclorder, "cupsJobSheetsEnd", "End Banner",
370                 "job-sheets", suppattr, defattr, 1, 2);
371  }
372
373  cupsFilePuts(dstfp, "*CloseGroup: CUPS\n");
374  cupsFileClose(dstfp);
375
376  ippDelete(response);
377
378  return (buffer);
379}
380
381
382/*
383 * 'cupsAdminExportSamba()' - Export a printer to Samba.
384 *
385 * @deprecated@
386 */
387
388int					/* O - 1 on success, 0 on failure */
389cupsAdminExportSamba(
390    const char *dest,			/* I - Destination to export */
391    const char *ppd,			/* I - PPD file */
392    const char *samba_server,		/* I - Samba server */
393    const char *samba_user,		/* I - Samba username */
394    const char *samba_password,		/* I - Samba password */
395    FILE       *logfile)		/* I - Log file, if any */
396{
397  int			status;		/* Status of Samba commands */
398  int			have_drivers;	/* Have drivers? */
399  char			file[1024],	/* File to test for */
400			authfile[1024],	/* Temporary authentication file */
401			address[1024],	/* Address for command */
402			subcmd[1024],	/* Sub-command */
403			message[1024];	/* Error message */
404  cups_file_t		*fp;		/* Authentication file */
405  cups_lang_t		*language;	/* Current language */
406  _cups_globals_t	*cg = _cupsGlobals();
407					/* Global data */
408
409
410 /*
411  * Range check input...
412  */
413
414  if (!dest || !ppd || !samba_server || !samba_user || !samba_password)
415  {
416    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
417    return (0);
418  }
419
420 /*
421  * Create a temporary authentication file for Samba...
422  */
423
424  if ((fp = cupsTempFile2(authfile, sizeof(authfile))) == NULL)
425  {
426    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
427    return (0);
428  }
429
430  cupsFilePrintf(fp, "username = %s\n", samba_user);
431  cupsFilePrintf(fp, "password = %s\n", samba_password);
432  cupsFileClose(fp);
433
434 /*
435  * See which drivers are available; the new CUPS v6 and Adobe drivers
436  * depend on the Windows 2k PS driver, so copy that driver first:
437  *
438  * Files:
439  *
440  *     ps5ui.dll
441  *     pscript.hlp
442  *     pscript.ntf
443  *     pscript5.dll
444  */
445
446  have_drivers = 0;
447  language     = cupsLangDefault();
448
449  snprintf(file, sizeof(file), "%s/drivers/pscript5.dll", cg->cups_datadir);
450  if (!access(file, 0))
451  {
452    have_drivers |= 1;
453
454   /*
455    * Windows 2k driver is installed; do the smbclient commands needed
456    * to copy the Win2k drivers over...
457    */
458
459    snprintf(address, sizeof(address), "//%s/print$", samba_server);
460
461    snprintf(subcmd, sizeof(subcmd),
462             "mkdir W32X86;"
463	     "put %s W32X86/%s.ppd;"
464	     "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
465	     "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
466	     "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
467	     "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
468	     ppd, dest, cg->cups_datadir, cg->cups_datadir,
469	     cg->cups_datadir, cg->cups_datadir);
470
471    if ((status = do_samba_command("smbclient", address, subcmd,
472                                   authfile, logfile)) != 0)
473    {
474      snprintf(message, sizeof(message),
475               _cupsLangString(language,
476	                       _("Unable to copy Windows 2000 printer "
477	                         "driver files (%d).")), status);
478
479      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
480
481      if (logfile)
482	_cupsLangPuts(logfile, message);
483
484      unlink(authfile);
485
486      return (0);
487    }
488
489   /*
490    * See if we also have the CUPS driver files; if so, use them!
491    */
492
493    snprintf(file, sizeof(file), "%s/drivers/cupsps6.dll", cg->cups_datadir);
494    if (!access(file, 0))
495    {
496     /*
497      * Copy the CUPS driver files over...
498      */
499
500      snprintf(subcmd, sizeof(subcmd),
501               "put %s/drivers/cups6.ini W32X86/cups6.ini;"
502               "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
503	       "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
504	       cg->cups_datadir, cg->cups_datadir, cg->cups_datadir);
505
506      if ((status = do_samba_command("smbclient", address, subcmd,
507                                     authfile, logfile)) != 0)
508      {
509	snprintf(message, sizeof(message),
510        	 _cupsLangString(language,
511	                         _("Unable to copy CUPS printer driver "
512				   "files (%d).")), status);
513
514	_cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
515
516	if (logfile)
517	  _cupsLangPuts(logfile, message);
518
519        unlink(authfile);
520
521	return (0);
522      }
523
524     /*
525      * Do the rpcclient command needed for the CUPS drivers...
526      */
527
528      snprintf(subcmd, sizeof(subcmd),
529               "adddriver \"Windows NT x86\" \"%s:"
530	       "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
531	       "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
532	       "cups6.ini,cupsps6.dll,cupsui6.dll\"",
533	       dest, dest, dest);
534    }
535    else
536    {
537     /*
538      * Don't have the CUPS drivers, so just use the standard Windows
539      * drivers...
540      */
541
542      snprintf(subcmd, sizeof(subcmd),
543               "adddriver \"Windows NT x86\" \"%s:"
544	       "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
545	       "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
546	       dest, dest, dest);
547    }
548
549    if ((status = do_samba_command("rpcclient", samba_server, subcmd,
550                                   authfile, logfile)) != 0)
551    {
552      snprintf(message, sizeof(message),
553               _cupsLangString(language,
554                	       _("Unable to install Windows 2000 printer "
555		        	 "driver files (%d).")), status);
556
557      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
558
559      if (logfile)
560	_cupsLangPuts(logfile, message);
561
562      unlink(authfile);
563
564      return (0);
565    }
566  }
567
568 /*
569  * See if we have the Win9x PS driver...
570  */
571
572  snprintf(file, sizeof(file), "%s/drivers/ADOBEPS4.DRV", cg->cups_datadir);
573  if (!access(file, 0))
574  {
575    have_drivers |= 2;
576
577   /*
578    * Do the smbclient commands needed for the Adobe Win9x drivers...
579    */
580
581    snprintf(address, sizeof(address), "//%s/print$", samba_server);
582
583    snprintf(subcmd, sizeof(subcmd),
584             "mkdir WIN40;"
585	     "put %s WIN40/%s.PPD;"
586	     "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
587	     "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
588	     "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
589	     "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
590	     "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
591	     ppd, dest, cg->cups_datadir, cg->cups_datadir,
592	     cg->cups_datadir, cg->cups_datadir, cg->cups_datadir);
593
594    if ((status = do_samba_command("smbclient", address, subcmd,
595                                   authfile, logfile)) != 0)
596    {
597      snprintf(message, sizeof(message),
598               _cupsLangString(language,
599                	       _("Unable to copy Windows 9x printer "
600		        	 "driver files (%d).")), status);
601
602      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
603
604      if (logfile)
605	_cupsLangPuts(logfile, message);
606
607      unlink(authfile);
608
609      return (0);
610    }
611
612   /*
613    * Do the rpcclient commands needed for the Adobe Win9x drivers...
614    */
615
616    snprintf(subcmd, sizeof(subcmd),
617	     "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
618	     "ADOBEPS4.HLP:PSMON.DLL:RAW:"
619	     "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
620	     "ICONLIB.DLL\"",
621	     dest, dest, dest);
622
623    if ((status = do_samba_command("rpcclient", samba_server, subcmd,
624                                   authfile, logfile)) != 0)
625    {
626      snprintf(message, sizeof(message),
627               _cupsLangString(language,
628                	       _("Unable to install Windows 9x printer "
629		        	 "driver files (%d).")), status);
630
631      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
632
633      if (logfile)
634	_cupsLangPuts(logfile, message);
635
636      unlink(authfile);
637
638      return (0);
639    }
640  }
641
642 /*
643  * See if we have the 64-bit Windows PS driver...
644  *
645  * Files:
646  *
647  *     x64/ps5ui.dll
648  *     x64/pscript.hlp
649  *     x64/pscript.ntf
650  *     x64/pscript5.dll
651  */
652
653  snprintf(file, sizeof(file), "%s/drivers/x64/pscript5.dll", cg->cups_datadir);
654  if (!access(file, 0))
655  {
656    have_drivers |= 4;
657
658   /*
659    * 64-bit Windows driver is installed; do the smbclient commands needed
660    * to copy the Win64 drivers over...
661    */
662
663    snprintf(address, sizeof(address), "//%s/print$", samba_server);
664
665    snprintf(subcmd, sizeof(subcmd),
666             "mkdir x64;"
667	     "put %s x64/%s.ppd;"
668	     "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
669	     "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
670	     "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
671	     "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
672	     ppd, dest, cg->cups_datadir, cg->cups_datadir,
673	     cg->cups_datadir, cg->cups_datadir);
674
675    if ((status = do_samba_command("smbclient", address, subcmd,
676                                   authfile, logfile)) != 0)
677    {
678      snprintf(message, sizeof(message),
679               _cupsLangString(language,
680	                       _("Unable to copy 64-bit Windows printer "
681	                         "driver files (%d).")), status);
682
683      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
684
685      if (logfile)
686	_cupsLangPuts(logfile, message);
687
688      unlink(authfile);
689
690      return (0);
691    }
692
693   /*
694    * See if we also have the CUPS driver files; if so, use them!
695    */
696
697    snprintf(file, sizeof(file), "%s/drivers/x64/cupsps6.dll", cg->cups_datadir);
698    if (!access(file, 0))
699    {
700     /*
701      * Copy the CUPS driver files over...
702      */
703
704      snprintf(subcmd, sizeof(subcmd),
705               "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
706               "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
707	       "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
708	       cg->cups_datadir, cg->cups_datadir, cg->cups_datadir);
709
710      if ((status = do_samba_command("smbclient", address, subcmd,
711                                     authfile, logfile)) != 0)
712      {
713	snprintf(message, sizeof(message),
714        	 _cupsLangString(language,
715	                         _("Unable to copy 64-bit CUPS printer driver "
716				   "files (%d).")), status);
717
718	_cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
719
720	if (logfile)
721	  _cupsLangPuts(logfile, message);
722
723        unlink(authfile);
724
725	return (0);
726      }
727
728     /*
729      * Do the rpcclient command needed for the CUPS drivers...
730      */
731
732      snprintf(subcmd, sizeof(subcmd),
733               "adddriver \"Windows x64\" \"%s:"
734	       "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
735	       "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
736	       "cups6.ini,cupsps6.dll,cupsui6.dll\"",
737	       dest, dest, dest);
738    }
739    else
740    {
741     /*
742      * Don't have the CUPS drivers, so just use the standard Windows
743      * drivers...
744      */
745
746      snprintf(subcmd, sizeof(subcmd),
747               "adddriver \"Windows x64\" \"%s:"
748	       "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
749	       "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
750	       dest, dest, dest);
751    }
752
753    if ((status = do_samba_command("rpcclient", samba_server, subcmd,
754                                   authfile, logfile)) != 0)
755    {
756      snprintf(message, sizeof(message),
757               _cupsLangString(language,
758                	       _("Unable to install Windows 2000 printer "
759		        	 "driver files (%d).")), status);
760
761      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
762
763      if (logfile)
764	_cupsLangPuts(logfile, message);
765
766      unlink(authfile);
767
768      return (0);
769    }
770  }
771
772  if (logfile && !(have_drivers & 1))
773  {
774    if (!have_drivers)
775      strlcpy(message,
776              _cupsLangString(language,
777                	      _("No Windows printer drivers are installed.")),
778              sizeof(message));
779    else
780      strlcpy(message,
781              _cupsLangString(language,
782                	      _("Warning, no Windows 2000 printer drivers "
783				"are installed.")),
784              sizeof(message));
785
786    _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND, message, 0);
787    _cupsLangPuts(logfile, message);
788  }
789
790  if (have_drivers == 0)
791  {
792    _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND, message, 0);
793
794    unlink(authfile);
795
796    return (0);
797  }
798
799 /*
800  * Finally, associate the drivers we just added with the queue...
801  */
802
803  snprintf(subcmd, sizeof(subcmd), "setdriver %s %s", dest, dest);
804
805  if ((status = do_samba_command("rpcclient", samba_server, subcmd,
806                                 authfile, logfile)) != 0)
807  {
808    snprintf(message, sizeof(message),
809             _cupsLangString(language,
810        		     _("Unable to set Windows printer driver (%d).")),
811        		     status);
812
813    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
814
815    if (logfile)
816      _cupsLangPuts(logfile, message);
817
818    unlink(authfile);
819
820    return (0);
821  }
822
823  unlink(authfile);
824
825  return (1);
826}
827
828
829/*
830 * 'cupsAdminGetServerSettings()' - Get settings from the server.
831 *
832 * The returned settings should be freed with cupsFreeOptions() when
833 * you are done with them.
834 *
835 * @since CUPS 1.3/OS X 10.5@
836 */
837
838int					/* O - 1 on success, 0 on failure */
839cupsAdminGetServerSettings(
840    http_t        *http,		/* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
841    int           *num_settings,	/* O - Number of settings */
842    cups_option_t **settings)		/* O - Settings */
843{
844  int		i;			/* Looping var */
845  cups_file_t	*cupsd;			/* cupsd.conf file */
846  char		cupsdconf[1024];	/* cupsd.conf filename */
847  int		remote;			/* Remote cupsd.conf file? */
848  http_status_t	status;			/* Status of getting cupsd.conf */
849  char		line[1024],		/* Line from cupsd.conf file */
850		*value;			/* Value on line */
851  cups_option_t	*setting;		/* Current setting */
852  _cups_globals_t *cg = _cupsGlobals();	/* Global data */
853
854
855 /*
856  * Range check input...
857  */
858
859  if (!http)
860  {
861   /*
862    * See if we are connected to the same server...
863    */
864
865    if (cg->http)
866    {
867     /*
868      * Compare the connection hostname, port, and encryption settings to
869      * the cached defaults; these were initialized the first time we
870      * connected...
871      */
872
873      if (strcmp(cg->http->hostname, cg->server) ||
874          cg->ipp_port != httpAddrPort(cg->http->hostaddr) ||
875	  (cg->http->encryption != cg->encryption &&
876	   cg->http->encryption == HTTP_ENCRYPTION_NEVER))
877      {
878       /*
879	* Need to close the current connection because something has changed...
880	*/
881
882	httpClose(cg->http);
883	cg->http = NULL;
884      }
885    }
886
887   /*
888    * (Re)connect as needed...
889    */
890
891    if (!cg->http)
892    {
893      if ((cg->http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC,
894                                   cupsEncryption(), 1, 0, NULL)) == NULL)
895      {
896	if (errno)
897	  _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, NULL, 0);
898	else
899	  _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE,
900			_("Unable to connect to host."), 1);
901
902	if (num_settings)
903	  *num_settings = 0;
904
905	if (settings)
906	  *settings = NULL;
907
908	return (0);
909      }
910    }
911
912    http = cg->http;
913  }
914
915  if (!http || !num_settings || !settings)
916  {
917    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
918
919    if (num_settings)
920      *num_settings = 0;
921
922    if (settings)
923      *settings = NULL;
924
925    return (0);
926  }
927
928  *num_settings = 0;
929  *settings     = NULL;
930
931 /*
932  * Get the cupsd.conf file...
933  */
934
935  if ((status = get_cupsd_conf(http, cg, cg->cupsd_update, cupsdconf,
936                               sizeof(cupsdconf), &remote)) == HTTP_STATUS_OK)
937  {
938    if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
939    {
940      char	message[1024];		/* Message string */
941
942
943      snprintf(message, sizeof(message),
944               _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
945               cupsdconf, strerror(errno));
946      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
947    }
948  }
949  else
950    cupsd = NULL;
951
952  if (cupsd)
953  {
954   /*
955    * Read the file, keeping track of what settings are enabled...
956    */
957
958    int		remote_access = 0,	/* Remote access allowed? */
959		remote_admin = 0,	/* Remote administration allowed? */
960		remote_any = 0,		/* Remote access from anywhere allowed? */
961		browsing = 1,		/* Browsing enabled? */
962		cancel_policy = 1,	/* Cancel-job policy set? */
963		debug_logging = 0;	/* LogLevel debug set? */
964    int		linenum = 0,		/* Line number in file */
965		in_location = 0,	/* In a location section? */
966		in_policy = 0,		/* In a policy section? */
967		in_cancel_job = 0,	/* In a cancel-job section? */
968		in_admin_location = 0;	/* In the /admin location? */
969
970
971    invalidate_cupsd_cache(cg);
972
973    cg->cupsd_update = time(NULL);
974    httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname));
975
976    while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
977    {
978      if (!value && strncmp(line, "</", 2))
979        value = line + strlen(line);
980
981      if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) && value)
982      {
983	char	*port;			/* Pointer to port number, if any */
984
985
986	if ((port = strrchr(value, ':')) != NULL)
987	  *port = '\0';
988	else if (isdigit(*value & 255))
989	{
990	 /*
991	  * Listen on a port number implies remote access...
992	  */
993
994	  remote_access = 1;
995	  continue;
996	}
997
998	if (_cups_strcasecmp(value, "localhost") && strcmp(value, "127.0.0.1")
999#ifdef AF_LOCAL
1000            && *value != '/'
1001#endif /* AF_LOCAL */
1002#ifdef AF_INET6
1003            && strcmp(value, "[::1]")
1004#endif /* AF_INET6 */
1005	    )
1006	  remote_access = 1;
1007      }
1008      else if (!_cups_strcasecmp(line, "Browsing"))
1009      {
1010	browsing = !_cups_strcasecmp(value, "yes") ||
1011	           !_cups_strcasecmp(value, "on") ||
1012	           !_cups_strcasecmp(value, "true");
1013      }
1014      else if (!_cups_strcasecmp(line, "LogLevel"))
1015      {
1016	debug_logging = !_cups_strncasecmp(value, "debug", 5);
1017      }
1018      else if (!_cups_strcasecmp(line, "<Policy") &&
1019               !_cups_strcasecmp(value, "default"))
1020      {
1021	in_policy = 1;
1022      }
1023      else if (!_cups_strcasecmp(line, "</Policy>"))
1024      {
1025	in_policy = 0;
1026      }
1027      else if (!_cups_strcasecmp(line, "<Limit") && in_policy && value)
1028      {
1029       /*
1030	* See if the policy limit is for the Cancel-Job operation...
1031	*/
1032
1033	char	*valptr;		/* Pointer into value */
1034
1035
1036	while (*value)
1037	{
1038	  for (valptr = value; *valptr && !_cups_isspace(*valptr); valptr ++);
1039
1040	  if (*valptr)
1041	    *valptr++ = '\0';
1042
1043          if (!_cups_strcasecmp(value, "cancel-job") ||
1044              !_cups_strcasecmp(value, "all"))
1045	  {
1046	    in_cancel_job = 1;
1047	    break;
1048	  }
1049
1050          for (value = valptr; _cups_isspace(*value); value ++);
1051	}
1052      }
1053      else if (!_cups_strcasecmp(line, "</Limit>"))
1054      {
1055	in_cancel_job = 0;
1056      }
1057      else if (!_cups_strcasecmp(line, "Require") && in_cancel_job)
1058      {
1059	cancel_policy = 0;
1060      }
1061      else if (!_cups_strcasecmp(line, "<Location") && value)
1062      {
1063        in_admin_location = !_cups_strcasecmp(value, "/admin");
1064	in_location       = 1;
1065      }
1066      else if (!_cups_strcasecmp(line, "</Location>"))
1067      {
1068	in_admin_location = 0;
1069	in_location       = 0;
1070      }
1071      else if (!_cups_strcasecmp(line, "Allow") && value &&
1072               _cups_strcasecmp(value, "localhost") &&
1073               _cups_strcasecmp(value, "127.0.0.1")
1074#ifdef AF_LOCAL
1075	       && *value != '/'
1076#endif /* AF_LOCAL */
1077#ifdef AF_INET6
1078	       && strcmp(value, "::1")
1079#endif /* AF_INET6 */
1080	       )
1081      {
1082        if (in_admin_location)
1083	  remote_admin = 1;
1084        else if (!_cups_strcasecmp(value, "all"))
1085	  remote_any = 1;
1086      }
1087      else if (line[0] != '<' && !in_location && !in_policy &&
1088	       _cups_strcasecmp(line, "Allow") &&
1089               _cups_strcasecmp(line, "AuthType") &&
1090	       _cups_strcasecmp(line, "Deny") &&
1091	       _cups_strcasecmp(line, "Order") &&
1092	       _cups_strcasecmp(line, "Require") &&
1093	       _cups_strcasecmp(line, "Satisfy"))
1094        cg->cupsd_num_settings = cupsAddOption(line, value,
1095	                                       cg->cupsd_num_settings,
1096					       &(cg->cupsd_settings));
1097    }
1098
1099    cupsFileClose(cupsd);
1100
1101    cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
1102                                           debug_logging ? "1" : "0",
1103					   cg->cupsd_num_settings,
1104					   &(cg->cupsd_settings));
1105
1106    cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
1107                                           (remote_access && remote_admin) ?
1108					       "1" : "0",
1109					   cg->cupsd_num_settings,
1110					   &(cg->cupsd_settings));
1111
1112    cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
1113                                           remote_any ? "1" : "0",
1114					   cg->cupsd_num_settings,
1115					   &(cg->cupsd_settings));
1116
1117    cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
1118                                           (remote_access && browsing) ? "1" :
1119                                                                         "0",
1120					   cg->cupsd_num_settings,
1121					   &(cg->cupsd_settings));
1122
1123    cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
1124                                           cancel_policy ? "1" : "0",
1125					   cg->cupsd_num_settings,
1126					   &(cg->cupsd_settings));
1127  }
1128  else if (status != HTTP_STATUS_NOT_MODIFIED)
1129    invalidate_cupsd_cache(cg);
1130
1131 /*
1132  * Remove any temporary files and copy the settings array...
1133  */
1134
1135  if (remote)
1136    unlink(cupsdconf);
1137
1138  for (i = cg->cupsd_num_settings, setting = cg->cupsd_settings;
1139       i > 0;
1140       i --, setting ++)
1141    *num_settings = cupsAddOption(setting->name, setting->value,
1142                                  *num_settings, settings);
1143
1144  return (cg->cupsd_num_settings > 0);
1145}
1146
1147
1148/*
1149 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1150 *
1151 * @since CUPS 1.3/OS X 10.5@
1152 */
1153
1154int					/* O - 1 on success, 0 on failure */
1155cupsAdminSetServerSettings(
1156    http_t        *http,		/* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1157    int           num_settings,		/* I - Number of settings */
1158    cups_option_t *settings)		/* I - Settings */
1159{
1160  int		i;			/* Looping var */
1161  http_status_t status;			/* GET/PUT status */
1162  const char	*server_port_env;	/* SERVER_PORT env var */
1163  int		server_port;		/* IPP port for server */
1164  cups_file_t	*cupsd;			/* cupsd.conf file */
1165  char		cupsdconf[1024];	/* cupsd.conf filename */
1166  int		remote;			/* Remote cupsd.conf file? */
1167  char		tempfile[1024];		/* Temporary new cupsd.conf */
1168  cups_file_t	*temp;			/* Temporary file */
1169  char		line[1024],		/* Line from cupsd.conf file */
1170		*value;			/* Value on line */
1171  int		linenum,		/* Line number in file */
1172		in_location,		/* In a location section? */
1173		in_policy,		/* In a policy section? */
1174		in_default_policy,	/* In the default policy section? */
1175		in_cancel_job,		/* In a cancel-job section? */
1176		in_admin_location,	/* In the /admin location? */
1177		in_conf_location,	/* In the /admin/conf location? */
1178		in_root_location;	/* In the / location? */
1179  const char	*val;			/* Setting value */
1180  int		share_printers,		/* Share local printers */
1181		remote_admin,		/* Remote administration allowed? */
1182		remote_any,		/* Remote access from anywhere? */
1183		user_cancel_any,	/* Cancel-job policy set? */
1184		debug_logging;		/* LogLevel debug set? */
1185  int		wrote_port_listen,	/* Wrote the port/listen lines? */
1186		wrote_browsing,		/* Wrote the browsing lines? */
1187		wrote_policy,		/* Wrote the policy? */
1188		wrote_loglevel,		/* Wrote the LogLevel line? */
1189		wrote_admin_location,	/* Wrote the /admin location? */
1190		wrote_conf_location,	/* Wrote the /admin/conf location? */
1191		wrote_root_location;	/* Wrote the / location? */
1192  int		indent;			/* Indentation */
1193  int		cupsd_num_settings;	/* New number of settings */
1194  int		old_share_printers,	/* Share local printers */
1195		old_remote_admin,	/* Remote administration allowed? */
1196		old_user_cancel_any,	/* Cancel-job policy set? */
1197		old_debug_logging;	/* LogLevel debug set? */
1198  cups_option_t	*cupsd_settings,	/* New settings */
1199		*setting;		/* Current setting */
1200  _cups_globals_t *cg = _cupsGlobals();	/* Global data */
1201
1202
1203 /*
1204  * Range check input...
1205  */
1206
1207  if (!http)
1208    http = _cupsConnect();
1209
1210  if (!http || !num_settings || !settings)
1211  {
1212    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
1213
1214    return (0);
1215  }
1216
1217 /*
1218  * Get the cupsd.conf file...
1219  */
1220
1221  if (get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf),
1222                     &remote) == HTTP_STATUS_OK)
1223  {
1224    if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
1225    {
1226      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
1227      return (0);
1228    }
1229  }
1230  else
1231    return (0);
1232
1233 /*
1234  * Get current settings...
1235  */
1236
1237  if (!cupsAdminGetServerSettings(http, &cupsd_num_settings,
1238				  &cupsd_settings))
1239    return (0);
1240
1241  if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, cupsd_num_settings,
1242                           cupsd_settings)) != NULL)
1243    old_debug_logging = atoi(val);
1244  else
1245    old_debug_logging = 0;
1246
1247  DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
1248                old_debug_logging));
1249
1250  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, cupsd_num_settings,
1251                           cupsd_settings)) != NULL)
1252    old_remote_admin = atoi(val);
1253  else
1254    old_remote_admin = 0;
1255
1256  DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
1257                old_remote_admin));
1258
1259  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, cupsd_num_settings,
1260                           cupsd_settings)) != NULL)
1261    remote_any = atoi(val);
1262  else
1263    remote_any = 0;
1264
1265  DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
1266                remote_any));
1267
1268  if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, cupsd_num_settings,
1269                           cupsd_settings)) != NULL)
1270    old_share_printers = atoi(val);
1271  else
1272    old_share_printers = 0;
1273
1274  DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
1275                old_share_printers));
1276
1277  if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, cupsd_num_settings,
1278                           cupsd_settings)) != NULL)
1279    old_user_cancel_any = atoi(val);
1280  else
1281    old_user_cancel_any = 0;
1282
1283  DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
1284                old_user_cancel_any));
1285
1286  cupsFreeOptions(cupsd_num_settings, cupsd_settings);
1287
1288 /*
1289  * Get basic settings...
1290  */
1291
1292  if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings,
1293                           settings)) != NULL)
1294  {
1295    debug_logging = atoi(val);
1296
1297    if (debug_logging == old_debug_logging)
1298    {
1299     /*
1300      * No change to this setting...
1301      */
1302
1303      debug_logging = -1;
1304    }
1305  }
1306  else
1307    debug_logging = -1;
1308
1309  DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
1310                debug_logging));
1311
1312  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, num_settings,
1313                           settings)) != NULL)
1314    remote_any = atoi(val);
1315
1316  DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d",
1317                remote_any));
1318
1319  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings,
1320                           settings)) != NULL)
1321  {
1322    remote_admin = atoi(val);
1323
1324    if (remote_admin == old_remote_admin)
1325    {
1326     /*
1327      * No change to this setting...
1328      */
1329
1330      remote_admin = -1;
1331    }
1332  }
1333  else
1334    remote_admin = -1;
1335
1336  DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
1337                remote_admin));
1338
1339  if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings,
1340                           settings)) != NULL)
1341  {
1342    share_printers = atoi(val);
1343
1344    if (share_printers == old_share_printers)
1345    {
1346     /*
1347      * No change to this setting...
1348      */
1349
1350      share_printers = -1;
1351    }
1352  }
1353  else
1354    share_printers = -1;
1355
1356  DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
1357                share_printers));
1358
1359  if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings,
1360                           settings)) != NULL)
1361  {
1362    user_cancel_any = atoi(val);
1363
1364    if (user_cancel_any == old_user_cancel_any)
1365    {
1366     /*
1367      * No change to this setting...
1368      */
1369
1370      user_cancel_any = -1;
1371    }
1372  }
1373  else
1374    user_cancel_any = -1;
1375
1376  DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
1377                user_cancel_any));
1378
1379 /*
1380  * Create a temporary file for the new cupsd.conf file...
1381  */
1382
1383  if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
1384  {
1385    cupsFileClose(cupsd);
1386
1387    if (remote)
1388      unlink(cupsdconf);
1389
1390    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
1391    return (0);
1392  }
1393
1394 /*
1395  * Copy the old file to the new, making changes along the way...
1396  */
1397
1398  cupsd_num_settings   = 0;
1399  in_admin_location    = 0;
1400  in_cancel_job        = 0;
1401  in_conf_location     = 0;
1402  in_default_policy    = 0;
1403  in_location          = 0;
1404  in_policy            = 0;
1405  in_root_location     = 0;
1406  linenum              = 0;
1407  wrote_admin_location = 0;
1408  wrote_browsing       = 0;
1409  wrote_conf_location  = 0;
1410  wrote_loglevel       = 0;
1411  wrote_policy         = 0;
1412  wrote_port_listen    = 0;
1413  wrote_root_location  = 0;
1414  indent               = 0;
1415
1416  if ((server_port_env = getenv("SERVER_PORT")) != NULL)
1417  {
1418    if ((server_port = atoi(server_port_env)) <= 0)
1419      server_port = ippPort();
1420  }
1421  else
1422    server_port = ippPort();
1423
1424  if (server_port <= 0)
1425    server_port = IPP_PORT;
1426
1427  while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
1428  {
1429    if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) &&
1430        (remote_admin >= 0 || remote_any > 0 || share_printers >= 0))
1431    {
1432      if (!wrote_port_listen)
1433      {
1434        wrote_port_listen = 1;
1435
1436	if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
1437	{
1438	  cupsFilePuts(temp, "# Allow remote access\n");
1439	  cupsFilePrintf(temp, "Port %d\n", server_port);
1440	}
1441	else
1442	{
1443	  cupsFilePuts(temp, "# Only listen for connections from the local "
1444	                     "machine.\n");
1445	  cupsFilePrintf(temp, "Listen localhost:%d\n", server_port);
1446	}
1447
1448#ifdef CUPS_DEFAULT_DOMAINSOCKET
1449        if ((!value || strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)) &&
1450	    !access(CUPS_DEFAULT_DOMAINSOCKET, 0))
1451          cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
1452#endif /* CUPS_DEFAULT_DOMAINSOCKET */
1453      }
1454      else if (value && value[0] == '/'
1455#ifdef CUPS_DEFAULT_DOMAINSOCKET
1456               && strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)
1457#endif /* CUPS_DEFAULT_DOMAINSOCKET */
1458               )
1459        cupsFilePrintf(temp, "Listen %s\n", value);
1460    }
1461    else if ((!_cups_strcasecmp(line, "Browsing") ||
1462              !_cups_strcasecmp(line, "BrowseLocalProtocols")) &&
1463	     share_printers >= 0)
1464    {
1465      if (!wrote_browsing)
1466      {
1467	int new_share_printers = (share_printers > 0 ||
1468				  (share_printers == -1 &&
1469				   old_share_printers > 0));
1470
1471        wrote_browsing = 1;
1472
1473        if (new_share_printers)
1474	{
1475	  const char *localp = cupsGetOption("BrowseLocalProtocols",
1476					     num_settings, settings);
1477
1478          if (!localp || !localp[0])
1479	    localp = cupsGetOption("BrowseLocalProtocols", cupsd_num_settings,
1480	                           cupsd_settings);
1481
1482	  cupsFilePuts(temp, "# Share local printers on the local network.\n");
1483	  cupsFilePuts(temp, "Browsing On\n");
1484
1485	  if (!localp)
1486	    localp = CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS;
1487
1488	  cupsFilePrintf(temp, "BrowseLocalProtocols %s\n", localp);
1489
1490	  cupsd_num_settings = cupsAddOption("BrowseLocalProtocols", localp,
1491					     cupsd_num_settings,
1492					     &cupsd_settings);
1493        }
1494	else
1495	{
1496	  cupsFilePuts(temp, "# Disable printer sharing.\n");
1497	  cupsFilePuts(temp, "Browsing Off\n");
1498	}
1499      }
1500    }
1501    else if (!_cups_strcasecmp(line, "LogLevel") && debug_logging >= 0)
1502    {
1503      wrote_loglevel = 1;
1504
1505      if (debug_logging)
1506      {
1507        cupsFilePuts(temp,
1508	             "# Show troubleshooting information in error_log.\n");
1509	cupsFilePuts(temp, "LogLevel debug\n");
1510      }
1511      else
1512      {
1513        cupsFilePuts(temp, "# Show general information in error_log.\n");
1514	cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n");
1515      }
1516    }
1517    else if (!_cups_strcasecmp(line, "<Policy"))
1518    {
1519      in_default_policy = !_cups_strcasecmp(value, "default");
1520      in_policy         = 1;
1521
1522      cupsFilePrintf(temp, "%s %s>\n", line, value);
1523      indent += 2;
1524    }
1525    else if (!_cups_strcasecmp(line, "</Policy>"))
1526    {
1527      indent -= 2;
1528      if (!wrote_policy && in_default_policy)
1529      {
1530	wrote_policy = 1;
1531
1532        if (!user_cancel_any)
1533	  cupsFilePuts(temp, "  # Only the owner or an administrator can "
1534	                     "cancel a job...\n"
1535	                     "  <Limit Cancel-Job>\n"
1536	                     "    Order deny,allow\n"
1537			     "    Require user @OWNER "
1538			     CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
1539			     "  </Limit>\n");
1540      }
1541
1542      in_policy         = 0;
1543      in_default_policy = 0;
1544
1545      cupsFilePuts(temp, "</Policy>\n");
1546    }
1547    else if (!_cups_strcasecmp(line, "<Location"))
1548    {
1549      in_location = 1;
1550      indent += 2;
1551      if (!strcmp(value, "/admin"))
1552	in_admin_location = 1;
1553      if (!strcmp(value, "/admin/conf"))
1554	in_conf_location = 1;
1555      else if (!strcmp(value, "/"))
1556	in_root_location = 1;
1557
1558      cupsFilePrintf(temp, "%s %s>\n", line, value);
1559    }
1560    else if (!_cups_strcasecmp(line, "</Location>"))
1561    {
1562      in_location = 0;
1563      indent -= 2;
1564      if (in_admin_location && remote_admin >= 0)
1565      {
1566	wrote_admin_location = 1;
1567
1568	if (remote_admin)
1569          cupsFilePuts(temp, "  # Allow remote administration...\n");
1570	else if (remote_admin == 0)
1571          cupsFilePuts(temp, "  # Restrict access to the admin pages...\n");
1572
1573        cupsFilePuts(temp, "  Order allow,deny\n");
1574
1575	if (remote_admin)
1576	  cupsFilePrintf(temp, "  Allow %s\n",
1577	                 remote_any > 0 ? "all" : "@LOCAL");
1578      }
1579      else if (in_conf_location && remote_admin >= 0)
1580      {
1581	wrote_conf_location = 1;
1582
1583	if (remote_admin)
1584          cupsFilePuts(temp, "  # Allow remote access to the configuration "
1585	                     "files...\n");
1586	else
1587          cupsFilePuts(temp, "  # Restrict access to the configuration "
1588	                     "files...\n");
1589
1590        cupsFilePuts(temp, "  Order allow,deny\n");
1591
1592	if (remote_admin)
1593	  cupsFilePrintf(temp, "  Allow %s\n",
1594	                 remote_any > 0 ? "all" : "@LOCAL");
1595      }
1596      else if (in_root_location &&
1597               (remote_admin >= 0 || remote_any > 0 || share_printers >= 0))
1598      {
1599	wrote_root_location = 1;
1600
1601	if (remote_admin > 0 && share_printers > 0)
1602          cupsFilePuts(temp, "  # Allow shared printing and remote "
1603	                     "administration...\n");
1604	else if (remote_admin > 0)
1605          cupsFilePuts(temp, "  # Allow remote administration...\n");
1606	else if (share_printers > 0)
1607          cupsFilePuts(temp, "  # Allow shared printing...\n");
1608	else if (remote_any > 0)
1609          cupsFilePuts(temp, "  # Allow remote access...\n");
1610	else
1611          cupsFilePuts(temp, "  # Restrict access to the server...\n");
1612
1613        cupsFilePuts(temp, "  Order allow,deny\n");
1614
1615	if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
1616	  cupsFilePrintf(temp, "  Allow %s\n",
1617	                 remote_any > 0 ? "all" : "@LOCAL");
1618      }
1619
1620      in_admin_location = 0;
1621      in_conf_location  = 0;
1622      in_root_location  = 0;
1623
1624      cupsFilePuts(temp, "</Location>\n");
1625    }
1626    else if (!_cups_strcasecmp(line, "<Limit"))
1627    {
1628      if (in_default_policy)
1629      {
1630       /*
1631	* See if the policy limit is for the Cancel-Job operation...
1632	*/
1633
1634	char	*valptr;		/* Pointer into value */
1635
1636
1637	if (!_cups_strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
1638	{
1639	 /*
1640	  * Don't write anything for this limit section...
1641	  */
1642
1643	  in_cancel_job = 2;
1644	}
1645	else
1646	{
1647	  cupsFilePrintf(temp, "%*s%s", indent, "", line);
1648
1649	  while (*value)
1650	  {
1651	    for (valptr = value; *valptr && !_cups_isspace(*valptr); valptr ++);
1652
1653	    if (*valptr)
1654	      *valptr++ = '\0';
1655
1656	    if (!_cups_strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
1657	    {
1658	     /*
1659	      * Write everything except for this definition...
1660	      */
1661
1662	      in_cancel_job = 1;
1663	    }
1664	    else
1665	      cupsFilePrintf(temp, " %s", value);
1666
1667	    for (value = valptr; _cups_isspace(*value); value ++);
1668	  }
1669
1670	  cupsFilePuts(temp, ">\n");
1671	}
1672      }
1673      else
1674        cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value);
1675
1676      indent += 2;
1677    }
1678    else if (!_cups_strcasecmp(line, "</Limit>") && in_cancel_job)
1679    {
1680      indent -= 2;
1681
1682      if (in_cancel_job == 1)
1683	cupsFilePuts(temp, "  </Limit>\n");
1684
1685      wrote_policy = 1;
1686
1687      if (!user_cancel_any)
1688	cupsFilePuts(temp, "  # Only the owner or an administrator can cancel "
1689			   "a job...\n"
1690			   "  <Limit Cancel-Job>\n"
1691			   "    Order deny,allow\n"
1692			   "    Require user @OWNER "
1693			   CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
1694			   "  </Limit>\n");
1695
1696      in_cancel_job = 0;
1697    }
1698    else if ((((in_admin_location || in_conf_location || in_root_location) &&
1699               (remote_admin >= 0 || remote_any > 0)) ||
1700              (in_root_location && share_printers >= 0)) &&
1701             (!_cups_strcasecmp(line, "Allow") || !_cups_strcasecmp(line, "Deny") ||
1702	      !_cups_strcasecmp(line, "Order")))
1703      continue;
1704    else if (in_cancel_job == 2)
1705      continue;
1706    else if (line[0] == '<')
1707    {
1708      if (value)
1709      {
1710        cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value);
1711	indent += 2;
1712      }
1713      else
1714      {
1715	if (line[1] == '/')
1716	  indent -= 2;
1717
1718	cupsFilePrintf(temp, "%*s%s\n", indent, "", line);
1719      }
1720    }
1721    else if (!in_policy && !in_location &&
1722             (val = cupsGetOption(line, num_settings, settings)) != NULL)
1723    {
1724     /*
1725      * Replace this directive's value with the new one...
1726      */
1727
1728      cupsd_num_settings = cupsAddOption(line, val, cupsd_num_settings,
1729                                         &cupsd_settings);
1730
1731     /*
1732      * Write the new value in its place, without indentation since we
1733      * only support setting root directives, not in sections...
1734      */
1735
1736      cupsFilePrintf(temp, "%s %s\n", line, val);
1737    }
1738    else if (value)
1739    {
1740      if (!in_policy && !in_location)
1741      {
1742       /*
1743        * Record the non-policy, non-location directives that we find
1744	* in the server settings, since we cache this info and record it
1745	* in cupsAdminGetServerSettings()...
1746	*/
1747
1748	cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings,
1749                                           &cupsd_settings);
1750      }
1751
1752      cupsFilePrintf(temp, "%*s%s %s\n", indent, "", line, value);
1753    }
1754    else
1755      cupsFilePrintf(temp, "%*s%s\n", indent, "", line);
1756  }
1757
1758 /*
1759  * Write any missing info...
1760  */
1761
1762  if (!wrote_browsing && share_printers >= 0)
1763  {
1764    if (share_printers > 0)
1765    {
1766      cupsFilePuts(temp, "# Share local printers on the local network.\n");
1767      cupsFilePuts(temp, "Browsing On\n");
1768    }
1769    else
1770    {
1771      cupsFilePuts(temp, "# Disable printer sharing and shared printers.\n");
1772      cupsFilePuts(temp, "Browsing Off\n");
1773    }
1774  }
1775
1776  if (!wrote_loglevel && debug_logging >= 0)
1777  {
1778    if (debug_logging)
1779    {
1780      cupsFilePuts(temp, "# Show troubleshooting information in error_log.\n");
1781      cupsFilePuts(temp, "LogLevel debug\n");
1782    }
1783    else
1784    {
1785      cupsFilePuts(temp, "# Show general information in error_log.\n");
1786      cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n");
1787    }
1788  }
1789
1790  if (!wrote_port_listen &&
1791      (remote_admin >= 0 || remote_any > 0 || share_printers >= 0))
1792  {
1793    if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
1794    {
1795      cupsFilePuts(temp, "# Allow remote access\n");
1796      cupsFilePrintf(temp, "Port %d\n", ippPort());
1797    }
1798    else
1799    {
1800      cupsFilePuts(temp,
1801                   "# Only listen for connections from the local machine.\n");
1802      cupsFilePrintf(temp, "Listen localhost:%d\n", ippPort());
1803    }
1804
1805#ifdef CUPS_DEFAULT_DOMAINSOCKET
1806    if (!access(CUPS_DEFAULT_DOMAINSOCKET, 0))
1807      cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
1808#endif /* CUPS_DEFAULT_DOMAINSOCKET */
1809  }
1810
1811  if (!wrote_root_location &&
1812      (remote_admin >= 0 || remote_any > 0 || share_printers >= 0))
1813  {
1814    if (remote_admin > 0 && share_printers > 0)
1815      cupsFilePuts(temp,
1816                   "# Allow shared printing and remote administration...\n");
1817    else if (remote_admin > 0)
1818      cupsFilePuts(temp, "# Allow remote administration...\n");
1819    else if (share_printers > 0)
1820      cupsFilePuts(temp, "# Allow shared printing...\n");
1821    else if (remote_any > 0)
1822      cupsFilePuts(temp, "# Allow remote access...\n");
1823    else
1824      cupsFilePuts(temp, "# Restrict access to the server...\n");
1825
1826    cupsFilePuts(temp, "<Location />\n"
1827                       "  Order allow,deny\n");
1828
1829    if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
1830      cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
1831
1832    cupsFilePuts(temp, "</Location>\n");
1833  }
1834
1835  if (!wrote_admin_location && remote_admin >= 0)
1836  {
1837    if (remote_admin)
1838      cupsFilePuts(temp, "# Allow remote administration...\n");
1839    else
1840      cupsFilePuts(temp, "# Restrict access to the admin pages...\n");
1841
1842    cupsFilePuts(temp, "<Location /admin>\n"
1843                       "  Order allow,deny\n");
1844
1845    if (remote_admin)
1846      cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
1847
1848    cupsFilePuts(temp, "</Location>\n");
1849  }
1850
1851  if (!wrote_conf_location && remote_admin >= 0)
1852  {
1853    if (remote_admin)
1854      cupsFilePuts(temp,
1855                   "# Allow remote access to the configuration files...\n");
1856    else
1857      cupsFilePuts(temp, "# Restrict access to the configuration files...\n");
1858
1859    cupsFilePuts(temp, "<Location /admin/conf>\n"
1860                       "  AuthType Default\n"
1861                       "  Require user @SYSTEM\n"
1862                       "  Order allow,deny\n");
1863
1864    if (remote_admin)
1865      cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
1866
1867    cupsFilePuts(temp, "</Location>\n");
1868  }
1869
1870  if (!wrote_policy && user_cancel_any >= 0)
1871  {
1872    cupsFilePuts(temp, "<Policy default>\n"
1873                       "  # Job-related operations must be done by the owner "
1874		       "or an administrator...\n"
1875                       "  <Limit Send-Document Send-URI Hold-Job Release-Job "
1876		       "Restart-Job Purge-Jobs Set-Job-Attributes "
1877		       "Create-Job-Subscription Renew-Subscription "
1878		       "Cancel-Subscription Get-Notifications Reprocess-Job "
1879		       "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1880		       "CUPS-Move-Job>\n"
1881                       "    Require user @OWNER @SYSTEM\n"
1882                       "    Order deny,allow\n"
1883                       "  </Limit>\n"
1884                       "  # All administration operations require an "
1885		       "administrator to authenticate...\n"
1886		       "  <Limit Pause-Printer Resume-Printer "
1887                       "Set-Printer-Attributes Enable-Printer "
1888		       "Disable-Printer Pause-Printer-After-Current-Job "
1889		       "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1890		       "Activate-Printer Restart-Printer Shutdown-Printer "
1891		       "Startup-Printer Promote-Job Schedule-Job-After "
1892		       "CUPS-Add-Printer CUPS-Delete-Printer "
1893		       "CUPS-Add-Class CUPS-Delete-Class "
1894		       "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1895		       "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1896                       "    AuthType Default\n"
1897		       "    Require user @SYSTEM\n"
1898                       "    Order deny,allow\n"
1899                       "</Limit>\n");
1900
1901    if (!user_cancel_any)
1902      cupsFilePuts(temp, "  # Only the owner or an administrator can cancel "
1903                         "a job...\n"
1904	                 "  <Limit Cancel-Job>\n"
1905	                 "    Order deny,allow\n"
1906	                 "    Require user @OWNER "
1907			 CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
1908			 "  </Limit>\n");
1909
1910    cupsFilePuts(temp, "  <Limit All>\n"
1911                       "  Order deny,allow\n"
1912                       "  </Limit>\n"
1913		       "</Policy>\n");
1914  }
1915
1916  for (i = num_settings, setting = settings; i > 0; i --, setting ++)
1917    if (setting->name[0] != '_' &&
1918        _cups_strcasecmp(setting->name, "Listen") &&
1919	_cups_strcasecmp(setting->name, "Port") &&
1920        !cupsGetOption(setting->name, cupsd_num_settings, cupsd_settings))
1921    {
1922     /*
1923      * Add this directive to the list of directives we have written...
1924      */
1925
1926      cupsd_num_settings = cupsAddOption(setting->name, setting->value,
1927                                         cupsd_num_settings, &cupsd_settings);
1928
1929     /*
1930      * Write the new value, without indentation since we only support
1931      * setting root directives, not in sections...
1932      */
1933
1934      cupsFilePrintf(temp, "%s %s\n", setting->name, setting->value);
1935    }
1936
1937  cupsFileClose(cupsd);
1938  cupsFileClose(temp);
1939
1940 /*
1941  * Upload the configuration file to the server...
1942  */
1943
1944  status = cupsPutFile(http, "/admin/conf/cupsd.conf", tempfile);
1945
1946  if (status == HTTP_STATUS_CREATED)
1947  {
1948   /*
1949    * Updated OK, add the basic settings...
1950    */
1951
1952    if (debug_logging >= 0)
1953      cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
1954                                	 debug_logging ? "1" : "0",
1955					 cupsd_num_settings, &cupsd_settings);
1956    else
1957      cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
1958                                	 old_debug_logging ? "1" : "0",
1959					 cupsd_num_settings, &cupsd_settings);
1960
1961    if (remote_admin >= 0)
1962      cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
1963                                	 remote_admin ? "1" : "0",
1964					 cupsd_num_settings, &cupsd_settings);
1965    else
1966      cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
1967                                	 old_remote_admin ? "1" : "0",
1968					 cupsd_num_settings, &cupsd_settings);
1969
1970    cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
1971                                       remote_any ? "1" : "0",
1972				       cupsd_num_settings, &cupsd_settings);
1973
1974    if (share_printers >= 0)
1975      cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
1976                                	 share_printers ? "1" : "0",
1977					 cupsd_num_settings, &cupsd_settings);
1978    else
1979      cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
1980                                	 old_share_printers ? "1" : "0",
1981					 cupsd_num_settings, &cupsd_settings);
1982
1983    if (user_cancel_any >= 0)
1984      cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
1985                                	 user_cancel_any ? "1" : "0",
1986					 cupsd_num_settings, &cupsd_settings);
1987    else
1988      cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
1989                                	 old_user_cancel_any ? "1" : "0",
1990					 cupsd_num_settings, &cupsd_settings);
1991
1992   /*
1993    * Save the new values...
1994    */
1995
1996    invalidate_cupsd_cache(cg);
1997
1998    cg->cupsd_num_settings = cupsd_num_settings;
1999    cg->cupsd_settings     = cupsd_settings;
2000    cg->cupsd_update       = time(NULL);
2001
2002    httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname));
2003  }
2004  else
2005    cupsFreeOptions(cupsd_num_settings, cupsd_settings);
2006
2007 /*
2008  * Remote our temp files and return...
2009  */
2010
2011  if (remote)
2012    unlink(cupsdconf);
2013
2014  unlink(tempfile);
2015
2016  return (status == HTTP_STATUS_CREATED);
2017}
2018
2019
2020/*
2021 * 'do_samba_command()' - Do a SAMBA command.
2022 */
2023
2024static int				/* O - Status of command */
2025do_samba_command(const char *command,	/* I - Command to run */
2026                 const char *address,	/* I - Address for command */
2027                 const char *subcmd,	/* I - Sub-command */
2028		 const char *authfile,	/* I - Samba authentication file */
2029		 FILE *logfile)		/* I - Optional log file */
2030{
2031#ifdef WIN32
2032  return (1);				/* Always fail on Windows... */
2033
2034#else
2035  int		status;			/* Status of command */
2036  int		pid;			/* Process ID of child */
2037
2038
2039  if (logfile)
2040    _cupsLangPrintf(logfile,
2041                    _("Running command: %s %s -N -A %s -c \'%s\'"),
2042        	    command, address, authfile, subcmd);
2043
2044  if ((pid = fork()) == 0)
2045  {
2046   /*
2047    * Child goes here, redirect stdin/out/err and execute the command...
2048    */
2049
2050    int fd = open("/dev/null", O_RDONLY);
2051
2052    if (fd > 0)
2053    {
2054      dup2(fd, 0);
2055      close(fd);
2056    }
2057
2058    if (logfile)
2059      dup2(fileno(logfile), 1);
2060    else if ((fd = open("/dev/null", O_WRONLY)) > 1)
2061    {
2062      dup2(fd, 1);
2063      close(fd);
2064    }
2065
2066    dup2(1, 2);
2067
2068    execlp(command, command, address, "-N", "-A", authfile, "-c", subcmd,
2069           (char *)0);
2070    exit(errno);
2071  }
2072  else if (pid < 0)
2073  {
2074    status = -1;
2075
2076    if (logfile)
2077      _cupsLangPrintf(logfile, _("Unable to run \"%s\": %s"),
2078                      command, strerror(errno));
2079  }
2080  else
2081  {
2082   /*
2083    * Wait for the process to complete...
2084    */
2085
2086    while (wait(&status) != pid);
2087  }
2088
2089  if (logfile)
2090    _cupsLangPuts(logfile, "");
2091
2092  DEBUG_printf(("9do_samba_command: status=%d", status));
2093
2094  if (WIFEXITED(status))
2095    return (WEXITSTATUS(status));
2096  else
2097    return (-WTERMSIG(status));
2098#endif /* WIN32 */
2099}
2100
2101
2102/*
2103 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2104 */
2105
2106static http_status_t			/* O - Status of request */
2107get_cupsd_conf(
2108    http_t          *http,		/* I - Connection to server */
2109    _cups_globals_t *cg,		/* I - Global data */
2110    time_t          last_update,	/* I - Last update time for file */
2111    char            *name,		/* I - Filename buffer */
2112    size_t          namesize,		/* I - Size of filename buffer */
2113    int             *remote)		/* O - Remote file? */
2114{
2115  int		fd;			/* Temporary file descriptor */
2116#ifndef WIN32
2117  struct stat	info;			/* cupsd.conf file information */
2118#endif /* WIN32 */
2119  http_status_t	status;			/* Status of getting cupsd.conf */
2120  char		host[HTTP_MAX_HOST];	/* Hostname for connection */
2121
2122
2123 /*
2124  * See if we already have the data we need...
2125  */
2126
2127  httpGetHostname(http, host, sizeof(host));
2128
2129  if (_cups_strcasecmp(cg->cupsd_hostname, host))
2130    invalidate_cupsd_cache(cg);
2131
2132  snprintf(name, namesize, "%s/cupsd.conf", cg->cups_serverroot);
2133  *remote = 0;
2134
2135#ifndef WIN32
2136  if (!_cups_strcasecmp(host, "localhost") && !access(name, R_OK))
2137  {
2138   /*
2139    * Read the local file rather than using HTTP...
2140    */
2141
2142    if (stat(name, &info))
2143    {
2144      char	message[1024];		/* Message string */
2145
2146
2147      snprintf(message, sizeof(message),
2148               _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2149               name, strerror(errno));
2150      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
2151
2152      *name = '\0';
2153
2154      return (HTTP_STATUS_SERVER_ERROR);
2155    }
2156    else if (last_update && info.st_mtime <= last_update)
2157      status = HTTP_STATUS_NOT_MODIFIED;
2158    else
2159      status = HTTP_STATUS_OK;
2160  }
2161  else
2162#endif /* !WIN32 */
2163  {
2164   /*
2165    * Read cupsd.conf via a HTTP GET request...
2166    */
2167
2168    if ((fd = cupsTempFd(name, (int)namesize)) < 0)
2169    {
2170      *name = '\0';
2171
2172      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
2173
2174      invalidate_cupsd_cache(cg);
2175
2176      return (HTTP_STATUS_SERVER_ERROR);
2177    }
2178
2179    *remote = 1;
2180
2181    httpClearFields(http);
2182
2183    if (last_update)
2184      httpSetField(http, HTTP_FIELD_IF_MODIFIED_SINCE,
2185                   httpGetDateString(last_update));
2186
2187    status = cupsGetFd(http, "/admin/conf/cupsd.conf", fd);
2188
2189    close(fd);
2190
2191    if (status != HTTP_STATUS_OK)
2192    {
2193      unlink(name);
2194      *name = '\0';
2195    }
2196  }
2197
2198  return (status);
2199}
2200
2201
2202/*
2203 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2204 */
2205
2206static void
2207invalidate_cupsd_cache(
2208    _cups_globals_t *cg)		/* I - Global data */
2209{
2210  cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
2211
2212  cg->cupsd_hostname[0]  = '\0';
2213  cg->cupsd_update       = 0;
2214  cg->cupsd_num_settings = 0;
2215  cg->cupsd_settings     = NULL;
2216}
2217
2218
2219/*
2220 * 'write_option()' - Write a CUPS option to a PPD file.
2221 */
2222
2223static void
2224write_option(cups_file_t     *dstfp,	/* I - PPD file */
2225             int             order,	/* I - Order dependency */
2226             const char      *name,	/* I - Option name */
2227	     const char      *text,	/* I - Option text */
2228             const char      *attrname,	/* I - Attribute name */
2229             ipp_attribute_t *suppattr,	/* I - IPP -supported attribute */
2230	     ipp_attribute_t *defattr,	/* I - IPP -default attribute */
2231	     int             defval,	/* I - Default value number */
2232	     int             valcount)	/* I - Number of values */
2233{
2234  int	i;				/* Looping var */
2235
2236
2237  cupsFilePrintf(dstfp, "*JCLOpenUI *%s/%s: PickOne\n"
2238                        "*OrderDependency: %d JCLSetup *%s\n",
2239                 name, text, order, name);
2240
2241  if (defattr->value_tag == IPP_TAG_INTEGER)
2242  {
2243   /*
2244    * Do numeric options with a range or list...
2245    */
2246
2247    cupsFilePrintf(dstfp, "*Default%s: %d\n", name,
2248                   defattr->values[defval].integer);
2249
2250    if (suppattr->value_tag == IPP_TAG_RANGE)
2251    {
2252     /*
2253      * List each number in the range...
2254      */
2255
2256      for (i = suppattr->values[0].range.lower;
2257           i <= suppattr->values[0].range.upper;
2258	   i ++)
2259      {
2260        cupsFilePrintf(dstfp, "*%s %d: \"", name, i);
2261
2262        if (valcount == 1)
2263	  cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2264	                 attrname, i);
2265        else if (defval == 0)
2266	  cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\"\n", attrname, i);
2267        else if (defval < (valcount - 1))
2268	  cupsFilePrintf(dstfp, ",%d\"\n", i);
2269        else
2270	  cupsFilePrintf(dstfp, ",%d\n\"\n*End\n", i);
2271      }
2272    }
2273    else
2274    {
2275     /*
2276      * List explicit numbers...
2277      */
2278
2279      for (i = 0; i < suppattr->num_values; i ++)
2280      {
2281        cupsFilePrintf(dstfp, "*%s %d: \"", name, suppattr->values[i].integer);
2282
2283        if (valcount == 1)
2284	  cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname,
2285	          suppattr->values[i].integer);
2286        else if (defval == 0)
2287	  cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\"\n", attrname,
2288	          suppattr->values[i].integer);
2289        else if (defval < (valcount - 1))
2290	  cupsFilePrintf(dstfp, ",%d\"\n", suppattr->values[i].integer);
2291        else
2292	  cupsFilePrintf(dstfp, ",%d\n\"\n*End\n", suppattr->values[i].integer);
2293      }
2294    }
2295  }
2296  else
2297  {
2298   /*
2299    * Do text options with a list...
2300    */
2301
2302    cupsFilePrintf(dstfp, "*Default%s: %s\n", name,
2303                   defattr->values[defval].string.text);
2304
2305    for (i = 0; i < suppattr->num_values; i ++)
2306    {
2307      cupsFilePrintf(dstfp, "*%s %s: \"", name,
2308                     suppattr->values[i].string.text);
2309
2310      if (valcount == 1)
2311	cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname,
2312	        suppattr->values[i].string.text);
2313      else if (defval == 0)
2314	cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%s\"\n", attrname,
2315	        suppattr->values[i].string.text);
2316      else if (defval < (valcount - 1))
2317	cupsFilePrintf(dstfp, ",%s\"\n", suppattr->values[i].string.text);
2318      else
2319	cupsFilePrintf(dstfp, ",%s\n\"\n*End\n",
2320	               suppattr->values[i].string.text);
2321    }
2322  }
2323
2324  cupsFilePrintf(dstfp, "*JCLCloseUI: *%s\n\n", name);
2325}
2326
2327
2328/*
2329 * End of "$Id: adminutil.c 11645 2014-02-27 16:35:53Z msweet $".
2330 */
2331