spoolss: Use correct values for secdesc and devmode pointers
[vlendec/samba-autobuild/.git] / source3 / rpc_client / init_spoolss.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Guenther Deschner                  2009.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "../librpc/gen_ndr/ndr_spoolss.h"
22 #include "rpc_client/init_spoolss.h"
23 #include "../libcli/security/security.h"
24 #include "secrets.h"
25 #include "passdb/machine_sid.h"
26
27 /*******************************************************************
28 ********************************************************************/
29
30 bool init_systemtime(struct spoolss_Time *r,
31                      struct tm *unixtime)
32 {
33         if (!r || !unixtime) {
34                 return false;
35         }
36
37         r->year         = unixtime->tm_year+1900;
38         r->month        = unixtime->tm_mon+1;
39         r->day_of_week  = unixtime->tm_wday;
40         r->day          = unixtime->tm_mday;
41         r->hour         = unixtime->tm_hour;
42         r->minute       = unixtime->tm_min;
43         r->second       = unixtime->tm_sec;
44         r->millisecond  = 0;
45
46         return true;
47 }
48
49 time_t spoolss_Time_to_time_t(const struct spoolss_Time *r)
50 {
51         struct tm unixtime;
52
53         unixtime.tm_year        = r->year - 1900;
54         unixtime.tm_mon         = r->month - 1;
55         unixtime.tm_wday        = r->day_of_week;
56         unixtime.tm_mday        = r->day;
57         unixtime.tm_hour        = r->hour;
58         unixtime.tm_min         = r->minute;
59         unixtime.tm_sec         = r->second;
60
61         return mktime(&unixtime);
62 }
63
64 /*******************************************************************
65  ********************************************************************/
66
67 bool spoolss_timestr_to_NTTIME(const char *str,
68                                NTTIME *data)
69 {
70         struct tm tm;
71         time_t t;
72
73         if (strequal(str, "01/01/1601")) {
74                 *data = 0;
75                 return true;
76         }
77
78         ZERO_STRUCT(tm);
79
80         if (sscanf(str, "%d/%d/%d",
81                    &tm.tm_mon, &tm.tm_mday, &tm.tm_year) != 3) {
82                 return false;
83         }
84         tm.tm_mon -= 1;
85         tm.tm_year -= 1900;
86         tm.tm_isdst = -1;
87
88         t = mktime(&tm);
89         unix_to_nt_time(data, t);
90
91         return true;
92 }
93
94 /*******************************************************************
95  ********************************************************************/
96
97 bool spoolss_driver_version_to_qword(const char *str,
98                                      uint64_t *data)
99 {
100         unsigned int v1, v2, v3, v4;
101
102         if (sscanf(str, "%u.%u.%u.%u", &v1, &v2, &v3, &v4) != 4) {
103                 return false;
104         }
105
106         *data = ((uint64_t)(v1 & 0xFFFF) << 48) +
107                 ((uint64_t)(v2 & 0xFFFF) << 32) +
108                 ((uint64_t)(v3 & 0xFFFF) << 16) +
109                 (uint64_t)(v4 & 0xFFFF);
110
111         return true;
112 }
113
114 /*******************************************************************
115  ********************************************************************/
116
117 WERROR pull_spoolss_PrinterData(TALLOC_CTX *mem_ctx,
118                                 const DATA_BLOB *blob,
119                                 union spoolss_PrinterData *data,
120                                 enum winreg_Type type)
121 {
122         enum ndr_err_code ndr_err;
123         ndr_err = ndr_pull_union_blob(blob, mem_ctx, data, type,
124                         (ndr_pull_flags_fn_t)ndr_pull_spoolss_PrinterData);
125         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
126                 return WERR_GEN_FAILURE;
127         }
128         return WERR_OK;
129 }
130
131 /*******************************************************************
132  ********************************************************************/
133
134 WERROR push_spoolss_PrinterData(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
135                                 enum winreg_Type type,
136                                 union spoolss_PrinterData *data)
137 {
138         enum ndr_err_code ndr_err;
139         ndr_err = ndr_push_union_blob(blob, mem_ctx, data, type,
140                         (ndr_push_flags_fn_t)ndr_push_spoolss_PrinterData);
141         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
142                 return WERR_GEN_FAILURE;
143         }
144         return WERR_OK;
145 }
146
147 /*******************************************************************
148  ********************************************************************/
149
150 void spoolss_printerinfo2_to_setprinterinfo2(const struct spoolss_PrinterInfo2 *i,
151                                              struct spoolss_SetPrinterInfo2 *s)
152 {
153         s->servername           = i->servername;
154         s->printername          = i->printername;
155         s->sharename            = i->sharename;
156         s->portname             = i->portname;
157         s->drivername           = i->drivername;
158         s->comment              = i->comment;
159         s->location             = i->location;
160         s->devmode_ptr          = 0;
161         s->sepfile              = i->sepfile;
162         s->printprocessor       = i->printprocessor;
163         s->datatype             = i->datatype;
164         s->parameters           = i->parameters;
165         s->secdesc_ptr          = 0;
166         s->attributes           = i->attributes;
167         s->priority             = i->priority;
168         s->defaultpriority      = i->defaultpriority;
169         s->starttime            = i->starttime;
170         s->untiltime            = i->untiltime;
171         s->status               = i->status;
172         s->cjobs                = i->cjobs;
173         s->averageppm           = i->averageppm;
174 }
175
176 /****************************************************************************
177 ****************************************************************************/
178
179 bool driver_info_ctr_to_info8(struct spoolss_AddDriverInfoCtr *r,
180                               struct spoolss_DriverInfo8 *_info8)
181 {
182         struct spoolss_DriverInfo8 info8;
183
184         ZERO_STRUCT(info8);
185
186         switch (r->level) {
187         case 3:
188                 info8.version           = r->info.info3->version;
189                 info8.driver_name       = r->info.info3->driver_name;
190                 info8.architecture      = r->info.info3->architecture;
191                 info8.driver_path       = r->info.info3->driver_path;
192                 info8.data_file         = r->info.info3->data_file;
193                 info8.config_file       = r->info.info3->config_file;
194                 info8.help_file         = r->info.info3->help_file;
195                 info8.monitor_name      = r->info.info3->monitor_name;
196                 info8.default_datatype  = r->info.info3->default_datatype;
197                 if (r->info.info3->dependent_files && r->info.info3->dependent_files->string) {
198                         info8.dependent_files   = r->info.info3->dependent_files->string;
199                 }
200                 break;
201         case 6:
202                 info8.version           = r->info.info6->version;
203                 info8.driver_name       = r->info.info6->driver_name;
204                 info8.architecture      = r->info.info6->architecture;
205                 info8.driver_path       = r->info.info6->driver_path;
206                 info8.data_file         = r->info.info6->data_file;
207                 info8.config_file       = r->info.info6->config_file;
208                 info8.help_file         = r->info.info6->help_file;
209                 info8.monitor_name      = r->info.info6->monitor_name;
210                 info8.default_datatype  = r->info.info6->default_datatype;
211                 if (r->info.info6->dependent_files && r->info.info6->dependent_files->string) {
212                         info8.dependent_files   = r->info.info6->dependent_files->string;
213                 }
214                 info8.driver_date       = r->info.info6->driver_date;
215                 info8.driver_version    = r->info.info6->driver_version;
216                 info8.manufacturer_name = r->info.info6->manufacturer_name;
217                 info8.manufacturer_url  = r->info.info6->manufacturer_url;
218                 info8.hardware_id       = r->info.info6->hardware_id;
219                 info8.provider          = r->info.info6->provider;
220                 break;
221         case 8:
222                 info8.version           = r->info.info8->version;
223                 info8.driver_name       = r->info.info8->driver_name;
224                 info8.architecture      = r->info.info8->architecture;
225                 info8.driver_path       = r->info.info8->driver_path;
226                 info8.data_file         = r->info.info8->data_file;
227                 info8.config_file       = r->info.info8->config_file;
228                 info8.help_file         = r->info.info8->help_file;
229                 info8.monitor_name      = r->info.info8->monitor_name;
230                 info8.default_datatype  = r->info.info8->default_datatype;
231                 if (r->info.info8->dependent_files && r->info.info8->dependent_files->string) {
232                         info8.dependent_files   = r->info.info8->dependent_files->string;
233                 }
234                 if (r->info.info8->previous_names && r->info.info8->previous_names->string) {
235                         info8.previous_names    = r->info.info8->previous_names->string;
236                 }
237                 info8.driver_date       = r->info.info8->driver_date;
238                 info8.driver_version    = r->info.info8->driver_version;
239                 info8.manufacturer_name = r->info.info8->manufacturer_name;
240                 info8.manufacturer_url  = r->info.info8->manufacturer_url;
241                 info8.hardware_id       = r->info.info8->hardware_id;
242                 info8.provider          = r->info.info8->provider;
243                 info8.print_processor   = r->info.info8->print_processor;
244                 info8.vendor_setup      = r->info.info8->vendor_setup;
245                 if (r->info.info8->color_profiles && r->info.info8->color_profiles->string) {
246                         info8.color_profiles = r->info.info8->color_profiles->string;
247                 }
248                 info8.inf_path          = r->info.info8->inf_path;
249                 info8.printer_driver_attributes = r->info.info8->printer_driver_attributes;
250                 if (r->info.info8->core_driver_dependencies && r->info.info8->core_driver_dependencies->string) {
251                         info8.core_driver_dependencies = r->info.info8->core_driver_dependencies->string;
252                 }
253                 info8.min_inbox_driver_ver_date = r->info.info8->min_inbox_driver_ver_date;
254                 info8.min_inbox_driver_ver_version = r->info.info8->min_inbox_driver_ver_version;
255                 break;
256         default:
257                 return false;
258         }
259
260         *_info8 = info8;
261
262         return true;
263 }
264
265 /****************************************************************************
266  Create and allocate a default devicemode.
267 ****************************************************************************/
268
269 WERROR spoolss_create_default_devmode(TALLOC_CTX *mem_ctx,
270                                       const char *devicename,
271                                       struct spoolss_DeviceMode **devmode)
272 {
273         struct spoolss_DeviceMode *dm;
274         char *dname;
275
276         dm = talloc_zero(mem_ctx, struct spoolss_DeviceMode);
277         if (dm == NULL) {
278                 return WERR_NOT_ENOUGH_MEMORY;
279         }
280
281         dname = talloc_asprintf(dm, "%s", devicename);
282         if (dname == NULL) {
283                 return WERR_NOT_ENOUGH_MEMORY;
284         }
285         if (strlen(dname) > MAXDEVICENAME) {
286                 dname[MAXDEVICENAME] = '\0';
287         }
288         dm->devicename = dname;
289
290         dm->formname = talloc_strdup(dm, "Letter");
291         if (dm->formname == NULL) {
292                 return WERR_NOT_ENOUGH_MEMORY;
293         }
294
295         dm->specversion          = DMSPEC_NT4_AND_ABOVE;
296         dm->driverversion        = 0x0400;
297         dm->size                 = 0x00DC;
298         dm->__driverextra_length = 0;
299         dm->fields               = DEVMODE_FORMNAME |
300                                    DEVMODE_TTOPTION |
301                                    DEVMODE_PRINTQUALITY |
302                                    DEVMODE_DEFAULTSOURCE |
303                                    DEVMODE_COPIES |
304                                    DEVMODE_SCALE |
305                                    DEVMODE_PAPERSIZE |
306                                    DEVMODE_ORIENTATION;
307         dm->orientation          = DMORIENT_PORTRAIT;
308         dm->papersize            = DMPAPER_LETTER;
309         dm->paperlength          = 0;
310         dm->paperwidth           = 0;
311         dm->scale                = 0x64;
312         dm->copies               = 1;
313         dm->defaultsource        = DMBIN_FORMSOURCE;
314         dm->printquality         = DMRES_HIGH;           /* 0x0258 */
315         dm->color                = DMRES_MONOCHROME;
316         dm->duplex               = DMDUP_SIMPLEX;
317         dm->yresolution          = 0;
318         dm->ttoption             = DMTT_SUBDEV;
319         dm->collate              = DMCOLLATE_FALSE;
320         dm->icmmethod            = 0;
321         dm->icmintent            = 0;
322         dm->mediatype            = 0;
323         dm->dithertype           = 0;
324
325         dm->logpixels            = 0;
326         dm->bitsperpel           = 0;
327         dm->pelswidth            = 0;
328         dm->pelsheight           = 0;
329         dm->displayflags         = 0;
330         dm->displayfrequency     = 0;
331         dm->reserved1            = 0;
332         dm->reserved2            = 0;
333         dm->panningwidth         = 0;
334         dm->panningheight        = 0;
335
336         dm->driverextra_data.data = NULL;
337         dm->driverextra_data.length = 0;
338
339         *devmode = dm;
340         return WERR_OK;
341 }
342
343 WERROR spoolss_create_default_secdesc(TALLOC_CTX *mem_ctx,
344                                       struct spoolss_security_descriptor **secdesc)
345 {
346         struct security_ace ace[7];     /* max number of ace entries */
347         int i = 0;
348         uint32_t sa;
349         struct security_acl *psa = NULL;
350         struct security_descriptor *psd = NULL;
351         struct dom_sid adm_sid;
352         size_t sd_size;
353
354         /* Create an ACE where Everyone is allowed to print */
355
356         sa = PRINTER_ACE_PRINT;
357         init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
358                      sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
359
360         /* Add the domain admins group if we are a DC */
361
362         if ( IS_DC ) {
363                 struct dom_sid domadmins_sid;
364
365                 sid_compose(&domadmins_sid, get_global_sam_sid(),
366                             DOMAIN_RID_ADMINS);
367
368                 sa = PRINTER_ACE_FULL_CONTROL;
369                 init_sec_ace(&ace[i++], &domadmins_sid,
370                         SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
371                         SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
372                 init_sec_ace(&ace[i++], &domadmins_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
373                         sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
374         }
375         else if (secrets_fetch_domain_sid(lp_workgroup(), &adm_sid)) {
376                 sid_append_rid(&adm_sid, DOMAIN_RID_ADMINISTRATOR);
377
378                 sa = PRINTER_ACE_FULL_CONTROL;
379                 init_sec_ace(&ace[i++], &adm_sid,
380                         SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
381                         SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
382                 init_sec_ace(&ace[i++], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
383                         sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
384         }
385
386         /* add BUILTIN\Administrators as FULL CONTROL */
387
388         sa = PRINTER_ACE_FULL_CONTROL;
389         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
390                 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
391                 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
392         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
393                 SEC_ACE_TYPE_ACCESS_ALLOWED,
394                 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
395
396         /* add BUILTIN\Print Operators as FULL CONTROL */
397
398         sa = PRINTER_ACE_FULL_CONTROL;
399         init_sec_ace(&ace[i++], &global_sid_Builtin_Print_Operators,
400                 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
401                 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
402         init_sec_ace(&ace[i++], &global_sid_Builtin_Print_Operators,
403                 SEC_ACE_TYPE_ACCESS_ALLOWED,
404                 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
405
406         /* Make the security descriptor owned by the BUILTIN\Administrators */
407
408         /* The ACL revision number in rpc_secdesc.h differs from the one
409            created by NT when setting ACE entries in printer
410            descriptors.  NT4 complains about the property being edited by a
411            NT5 machine. */
412
413         if ((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, i, ace)) != NULL) {
414                 psd = make_sec_desc(mem_ctx,
415                                     SD_REVISION,
416                                     SEC_DESC_SELF_RELATIVE,
417                                     &global_sid_Builtin_Administrators,
418                                     &global_sid_Builtin_Administrators,
419                                     NULL,
420                                     psa,
421                                     &sd_size);
422         }
423
424         if (psd == NULL) {
425                 DEBUG(0,("construct_default_printer_sd: Failed to make SEC_DESC.\n"));
426                 return WERR_NOT_ENOUGH_MEMORY;
427         }
428
429         DEBUG(4,("construct_default_printer_sdb: size = %u.\n",
430                  (unsigned int)sd_size));
431
432         *secdesc = psd;
433
434         return WERR_OK;
435 }