summaryrefslogtreecommitdiff
path: root/systemv/cupsaddsmb.c
blob: 18b1a2f6b66a334ddf07ab24461da8638881b375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * "$Id: cupsaddsmb.c 7033 2007-10-19 02:11:28Z mike $"
 *
 *   "cupsaddsmb" command for the Common UNIX Printing System (CUPS).
 *
 *   Copyright 2007 by Apple Inc.
 *   Copyright 2001-2006 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Apple Inc. and are protected by Federal copyright
 *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
 *   which should have been included with this file.  If this file is
 *   file is missing or damaged, see the license at "http://www.cups.org/".
 *
 * Contents:
 *
 *   main()        - Export printers on the command-line.
 *   export_dest() - Export a destination to SAMBA.
 *   usage()       - Show program usage and exit...
 */

/*
 * Include necessary headers...
 */

#include <stdio.h>
#include <stdlib.h>
#include <cups/string.h>
#include <cups/adminutil.h>
#include <cups/i18n.h>
#include <cups/debug.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>


/*
 * Local globals...
 */

int		Verbosity = 0;
const char	*SAMBAUser,
		*SAMBAPassword,
		*SAMBAServer;


/*
 * Local functions...
 */

int	export_dest(http_t *http, const char *dest);
void	usage(void);


/*
 * 'main()' - Export printers on the command-line.
 */

int					/* O - Exit status */
main(int  argc,				/* I - Number of command-line arguments */
     char *argv[])			/* I - Command-line arguments */
{
  int		i, j;			/* Looping vars */
  int		status;			/* Status from export_dest() */
  int		export_all;		/* Export all printers? */
  http_t	*http;			/* Connection to server */
  int		num_dests;		/* Number of printers */
  cups_dest_t	*dests;			/* Printers */


  _cupsSetLocale(argv);

 /*
  * Parse command-line arguments...
  */

  export_all    = 0;
  http          = NULL;
  SAMBAUser     = cupsUser();
  SAMBAPassword = NULL;
  SAMBAServer   = NULL;

  for (i = 1; i < argc; i ++)
    if (!strcmp(argv[i], "-E"))
    {
#ifdef HAVE_SSL
      cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
#else
      _cupsLangPrintf(stderr,
	              _("%s: Sorry, no encryption support compiled in!\n"),
	              argv[0]);
#endif /* HAVE_SSL */
    }
    else if (!strcmp(argv[i], "-H"))
    {
      i ++;
      if (i >= argc)
        usage();

      SAMBAServer = argv[i];
    }
    else if (!strcmp(argv[i], "-U"))
    {
      char	*sep;			/* Separator for password */


      i ++;
      if (i >= argc)
        usage();

      SAMBAUser = argv[i];

      if ((sep = strchr(argv[i], '%')) != NULL)
      {
       /*
        * Nul-terminate the username at the first % and point the
	* password at the rest...
	*/

        *sep++ = '\0';

        SAMBAPassword = sep;
      }
    }
    else if (!strcmp(argv[i], "-a"))
      export_all = 1;
    else if (!strcmp(argv[i], "-h"))
    {
      i ++;
      if (i >= argc)
        usage();

      cupsSetServer(argv[i]);
    }
    else if (!strcmp(argv[i], "-v"))
      Verbosity = 1;
    else if (argv[i][0] != '-')
    {
      if (!http)
      {
       /*
	* Connect to the server...
	*/

	if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
                                       cupsEncryption())) == NULL)
	{
	  _cupsLangPrintf(stderr, _("%s: Unable to connect to server\n"), argv[0]);
	  exit(1);
	}
      }

      if (SAMBAServer == NULL)
      {
	SAMBAServer = cupsServer();

	if (SAMBAServer[0] == '/')	/* Use localhost instead of domain socket */
	  SAMBAServer = "localhost";
      }

      if ((status = export_dest(http, argv[i])) != 0)
	return (status);
    }
    else
      usage();

 /*
  * Connect to the server...
  */

  if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
                                 cupsEncryption())) == NULL)
  {
    _cupsLangPrintf(stderr, _("%s: Unable to connect to server\n"), argv[0]);
    exit(1);
  }

 /*
  * See if the user specified "-a"...
  */

  if (export_all)
  {
   /*
    * Export all printers...
    */

    if (SAMBAServer == NULL)
    {
      SAMBAServer = cupsServer();

      if (SAMBAServer[0] == '/')	/* Use localhost instead of domain socket */
	SAMBAServer = "localhost";
    }

    num_dests = cupsGetDests2(http, &dests);

    for (j = 0, status = 0; j < num_dests; j ++)
      if (!dests[j].instance)
      {
        if ((status = export_dest(http, dests[j].name)) != 0)
	  break;
      }

    cupsFreeDests(num_dests, dests);

    if (status)
      return (status);
  }

  return (0);
}


/*
 * 'export_dest()' - Export a destination to SAMBA.
 */

int					/* O - 0 on success, non-zero on error */
export_dest(http_t     *http,		/* I - Connection to server */
            const char *dest)		/* I - Destination to export */
{
  int		status;			/* Status of export */
  char		ppdfile[1024],		/* PPD file for printer drivers */
		prompt[1024];		/* Password prompt */


 /*
  * Get the Windows PPD file for the printer...
  */

  if (!cupsAdminCreateWindowsPPD(http, dest, ppdfile, sizeof(ppdfile)))
  {
    _cupsLangPrintf(stderr,
                    _("cupsaddsmb: No PPD file for printer \"%s\" - "
		      "%s\n"),
        	    dest, cupsLastErrorString());
    return (1);
  }

 /*
  * Try to export it...
  */

  for (status = 0; !status;)
  {
   /*
    * Get the password, as needed...
    */

    if (!SAMBAPassword)
    {
      snprintf(prompt, sizeof(prompt),
               _cupsLangString(cupsLangDefault(),
	                       _("Password for %s required to access %s via "
			         "SAMBA: ")),
	       SAMBAUser, SAMBAServer);

      if ((SAMBAPassword = cupsGetPassword(prompt)) == NULL)
	break;
    }

    status = cupsAdminExportSamba(dest, ppdfile, SAMBAServer,
                                  SAMBAUser, SAMBAPassword,
				  Verbosity ? stderr : NULL);

    if (!status && cupsLastError() == IPP_NOT_FOUND)
      break;
  }

  unlink(ppdfile);

  return (!status);
}


/*
 * 'usage()' - Show program usage and exit...
 */

void
usage(void)
{
  _cupsLangPuts(stdout,
                _("Usage: cupsaddsmb [options] printer1 ... printerN\n"
		  "       cupsaddsmb [options] -a\n"
		  "\n"
		  "Options:\n"
		  "  -E               Encrypt the connection to the server\n"
		  "  -H samba-server  Use the named SAMBA server\n"
		  "  -U samba-user    Authenticate using the named SAMBA user\n"
		  "  -a               Export all printers\n"
		  "  -h cups-server   Use the named CUPS server\n"
		  "  -v               Be verbose (show commands)\n"));
  exit(1);
}


/*
 * End of "$Id: cupsaddsmb.c 7033 2007-10-19 02:11:28Z mike $".
 */