b46fc5875990335a77bd637793d9b505d7ce3014
[samba.git] / source3 / rpc_server / srv_spoolss_util.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *
4  *  SPOOLSS RPC Pipe server / winreg client routines
5  *
6  *  Copyright (c) 2010      Andreas Schneider <asn@samba.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "srv_spoolss_util.h"
24 #include "../librpc/gen_ndr/srv_winreg.h"
25 #include "../librpc/gen_ndr/cli_winreg.h"
26
27 #define TOP_LEVEL_PRINT_KEY "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print"
28 #define TOP_LEVEL_PRINT_PRINTERS_KEY TOP_LEVEL_PRINT_KEY "\\Printers"
29 #define TOP_LEVEL_CONTROL_KEY "SYSTEM\\CurrentControlSet\\Control\\Print"
30 #define TOP_LEVEL_CONTROL_FORMS_KEY TOP_LEVEL_CONTROL_KEY "\\Forms"
31
32 #define EMPTY_STRING ""
33 static const char *empty_string_array[1] = { NULL };
34 #define EMPTY_STRING_ARRAY empty_string_array
35
36 #define CHECK_ERROR(result) \
37         if (W_ERROR_IS_OK(result)) continue; \
38         if (W_ERROR_EQUAL(result, WERR_NOT_FOUND)) result = WERR_OK; \
39         if (!W_ERROR_IS_OK(result)) break
40
41 /*        FLAGS,                NAME,                              with,   height,   left, top, right, bottom */
42 static const struct spoolss_FormInfo1 builtin_forms1[] = {
43         { SPOOLSS_FORM_BUILTIN, "10x11",                          {0x3e030,0x44368}, {0x0,0x0,0x3e030,0x44368} },
44         { SPOOLSS_FORM_BUILTIN, "10x14",                          {0x3e030,0x56d10}, {0x0,0x0,0x3e030,0x56d10} },
45         { SPOOLSS_FORM_BUILTIN, "11x17",                          {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
46         { SPOOLSS_FORM_BUILTIN, "12x11",                          {0x4a724,0x443e1}, {0x0,0x0,0x4a724,0x443e1} },
47         { SPOOLSS_FORM_BUILTIN, "15x11",                          {0x5d048,0x44368}, {0x0,0x0,0x5d048,0x44368} },
48         { SPOOLSS_FORM_BUILTIN, "6 3/4 Envelope",                 {0x167ab,0x284ec}, {0x0,0x0,0x167ab,0x284ec} },
49         { SPOOLSS_FORM_BUILTIN, "9x11",                           {0x37cf8,0x44368}, {0x0,0x0,0x37cf8,0x44368} },
50         { SPOOLSS_FORM_BUILTIN, "A0",                             {0xcd528,0x122488},{0x0,0x0,0xcd528,0x122488} },
51         { SPOOLSS_FORM_BUILTIN, "A1",                             {0x91050,0xcd528}, {0x0,0x0,0x91050,0xcd528} },
52         { SPOOLSS_FORM_BUILTIN, "A2",                             {0x668a0,0x91050}, {0x0,0x0,0x668a0,0x91050} },
53         { SPOOLSS_FORM_BUILTIN, "A3 Extra Transverse",            {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
54         { SPOOLSS_FORM_BUILTIN, "A3 Extra",                       {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
55         { SPOOLSS_FORM_BUILTIN, "A3 Rotated",                     {0x668a0,0x48828}, {0x0,0x0,0x668a0,0x48828} },
56         { SPOOLSS_FORM_BUILTIN, "A3 Transverse",                  {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
57         { SPOOLSS_FORM_BUILTIN, "A3",                             {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
58         { SPOOLSS_FORM_BUILTIN, "A4 Extra",                       {0x397c2,0x4eb16}, {0x0,0x0,0x397c2,0x4eb16} },
59         { SPOOLSS_FORM_BUILTIN, "A4 Plus",                        {0x33450,0x50910}, {0x0,0x0,0x33450,0x50910} },
60         { SPOOLSS_FORM_BUILTIN, "A4 Rotated",                     {0x48828,0x33450}, {0x0,0x0,0x48828,0x33450} },
61         { SPOOLSS_FORM_BUILTIN, "A4 Small",                       {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
62         { SPOOLSS_FORM_BUILTIN, "A4 Transverse",                  {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
63         { SPOOLSS_FORM_BUILTIN, "A4",                             {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
64         { SPOOLSS_FORM_BUILTIN, "A5 Extra",                       {0x2a7b0,0x395f8}, {0x0,0x0,0x2a7b0,0x395f8} },
65         { SPOOLSS_FORM_BUILTIN, "A5 Rotated",                     {0x33450,0x24220}, {0x0,0x0,0x33450,0x24220} },
66         { SPOOLSS_FORM_BUILTIN, "A5 Transverse",                  {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
67         { SPOOLSS_FORM_BUILTIN, "A5",                             {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
68         { SPOOLSS_FORM_BUILTIN, "A6 Rotated",                     {0x24220,0x19a28}, {0x0,0x0,0x24220,0x19a28} },
69         { SPOOLSS_FORM_BUILTIN, "A6",                             {0x19a28,0x24220}, {0x0,0x0,0x19a28,0x24220} },
70         { SPOOLSS_FORM_BUILTIN, "B4 (ISO)",                       {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
71         { SPOOLSS_FORM_BUILTIN, "B4 (JIS) Rotated",               {0x58de0,0x3ebe8}, {0x0,0x0,0x58de0,0x3ebe8} },
72         { SPOOLSS_FORM_BUILTIN, "B4 (JIS)",                       {0x3ebe8,0x58de0}, {0x0,0x0,0x3ebe8,0x58de0} },
73         { SPOOLSS_FORM_BUILTIN, "B5 (ISO) Extra",                 {0x31128,0x43620}, {0x0,0x0,0x31128,0x43620} },
74         { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Rotated",               {0x3ebe8,0x2c6f0}, {0x0,0x0,0x3ebe8,0x2c6f0} },
75         { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Transverse",            {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
76         { SPOOLSS_FORM_BUILTIN, "B5 (JIS)",                       {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
77         { SPOOLSS_FORM_BUILTIN, "B6 (JIS) Rotated",               {0x2c6f0,0x1f400}, {0x0,0x0,0x2c6f0,0x1f400} },
78         { SPOOLSS_FORM_BUILTIN, "B6 (JIS)",                       {0x1f400,0x2c6f0}, {0x0,0x0,0x1f400,0x2c6f0} },
79         { SPOOLSS_FORM_BUILTIN, "C size sheet",                   {0x696b8,0x886d0}, {0x0,0x0,0x696b8,0x886d0} },
80         { SPOOLSS_FORM_BUILTIN, "D size sheet",                   {0x886d0,0xd2d70}, {0x0,0x0,0x886d0,0xd2d70} },
81         { SPOOLSS_FORM_BUILTIN, "Double Japan Postcard Rotated",  {0x24220,0x30d40}, {0x0,0x0,0x24220,0x30d40} },
82         { SPOOLSS_FORM_BUILTIN, "E size sheet",                   {0xd2d70,0x110da0},{0x0,0x0,0xd2d70,0x110da0} },
83         { SPOOLSS_FORM_BUILTIN, "Envelope #10",                   {0x19947,0x3ae94}, {0x0,0x0,0x19947,0x3ae94} },
84         { SPOOLSS_FORM_BUILTIN, "Envelope #11",                   {0x1be7c,0x40565}, {0x0,0x0,0x1be7c,0x40565} },
85         { SPOOLSS_FORM_BUILTIN, "Envelope #12",                   {0x1d74a,0x44368}, {0x0,0x0,0x1d74a,0x44368} },
86         { SPOOLSS_FORM_BUILTIN, "Envelope #14",                   {0x1f018,0x47504}, {0x0,0x0,0x1f018,0x47504} },
87         { SPOOLSS_FORM_BUILTIN, "Envelope #9",                    {0x18079,0x37091}, {0x0,0x0,0x18079,0x37091} },
88         { SPOOLSS_FORM_BUILTIN, "Envelope B4",                    {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
89         { SPOOLSS_FORM_BUILTIN, "Envelope B5",                    {0x2af80,0x3d090}, {0x0,0x0,0x2af80,0x3d090} },
90         { SPOOLSS_FORM_BUILTIN, "Envelope B6",                    {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
91         { SPOOLSS_FORM_BUILTIN, "Envelope C3",                    {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
92         { SPOOLSS_FORM_BUILTIN, "Envelope C4",                    {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
93         { SPOOLSS_FORM_BUILTIN, "Envelope C5",                    {0x278d0,0x37e88}, {0x0,0x0,0x278d0,0x37e88} },
94         { SPOOLSS_FORM_BUILTIN, "Envelope C6",                    {0x1bd50,0x278d0}, {0x0,0x0,0x1bd50,0x278d0} },
95         { SPOOLSS_FORM_BUILTIN, "Envelope C65",                   {0x1bd50,0x37e88}, {0x0,0x0,0x1bd50,0x37e88} },
96         { SPOOLSS_FORM_BUILTIN, "Envelope DL",                    {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
97         { SPOOLSS_FORM_BUILTIN, "Envelope Invite",                {0x35b60,0x35b60}, {0x0,0x0,0x35b60,0x35b60} },
98         { SPOOLSS_FORM_BUILTIN, "Envelope Monarch",               {0x18079,0x2e824}, {0x0,0x0,0x18079,0x2e824} },
99         { SPOOLSS_FORM_BUILTIN, "Envelope",                       {0x1adb0,0x38270}, {0x0,0x0,0x1adb0,0x38270} },
100         { SPOOLSS_FORM_BUILTIN, "Executive",                      {0x2cf56,0x411cc}, {0x0,0x0,0x2cf56,0x411cc} },
101         { SPOOLSS_FORM_BUILTIN, "Folio",                          {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
102         { SPOOLSS_FORM_BUILTIN, "German Legal Fanfold",           {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
103         { SPOOLSS_FORM_BUILTIN, "German Std Fanfold",             {0x34b5c,0x4a6a0}, {0x0,0x0,0x34b5c,0x4a6a0} },
104         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #3 Rotated", {0x395f8,0x1d4c0}, {0x0,0x0,0x395f8,0x1d4c0} },
105         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #4 Rotated", {0x320c8,0x15f90}, {0x0,0x0,0x320c8,0x15f90} },
106         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #2 Rotated", {0x510e0,0x3a980}, {0x0,0x0,0x510e0,0x3a980} },
107         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #3 Rotated", {0x43a08,0x34bc0}, {0x0,0x0,0x43a08,0x34bc0} },
108         { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4 Rotated",  {0x395f8,0x19a28}, {0x0,0x0,0x395f8,0x19a28} },
109         { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4",          {0x19a28,0x395f8}, {0x0,0x0,0x19a28,0x395f8} },
110         { SPOOLSS_FORM_BUILTIN, "Japanese Double Postcard",       {0x30d40,0x24220}, {0x0,0x0,0x30d40,0x24220} },
111         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #3",      {0x1d4c0,0x395f8}, {0x0,0x0,0x1d4c0,0x395f8} },
112         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #4",      {0x15f90,0x320c8}, {0x0,0x0,0x15f90,0x320c8} },
113         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #2",      {0x3a980,0x510e0}, {0x0,0x0,0x3a980,0x510e0} },
114         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #3",      {0x34bc0,0x43a08}, {0x0,0x0,0x34bc0,0x43a08} },
115         { SPOOLSS_FORM_BUILTIN, "Japanese Postcard Rotated",      {0x24220,0x186a0}, {0x0,0x0,0x24220,0x186a0} },
116         { SPOOLSS_FORM_BUILTIN, "Japanese Postcard",              {0x186a0,0x24220}, {0x0,0x0,0x186a0,0x24220} },
117         { SPOOLSS_FORM_BUILTIN, "Ledger",                         {0x696b8,0x44368}, {0x0,0x0,0x696b8,0x44368} },
118         { SPOOLSS_FORM_BUILTIN, "Legal Extra",                    {0x3ae94,0x5d048}, {0x0,0x0,0x3ae94,0x5d048} },
119         { SPOOLSS_FORM_BUILTIN, "Legal",                          {0x34b5c,0x56d10}, {0x0,0x0,0x34b5c,0x56d10} },
120         { SPOOLSS_FORM_BUILTIN, "Letter Extra Transverse",        {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
121         { SPOOLSS_FORM_BUILTIN, "Letter Extra",                   {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
122         { SPOOLSS_FORM_BUILTIN, "Letter Plus",                    {0x34b5c,0x4eb16}, {0x0,0x0,0x34b5c,0x4eb16} },
123         { SPOOLSS_FORM_BUILTIN, "Letter Rotated",                 {0x44368,0x34b5c}, {0x0,0x0,0x44368,0x34b5c} },
124         { SPOOLSS_FORM_BUILTIN, "Letter Small",                   {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
125         { SPOOLSS_FORM_BUILTIN, "Letter Transverse",              {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
126         { SPOOLSS_FORM_BUILTIN, "Letter",                         {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
127         { SPOOLSS_FORM_BUILTIN, "Note",                           {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
128         { SPOOLSS_FORM_BUILTIN, "PRC 16K Rotated",                {0x3f7a0,0x2de60}, {0x0,0x0,0x3f7a0,0x2de60} },
129         { SPOOLSS_FORM_BUILTIN, "PRC 16K",                        {0x2de60,0x3f7a0}, {0x0,0x0,0x2de60,0x3f7a0} },
130         { SPOOLSS_FORM_BUILTIN, "PRC 32K Rotated",                {0x2cec0,0x1fbd0}, {0x0,0x0,0x2cec0,0x1fbd0} },
131         { SPOOLSS_FORM_BUILTIN, "PRC 32K",                        {0x1fbd0,0x2cec0}, {0x0,0x0,0x1fbd0,0x2cec0} },
132         { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big) Rotated",           {0x318f8,0x222e0}, {0x0,0x0,0x318f8,0x222e0} },
133         { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big)",                   {0x222e0,0x318f8}, {0x0,0x0,0x222e0,0x318f8} },
134         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1 Rotated",        {0x28488,0x18e70}, {0x0,0x0,0x28488,0x18e70} },
135         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1",                {0x18e70,0x28488}, {0x0,0x0,0x18e70,0x28488} },
136         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10 Rotated",       {0x6fd10,0x4f1a0}, {0x0,0x0,0x6fd10,0x4f1a0} },
137         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10",               {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
138         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2 Rotated",        {0x2af80,0x18e70}, {0x0,0x0,0x2af80,0x18e70} },
139         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2",                {0x18e70,0x2af80}, {0x0,0x0,0x18e70,0x2af80} },
140         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3 Rotated",        {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
141         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3",                {0x1e848,0x2af80}, {0x0,0x0,0x1e848,0x2af80} },
142         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4 Rotated",        {0x32c80,0x1adb0}, {0x0,0x0,0x32c80,0x1adb0} },
143         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4",                {0x1adb0,0x32c80}, {0x0,0x0,0x1adb0,0x32c80} },
144         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5 Rotated",        {0x35b60,0x1adb0}, {0x0,0x0,0x35b60,0x1adb0} },
145         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5",                {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
146         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6 Rotated",        {0x38270,0x1d4c0}, {0x0,0x0,0x38270,0x1d4c0} },
147         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6",                {0x1d4c0,0x38270}, {0x0,0x0,0x1d4c0,0x38270} },
148         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7 Rotated",        {0x38270,0x27100}, {0x0,0x0,0x38270,0x27100} },
149         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7",                {0x27100,0x38270}, {0x0,0x0,0x27100,0x38270} },
150         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8 Rotated",        {0x4b708,0x1d4c0}, {0x0,0x0,0x4b708,0x1d4c0} },
151         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8",                {0x1d4c0,0x4b708}, {0x0,0x0,0x1d4c0,0x4b708} },
152         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9 Rotated",        {0x4f1a0,0x37e88}, {0x0,0x0,0x4f1a0,0x37e88} },
153         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9",                {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
154         { SPOOLSS_FORM_BUILTIN, "Quarto",                         {0x347d8,0x43238}, {0x0,0x0,0x347d8,0x43238} },
155         { SPOOLSS_FORM_BUILTIN, "Reserved48",                     {0x1,0x1},         {0x0,0x0,0x1,0x1} },
156         { SPOOLSS_FORM_BUILTIN, "Reserved49",                     {0x1,0x1},         {0x0,0x0,0x1,0x1} },
157         { SPOOLSS_FORM_BUILTIN, "Statement",                      {0x221b4,0x34b5c}, {0x0,0x0,0x221b4,0x34b5c} },
158         { SPOOLSS_FORM_BUILTIN, "Super A",                        {0x376b8,0x56ea0}, {0x0,0x0,0x376b8,0x56ea0} },
159         { SPOOLSS_FORM_BUILTIN, "Super B",                        {0x4a768,0x76e58}, {0x0,0x0,0x4a768,0x76e58} },
160         { SPOOLSS_FORM_BUILTIN, "Tabloid Extra",                  {0x4a6a0,0x6f9f0}, {0x0,0x0,0x4a6a0,0x6f9f0} },
161         { SPOOLSS_FORM_BUILTIN, "Tabloid",                        {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
162         { SPOOLSS_FORM_BUILTIN, "US Std Fanfold",                 {0x5c3e1,0x44368}, {0x0,0x0,0x5c3e1,0x44368} }
163 };
164
165 /********************************************************************
166  static helper functions
167 ********************************************************************/
168
169 /****************************************************************************
170  Update the changeid time.
171 ****************************************************************************/
172 /**
173  * @internal
174  *
175  * @brief Update the ChangeID time of a printer.
176  *
177  * This is SO NASTY as some drivers need this to change, others need it
178  * static. This value will change every second, and I must hope that this
179  * is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
180  * UTAH ! JRA.
181  *
182  * @return              The ChangeID.
183  */
184 static uint32_t winreg_printer_rev_changeid(void)
185 {
186         struct timeval tv;
187
188         get_process_uptime(&tv);
189
190 #if 1   /* JERRY */
191         /* Return changeid as msec since spooler restart */
192         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
193 #else
194         /*
195          * This setting seems to work well but is too untested
196          * to replace the above calculation.  Left in for experiementation
197          * of the reader            --jerry (Tue Mar 12 09:15:05 CST 2002)
198          */
199         return tv.tv_sec * 10 + tv.tv_usec / 100000;
200 #endif
201 }
202
203 /**
204  * @internal
205  *
206  * @brief Connect to the interal winreg server and open the given printer key.
207  *
208  * The function will create the needed subkeys if they don't exist.
209  *
210  * @param[in]  mem_ctx       The memory context to use.
211  *
212  * @param[in]  server_info   The supplied server info.
213  *
214  * @param[out] winreg_pipe   A pointer for the winreg rpc client pipe.
215  *
216  * @param[in]  path          The path to the key to open.
217  *
218  * @param[in]  key           The key to open.
219  *
220  * @param[in]  create_key    Set to true if the key should be created if it
221  *                           doesn't exist.
222  *
223  * @param[in]  access_mask   The access mask to open the key.
224  *
225  * @param[out] hive_handle   A policy handle for the opened hive.
226  *
227  * @param[out] key_handle    A policy handle for the opened key.
228  *
229  * @return                   WERR_OK on success, the corresponding DOS error
230  *                           code if something gone wrong.
231  */
232 static WERROR winreg_printer_openkey(TALLOC_CTX *mem_ctx,
233                               struct auth_serversupplied_info *server_info,
234                               struct rpc_pipe_client **winreg_pipe,
235                               const char *path,
236                               const char *key,
237                               bool create_key,
238                               uint32_t access_mask,
239                               struct policy_handle *hive_handle,
240                               struct policy_handle *key_handle)
241 {
242         struct rpc_pipe_client *pipe_handle;
243         struct winreg_String wkey, wkeyclass;
244         char *keyname;
245         NTSTATUS status;
246         WERROR result = WERR_OK;
247
248         /* create winreg connection */
249         status = rpc_pipe_open_internal(mem_ctx,
250                                         &ndr_table_winreg.syntax_id,
251                                         rpc_winreg_dispatch,
252                                         server_info,
253                                         &pipe_handle);
254         if (!NT_STATUS_IS_OK(status)) {
255                 DEBUG(0, ("winreg_printer_openkey: Could not connect to winreg_pipe: %s\n",
256                           nt_errstr(status)));
257                 return ntstatus_to_werror(status);
258         }
259
260         status = rpccli_winreg_OpenHKLM(pipe_handle,
261                                         mem_ctx,
262                                         NULL,
263                                         access_mask,
264                                         hive_handle,
265                                         &result);
266         if (!NT_STATUS_IS_OK(status)) {
267                 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
268                           nt_errstr(status)));
269                 talloc_free(pipe_handle);
270                 if (!W_ERROR_IS_OK(result)) {
271                         return result;
272                 }
273                 return ntstatus_to_werror(status);
274         }
275
276         if (key && *key) {
277                 keyname = talloc_asprintf(mem_ctx, "%s\\%s", path, key);
278         } else {
279                 keyname = talloc_strdup(mem_ctx, path);
280         }
281         if (keyname == NULL) {
282                 talloc_free(pipe_handle);
283                 return WERR_NOMEM;
284         }
285
286         ZERO_STRUCT(wkey);
287         wkey.name = keyname;
288
289         if (create_key) {
290                 enum winreg_CreateAction action = REG_ACTION_NONE;
291
292                 ZERO_STRUCT(wkeyclass);
293                 wkeyclass.name = "";
294
295                 status = rpccli_winreg_CreateKey(pipe_handle,
296                                                  mem_ctx,
297                                                  hive_handle,
298                                                  wkey,
299                                                  wkeyclass,
300                                                  0,
301                                                  access_mask,
302                                                  NULL,
303                                                  key_handle,
304                                                  &action,
305                                                  &result);
306                 switch (action) {
307                         case REG_ACTION_NONE:
308                                 DEBUG(8, ("winreg_printer_openkey:createkey did nothing -- huh?\n"));
309                                 break;
310                         case REG_CREATED_NEW_KEY:
311                                 DEBUG(8, ("winreg_printer_openkey: createkey created %s\n", keyname));
312                                 break;
313                         case REG_OPENED_EXISTING_KEY:
314                                 DEBUG(8, ("winreg_printer_openkey: createkey opened existing %s\n", keyname));
315                                 break;
316                 }
317         } else {
318                 status = rpccli_winreg_OpenKey(pipe_handle,
319                                                mem_ctx,
320                                                hive_handle,
321                                                wkey,
322                                                0,
323                                                access_mask,
324                                                key_handle,
325                                                &result);
326         }
327         if (!NT_STATUS_IS_OK(status)) {
328                 talloc_free(pipe_handle);
329                 if (!W_ERROR_IS_OK(result)) {
330                         return result;
331                 }
332                 return ntstatus_to_werror(status);
333         }
334
335         *winreg_pipe = pipe_handle;
336
337         return WERR_OK;
338 }
339
340 /**
341  * @brief Create the registry keyname for the given printer.
342  *
343  * @param[in]  mem_ctx  The memory context to use.
344  *
345  * @param[in]  printer  The name of the printer to get the registry key.
346  *
347  * @return     The registry key or NULL on error.
348  */
349 static char *winreg_printer_data_keyname(TALLOC_CTX *mem_ctx, const char *printer) {
350         return talloc_asprintf(mem_ctx, "%s\\%s", TOP_LEVEL_PRINT_PRINTERS_KEY, printer);
351 }
352
353 /**
354  * @internal
355  *
356  * @brief Enumerate values of an opened key handle and retrieve the data.
357  *
358  * @param[in]  mem_ctx  The memory context to use.
359  *
360  * @param[in]  pipe_handle The pipe handle for the rpc connection.
361  *
362  * @param[in]  key_hnd  The opened key handle.
363  *
364  * @param[out] pnum_values A pointer to store he number of values found.
365  *
366  * @param[out] pnum_values A pointer to store the number of values we found.
367  *
368  * @return                   WERR_OK on success, the corresponding DOS error
369  *                           code if something gone wrong.
370  */
371 static WERROR winreg_printer_enumvalues(TALLOC_CTX *mem_ctx,
372                                         struct rpc_pipe_client *pipe_handle,
373                                         struct policy_handle *key_hnd,
374                                         uint32_t *pnum_values,
375                                         struct spoolss_PrinterEnumValues **penum_values)
376 {
377         TALLOC_CTX *tmp_ctx;
378         uint32_t num_subkeys, max_subkeylen, max_classlen;
379         uint32_t num_values, max_valnamelen, max_valbufsize;
380         uint32_t secdescsize;
381         uint32_t i;
382         NTTIME last_changed_time;
383         struct winreg_String classname;
384
385         struct spoolss_PrinterEnumValues *enum_values;
386
387         WERROR result = WERR_OK;
388         NTSTATUS status;
389
390         tmp_ctx = talloc_new(mem_ctx);
391         if (tmp_ctx == NULL) {
392                 return WERR_NOMEM;
393         }
394
395         ZERO_STRUCT(classname);
396
397         status = rpccli_winreg_QueryInfoKey(pipe_handle,
398                                             tmp_ctx,
399                                             key_hnd,
400                                             &classname,
401                                             &num_subkeys,
402                                             &max_subkeylen,
403                                             &max_classlen,
404                                             &num_values,
405                                             &max_valnamelen,
406                                             &max_valbufsize,
407                                             &secdescsize,
408                                             &last_changed_time,
409                                             &result);
410         if (!NT_STATUS_IS_OK(status)) {
411                 DEBUG(0, ("winreg_printer_enumvalues: Could not query info: %s\n",
412                           nt_errstr(status)));
413                 if (!W_ERROR_IS_OK(result)) {
414                         goto error;
415                 }
416                 result = ntstatus_to_werror(status);
417                 goto error;
418         }
419
420         if (num_values == 0) {
421                 *pnum_values = 0;
422                 TALLOC_FREE(tmp_ctx);
423                 return WERR_OK;
424         }
425
426         enum_values = TALLOC_ARRAY(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
427         if (enum_values == NULL) {
428                 result = WERR_NOMEM;
429                 goto error;
430         }
431
432         for (i = 0; i < num_values; i++) {
433                 struct spoolss_PrinterEnumValues val;
434                 struct winreg_ValNameBuf name_buf;
435                 enum winreg_Type type = REG_NONE;
436                 uint8_t *data = NULL;
437                 uint32_t data_size;
438                 uint32_t length;
439                 char n = '\0';;
440
441                 name_buf.name = &n;
442                 name_buf.size = max_valnamelen + 2;
443                 name_buf.length = 0;
444
445                 data_size = max_valbufsize;
446                 data = (uint8_t *) TALLOC(tmp_ctx, data_size);
447                 length = 0;
448
449                 status = rpccli_winreg_EnumValue(pipe_handle,
450                                                  tmp_ctx,
451                                                  key_hnd,
452                                                  i,
453                                                  &name_buf,
454                                                  &type,
455                                                  data,
456                                                  &data_size,
457                                                  &length,
458                                                  &result);
459                 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
460                         result = WERR_OK;
461                         status = NT_STATUS_OK;
462                         break;
463                 }
464
465                 if (!NT_STATUS_IS_OK(status)) {
466                         DEBUG(0, ("winreg_printer_enumvalues: Could not enumerate values: %s\n",
467                                   nt_errstr(status)));
468                         if (!W_ERROR_IS_OK(result)) {
469                                 goto error;
470                         }
471                         result = ntstatus_to_werror(status);
472                         goto error;
473                 }
474
475                 if (name_buf.name == NULL) {
476                         result = WERR_INVALID_PARAMETER;
477                         goto error;
478                 }
479
480                 val.value_name = talloc_strdup(enum_values, name_buf.name);
481                 if (val.value_name == NULL) {
482                         result = WERR_NOMEM;
483                         goto error;
484                 }
485                 val.value_name_len = strlen_m_term(val.value_name) * 2;
486
487                 val.type = type;
488                 val.data_length = data_size;
489                 val.data = NULL;
490                 if (val.data_length) {
491                         val.data = talloc(enum_values, DATA_BLOB);
492                         if (val.data == NULL) {
493                                 result = WERR_NOMEM;
494                                 goto error;
495                         }
496                         *val.data = data_blob_talloc(enum_values, data, data_size);
497                 }
498
499                 enum_values[i] = val;
500         }
501
502         *pnum_values = num_values;
503         if (penum_values) {
504                 *penum_values = talloc_move(mem_ctx, &enum_values);
505         }
506
507         result = WERR_OK;
508
509  error:
510         TALLOC_FREE(tmp_ctx);
511         return result;
512 }
513
514 /**
515  * @internal
516  *
517  * @brief Enumerate subkeys of an opened key handle and get the names.
518  *
519  * @param[in]  mem_ctx  The memory context to use.
520  *
521  * @param[in]  pipe_handle The pipe handle for the rpc connection.
522  *
523  * @param[in]  key_hnd  The opened key handle.
524  *
525  * @param[in]  pnum_subkeys A pointer to store the number of found subkeys.
526  *
527  * @param[in]  psubkeys A pointer to an array to store the found names of
528  *                      subkeys.
529  *
530  * @return                   WERR_OK on success, the corresponding DOS error
531  *                           code if something gone wrong.
532  */
533 static WERROR winreg_printer_enumkeys(TALLOC_CTX *mem_ctx,
534                                       struct rpc_pipe_client *pipe_handle,
535                                       struct policy_handle *key_hnd,
536                                       uint32_t *pnum_subkeys,
537                                       const char ***psubkeys)
538 {
539         TALLOC_CTX *tmp_ctx;
540         const char **subkeys;
541         uint32_t num_subkeys, max_subkeylen, max_classlen;
542         uint32_t num_values, max_valnamelen, max_valbufsize;
543         uint32_t i;
544         NTTIME last_changed_time;
545         uint32_t secdescsize;
546         struct winreg_String classname;
547         WERROR result = WERR_OK;
548         NTSTATUS status;
549
550         tmp_ctx = talloc_new(mem_ctx);
551         if (tmp_ctx == NULL) {
552                 return WERR_NOMEM;
553         }
554
555         ZERO_STRUCT(classname);
556
557         status = rpccli_winreg_QueryInfoKey(pipe_handle,
558                                             tmp_ctx,
559                                             key_hnd,
560                                             &classname,
561                                             &num_subkeys,
562                                             &max_subkeylen,
563                                             &max_classlen,
564                                             &num_values,
565                                             &max_valnamelen,
566                                             &max_valbufsize,
567                                             &secdescsize,
568                                             &last_changed_time,
569                                             &result);
570         if (!NT_STATUS_IS_OK(status)) {
571                 DEBUG(0, ("winreg_printer_enumkeys: Could not query info: %s\n",
572                           nt_errstr(status)));
573                 if (!W_ERROR_IS_OK(result)) {
574                         goto error;
575                 }
576                 result = ntstatus_to_werror(status);
577                 goto error;
578         }
579
580         subkeys = talloc_zero_array(tmp_ctx, const char *, num_subkeys + 2);
581         if (subkeys == NULL) {
582                 result = WERR_NOMEM;
583                 goto error;
584         }
585
586         if (num_subkeys == 0) {
587                 subkeys[0] = talloc_strdup(subkeys, "");
588                 if (subkeys[0] == NULL) {
589                         result = WERR_NOMEM;
590                         goto error;
591                 }
592                 *pnum_subkeys = 0;
593                 if (psubkeys) {
594                         *psubkeys = talloc_move(mem_ctx, &subkeys);
595                 }
596
597                 TALLOC_FREE(tmp_ctx);
598                 return WERR_OK;
599         }
600
601         for (i = 0; i < num_subkeys; i++) {
602                 char c = '\0';
603                 char n = '\0';
604                 char *name = NULL;
605                 struct winreg_StringBuf class_buf;
606                 struct winreg_StringBuf name_buf;
607                 NTTIME modtime;
608
609                 class_buf.name = &c;
610                 class_buf.size = max_classlen + 2;
611                 class_buf.length = 0;
612
613                 name_buf.name = &n;
614                 name_buf.size = max_subkeylen + 2;
615                 name_buf.length = 0;
616
617                 ZERO_STRUCT(modtime);
618
619                 status = rpccli_winreg_EnumKey(pipe_handle,
620                                                tmp_ctx,
621                                                key_hnd,
622                                                i,
623                                                &name_buf,
624                                                &class_buf,
625                                                &modtime,
626                                                &result);
627                 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
628                         result = WERR_OK;
629                         status = NT_STATUS_OK;
630                         break;
631                 }
632
633                 if (!NT_STATUS_IS_OK(status)) {
634                         DEBUG(0, ("winreg_printer_enumkeys: Could not enumerate keys: %s\n",
635                                   nt_errstr(status)));
636                         if (!W_ERROR_IS_OK(result)) {
637                                 goto error;
638                         }
639                         result = ntstatus_to_werror(status);
640                         goto error;
641                 }
642
643                 if (name_buf.name == NULL) {
644                         result = WERR_INVALID_PARAMETER;
645                         goto error;
646                 }
647
648                 name = talloc_strdup(subkeys, name_buf.name);
649                 if (name == NULL) {
650                         result = WERR_NOMEM;
651                         goto error;
652                 }
653
654                 subkeys[i] = name;
655         }
656
657         *pnum_subkeys = num_subkeys;
658         if (psubkeys) {
659                 *psubkeys = talloc_move(mem_ctx, &subkeys);
660         }
661
662  error:
663         TALLOC_FREE(tmp_ctx);
664         return result;
665 }
666
667 /**
668  * @internal
669  *
670  * @brief A function to delete a key and its subkeys recurively.
671  *
672  * @param[in]  mem_ctx  The memory context to use.
673  *
674  * @param[in]  pipe_handle The pipe handle for the rpc connection.
675  *
676  * @param[in]  hive_handle A opened hive handle to the key.
677  *
678  * @param[in]  access_mask The access mask to access the key.
679  *
680  * @param[in]  key      The key to delete
681  *
682  * @return              WERR_OK on success, the corresponding DOS error
683  *                      code if something gone wrong.
684  */
685 static WERROR winreg_printer_delete_subkeys(TALLOC_CTX *mem_ctx,
686                                             struct rpc_pipe_client *pipe_handle,
687                                             struct policy_handle *hive_handle,
688                                             uint32_t access_mask,
689                                             const char *key)
690 {
691         const char **subkeys = NULL;
692         uint32_t num_subkeys = 0;
693         struct policy_handle key_hnd;
694         struct winreg_String wkey;
695         WERROR result = WERR_OK;
696         NTSTATUS status;
697         uint32_t i;
698
699         ZERO_STRUCT(key_hnd);
700         wkey.name = key;
701
702         DEBUG(2, ("winreg_printer_delete_subkeys: delete key %s\n", key));
703         /* open the key */
704         status = rpccli_winreg_OpenKey(pipe_handle,
705                                        mem_ctx,
706                                        hive_handle,
707                                        wkey,
708                                        0,
709                                        access_mask,
710                                        &key_hnd,
711                                        &result);
712         if (!NT_STATUS_IS_OK(status)) {
713                 DEBUG(0, ("winreg_printer_delete_subkeys: Could not open key %s: %s\n",
714                           wkey.name, nt_errstr(status)));
715                 if (!W_ERROR_IS_OK(result)) {
716                         return result;
717                 }
718                 return ntstatus_to_werror(status);
719         }
720
721         result = winreg_printer_enumkeys(mem_ctx,
722                                          pipe_handle,
723                                          &key_hnd,
724                                          &num_subkeys,
725                                          &subkeys);
726         if (!W_ERROR_IS_OK(result)) {
727                 goto done;
728         }
729
730         for (i = 0; i < num_subkeys; i++) {
731                 /* create key + subkey */
732                 char *subkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkeys[i]);
733                 if (subkey == NULL) {
734                         goto done;
735                 }
736
737                 DEBUG(2, ("winreg_printer_delete_subkeys: delete subkey %s\n", subkey));
738                 result = winreg_printer_delete_subkeys(mem_ctx,
739                                                        pipe_handle,
740                                                        hive_handle,
741                                                        access_mask,
742                                                        subkey);
743                 if (!W_ERROR_IS_OK(result)) {
744                         goto done;
745                 }
746         }
747
748         if (is_valid_policy_hnd(&key_hnd)) {
749                 rpccli_winreg_CloseKey(pipe_handle, mem_ctx, &key_hnd, NULL);
750         }
751
752         wkey.name = key;
753
754         status = rpccli_winreg_DeleteKey(pipe_handle,
755                                          mem_ctx,
756                                          hive_handle,
757                                          wkey,
758                                          &result);
759
760 done:
761         if (is_valid_policy_hnd(&key_hnd)) {
762                 rpccli_winreg_CloseKey(pipe_handle, mem_ctx, &key_hnd, NULL);
763         }
764
765         return result;
766 }
767
768 static WERROR winreg_printer_write_sz(TALLOC_CTX *mem_ctx,
769                                       struct rpc_pipe_client *pipe_handle,
770                                       struct policy_handle *key_handle,
771                                       const char *value,
772                                       const char *data)
773 {
774         struct winreg_String wvalue;
775         DATA_BLOB blob;
776         WERROR result = WERR_OK;
777         NTSTATUS status;
778
779         wvalue.name = value;
780         if (data == NULL) {
781                 blob = data_blob_string_const("");
782         } else {
783                 if (!push_reg_sz(mem_ctx, &blob, data)) {
784                         DEBUG(0, ("winreg_printer_write_sz: Could not marshall string %s for %s\n",
785                                 data, wvalue.name));
786                         return WERR_NOMEM;
787                 }
788         }
789         status = rpccli_winreg_SetValue(pipe_handle,
790                                         mem_ctx,
791                                         key_handle,
792                                         wvalue,
793                                         REG_SZ,
794                                         blob.data,
795                                         blob.length,
796                                         &result);
797         if (!NT_STATUS_IS_OK(status)) {
798                 DEBUG(0, ("winreg_printer_write_sz: Could not set value %s: %s\n",
799                         wvalue.name, win_errstr(result)));
800                 if (!W_ERROR_IS_OK(result)) {
801                         result = ntstatus_to_werror(status);
802                 }
803         }
804
805         return result;
806 }
807
808 static WERROR winreg_printer_write_dword(TALLOC_CTX *mem_ctx,
809                                          struct rpc_pipe_client *pipe_handle,
810                                          struct policy_handle *key_handle,
811                                          const char *value,
812                                          uint32_t data)
813 {
814         struct winreg_String wvalue;
815         DATA_BLOB blob;
816         WERROR result = WERR_OK;
817         NTSTATUS status;
818
819         wvalue.name = value;
820         blob = data_blob_talloc(mem_ctx, NULL, 4);
821         SIVAL(blob.data, 0, data);
822
823         status = rpccli_winreg_SetValue(pipe_handle,
824                                         mem_ctx,
825                                         key_handle,
826                                         wvalue,
827                                         REG_DWORD,
828                                         blob.data,
829                                         blob.length,
830                                         &result);
831         if (!NT_STATUS_IS_OK(status)) {
832                 DEBUG(0, ("winreg_printer_write_dword: Could not set value %s: %s\n",
833                         wvalue.name, win_errstr(result)));
834                 if (!W_ERROR_IS_OK(result)) {
835                         result = ntstatus_to_werror(status);
836                 }
837         }
838
839         return result;
840 }
841
842 static WERROR winreg_printer_write_binary(TALLOC_CTX *mem_ctx,
843                                           struct rpc_pipe_client *pipe_handle,
844                                           struct policy_handle *key_handle,
845                                           const char *value,
846                                           DATA_BLOB blob)
847 {
848         struct winreg_String wvalue;
849         WERROR result = WERR_OK;
850         NTSTATUS status;
851
852         wvalue.name = value;
853         status = rpccli_winreg_SetValue(pipe_handle,
854                                         mem_ctx,
855                                         key_handle,
856                                         wvalue,
857                                         REG_BINARY,
858                                         blob.data,
859                                         blob.length,
860                                         &result);
861         if (!NT_STATUS_IS_OK(status)) {
862                 DEBUG(0, ("winreg_printer_write_binary: Could not set value %s: %s\n",
863                         wvalue.name, win_errstr(result)));
864                 if (!W_ERROR_IS_OK(result)) {
865                         result = ntstatus_to_werror(status);
866                 }
867         }
868
869         return result;
870 }
871
872 static WERROR winreg_printer_query_binary(TALLOC_CTX *mem_ctx,
873                                           struct rpc_pipe_client *pipe_handle,
874                                           struct policy_handle *key_handle,
875                                           const char *value,
876                                           DATA_BLOB *data)
877 {
878         struct winreg_String wvalue;
879         enum winreg_Type type;
880         WERROR result = WERR_OK;
881         uint32_t value_len = 0;
882         uint32_t data_size = 0;
883         NTSTATUS status;
884         DATA_BLOB blob;
885
886         wvalue.name = value;
887         status = rpccli_winreg_QueryValue(pipe_handle,
888                                           mem_ctx,
889                                           key_handle,
890                                           &wvalue,
891                                           &type,
892                                           NULL,
893                                           &data_size,
894                                           &value_len,
895                                           &result);
896         if (!NT_STATUS_IS_OK(status)) {
897                 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
898                           wvalue.name, nt_errstr(status)));
899                 if (!W_ERROR_IS_OK(result)) {
900                         goto done;
901                 }
902                 result = ntstatus_to_werror(status);
903                 goto done;
904         }
905
906         if (type != REG_BINARY) {
907                 result = WERR_INVALID_DATATYPE;
908                 goto done;
909         }
910         blob = data_blob_talloc(mem_ctx, NULL, data_size);
911         if (blob.data == NULL) {
912                 result = WERR_NOMEM;
913                 goto done;
914         }
915         value_len = 0;
916
917         status = rpccli_winreg_QueryValue(pipe_handle,
918                                           mem_ctx,
919                                           key_handle,
920                                           &wvalue,
921                                           &type,
922                                           blob.data,
923                                           &data_size,
924                                           &value_len,
925                                           &result);
926         if (!NT_STATUS_IS_OK(status)) {
927                 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
928                           wvalue.name, nt_errstr(status)));
929                 if (!W_ERROR_IS_OK(result)) {
930                         result = ntstatus_to_werror(status);
931                 }
932                 goto done;
933         }
934
935         if (data) {
936                 data->data = blob.data;
937                 data->length = blob.length;
938         }
939 done:
940         return result;
941 }
942
943 static WERROR winreg_printer_query_dword(TALLOC_CTX *mem_ctx,
944                                          struct rpc_pipe_client *pipe_handle,
945                                          struct policy_handle *key_handle,
946                                          const char *value,
947                                          uint32_t *data)
948 {
949         struct winreg_String wvalue;
950         enum winreg_Type type;
951         WERROR result = WERR_OK;
952         uint32_t value_len = 0;
953         uint32_t data_size = 0;
954         NTSTATUS status;
955         DATA_BLOB blob;
956
957         wvalue.name = value;
958         status = rpccli_winreg_QueryValue(pipe_handle,
959                                           mem_ctx,
960                                           key_handle,
961                                           &wvalue,
962                                           &type,
963                                           NULL,
964                                           &data_size,
965                                           &value_len,
966                                           &result);
967         if (!NT_STATUS_IS_OK(status)) {
968                 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
969                           wvalue.name, nt_errstr(status)));
970                 if (!W_ERROR_IS_OK(result)) {
971                         goto done;
972                 }
973                 result = ntstatus_to_werror(status);
974                 goto done;
975         }
976
977         if (type != REG_DWORD) {
978                 result = WERR_INVALID_DATATYPE;
979                 goto done;
980         }
981
982         if (data_size != 4) {
983                 result = WERR_INVALID_DATA;
984                 goto done;
985         }
986
987         blob = data_blob_talloc(mem_ctx, NULL, data_size);
988         if (blob.data == NULL) {
989                 result = WERR_NOMEM;
990                 goto done;
991         }
992         value_len = 0;
993
994         status = rpccli_winreg_QueryValue(pipe_handle,
995                                           mem_ctx,
996                                           key_handle,
997                                           &wvalue,
998                                           &type,
999                                           blob.data,
1000                                           &data_size,
1001                                           &value_len,
1002                                           &result);
1003         if (!NT_STATUS_IS_OK(status)) {
1004                 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
1005                           wvalue.name, nt_errstr(status)));
1006                 if (!W_ERROR_IS_OK(result)) {
1007                         result = ntstatus_to_werror(status);
1008                 }
1009                 goto done;
1010         }
1011
1012         if (data) {
1013                 *data = IVAL(blob.data, 0);
1014         }
1015 done:
1016         return result;
1017 }
1018
1019 static WERROR winreg_printer_write_multi_sz(TALLOC_CTX *mem_ctx,
1020                                             struct rpc_pipe_client *pipe_handle,
1021                                             struct policy_handle *key_handle,
1022                                             const char *value,
1023                                             const char **data)
1024 {
1025         struct winreg_String wvalue;
1026         DATA_BLOB blob;
1027         WERROR result = WERR_OK;
1028         NTSTATUS status;
1029
1030         wvalue.name = value;
1031         if (!push_reg_multi_sz(mem_ctx, &blob, data)) {
1032                 return WERR_NOMEM;
1033         }
1034         status = rpccli_winreg_SetValue(pipe_handle,
1035                                         mem_ctx,
1036                                         key_handle,
1037                                         wvalue,
1038                                         REG_MULTI_SZ,
1039                                         blob.data,
1040                                         blob.length,
1041                                         &result);
1042         if (!NT_STATUS_IS_OK(status)) {
1043                 DEBUG(0, ("winreg_printer_write_multi_sz: Could not set value %s: %s\n",
1044                         wvalue.name, win_errstr(result)));
1045                 if (!W_ERROR_IS_OK(result)) {
1046                         result = ntstatus_to_werror(status);
1047                 }
1048         }
1049
1050         return result;
1051 }
1052
1053 static WERROR winreg_printer_opendriver(TALLOC_CTX *mem_ctx,
1054                                         struct auth_serversupplied_info *server_info,
1055                                         const char *drivername,
1056                                         const char *architecture,
1057                                         uint32_t version,
1058                                         uint32_t access_mask,
1059                                         bool create,
1060                                         struct rpc_pipe_client **winreg_pipe,
1061                                         struct policy_handle *hive_hnd,
1062                                         struct policy_handle *key_hnd)
1063 {
1064         WERROR result;
1065         char *key_name;
1066
1067         key_name = talloc_asprintf(mem_ctx, "%s\\Environments\\%s\\Drivers\\Version-%u",
1068                                    TOP_LEVEL_CONTROL_KEY,
1069                                    architecture, version);
1070         if (!key_name) {
1071                 return WERR_NOMEM;
1072         }
1073
1074         result = winreg_printer_openkey(mem_ctx,
1075                                         server_info,
1076                                         winreg_pipe,
1077                                         key_name,
1078                                         drivername,
1079                                         create,
1080                                         access_mask,
1081                                         hive_hnd,
1082                                         key_hnd);
1083         return result;
1084 }
1085
1086 static WERROR winreg_enumval_to_dword(TALLOC_CTX *mem_ctx,
1087                                       struct spoolss_PrinterEnumValues *v,
1088                                       const char *valuename, uint32_t *dw)
1089 {
1090         /* just return if it is not the one we are looking for */
1091         if (strcmp(valuename, v->value_name) != 0) {
1092                 return WERR_NOT_FOUND;
1093         }
1094
1095         if (v->type != REG_DWORD) {
1096                 return WERR_INVALID_DATATYPE;
1097         }
1098
1099         *dw = IVAL(v->data->data, 0);
1100         return WERR_OK;
1101 }
1102
1103 static WERROR winreg_enumval_to_sz(TALLOC_CTX *mem_ctx,
1104                                    struct spoolss_PrinterEnumValues *v,
1105                                    const char *valuename, const char **_str)
1106 {
1107         /* just return if it is not the one we are looking for */
1108         if (strcmp(valuename, v->value_name) != 0) {
1109                 return WERR_NOT_FOUND;
1110         }
1111
1112         if (v->type != REG_SZ) {
1113                 return WERR_INVALID_DATATYPE;
1114         }
1115
1116         if (!pull_reg_sz(mem_ctx, v->data, _str)) {
1117                 return WERR_NOMEM;
1118         }
1119
1120         return WERR_OK;
1121 }
1122
1123 static WERROR winreg_enumval_to_multi_sz(TALLOC_CTX *mem_ctx,
1124                                          struct spoolss_PrinterEnumValues *v,
1125                                          const char *valuename,
1126                                          const char ***array)
1127 {
1128         /* just return if it is not the one we are looking for */
1129         if (strcmp(valuename, v->value_name) != 0) {
1130                 return WERR_NOT_FOUND;
1131         }
1132
1133         if (v->type != REG_MULTI_SZ) {
1134                 return WERR_INVALID_DATATYPE;
1135         }
1136
1137         if (!pull_reg_multi_sz(mem_ctx, v->data, array)) {
1138                 return WERR_NOMEM;
1139         }
1140
1141         return WERR_OK;
1142 }
1143
1144 static WERROR winreg_printer_write_date(TALLOC_CTX *mem_ctx,
1145                                         struct rpc_pipe_client *pipe_handle,
1146                                         struct policy_handle *key_handle,
1147                                         const char *value,
1148                                         NTTIME data)
1149 {
1150         struct winreg_String wvalue;
1151         DATA_BLOB blob;
1152         WERROR result = WERR_OK;
1153         NTSTATUS status;
1154         const char *str;
1155         struct tm *tm;
1156         time_t t;
1157
1158         t = nt_time_to_unix(data);
1159         tm = localtime(&t);
1160         str = talloc_asprintf(mem_ctx, "%02d/%02d/%04d",
1161                               tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
1162         if (!str) {
1163                 return WERR_NOMEM;
1164         }
1165
1166         wvalue.name = value;
1167         if (!push_reg_sz(mem_ctx, &blob, str)) {
1168                 return WERR_NOMEM;
1169         }
1170         status = rpccli_winreg_SetValue(pipe_handle,
1171                                         mem_ctx,
1172                                         key_handle,
1173                                         wvalue,
1174                                         REG_SZ,
1175                                         blob.data,
1176                                         blob.length,
1177                                         &result);
1178         if (!NT_STATUS_IS_OK(status)) {
1179                 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
1180                         wvalue.name, win_errstr(result)));
1181                 if (!W_ERROR_IS_OK(result)) {
1182                         result = ntstatus_to_werror(status);
1183                 }
1184         }
1185
1186         return result;
1187 }
1188
1189 static WERROR winreg_printer_date_to_NTTIME(const char *str, NTTIME *data)
1190 {
1191         struct tm tm;
1192         time_t t;
1193
1194         ZERO_STRUCT(tm);
1195
1196         if (sscanf(str, "%d/%d/%d",
1197                    &tm.tm_mon, &tm.tm_mday, &tm.tm_year) != 3) {
1198                 return WERR_INVALID_PARAMETER;
1199         }
1200         tm.tm_mon -= 1;
1201         tm.tm_year -= 1900;
1202         tm.tm_isdst = -1;
1203
1204         t = mktime(&tm);
1205         unix_to_nt_time(data, t);
1206
1207         return WERR_OK;
1208 }
1209
1210 static WERROR winreg_printer_write_ver(TALLOC_CTX *mem_ctx,
1211                                        struct rpc_pipe_client *pipe_handle,
1212                                        struct policy_handle *key_handle,
1213                                        const char *value,
1214                                        uint64_t data)
1215 {
1216         struct winreg_String wvalue;
1217         DATA_BLOB blob;
1218         WERROR result = WERR_OK;
1219         NTSTATUS status;
1220         char *str;
1221
1222         /* FIXME: check format is right,
1223          *      this needs to be something like: 6.1.7600.16385 */
1224         str = talloc_asprintf(mem_ctx, "%u.%u.%u.%u",
1225                               (unsigned)((data >> 48) & 0xFFFF),
1226                               (unsigned)((data >> 32) & 0xFFFF),
1227                               (unsigned)((data >> 16) & 0xFFFF),
1228                               (unsigned)(data & 0xFFFF));
1229         if (!str) {
1230                 return WERR_NOMEM;
1231         }
1232
1233         wvalue.name = value;
1234         if (!push_reg_sz(mem_ctx, &blob, str)) {
1235                 return WERR_NOMEM;
1236         }
1237         status = rpccli_winreg_SetValue(pipe_handle,
1238                                         mem_ctx,
1239                                         key_handle,
1240                                         wvalue,
1241                                         REG_SZ,
1242                                         blob.data,
1243                                         blob.length,
1244                                         &result);
1245         if (!NT_STATUS_IS_OK(status)) {
1246                 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
1247                         wvalue.name, win_errstr(result)));
1248                 if (!W_ERROR_IS_OK(result)) {
1249                         result = ntstatus_to_werror(status);
1250                 }
1251         }
1252
1253         return result;
1254 }
1255
1256 static WERROR winreg_printer_ver_to_dword(const char *str, uint64_t *data)
1257 {
1258         unsigned int v1, v2, v3, v4;
1259
1260         if (sscanf(str, "%u.%u.%u.%u", &v1, &v2, &v3, &v4) != 4) {
1261                 return WERR_INVALID_PARAMETER;
1262         }
1263
1264         *data = ((uint64_t)(v1 & 0xFFFF) << 48) +
1265                 ((uint64_t)(v2 & 0xFFFF) << 32) +
1266                 ((uint64_t)(v3 & 0xFFFF) << 16) +
1267                 (uint64_t)(v2 & 0xFFFF);
1268
1269         return WERR_OK;
1270 }
1271
1272 /********************************************************************
1273  Public winreg function for spoolss
1274 ********************************************************************/
1275
1276 WERROR winreg_create_printer(TALLOC_CTX *mem_ctx,
1277                              struct auth_serversupplied_info *server_info,
1278                              const char *servername,
1279                              const char *sharename)
1280 {
1281         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1282         struct rpc_pipe_client *winreg_pipe = NULL;
1283         struct policy_handle hive_hnd, key_hnd;
1284         struct spoolss_SetPrinterInfo2 *info2;
1285         struct security_descriptor *secdesc;
1286         struct winreg_String wkey, wkeyclass;
1287         const char *path;
1288         const char *subkeys[] = { SPOOL_DSDRIVER_KEY, SPOOL_DSSPOOLER_KEY, SPOOL_PRINTERDATA_KEY };
1289         uint32_t i, count = ARRAY_SIZE(subkeys);
1290         uint32_t info2_mask = 0;
1291         WERROR result = WERR_OK;
1292         TALLOC_CTX *tmp_ctx;
1293
1294         tmp_ctx = talloc_new(mem_ctx);
1295         if (tmp_ctx == NULL) {
1296                 return WERR_NOMEM;
1297         }
1298
1299         path = winreg_printer_data_keyname(tmp_ctx, sharename);
1300         if (path == NULL) {
1301                 TALLOC_FREE(tmp_ctx);
1302                 return WERR_NOMEM;
1303         }
1304
1305         ZERO_STRUCT(hive_hnd);
1306         ZERO_STRUCT(key_hnd);
1307
1308         result = winreg_printer_openkey(tmp_ctx,
1309                                         server_info,
1310                                         &winreg_pipe,
1311                                         path,
1312                                         "",
1313                                         false,
1314                                         access_mask,
1315                                         &hive_hnd,
1316                                         &key_hnd);
1317         if (W_ERROR_IS_OK(result)) {
1318                 DEBUG(2, ("winreg_create_printer: Skipping, %s already exists\n", path));
1319                 goto done;
1320         } else if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1321                 DEBUG(2, ("winreg_create_printer: Creating default values in %s\n", path));
1322         } else if (!W_ERROR_IS_OK(result)) {
1323                 DEBUG(0, ("winreg_create_printer: Could not open key %s: %s\n",
1324                         path, win_errstr(result)));
1325                 goto done;
1326         }
1327
1328         /* Create the main key */
1329         result = winreg_printer_openkey(tmp_ctx,
1330                                         server_info,
1331                                         &winreg_pipe,
1332                                         path,
1333                                         "",
1334                                         true,
1335                                         access_mask,
1336                                         &hive_hnd,
1337                                         &key_hnd);
1338         if (!W_ERROR_IS_OK(result)) {
1339                 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
1340                         path, win_errstr(result)));
1341                 goto done;
1342         }
1343
1344         if (is_valid_policy_hnd(&key_hnd)) {
1345                 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1346         }
1347
1348         /* Create subkeys */
1349         for (i = 0; i < count; i++) {
1350                 NTSTATUS status;
1351                 enum winreg_CreateAction action = REG_ACTION_NONE;
1352
1353                 ZERO_STRUCT(key_hnd);
1354                 ZERO_STRUCT(wkey);
1355
1356                 wkey.name = talloc_asprintf(tmp_ctx, "%s\\%s", path, subkeys[i]);
1357                 if (wkey.name == NULL) {
1358                         result = WERR_NOMEM;
1359                         goto done;
1360                 }
1361
1362                 ZERO_STRUCT(wkeyclass);
1363                 wkeyclass.name = "";
1364
1365                 status = rpccli_winreg_CreateKey(winreg_pipe,
1366                                                  tmp_ctx,
1367                                                  &hive_hnd,
1368                                                  wkey,
1369                                                  wkeyclass,
1370                                                  0,
1371                                                  access_mask,
1372                                                  NULL,
1373                                                  &key_hnd,
1374                                                  &action,
1375                                                  &result);
1376                 if (!NT_STATUS_IS_OK(status)) {
1377                         DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
1378                                 wkey.name, win_errstr(result)));
1379                         if (!W_ERROR_IS_OK(result)) {
1380                                 result = ntstatus_to_werror(status);
1381                         }
1382                         goto done;
1383                 }
1384
1385                 switch (i) {
1386                 case 1: {
1387                         const char *dnssuffix;
1388                         const char *longname;
1389                         const char *uncname;
1390
1391                         result = winreg_printer_write_sz(tmp_ctx,
1392                                                          winreg_pipe,
1393                                                          &key_hnd,
1394                                                          SPOOL_REG_PRINTERNAME,
1395                                                          sharename);
1396                         if (!W_ERROR_IS_OK(result)) {
1397                                 goto done;
1398                         }
1399
1400                         result = winreg_printer_write_sz(tmp_ctx,
1401                                                          winreg_pipe,
1402                                                          &key_hnd,
1403                                                          SPOOL_REG_SHORTSERVERNAME,
1404                                                          global_myname());
1405                         if (!W_ERROR_IS_OK(result)) {
1406                                 goto done;
1407                         }
1408
1409                         /* We make the assumption that the netbios name
1410                          * is the same as the DNS name since the former
1411                          * will be what we used to join the domain
1412                          */
1413                         dnssuffix = get_mydnsdomname(tmp_ctx);
1414                         if (dnssuffix != NULL && dnssuffix[0] != '\0') {
1415                                 longname = talloc_asprintf(tmp_ctx, "%s.%s", global_myname(), dnssuffix);
1416                         } else {
1417                                 longname = talloc_strdup(tmp_ctx, global_myname());
1418                         }
1419                         if (longname == NULL) {
1420                                 result = WERR_NOMEM;
1421                                 goto done;
1422                         }
1423
1424                         result = winreg_printer_write_sz(tmp_ctx,
1425                                                          winreg_pipe,
1426                                                          &key_hnd,
1427                                                          SPOOL_REG_SERVERNAME,
1428                                                          longname);
1429                         if (!W_ERROR_IS_OK(result)) {
1430                                 goto done;
1431                         }
1432
1433                         uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
1434                                                   longname, sharename);
1435                         if (uncname == NULL) {
1436                                 result = WERR_NOMEM;
1437                                 goto done;
1438                         }
1439
1440                         result = winreg_printer_write_sz(tmp_ctx,
1441                                                          winreg_pipe,
1442                                                          &key_hnd,
1443                                                          SPOOL_REG_UNCNAME,
1444                                                          uncname);
1445                         if (!W_ERROR_IS_OK(result)) {
1446                                 goto done;
1447                         }
1448
1449                         result = winreg_printer_write_dword(tmp_ctx,
1450                                                             winreg_pipe,
1451                                                             &key_hnd,
1452                                                             SPOOL_REG_VERSIONNUMBER,
1453                                                             4);
1454                         if (!W_ERROR_IS_OK(result)) {
1455                                 goto done;
1456                         }
1457
1458                         result = winreg_printer_write_dword(tmp_ctx,
1459                                                             winreg_pipe,
1460                                                             &key_hnd,
1461                                                             SPOOL_REG_PRINTSTARTTIME,
1462                                                             0);
1463                         if (!W_ERROR_IS_OK(result)) {
1464                                 goto done;
1465                         }
1466
1467                         result = winreg_printer_write_dword(tmp_ctx,
1468                                                             winreg_pipe,
1469                                                             &key_hnd,
1470                                                             SPOOL_REG_PRINTENDTIME,
1471                                                             0);
1472                         if (!W_ERROR_IS_OK(result)) {
1473                                 goto done;
1474                         }
1475
1476                         result = winreg_printer_write_dword(tmp_ctx,
1477                                                             winreg_pipe,
1478                                                             &key_hnd,
1479                                                             SPOOL_REG_PRIORITY,
1480                                                             1);
1481                         if (!W_ERROR_IS_OK(result)) {
1482                                 goto done;
1483                         }
1484
1485                         result = winreg_printer_write_dword(tmp_ctx,
1486                                                             winreg_pipe,
1487                                                             &key_hnd,
1488                                                             SPOOL_REG_PRINTKEEPPRINTEDJOBS,
1489                                                             0);
1490                         if (!W_ERROR_IS_OK(result)) {
1491                                 goto done;
1492                         }
1493                 } /* case 1 */
1494                 default:
1495                         break;
1496                 }
1497
1498                 if (is_valid_policy_hnd(&key_hnd)) {
1499                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1500                 }
1501         }
1502         info2 = talloc_zero(tmp_ctx, struct spoolss_SetPrinterInfo2);
1503         if (info2 == NULL) {
1504                 result = WERR_NOMEM;
1505                 goto done;
1506         }
1507
1508         if (servername != NULL) {
1509                 info2->printername = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
1510                                                      servername, sharename);
1511         } else {
1512                 info2->printername = sharename;
1513         }
1514         if (info2->printername == NULL) {
1515                 result = WERR_NOMEM;
1516                 goto done;
1517         }
1518         info2_mask |= SPOOLSS_PRINTER_INFO_PRINTERNAME;
1519
1520         info2->sharename = sharename;
1521         info2_mask |= SPOOLSS_PRINTER_INFO_SHARENAME;
1522
1523         info2->portname = SAMBA_PRINTER_PORT_NAME;
1524         info2_mask |= SPOOLSS_PRINTER_INFO_PORTNAME;
1525
1526         info2->printprocessor = "winprint";
1527         info2_mask |= SPOOLSS_PRINTER_INFO_PRINTPROCESSOR;
1528
1529         info2->datatype = "RAW";
1530         info2_mask |= SPOOLSS_PRINTER_INFO_DATATYPE;
1531
1532         info2->comment = "";
1533         info2_mask |= SPOOLSS_PRINTER_INFO_COMMENT;
1534
1535         info2->attributes = PRINTER_ATTRIBUTE_SAMBA;
1536         info2_mask |= SPOOLSS_PRINTER_INFO_ATTRIBUTES;
1537
1538         info2->starttime = 0; /* Minutes since 12:00am GMT */
1539         info2_mask |= SPOOLSS_PRINTER_INFO_STARTTIME;
1540
1541         info2->untiltime = 0; /* Minutes since 12:00am GMT */
1542         info2_mask |= SPOOLSS_PRINTER_INFO_UNTILTIME;
1543
1544         info2->priority = 1;
1545         info2_mask |= SPOOLSS_PRINTER_INFO_PRIORITY;
1546
1547         info2->defaultpriority = 1;
1548         info2_mask |= SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY;
1549
1550         result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1551         if (!W_ERROR_IS_OK(result)) {
1552                 goto done;
1553         }
1554         info2_mask |= SPOOLSS_PRINTER_INFO_SECDESC;
1555
1556         /*
1557          * Don't write a default Device Mode to the registry! The Device Mode is
1558          * only written to disk with a SetPrinter level 2 or 8.
1559          */
1560
1561         result = winreg_update_printer(tmp_ctx,
1562                                        server_info,
1563                                        sharename,
1564                                        info2_mask,
1565                                        info2,
1566                                        NULL,
1567                                        secdesc);
1568
1569 done:
1570         if (winreg_pipe != NULL) {
1571                 if (is_valid_policy_hnd(&key_hnd)) {
1572                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1573                 }
1574                 if (is_valid_policy_hnd(&hive_hnd)) {
1575                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1576                 }
1577         }
1578
1579         talloc_free(tmp_ctx);
1580         return result;
1581 }
1582
1583 WERROR winreg_update_printer(TALLOC_CTX *mem_ctx,
1584                              struct auth_serversupplied_info *server_info,
1585                              const char *sharename,
1586                              uint32_t info2_mask,
1587                              struct spoolss_SetPrinterInfo2 *info2,
1588                              struct spoolss_DeviceMode *devmode,
1589                              struct security_descriptor *secdesc)
1590 {
1591         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1592         struct rpc_pipe_client *winreg_pipe = NULL;
1593         struct policy_handle hive_hnd, key_hnd;
1594         int snum = lp_servicenumber(sharename);
1595         enum ndr_err_code ndr_err;
1596         DATA_BLOB blob;
1597         char *path;
1598         WERROR result = WERR_OK;
1599         TALLOC_CTX *tmp_ctx;
1600
1601         tmp_ctx = talloc_new(mem_ctx);
1602         if (tmp_ctx == NULL) {
1603                 return WERR_NOMEM;
1604         }
1605
1606         path = winreg_printer_data_keyname(tmp_ctx, sharename);
1607         if (path == NULL) {
1608                 TALLOC_FREE(tmp_ctx);
1609                 return WERR_NOMEM;
1610         }
1611
1612         ZERO_STRUCT(hive_hnd);
1613         ZERO_STRUCT(key_hnd);
1614
1615         result = winreg_printer_openkey(tmp_ctx,
1616                                         server_info,
1617                                         &winreg_pipe,
1618                                         path,
1619                                         "",
1620                                         true,
1621                                         access_mask,
1622                                         &hive_hnd,
1623                                         &key_hnd);
1624         if (!W_ERROR_IS_OK(result)) {
1625                 DEBUG(0, ("winreg_update_printer: Could not open key %s: %s\n",
1626                         path, win_errstr(result)));
1627                 goto done;
1628         }
1629
1630         if (info2_mask & SPOOLSS_PRINTER_INFO_ATTRIBUTES) {
1631                 result = winreg_printer_write_dword(tmp_ctx,
1632                                                     winreg_pipe,
1633                                                     &key_hnd,
1634                                                     "Attributes",
1635                                                     info2->attributes);
1636                 if (!W_ERROR_IS_OK(result)) {
1637                         goto done;
1638                 }
1639         }
1640
1641 #if 0
1642         if (info2_mask & SPOOLSS_PRINTER_INFO_AVERAGEPPM) {
1643                 result = winreg_printer_write_dword(tmp_ctx,
1644                                                     winreg_pipe,
1645                                                     &key_hnd,
1646                                                     "AveragePpm",
1647                                                     info2->attributes);
1648                 if (!W_ERROR_IS_OK(result)) {
1649                         goto done;
1650                 }
1651         }
1652 #endif
1653
1654         if (info2_mask & SPOOLSS_PRINTER_INFO_COMMENT) {
1655                 result = winreg_printer_write_sz(tmp_ctx,
1656                                                  winreg_pipe,
1657                                                  &key_hnd,
1658                                                  "Description",
1659                                                  info2->comment);
1660                 if (!W_ERROR_IS_OK(result)) {
1661                         goto done;
1662                 }
1663         }
1664
1665         if (info2_mask & SPOOLSS_PRINTER_INFO_DATATYPE) {
1666                 result = winreg_printer_write_sz(tmp_ctx,
1667                                                  winreg_pipe,
1668                                                  &key_hnd,
1669                                                  "Datatype",
1670                                                  info2->datatype);
1671                 if (!W_ERROR_IS_OK(result)) {
1672                         goto done;
1673                 }
1674         }
1675
1676         if (info2_mask & SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY) {
1677                 result = winreg_printer_write_dword(tmp_ctx,
1678                                                     winreg_pipe,
1679                                                     &key_hnd,
1680                                                     "Default Priority",
1681                                                     info2->defaultpriority);
1682                 if (!W_ERROR_IS_OK(result)) {
1683                         goto done;
1684                 }
1685         }
1686
1687         if (info2_mask & SPOOLSS_PRINTER_INFO_DEVMODE) {
1688                 /*
1689                  * Some client drivers freak out if there is a NULL devmode
1690                  * (probably the driver is not checking before accessing
1691                  * the devmode pointer)   --jerry
1692                  */
1693                 if (devmode == NULL && lp_default_devmode(snum) && info2 != NULL) {
1694                         result = spoolss_create_default_devmode(tmp_ctx,
1695                                                                 info2->printername,
1696                                                                 &devmode);
1697                         if (!W_ERROR_IS_OK(result)) {
1698                                 goto done;
1699                         }
1700                 }
1701                 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, devmode,
1702                                 (ndr_push_flags_fn_t) ndr_push_spoolss_DeviceMode);
1703                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1704                         DEBUG(0, ("winreg_update_printer: Failed to marshall device mode\n"));
1705                         result = WERR_NOMEM;
1706                         goto done;
1707                 }
1708
1709                 result = winreg_printer_write_binary(tmp_ctx,
1710                                                      winreg_pipe,
1711                                                      &key_hnd,
1712                                                      "Default DevMode",
1713                                                      blob);
1714                 if (!W_ERROR_IS_OK(result)) {
1715                         goto done;
1716                 }
1717         }
1718
1719         if (info2_mask & SPOOLSS_PRINTER_INFO_DRIVERNAME) {
1720                 result = winreg_printer_write_sz(tmp_ctx,
1721                                                  winreg_pipe,
1722                                                  &key_hnd,
1723                                                  "Printer Driver",
1724                                                  info2->drivername);
1725                 if (!W_ERROR_IS_OK(result)) {
1726                         goto done;
1727                 }
1728         }
1729
1730         if (info2_mask & SPOOLSS_PRINTER_INFO_LOCATION) {
1731                 result = winreg_printer_write_sz(tmp_ctx,
1732                                                  winreg_pipe,
1733                                                  &key_hnd,
1734                                                  "Location",
1735                                                  info2->location);
1736                 if (!W_ERROR_IS_OK(result)) {
1737                         goto done;
1738                 }
1739         }
1740
1741         if (info2_mask & SPOOLSS_PRINTER_INFO_PARAMETERS) {
1742                 result = winreg_printer_write_sz(tmp_ctx,
1743                                                  winreg_pipe,
1744                                                  &key_hnd,
1745                                                  "Parameters",
1746                                                  info2->parameters);
1747                 if (!W_ERROR_IS_OK(result)) {
1748                         goto done;
1749                 }
1750         }
1751
1752         if (info2_mask & SPOOLSS_PRINTER_INFO_PORTNAME) {
1753                 result = winreg_printer_write_sz(tmp_ctx,
1754                                                  winreg_pipe,
1755                                                  &key_hnd,
1756                                                  "Port",
1757                                                  info2->portname);
1758                 if (!W_ERROR_IS_OK(result)) {
1759                         goto done;
1760                 }
1761         }
1762
1763         if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTERNAME) {
1764                 /*
1765                  * in addprinter: no servername and the printer is the name
1766                  * in setprinter: servername is \\server
1767                  *                and printer is \\server\\printer
1768                  *
1769                  * Samba manages only local printers.
1770                  * we currently don't support things like i
1771                  * path=\\other_server\printer
1772                  *
1773                  * We only store the printername, not \\server\printername
1774                  */
1775                 const char *p = strrchr(info2->printername, '\\');
1776                 if (p == NULL) {
1777                         p = info2->printername;
1778                 } else {
1779                         p++;
1780                 }
1781                 result = winreg_printer_write_sz(tmp_ctx,
1782                                                  winreg_pipe,
1783                                                  &key_hnd,
1784                                                  "Name",
1785                                                  p);
1786                 if (!W_ERROR_IS_OK(result)) {
1787                         goto done;
1788                 }
1789         }
1790
1791         if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTPROCESSOR) {
1792                 result = winreg_printer_write_sz(tmp_ctx,
1793                                                  winreg_pipe,
1794                                                  &key_hnd,
1795                                                  "Print Processor",
1796                                                  info2->printprocessor);
1797                 if (!W_ERROR_IS_OK(result)) {
1798                         goto done;
1799                 }
1800         }
1801
1802         if (info2_mask & SPOOLSS_PRINTER_INFO_PRIORITY) {
1803                 result = winreg_printer_write_dword(tmp_ctx,
1804                                                     winreg_pipe,
1805                                                     &key_hnd,
1806                                                     "Priority",
1807                                                     info2->priority);
1808                 if (!W_ERROR_IS_OK(result)) {
1809                         goto done;
1810                 }
1811         }
1812
1813         if (info2_mask & SPOOLSS_PRINTER_INFO_SECDESC) {
1814                 /*
1815                  * We need a security descriptor, if it isn't specified by
1816                  * AddPrinter{Ex} then create a default descriptor.
1817                  */
1818                 if (secdesc == NULL) {
1819                         result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1820                         if (!W_ERROR_IS_OK(result)) {
1821                                 goto done;
1822                         }
1823                 }
1824                 result = winreg_set_printer_secdesc(tmp_ctx,
1825                                                     server_info,
1826                                                     sharename,
1827                                                     secdesc);
1828                 if (!W_ERROR_IS_OK(result)) {
1829                         goto done;
1830                 }
1831         }
1832
1833         if (info2_mask & SPOOLSS_PRINTER_INFO_SEPFILE) {
1834                 result = winreg_printer_write_sz(tmp_ctx,
1835                                                  winreg_pipe,
1836                                                  &key_hnd,
1837                                                  "Separator File",
1838                                                  info2->sepfile);
1839                 if (!W_ERROR_IS_OK(result)) {
1840                         goto done;
1841                 }
1842         }
1843
1844         if (info2_mask & SPOOLSS_PRINTER_INFO_SHARENAME) {
1845                 result = winreg_printer_write_sz(tmp_ctx,
1846                                                  winreg_pipe,
1847                                                  &key_hnd,
1848                                                  "Share Name",
1849                                                  info2->sharename);
1850                 if (!W_ERROR_IS_OK(result)) {
1851                         goto done;
1852                 }
1853         }
1854
1855         if (info2_mask & SPOOLSS_PRINTER_INFO_STARTTIME) {
1856                 result = winreg_printer_write_dword(tmp_ctx,
1857                                                     winreg_pipe,
1858                                                     &key_hnd,
1859                                                     "StartTime",
1860                                                     info2->starttime);
1861                 if (!W_ERROR_IS_OK(result)) {
1862                         goto done;
1863                 }
1864         }
1865
1866         if (info2_mask & SPOOLSS_PRINTER_INFO_STATUS) {
1867                 result = winreg_printer_write_dword(tmp_ctx,
1868                                                     winreg_pipe,
1869                                                     &key_hnd,
1870                                                     "Status",
1871                                                     info2->status);
1872                 if (!W_ERROR_IS_OK(result)) {
1873                         goto done;
1874                 }
1875         }
1876
1877         if (info2_mask & SPOOLSS_PRINTER_INFO_UNTILTIME) {
1878                 result = winreg_printer_write_dword(tmp_ctx,
1879                                                     winreg_pipe,
1880                                                     &key_hnd,
1881                                                     "UntilTime",
1882                                                     info2->untiltime);
1883                 if (!W_ERROR_IS_OK(result)) {
1884                         goto done;
1885                 }
1886         }
1887
1888         result = winreg_printer_write_dword(tmp_ctx,
1889                                             winreg_pipe,
1890                                             &key_hnd,
1891                                             "ChangeID",
1892                                             winreg_printer_rev_changeid());
1893         if (!W_ERROR_IS_OK(result)) {
1894                 goto done;
1895         }
1896
1897         result = WERR_OK;
1898 done:
1899         if (winreg_pipe != NULL) {
1900                 if (is_valid_policy_hnd(&key_hnd)) {
1901                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1902                 }
1903                 if (is_valid_policy_hnd(&hive_hnd)) {
1904                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1905                 }
1906         }
1907
1908         TALLOC_FREE(tmp_ctx);
1909         return result;
1910 }
1911
1912 WERROR winreg_get_printer(TALLOC_CTX *mem_ctx,
1913                           struct auth_serversupplied_info *server_info,
1914                           const char *servername,
1915                           const char *printer,
1916                           struct spoolss_PrinterInfo2 **pinfo2)
1917 {
1918         struct spoolss_PrinterInfo2 *info2;
1919         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1920         struct rpc_pipe_client *winreg_pipe = NULL;
1921         struct policy_handle hive_hnd, key_hnd;
1922         struct spoolss_PrinterEnumValues *enum_values = NULL;
1923         struct spoolss_PrinterEnumValues *v;
1924         enum ndr_err_code ndr_err;
1925         DATA_BLOB blob;
1926         int snum = lp_servicenumber(printer);
1927         uint32_t num_values = 0;
1928         uint32_t i;
1929         char *path;
1930         WERROR result = WERR_OK;
1931         TALLOC_CTX *tmp_ctx;
1932
1933         tmp_ctx = talloc_new(mem_ctx);
1934         if (tmp_ctx == NULL) {
1935                 return WERR_NOMEM;
1936         }
1937
1938         path = winreg_printer_data_keyname(tmp_ctx, printer);
1939         if (path == NULL) {
1940                 TALLOC_FREE(tmp_ctx);
1941                 return WERR_NOMEM;
1942         }
1943
1944         result = winreg_printer_openkey(tmp_ctx,
1945                                         server_info,
1946                                         &winreg_pipe,
1947                                         path,
1948                                         "",
1949                                         false,
1950                                         access_mask,
1951                                         &hive_hnd,
1952                                         &key_hnd);
1953         if (!W_ERROR_IS_OK(result)) {
1954                 DEBUG(0, ("winreg_get_printer: Could not open key %s: %s\n",
1955                           path, win_errstr(result)));
1956                 goto done;
1957         }
1958
1959         result = winreg_printer_enumvalues(tmp_ctx,
1960                                            winreg_pipe,
1961                                            &key_hnd,
1962                                            &num_values,
1963                                            &enum_values);
1964         if (!W_ERROR_IS_OK(result)) {
1965                 DEBUG(0, ("winreg_get_printer: Could not enumerate values in %s: %s\n",
1966                           path, win_errstr(result)));
1967                 goto done;
1968         }
1969
1970         info2 = talloc_zero(tmp_ctx, struct spoolss_PrinterInfo2);
1971         if (info2 == NULL) {
1972                 result = WERR_NOMEM;
1973                 goto done;
1974         }
1975
1976         info2->servername     = EMPTY_STRING;
1977         info2->printername    = EMPTY_STRING;
1978         info2->sharename      = EMPTY_STRING;
1979         info2->portname       = EMPTY_STRING;
1980         info2->drivername     = EMPTY_STRING;
1981         info2->comment        = EMPTY_STRING;
1982         info2->location       = EMPTY_STRING;
1983         info2->sepfile        = EMPTY_STRING;
1984         info2->printprocessor = EMPTY_STRING;
1985         info2->datatype       = EMPTY_STRING;
1986         info2->parameters     = EMPTY_STRING;
1987
1988         if (servername != NULL && servername[0] != '\0') {
1989                 info2->servername = talloc_asprintf(info2, "\\\\%s", servername);
1990                 if (info2->servername == NULL) {
1991                         result = WERR_NOMEM;
1992                         goto done;
1993                 }
1994         }
1995
1996         for (i = 0; i < num_values; i++) {
1997                 v = &enum_values[i];
1998
1999                 result = winreg_enumval_to_sz(info2,
2000                                               v,
2001                                               "Name",
2002                                               &info2->printername);
2003                 CHECK_ERROR(result);
2004
2005                 result = winreg_enumval_to_sz(info2,
2006                                               v,
2007                                               "Share Name",
2008                                               &info2->sharename);
2009                 CHECK_ERROR(result);
2010
2011                 result = winreg_enumval_to_sz(info2,
2012                                               v,
2013                                               "Port",
2014                                               &info2->portname);
2015                 CHECK_ERROR(result);
2016
2017                 result = winreg_enumval_to_sz(info2,
2018                                               v,
2019                                               "Description",
2020                                               &info2->comment);
2021                 CHECK_ERROR(result);
2022
2023                 result = winreg_enumval_to_sz(info2,
2024                                               v,
2025                                               "Location",
2026                                               &info2->location);
2027                 CHECK_ERROR(result);
2028
2029                 result = winreg_enumval_to_sz(info2,
2030                                               v,
2031                                               "Separator File",
2032                                               &info2->sepfile);
2033                 CHECK_ERROR(result);
2034
2035                 result = winreg_enumval_to_sz(info2,
2036                                               v,
2037                                               "Print Processor",
2038                                               &info2->printprocessor);
2039                 CHECK_ERROR(result);
2040
2041                 result = winreg_enumval_to_sz(info2,
2042                                               v,
2043                                               "Datatype",
2044                                               &info2->datatype);
2045                 CHECK_ERROR(result);
2046
2047                 result = winreg_enumval_to_sz(info2,
2048                                               v,
2049                                               "Parameters",
2050                                               &info2->parameters);
2051                 CHECK_ERROR(result);
2052
2053                 result = winreg_enumval_to_sz(info2,
2054                                               v,
2055                                               "Printer Driver",
2056                                               &info2->drivername);
2057                 CHECK_ERROR(result);
2058
2059                 result = winreg_enumval_to_dword(info2,
2060                                                  v,
2061                                                  "Attributes",
2062                                                  &info2->attributes);
2063                 CHECK_ERROR(result);
2064
2065                 result = winreg_enumval_to_dword(info2,
2066                                                  v,
2067                                                  "Priority",
2068                                                  &info2->priority);
2069                 CHECK_ERROR(result);
2070
2071                 result = winreg_enumval_to_dword(info2,
2072                                                  v,
2073                                                  "Default Priority",
2074                                                  &info2->defaultpriority);
2075                 CHECK_ERROR(result);
2076
2077                 result = winreg_enumval_to_dword(info2,
2078                                                  v,
2079                                                  "StartTime",
2080                                                  &info2->starttime);
2081                 CHECK_ERROR(result);
2082
2083                 result = winreg_enumval_to_dword(info2,
2084                                                  v,
2085                                                  "UntilTime",
2086                                                  &info2->untiltime);
2087                 CHECK_ERROR(result);
2088
2089                 result = winreg_enumval_to_dword(info2,
2090                                                  v,
2091                                                  "Status",
2092                                                  &info2->status);
2093                 CHECK_ERROR(result);
2094
2095                 result = winreg_enumval_to_dword(info2,
2096                                                  v,
2097                                                  "StartTime",
2098                                                  &info2->starttime);
2099                 CHECK_ERROR(result);
2100         }
2101
2102         if (!W_ERROR_IS_OK(result)) {
2103                 DEBUG(0, ("winreg_get_printer: winreg_enumval_to_TYPE() failed "
2104                                         "for %s: %s\n",
2105                                         v->value_name,
2106                                         win_errstr(result)));
2107                 goto done;
2108         }
2109
2110         /* Create the printername */
2111         if (info2->servername[0] != '\0') {
2112                 if (lp_force_printername(snum)) {
2113                         const char *p = talloc_asprintf(info2, "%s\\%s",
2114                                                         info2->servername,
2115                                                         info2->sharename);
2116                                 if (p == NULL) {
2117                                         result = WERR_NOMEM;
2118                                         goto done;
2119                                 }
2120                                 info2->printername = p;
2121                 } else {
2122                         char *p = talloc_asprintf(info2, "%s\\%s",
2123                                                   info2->servername,
2124                                                   info2->printername);
2125                         if (p == NULL) {
2126                                 result = WERR_NOMEM;
2127                                 goto done;
2128                         }
2129                         info2->printername = p;
2130                 }
2131         }
2132
2133         /* Construct the Device Mode */
2134         result = winreg_printer_query_binary(tmp_ctx,
2135                                              winreg_pipe,
2136                                              &key_hnd,
2137                                              "Default DevMode",
2138                                              &blob);
2139         if (W_ERROR_IS_OK(result)) {
2140                 info2->devmode = talloc_zero(info2, struct spoolss_DeviceMode);
2141                 if (info2->devmode == NULL) {
2142                         result = WERR_NOMEM;
2143                         goto done;
2144                 }
2145                 ndr_err = ndr_pull_struct_blob(&blob,
2146                                                info2->devmode,
2147                                                info2->devmode,
2148                                                (ndr_pull_flags_fn_t) ndr_pull_spoolss_DeviceMode);
2149                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2150                         DEBUG(0, ("winreg_get_printer: Failed to unmarshall device mode\n"));
2151                         result = WERR_NOMEM;
2152                         goto done;
2153                 }
2154         }
2155
2156         if (info2->devmode == NULL && lp_default_devmode(snum)) {
2157                 result = spoolss_create_default_devmode(info2,
2158                                                         info2->printername,
2159                                                         &info2->devmode);
2160                 if (!W_ERROR_IS_OK(result)) {
2161                         goto done;
2162                 }
2163         }
2164
2165         if (info2->devmode != NULL) {
2166                 info2->devmode->devicename = talloc_strdup(info2->devmode,
2167                                                            info2->printername);
2168                 if (info2->devmode->devicename == NULL) {
2169                         DEBUG(0, ("winreg_get_printer: Failed to set devicename\n"));
2170                         result = WERR_NOMEM;
2171                         goto done;
2172                 }
2173         }
2174
2175         result = winreg_get_printer_secdesc(info2,
2176                                             server_info,
2177                                             printer,
2178                                             &info2->secdesc);
2179         if (!W_ERROR_IS_OK(result)) {
2180                 goto done;
2181         }
2182
2183         /* Fix for OS/2 drivers. */
2184         if (get_remote_arch() == RA_OS2) {
2185                 spoolss_map_to_os2_driver(info2, &info2->drivername);
2186         }
2187
2188         if (pinfo2) {
2189                 *pinfo2 = talloc_move(mem_ctx, &info2);
2190         }
2191
2192         result = WERR_OK;
2193 done:
2194         if (winreg_pipe != NULL) {
2195                 if (is_valid_policy_hnd(&key_hnd)) {
2196                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2197                 }
2198                 if (is_valid_policy_hnd(&hive_hnd)) {
2199                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2200                 }
2201         }
2202
2203         TALLOC_FREE(tmp_ctx);
2204         return result;
2205 }
2206
2207 WERROR winreg_get_printer_secdesc(TALLOC_CTX *mem_ctx,
2208                                   struct auth_serversupplied_info *server_info,
2209                                   const char *sharename,
2210                                   struct spoolss_security_descriptor **psecdesc)
2211 {
2212         struct spoolss_security_descriptor *secdesc;
2213         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2214         struct rpc_pipe_client *winreg_pipe = NULL;
2215         struct policy_handle hive_hnd, key_hnd;
2216         enum ndr_err_code ndr_err;
2217         const char *path;
2218         DATA_BLOB blob;
2219         TALLOC_CTX *tmp_ctx;
2220         WERROR result;
2221
2222         tmp_ctx = talloc_new(mem_ctx);
2223         if (tmp_ctx == NULL) {
2224                 return WERR_NOMEM;
2225         }
2226
2227         path = winreg_printer_data_keyname(tmp_ctx, sharename);
2228         if (path == NULL) {
2229                 talloc_free(tmp_ctx);
2230                 return WERR_NOMEM;
2231         }
2232
2233         ZERO_STRUCT(hive_hnd);
2234         ZERO_STRUCT(key_hnd);
2235
2236         result = winreg_printer_openkey(tmp_ctx,
2237                                         server_info,
2238                                         &winreg_pipe,
2239                                         path,
2240                                         "",
2241                                         false,
2242                                         access_mask,
2243                                         &hive_hnd,
2244                                         &key_hnd);
2245         if (!W_ERROR_IS_OK(result)) {
2246                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2247                         goto create_default;
2248                 }
2249                 goto done;
2250         }
2251
2252         result = winreg_printer_query_binary(tmp_ctx,
2253                                              winreg_pipe,
2254                                              &key_hnd,
2255                                              "Security",
2256                                              &blob);
2257         if (!W_ERROR_IS_OK(result)) {
2258                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2259                         goto create_default;
2260                 }
2261                 goto done;
2262         }
2263
2264         secdesc = talloc_zero(tmp_ctx, struct spoolss_security_descriptor);
2265         if (secdesc == NULL) {
2266                 result = WERR_NOMEM;
2267                 goto done;
2268         }
2269         ndr_err = ndr_pull_struct_blob(&blob,
2270                                        secdesc,
2271                                        secdesc,
2272                                        (ndr_pull_flags_fn_t) ndr_pull_security_descriptor);
2273         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2274                 DEBUG(0, ("winreg_get_secdesc: Failed to unmarshall security descriptor\n"));
2275                 result = WERR_NOMEM;
2276                 goto done;
2277         }
2278
2279         if (psecdesc) {
2280                 *psecdesc = talloc_move(mem_ctx, &secdesc);
2281         }
2282
2283         result = WERR_OK;
2284         goto done;
2285
2286 create_default:
2287         result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
2288         if (!W_ERROR_IS_OK(result)) {
2289                 return result;
2290         }
2291
2292         /* If security descriptor is owned by S-1-1-0 and winbindd is up,
2293            this security descriptor has been created when winbindd was
2294            down.  Take ownership of security descriptor. */
2295         if (sid_equal(secdesc->owner_sid, &global_sid_World)) {
2296                 DOM_SID owner_sid;
2297
2298                 /* Change sd owner to workgroup administrator */
2299
2300                 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
2301                         struct spoolss_security_descriptor *new_secdesc;
2302                         size_t size;
2303
2304                         /* Create new sd */
2305                         sid_append_rid(&owner_sid, DOMAIN_RID_ADMINISTRATOR);
2306
2307                         new_secdesc = make_sec_desc(tmp_ctx,
2308                                                     secdesc->revision,
2309                                                     secdesc->type,
2310                                                     &owner_sid,
2311                                                     secdesc->group_sid,
2312                                                     secdesc->sacl,
2313                                                     secdesc->dacl,
2314                                                     &size);
2315
2316                         if (new_secdesc == NULL) {
2317                                 result = WERR_NOMEM;
2318                                 goto done;
2319                         }
2320
2321                         /* Swap with other one */
2322                         secdesc = new_secdesc;
2323                 }
2324         }
2325
2326         ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, secdesc,
2327                         (ndr_push_flags_fn_t) ndr_push_security_descriptor);
2328         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2329                 DEBUG(0, ("winreg_set_secdesc: Failed to marshall security descriptor\n"));
2330                 result = WERR_NOMEM;
2331                 goto done;
2332         }
2333
2334         result = winreg_printer_write_binary(tmp_ctx,
2335                                              winreg_pipe,
2336                                              &key_hnd,
2337                                              "Security",
2338                                              blob);
2339         if (!W_ERROR_IS_OK(result)) {
2340                 return result;
2341         }
2342
2343         if (psecdesc) {
2344                 *psecdesc = talloc_move(mem_ctx, &secdesc);
2345         }
2346
2347         result = WERR_OK;
2348 done:
2349         if (winreg_pipe != NULL) {
2350                 if (is_valid_policy_hnd(&key_hnd)) {
2351                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2352                 }
2353                 if (is_valid_policy_hnd(&hive_hnd)) {
2354                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2355                 }
2356         }
2357
2358         talloc_free(tmp_ctx);
2359         return result;
2360 }
2361
2362 WERROR winreg_set_printer_secdesc(TALLOC_CTX *mem_ctx,
2363                                   struct auth_serversupplied_info *server_info,
2364                                   const char *sharename,
2365                                   const struct spoolss_security_descriptor *secdesc)
2366 {
2367         const struct spoolss_security_descriptor *new_secdesc = secdesc;
2368         struct spoolss_security_descriptor *old_secdesc;
2369         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2370         struct rpc_pipe_client *winreg_pipe = NULL;
2371         struct policy_handle hive_hnd, key_hnd;
2372         enum ndr_err_code ndr_err;
2373         const char *path;
2374         DATA_BLOB blob;
2375         TALLOC_CTX *tmp_ctx;
2376         WERROR result;
2377
2378         tmp_ctx = talloc_new(mem_ctx);
2379         if (tmp_ctx == NULL) {
2380                 return WERR_NOMEM;
2381         }
2382
2383         path = winreg_printer_data_keyname(tmp_ctx, sharename);
2384         if (path == NULL) {
2385                 talloc_free(tmp_ctx);
2386                 return WERR_NOMEM;
2387         }
2388
2389         /*
2390          * The old owner and group sids of the security descriptor are not
2391          * present when new ACEs are added or removed by changing printer
2392          * permissions through NT.  If they are NULL in the new security
2393          * descriptor then copy them over from the old one.
2394          */
2395         if (!secdesc->owner_sid || !secdesc->group_sid) {
2396                 DOM_SID *owner_sid, *group_sid;
2397                 struct security_acl *dacl, *sacl;
2398                 size_t size;
2399
2400                 result = winreg_get_printer_secdesc(tmp_ctx,
2401                                                     server_info,
2402                                                     sharename,
2403                                                     &old_secdesc);
2404                 if (!W_ERROR_IS_OK(result)) {
2405                         talloc_free(tmp_ctx);
2406                         return result;
2407                 }
2408
2409                 /* Pick out correct owner and group sids */
2410                 owner_sid = secdesc->owner_sid ?
2411                             secdesc->owner_sid :
2412                             old_secdesc->owner_sid;
2413
2414                 group_sid = secdesc->group_sid ?
2415                             secdesc->group_sid :
2416                             old_secdesc->group_sid;
2417
2418                 dacl = secdesc->dacl ?
2419                        secdesc->dacl :
2420                        old_secdesc->dacl;
2421
2422                 sacl = secdesc->sacl ?
2423                        secdesc->sacl :
2424                        old_secdesc->sacl;
2425
2426                 /* Make a deep copy of the security descriptor */
2427                 new_secdesc = make_sec_desc(tmp_ctx,
2428                                             secdesc->revision,
2429                                             secdesc->type,
2430                                             owner_sid,
2431                                             group_sid,
2432                                             sacl,
2433                                             dacl,
2434                                             &size);
2435                 if (new_secdesc == NULL) {
2436                         talloc_free(tmp_ctx);
2437                         return WERR_NOMEM;
2438                 }
2439         }
2440
2441         ZERO_STRUCT(hive_hnd);
2442         ZERO_STRUCT(key_hnd);
2443
2444         result = winreg_printer_openkey(tmp_ctx,
2445                                         server_info,
2446                                         &winreg_pipe,
2447                                         path,
2448                                         "",
2449                                         false,
2450                                         access_mask,
2451                                         &hive_hnd,
2452                                         &key_hnd);
2453         if (!W_ERROR_IS_OK(result)) {
2454                 goto done;
2455         }
2456
2457         ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, new_secdesc,
2458                         (ndr_push_flags_fn_t) ndr_push_security_descriptor);
2459         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2460                 DEBUG(0, ("winreg_set_secdesc: Failed to marshall security descriptor\n"));
2461                 result = WERR_NOMEM;
2462                 goto done;
2463         }
2464
2465         result = winreg_printer_write_binary(tmp_ctx,
2466                                              winreg_pipe,
2467                                              &key_hnd,
2468                                              "Security",
2469                                              blob);
2470
2471 done:
2472         if (winreg_pipe != NULL) {
2473                 if (is_valid_policy_hnd(&key_hnd)) {
2474                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2475                 }
2476                 if (is_valid_policy_hnd(&hive_hnd)) {
2477                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2478                 }
2479         }
2480
2481         talloc_free(tmp_ctx);
2482         return result;
2483 }
2484
2485 /* Set printer data over the winreg pipe. */
2486 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
2487                                  struct auth_serversupplied_info *server_info,
2488                                  const char *printer,
2489                                  const char *key,
2490                                  const char *value,
2491                                  enum winreg_Type type,
2492                                  uint8_t *data,
2493                                  uint32_t data_size)
2494 {
2495         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2496         struct rpc_pipe_client *winreg_pipe = NULL;
2497         struct policy_handle hive_hnd, key_hnd;
2498         struct winreg_String wvalue;
2499         char *path;
2500         WERROR result = WERR_OK;
2501         NTSTATUS status;
2502         TALLOC_CTX *tmp_ctx;
2503
2504         tmp_ctx = talloc_new(mem_ctx);
2505         if (tmp_ctx == NULL) {
2506                 return WERR_NOMEM;
2507         }
2508
2509         path = winreg_printer_data_keyname(tmp_ctx, printer);
2510         if (path == NULL) {
2511                 TALLOC_FREE(tmp_ctx);
2512                 return WERR_NOMEM;
2513         }
2514
2515         ZERO_STRUCT(hive_hnd);
2516         ZERO_STRUCT(key_hnd);
2517
2518         DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
2519                         key, value, access_mask, printer));
2520         result = winreg_printer_openkey(tmp_ctx,
2521                                         server_info,
2522                                         &winreg_pipe,
2523                                         path,
2524                                         key,
2525                                         true,
2526                                         access_mask,
2527                                         &hive_hnd,
2528                                         &key_hnd);
2529         if (!W_ERROR_IS_OK(result)) {
2530                 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
2531                           key, win_errstr(result)));
2532                 goto done;
2533         }
2534
2535         wvalue.name = value;
2536         status = rpccli_winreg_SetValue(winreg_pipe,
2537                                         tmp_ctx,
2538                                         &key_hnd,
2539                                         wvalue,
2540                                         type,
2541                                         data,
2542                                         data_size,
2543                                         &result);
2544         if (!NT_STATUS_IS_OK(status)) {
2545                 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
2546                           value, nt_errstr(status)));
2547                 if (!W_ERROR_IS_OK(result)) {
2548                         goto done;
2549                 }
2550                 result = ntstatus_to_werror(status);
2551                 goto done;
2552         }
2553
2554         result = WERR_OK;
2555 done:
2556         if (winreg_pipe != NULL) {
2557                 if (is_valid_policy_hnd(&key_hnd)) {
2558                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2559                 }
2560                 if (is_valid_policy_hnd(&hive_hnd)) {
2561                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2562                 }
2563         }
2564
2565         TALLOC_FREE(tmp_ctx);
2566         return result;
2567 }
2568
2569 /* Get printer data over a winreg pipe. */
2570 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
2571                                  struct auth_serversupplied_info *server_info,
2572                                  const char *printer,
2573                                  const char *key,
2574                                  const char *value,
2575                                  enum winreg_Type *type,
2576                                  uint8_t **data,
2577                                  uint32_t *data_size)
2578 {
2579         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2580         struct rpc_pipe_client *winreg_pipe = NULL;
2581         struct policy_handle hive_hnd, key_hnd;
2582         struct winreg_String wvalue;
2583         enum winreg_Type type_in;
2584         char *path;
2585         uint8_t *data_in;
2586         uint32_t data_in_size = 0;
2587         uint32_t value_len = 0;
2588         WERROR result = WERR_OK;
2589         NTSTATUS status;
2590         TALLOC_CTX *tmp_ctx;
2591
2592         tmp_ctx = talloc_new(mem_ctx);
2593         if (tmp_ctx == NULL) {
2594                 return WERR_NOMEM;
2595         }
2596
2597         path = winreg_printer_data_keyname(tmp_ctx, printer);
2598         if (path == NULL) {
2599                 TALLOC_FREE(tmp_ctx);
2600                 return WERR_NOMEM;
2601         }
2602
2603         ZERO_STRUCT(hive_hnd);
2604         ZERO_STRUCT(key_hnd);
2605
2606         result = winreg_printer_openkey(tmp_ctx,
2607                                         server_info,
2608                                         &winreg_pipe,
2609                                         path,
2610                                         key,
2611                                         false,
2612                                         access_mask,
2613                                         &hive_hnd,
2614                                         &key_hnd);
2615         if (!W_ERROR_IS_OK(result)) {
2616                 DEBUG(0, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
2617                           key, win_errstr(result)));
2618                 goto done;
2619         }
2620
2621         wvalue.name = value;
2622
2623         /*
2624          * call QueryValue once with data == NULL to get the
2625          * needed memory size to be allocated, then allocate
2626          * data buffer and call again.
2627          */
2628         status = rpccli_winreg_QueryValue(winreg_pipe,
2629                                           tmp_ctx,
2630                                           &key_hnd,
2631                                           &wvalue,
2632                                           &type_in,
2633                                           NULL,
2634                                           &data_in_size,
2635                                           &value_len,
2636                                           &result);
2637         if (!NT_STATUS_IS_OK(status)) {
2638                 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2639                           value, nt_errstr(status)));
2640                 if (!W_ERROR_IS_OK(result)) {
2641                         goto done;
2642                 }
2643                 result = ntstatus_to_werror(status);
2644                 goto done;
2645         }
2646
2647         data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
2648         if (data_in == NULL) {
2649                 result = WERR_NOMEM;
2650                 goto done;
2651         }
2652         value_len = 0;
2653
2654         status = rpccli_winreg_QueryValue(winreg_pipe,
2655                                           tmp_ctx,
2656                                           &key_hnd,
2657                                           &wvalue,
2658                                           &type_in,
2659                                           data_in,
2660                                           &data_in_size,
2661                                           &value_len,
2662                                           &result);
2663         if (!NT_STATUS_IS_OK(status)) {
2664                 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2665                           value, nt_errstr(status)));
2666                 if (!W_ERROR_IS_OK(result)) {
2667                         result = ntstatus_to_werror(status);
2668                 }
2669                 goto done;
2670         }
2671
2672         *type = type_in;
2673         *data_size = data_in_size;
2674         if (data_in_size) {
2675                 *data = talloc_move(mem_ctx, &data_in);
2676         }
2677
2678         result = WERR_OK;
2679 done:
2680         if (winreg_pipe != NULL) {
2681                 if (is_valid_policy_hnd(&key_hnd)) {
2682                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2683                 }
2684                 if (is_valid_policy_hnd(&hive_hnd)) {
2685                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2686                 }
2687         }
2688
2689         TALLOC_FREE(tmp_ctx);
2690         return result;
2691 }
2692
2693 /* Enumerate on the values of a given key and provide the data. */
2694 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
2695                                   struct auth_serversupplied_info *server_info,
2696                                   const char *printer,
2697                                   const char *key,
2698                                   uint32_t *pnum_values,
2699                                   struct spoolss_PrinterEnumValues **penum_values)
2700 {
2701         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2702         struct rpc_pipe_client *winreg_pipe = NULL;
2703         struct policy_handle hive_hnd, key_hnd;
2704
2705         struct spoolss_PrinterEnumValues *enum_values = NULL;
2706         uint32_t num_values = 0;
2707         char *path;
2708         WERROR result = WERR_OK;
2709
2710         TALLOC_CTX *tmp_ctx;
2711
2712         tmp_ctx = talloc_new(mem_ctx);
2713         if (tmp_ctx == NULL) {
2714                 return WERR_NOMEM;
2715         }
2716
2717         path = winreg_printer_data_keyname(tmp_ctx, printer);
2718         if (path == NULL) {
2719                 TALLOC_FREE(tmp_ctx);
2720                 return WERR_NOMEM;
2721         }
2722
2723         result = winreg_printer_openkey(tmp_ctx,
2724                                         server_info,
2725                                         &winreg_pipe,
2726                                         path,
2727                                         key,
2728                                         false,
2729                                         access_mask,
2730                                         &hive_hnd,
2731                                         &key_hnd);
2732         if (!W_ERROR_IS_OK(result)) {
2733                 DEBUG(0, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
2734                           key, win_errstr(result)));
2735                 goto done;
2736         }
2737
2738         result = winreg_printer_enumvalues(tmp_ctx,
2739                                            winreg_pipe,
2740                                            &key_hnd,
2741                                            &num_values,
2742                                            &enum_values);
2743         if (!W_ERROR_IS_OK(result)) {
2744                 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2745                           key, win_errstr(result)));
2746                 goto done;
2747         }
2748
2749         *pnum_values = num_values;
2750         if (penum_values) {
2751                 *penum_values = talloc_move(mem_ctx, &enum_values);
2752         }
2753
2754         result = WERR_OK;
2755 done:
2756         if (winreg_pipe != NULL) {
2757                 if (is_valid_policy_hnd(&key_hnd)) {
2758                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2759                 }
2760                 if (is_valid_policy_hnd(&hive_hnd)) {
2761                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2762                 }
2763         }
2764
2765         TALLOC_FREE(tmp_ctx);
2766         return result;
2767 }
2768
2769 /* Delete printer data over a winreg pipe. */
2770 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
2771                                     struct auth_serversupplied_info *server_info,
2772                                     const char *printer,
2773                                     const char *key,
2774                                     const char *value)
2775 {
2776         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2777         struct rpc_pipe_client *winreg_pipe = NULL;
2778         struct policy_handle hive_hnd, key_hnd;
2779         struct winreg_String wvalue;
2780         char *path;
2781         WERROR result = WERR_OK;
2782         NTSTATUS status;
2783
2784         TALLOC_CTX *tmp_ctx;
2785
2786         tmp_ctx = talloc_new(mem_ctx);
2787         if (tmp_ctx == NULL) {
2788                 return WERR_NOMEM;
2789         }
2790
2791         path = winreg_printer_data_keyname(tmp_ctx, printer);
2792         if (path == NULL) {
2793                 TALLOC_FREE(tmp_ctx);
2794                 return WERR_NOMEM;
2795         }
2796
2797         ZERO_STRUCT(hive_hnd);
2798         ZERO_STRUCT(key_hnd);
2799
2800         result = winreg_printer_openkey(tmp_ctx,
2801                                         server_info,
2802                                         &winreg_pipe,
2803                                         path,
2804                                         key,
2805                                         false,
2806                                         access_mask,
2807                                         &hive_hnd,
2808                                         &key_hnd);
2809         if (!W_ERROR_IS_OK(result)) {
2810                 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
2811                           key, win_errstr(result)));
2812                 goto done;
2813         }
2814
2815         wvalue.name = value;
2816         status = rpccli_winreg_DeleteValue(winreg_pipe,
2817                                            tmp_ctx,
2818                                            &key_hnd,
2819                                            wvalue,
2820                                            &result);
2821         if (!NT_STATUS_IS_OK(status)) {
2822                 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
2823                           value, nt_errstr(status)));
2824                 if (!W_ERROR_IS_OK(result)) {
2825                         goto done;
2826                 }
2827                 result = ntstatus_to_werror(status);
2828                 goto done;
2829         }
2830
2831         result = WERR_OK;
2832 done:
2833         if (winreg_pipe != NULL) {
2834                 if (is_valid_policy_hnd(&key_hnd)) {
2835                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2836                 }
2837                 if (is_valid_policy_hnd(&hive_hnd)) {
2838                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2839                 }
2840         }
2841
2842         TALLOC_FREE(tmp_ctx);
2843         return result;
2844 }
2845
2846 /* Enumerate on the subkeys of a given key and provide the data. */
2847 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
2848                                struct auth_serversupplied_info *server_info,
2849                                const char *printer,
2850                                const char *key,
2851                                uint32_t *pnum_subkeys,
2852                                const char ***psubkeys)
2853 {
2854         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2855         struct rpc_pipe_client *winreg_pipe = NULL;
2856         struct policy_handle hive_hnd, key_hnd;
2857         char *path;
2858         const char **subkeys = NULL;
2859         uint32_t num_subkeys = -1;
2860
2861         WERROR result = WERR_OK;
2862
2863         TALLOC_CTX *tmp_ctx;
2864
2865         tmp_ctx = talloc_new(mem_ctx);
2866         if (tmp_ctx == NULL) {
2867                 return WERR_NOMEM;
2868         }
2869
2870         path = winreg_printer_data_keyname(tmp_ctx, printer);
2871         if (path == NULL) {
2872                 TALLOC_FREE(tmp_ctx);
2873                 return WERR_NOMEM;
2874         }
2875
2876         ZERO_STRUCT(hive_hnd);
2877         ZERO_STRUCT(key_hnd);
2878
2879         result = winreg_printer_openkey(tmp_ctx,
2880                                         server_info,
2881                                         &winreg_pipe,
2882                                         path,
2883                                         key,
2884                                         false,
2885                                         access_mask,
2886                                         &hive_hnd,
2887                                         &key_hnd);
2888         if (!W_ERROR_IS_OK(result)) {
2889                 DEBUG(0, ("winreg_enum_printer_key: Could not open key %s: %s\n",
2890                           key, win_errstr(result)));
2891                 goto done;
2892         }
2893
2894         result = winreg_printer_enumkeys(tmp_ctx,
2895                                          winreg_pipe,
2896                                          &key_hnd,
2897                                          &num_subkeys,
2898                                          &subkeys);
2899         if (!W_ERROR_IS_OK(result)) {
2900                 DEBUG(0, ("winreg_enum_printer_key: Could not enumerate subkeys in %s: %s\n",
2901                           key, win_errstr(result)));
2902                 goto done;
2903         }
2904
2905         *pnum_subkeys = num_subkeys;
2906         if (psubkeys) {
2907                 *psubkeys = talloc_move(mem_ctx, &subkeys);
2908         }
2909
2910         result = WERR_OK;
2911 done:
2912         if (winreg_pipe != NULL) {
2913                 if (is_valid_policy_hnd(&key_hnd)) {
2914                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2915                 }
2916                 if (is_valid_policy_hnd(&hive_hnd)) {
2917                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2918                 }
2919         }
2920
2921         TALLOC_FREE(tmp_ctx);
2922         return result;
2923 }
2924
2925 /* Delete a key with subkeys of a given printer. */
2926 WERROR winreg_delete_printer_key(TALLOC_CTX *mem_ctx,
2927                                  struct auth_serversupplied_info *server_info,
2928                                  const char *printer,
2929                                  const char *key)
2930 {
2931         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2932         struct rpc_pipe_client *winreg_pipe = NULL;
2933         struct policy_handle hive_hnd, key_hnd;
2934         char *keyname;
2935         char *path;
2936         WERROR result;
2937         TALLOC_CTX *tmp_ctx;
2938
2939         tmp_ctx = talloc_new(mem_ctx);
2940         if (tmp_ctx == NULL) {
2941                 return WERR_NOMEM;
2942         }
2943
2944         path = winreg_printer_data_keyname(tmp_ctx, printer);
2945         if (path == NULL) {
2946                 TALLOC_FREE(tmp_ctx);
2947                 return WERR_NOMEM;
2948         }
2949
2950         result = winreg_printer_openkey(tmp_ctx,
2951                                         server_info,
2952                                         &winreg_pipe,
2953                                         path,
2954                                         key,
2955                                         false,
2956                                         access_mask,
2957                                         &hive_hnd,
2958                                         &key_hnd);
2959         if (!W_ERROR_IS_OK(result)) {
2960                 /* key doesn't exist */
2961                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2962                         result = WERR_OK;
2963                         goto done;
2964                 }
2965
2966                 DEBUG(0, ("winreg_delete_printer_key: Could not open key %s: %s\n",
2967                           key, win_errstr(result)));
2968                 goto done;
2969         }
2970
2971         if (is_valid_policy_hnd(&key_hnd)) {
2972                 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2973         }
2974
2975         if (key == NULL || key[0] == '\0') {
2976                 keyname = path;
2977         } else {
2978                 keyname = talloc_asprintf(tmp_ctx,
2979                                           "%s\\%s",
2980                                           path,
2981                                           key);
2982                 if (keyname == NULL) {
2983                         result = WERR_NOMEM;
2984                         goto done;
2985                 }
2986         }
2987
2988         result = winreg_printer_delete_subkeys(tmp_ctx,
2989                                                winreg_pipe,
2990                                                &hive_hnd,
2991                                                access_mask,
2992                                                keyname);
2993         if (!W_ERROR_IS_OK(result)) {
2994                 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2995                           key, win_errstr(result)));
2996                 goto done;
2997         }
2998
2999 done:
3000         if (winreg_pipe != NULL) {
3001                 if (is_valid_policy_hnd(&key_hnd)) {
3002                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3003                 }
3004                 if (is_valid_policy_hnd(&hive_hnd)) {
3005                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3006                 }
3007         }
3008
3009         TALLOC_FREE(tmp_ctx);
3010         return result;
3011 }
3012
3013 WERROR winreg_printer_update_changeid(TALLOC_CTX *mem_ctx,
3014                                       struct auth_serversupplied_info *server_info,
3015                                       const char *printer)
3016 {
3017         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3018         struct rpc_pipe_client *winreg_pipe = NULL;
3019         struct policy_handle hive_hnd, key_hnd;
3020         char *path;
3021         WERROR result;
3022         TALLOC_CTX *tmp_ctx;
3023
3024         tmp_ctx = talloc_new(mem_ctx);
3025         if (tmp_ctx == NULL) {
3026                 return WERR_NOMEM;
3027         }
3028
3029         path = winreg_printer_data_keyname(tmp_ctx, printer);
3030         if (path == NULL) {
3031                 TALLOC_FREE(tmp_ctx);
3032                 return WERR_NOMEM;
3033         }
3034
3035         ZERO_STRUCT(hive_hnd);
3036         ZERO_STRUCT(key_hnd);
3037
3038         result = winreg_printer_openkey(tmp_ctx,
3039                                         server_info,
3040                                         &winreg_pipe,
3041                                         path,
3042                                         "",
3043                                         false,
3044                                         access_mask,
3045                                         &hive_hnd,
3046                                         &key_hnd);
3047         if (!W_ERROR_IS_OK(result)) {
3048                 DEBUG(0, ("winreg_printer_update_changeid: Could not open key %s: %s\n",
3049                           path, win_errstr(result)));
3050                 goto done;
3051         }
3052
3053         result = winreg_printer_write_dword(tmp_ctx,
3054                                             winreg_pipe,
3055                                             &key_hnd,
3056                                             "ChangeID",
3057                                             winreg_printer_rev_changeid());
3058         if (!W_ERROR_IS_OK(result)) {
3059                 goto done;
3060         }
3061
3062         result = WERR_OK;
3063 done:
3064         if (winreg_pipe != NULL) {
3065                 if (is_valid_policy_hnd(&key_hnd)) {
3066                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3067                 }
3068                 if (is_valid_policy_hnd(&hive_hnd)) {
3069                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3070                 }
3071         }
3072
3073         TALLOC_FREE(tmp_ctx);
3074         return result;
3075 }
3076
3077 WERROR winreg_printer_get_changeid(TALLOC_CTX *mem_ctx,
3078                                    struct auth_serversupplied_info *server_info,
3079                                    const char *printer,
3080                                    uint32_t *pchangeid)
3081 {
3082         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3083         struct rpc_pipe_client *winreg_pipe = NULL;
3084         struct policy_handle hive_hnd, key_hnd;
3085         uint32_t changeid = 0;
3086         char *path;
3087         WERROR result;
3088         TALLOC_CTX *tmp_ctx;
3089
3090         tmp_ctx = talloc_new(mem_ctx);
3091         if (tmp_ctx == NULL) {
3092                 return WERR_NOMEM;
3093         }
3094
3095         path = winreg_printer_data_keyname(tmp_ctx, printer);
3096         if (path == NULL) {
3097                 TALLOC_FREE(tmp_ctx);
3098                 return WERR_NOMEM;
3099         }
3100
3101         ZERO_STRUCT(hive_hnd);
3102         ZERO_STRUCT(key_hnd);
3103
3104         result = winreg_printer_openkey(tmp_ctx,
3105                                         server_info,
3106                                         &winreg_pipe,
3107                                         path,
3108                                         "",
3109                                         false,
3110                                         access_mask,
3111                                         &hive_hnd,
3112                                         &key_hnd);
3113         if (!W_ERROR_IS_OK(result)) {
3114                 DEBUG(0, ("winreg_printer_get_changeid: Could not open key %s: %s\n",
3115                           path, win_errstr(result)));
3116                 goto done;
3117         }
3118
3119         DEBUG(0, ("winreg_printer_get_changeid: get changeid from %s\n", path));
3120         result = winreg_printer_query_dword(tmp_ctx,
3121                                             winreg_pipe,
3122                                             &key_hnd,
3123                                             "ChangeID",
3124                                             &changeid);
3125         if (!W_ERROR_IS_OK(result)) {
3126                 goto done;
3127         }
3128
3129         if (pchangeid) {
3130                 *pchangeid = changeid;
3131         }
3132
3133         result = WERR_OK;
3134 done:
3135         if (winreg_pipe != NULL) {
3136                 if (is_valid_policy_hnd(&key_hnd)) {
3137                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3138                 }
3139                 if (is_valid_policy_hnd(&hive_hnd)) {
3140                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3141                 }
3142         }
3143
3144         TALLOC_FREE(tmp_ctx);
3145         return result;
3146 }
3147
3148 /*
3149  * The special behaviour of the spoolss forms is documented at the website:
3150  *
3151  * Managing Win32 Printserver Forms
3152  * http://unixwiz.net/techtips/winspooler-forms.html
3153  */
3154
3155 WERROR winreg_printer_addform1(TALLOC_CTX *mem_ctx,
3156                                struct auth_serversupplied_info *server_info,
3157                                struct spoolss_AddFormInfo1 *form)
3158 {
3159         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3160         struct rpc_pipe_client *winreg_pipe = NULL;
3161         struct policy_handle hive_hnd, key_hnd;
3162         struct winreg_String wvalue;
3163         DATA_BLOB blob;
3164         uint32_t num_info = 0;
3165         union spoolss_FormInfo *info = NULL;
3166         uint32_t i;
3167         WERROR result;
3168         NTSTATUS status;
3169         TALLOC_CTX *tmp_ctx;
3170
3171         tmp_ctx = talloc_new(mem_ctx);
3172         if (tmp_ctx == NULL) {
3173                 return WERR_NOMEM;
3174         }
3175
3176         ZERO_STRUCT(hive_hnd);
3177         ZERO_STRUCT(key_hnd);
3178
3179         result = winreg_printer_openkey(tmp_ctx,
3180                                         server_info,
3181                                         &winreg_pipe,
3182                                         TOP_LEVEL_CONTROL_FORMS_KEY,
3183                                         "",
3184                                         true,
3185                                         access_mask,
3186                                         &hive_hnd,
3187                                         &key_hnd);
3188         if (!W_ERROR_IS_OK(result)) {
3189                 DEBUG(0, ("winreg_printer_addform1: Could not open key %s: %s\n",
3190                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3191                 goto done;
3192         }
3193
3194         result = winreg_printer_enumforms1(tmp_ctx, server_info, &num_info, &info);
3195         if (!W_ERROR_IS_OK(result)) {
3196                 DEBUG(0, ("winreg_printer_addform: Could not enum keys %s: %s\n",
3197                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3198                 goto done;
3199         }
3200
3201         /* If form name already exists or is builtin return ALREADY_EXISTS */
3202         for (i = 0; i < num_info; i++) {
3203                 if (strequal(info[i].info1.form_name, form->form_name)) {
3204                         result = WERR_FILE_EXISTS;
3205                         goto done;
3206                 }
3207         }
3208
3209         wvalue.name = form->form_name;
3210
3211         blob = data_blob_talloc(tmp_ctx, NULL, 32);
3212         SIVAL(blob.data,  0, form->size.width);
3213         SIVAL(blob.data,  4, form->size.height);
3214         SIVAL(blob.data,  8, form->area.left);
3215         SIVAL(blob.data, 12, form->area.top);
3216         SIVAL(blob.data, 16, form->area.right);
3217         SIVAL(blob.data, 20, form->area.bottom);
3218         SIVAL(blob.data, 24, num_info + 1); /* FIXME */
3219         SIVAL(blob.data, 28, form->flags);
3220
3221         status = rpccli_winreg_SetValue(winreg_pipe,
3222                                         tmp_ctx,
3223                                         &key_hnd,
3224                                         wvalue,
3225                                         REG_BINARY,
3226                                         blob.data,
3227                                         blob.length,
3228                                         &result);
3229         if (!NT_STATUS_IS_OK(status)) {
3230                 DEBUG(0, ("winreg_printer_addform1: Could not set value %s: %s\n",
3231                           wvalue.name, nt_errstr(status)));
3232                 if (!W_ERROR_IS_OK(result)) {
3233                         goto done;
3234                 }
3235                 result = ntstatus_to_werror(status);
3236                 goto done;
3237         }
3238
3239         result = WERR_OK;
3240 done:
3241         if (winreg_pipe != NULL) {
3242                 if (is_valid_policy_hnd(&key_hnd)) {
3243                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3244                 }
3245                 if (is_valid_policy_hnd(&hive_hnd)) {
3246                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3247                 }
3248         }
3249
3250         TALLOC_FREE(info);
3251         TALLOC_FREE(tmp_ctx);
3252         return result;
3253 }
3254
3255 WERROR winreg_printer_enumforms1(TALLOC_CTX *mem_ctx,
3256                                  struct auth_serversupplied_info *server_info,
3257                                  uint32_t *pnum_info,
3258                                  union spoolss_FormInfo **pinfo)
3259 {
3260         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3261         struct rpc_pipe_client *winreg_pipe = NULL;
3262         struct policy_handle hive_hnd, key_hnd;
3263         union spoolss_FormInfo *info;
3264         struct spoolss_PrinterEnumValues *enum_values = NULL;
3265         uint32_t num_values = 0;
3266         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3267         uint32_t i;
3268         WERROR result;
3269         TALLOC_CTX *tmp_ctx;
3270
3271         tmp_ctx = talloc_new(mem_ctx);
3272         if (tmp_ctx == NULL) {
3273                 return WERR_NOMEM;
3274         }
3275
3276         ZERO_STRUCT(hive_hnd);
3277         ZERO_STRUCT(key_hnd);
3278
3279         result = winreg_printer_openkey(tmp_ctx,
3280                                         server_info,
3281                                         &winreg_pipe,
3282                                         TOP_LEVEL_CONTROL_FORMS_KEY,
3283                                         "",
3284                                         true,
3285                                         access_mask,
3286                                         &hive_hnd,
3287                                         &key_hnd);
3288         if (!W_ERROR_IS_OK(result)) {
3289                 /* key doesn't exist */
3290                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3291                         result = WERR_OK;
3292                         goto done;
3293                 }
3294
3295                 DEBUG(0, ("winreg_printer_enumforms1: Could not open key %s: %s\n",
3296                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3297                 goto done;
3298         }
3299
3300         result = winreg_printer_enumvalues(tmp_ctx,
3301                                            winreg_pipe,
3302                                            &key_hnd,
3303                                            &num_values,
3304                                            &enum_values);
3305         if (!W_ERROR_IS_OK(result)) {
3306                 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
3307                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3308                 goto done;
3309         }
3310
3311         info = TALLOC_ARRAY(tmp_ctx, union spoolss_FormInfo, num_builtin + num_values);
3312         if (info == NULL) {
3313                 result = WERR_NOMEM;
3314                 goto done;
3315         }
3316
3317         /* Enumerate BUILTIN forms */
3318         for (i = 0; i < num_builtin; i++) {
3319                 info[i].info1 = builtin_forms1[i];
3320         }
3321
3322         /* Enumerate registry forms */
3323         for (i = 0; i < num_values; i++) {
3324                 union spoolss_FormInfo val;
3325
3326                 if (enum_values[i].type != REG_BINARY ||
3327                     enum_values[i].data_length != 32) {
3328                         continue;
3329                 }
3330
3331                 val.info1.form_name = talloc_strdup(info, enum_values[i].value_name);
3332                 if (val.info1.form_name == NULL) {
3333                         result = WERR_NOMEM;
3334                         goto done;
3335                 }
3336
3337                 val.info1.size.width  = IVAL(enum_values[i].data->data,  0);
3338                 val.info1.size.height = IVAL(enum_values[i].data->data,  4);
3339                 val.info1.area.left   = IVAL(enum_values[i].data->data,  8);
3340                 val.info1.area.top    = IVAL(enum_values[i].data->data, 12);
3341                 val.info1.area.right  = IVAL(enum_values[i].data->data, 16);
3342                 val.info1.area.bottom = IVAL(enum_values[i].data->data, 20);
3343                 /* skip form index      IVAL(enum_values[i].data->data, 24)));*/
3344                 val.info1.flags       = IVAL(enum_values[i].data->data, 28);
3345
3346                 info[i + num_builtin] = val;
3347         }
3348
3349         *pnum_info = num_builtin + num_values;
3350         if (pinfo) {
3351                 *pinfo = talloc_move(mem_ctx, &info);
3352         }
3353
3354 done:
3355         if (winreg_pipe != NULL) {
3356                 if (is_valid_policy_hnd(&key_hnd)) {
3357                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3358                 }
3359                 if (is_valid_policy_hnd(&hive_hnd)) {
3360                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3361                 }
3362         }
3363
3364         TALLOC_FREE(enum_values);
3365         TALLOC_FREE(tmp_ctx);
3366         return result;
3367 }
3368
3369 WERROR winreg_printer_deleteform1(TALLOC_CTX *mem_ctx,
3370                                   struct auth_serversupplied_info *server_info,
3371                                   const char *form_name)
3372 {
3373         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3374         struct rpc_pipe_client *winreg_pipe = NULL;
3375         struct policy_handle hive_hnd, key_hnd;
3376         struct winreg_String wvalue;
3377         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3378         uint32_t i;
3379         WERROR result = WERR_OK;
3380         NTSTATUS status;
3381         TALLOC_CTX *tmp_ctx;
3382
3383         for (i = 0; i < num_builtin; i++) {
3384                 if (strequal(builtin_forms1[i].form_name, form_name)) {
3385                         return WERR_INVALID_PARAMETER;
3386                 }
3387         }
3388
3389         tmp_ctx = talloc_new(mem_ctx);
3390         if (tmp_ctx == NULL) {
3391                 return WERR_NOMEM;
3392         }
3393
3394         ZERO_STRUCT(hive_hnd);
3395         ZERO_STRUCT(key_hnd);
3396
3397         result = winreg_printer_openkey(tmp_ctx,
3398                                         server_info,
3399                                         &winreg_pipe,
3400                                         TOP_LEVEL_CONTROL_FORMS_KEY,
3401                                         "",
3402                                         false,
3403                                         access_mask,
3404                                         &hive_hnd,
3405                                         &key_hnd);
3406         if (!W_ERROR_IS_OK(result)) {
3407                 DEBUG(0, ("winreg_printer_deleteform1: Could not open key %s: %s\n",
3408                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3409                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3410                         result = WERR_INVALID_FORM_NAME;
3411                 }
3412                 goto done;
3413         }
3414
3415         wvalue.name = form_name;
3416         status = rpccli_winreg_DeleteValue(winreg_pipe,
3417                                            tmp_ctx,
3418                                            &key_hnd,
3419                                            wvalue,
3420                                            &result);
3421         if (!NT_STATUS_IS_OK(status)) {
3422                 /* If the value doesn't exist, return WERR_INVALID_FORM_NAME */
3423                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3424                         result = WERR_INVALID_FORM_NAME;
3425                         goto done;
3426                 }
3427                 DEBUG(0, ("winreg_printer_delteform1: Could not delete value %s: %s\n",
3428                           wvalue.name, nt_errstr(status)));
3429                 if (!W_ERROR_IS_OK(result)) {
3430                         goto done;
3431                 }
3432                 result = ntstatus_to_werror(status);
3433                 goto done;
3434         }
3435
3436         result = WERR_OK;
3437 done:
3438         if (winreg_pipe != NULL) {
3439                 if (is_valid_policy_hnd(&key_hnd)) {
3440                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3441                 }
3442                 if (is_valid_policy_hnd(&hive_hnd)) {
3443                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3444                 }
3445         }
3446
3447         TALLOC_FREE(tmp_ctx);
3448         return result;
3449 }
3450
3451 WERROR winreg_printer_setform1(TALLOC_CTX *mem_ctx,
3452                                struct auth_serversupplied_info *server_info,
3453                                const char *form_name,
3454                                struct spoolss_AddFormInfo1 *form)
3455 {
3456         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3457         struct rpc_pipe_client *winreg_pipe = NULL;
3458         struct policy_handle hive_hnd, key_hnd;
3459         struct winreg_String wvalue;
3460         DATA_BLOB blob;
3461         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3462         uint32_t i;
3463         WERROR result;
3464         NTSTATUS status;
3465         TALLOC_CTX *tmp_ctx = NULL;
3466
3467         for (i = 0; i < num_builtin; i++) {
3468                 if (strequal(builtin_forms1[i].form_name, form->form_name)) {
3469                         result = WERR_INVALID_PARAM;
3470                         goto done;
3471                 }
3472         }
3473
3474         tmp_ctx = talloc_new(mem_ctx);
3475         if (tmp_ctx == NULL) {
3476                 return WERR_NOMEM;
3477         }
3478
3479         ZERO_STRUCT(hive_hnd);
3480         ZERO_STRUCT(key_hnd);
3481
3482         result = winreg_printer_openkey(tmp_ctx,
3483                                         server_info,
3484                                         &winreg_pipe,
3485                                         TOP_LEVEL_CONTROL_FORMS_KEY,
3486                                         "",
3487                                         true,
3488                                         access_mask,
3489                                         &hive_hnd,
3490                                         &key_hnd);
3491         if (!W_ERROR_IS_OK(result)) {
3492                 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
3493                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3494                 goto done;
3495         }
3496
3497         /* If form_name != form->form_name then we renamed the form */
3498         if (strequal(form_name, form->form_name)) {
3499                 result = winreg_printer_deleteform1(tmp_ctx, server_info, form_name);
3500                 if (!W_ERROR_IS_OK(result)) {
3501                         DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
3502                                   TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3503                         goto done;
3504                 }
3505         }
3506
3507         wvalue.name = form->form_name;
3508
3509         blob = data_blob_talloc(tmp_ctx, NULL, 32);
3510         SIVAL(blob.data,  0, form->size.width);
3511         SIVAL(blob.data,  4, form->size.height);
3512         SIVAL(blob.data,  8, form->area.left);
3513         SIVAL(blob.data, 12, form->area.top);
3514         SIVAL(blob.data, 16, form->area.right);
3515         SIVAL(blob.data, 20, form->area.bottom);
3516         SIVAL(blob.data, 24, 42);
3517         SIVAL(blob.data, 28, form->flags);
3518
3519         status = rpccli_winreg_SetValue(winreg_pipe,
3520                                         tmp_ctx,
3521                                         &key_hnd,
3522                                         wvalue,
3523                                         REG_BINARY,
3524                                         blob.data,
3525                                         blob.length,
3526                                         &result);
3527         if (!NT_STATUS_IS_OK(status)) {
3528                 DEBUG(0, ("winreg_printer_setform1: Could not set value %s: %s\n",
3529                           wvalue.name, nt_errstr(status)));
3530                 if (!W_ERROR_IS_OK(result)) {
3531                         goto done;
3532                 }
3533                 result = ntstatus_to_werror(status);
3534                 goto done;
3535         }
3536
3537         result = WERR_OK;
3538 done:
3539         if (winreg_pipe != NULL) {
3540                 if (is_valid_policy_hnd(&key_hnd)) {
3541                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3542                 }
3543                 if (is_valid_policy_hnd(&hive_hnd)) {
3544                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3545                 }
3546         }
3547
3548         TALLOC_FREE(tmp_ctx);
3549         return result;
3550 }
3551
3552 WERROR winreg_printer_getform1(TALLOC_CTX *mem_ctx,
3553                                struct auth_serversupplied_info *server_info,
3554                                const char *form_name,
3555                                struct spoolss_FormInfo1 *r)
3556 {
3557         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3558         struct rpc_pipe_client *winreg_pipe = NULL;
3559         struct policy_handle hive_hnd, key_hnd;
3560         struct winreg_String wvalue;
3561         enum winreg_Type type_in;
3562         uint8_t *data_in;
3563         uint32_t data_in_size = 0;
3564         uint32_t value_len = 0;
3565         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3566         uint32_t i;
3567         WERROR result;
3568         NTSTATUS status;
3569         TALLOC_CTX *tmp_ctx;
3570
3571         /* check builtin forms first */
3572         for (i = 0; i < num_builtin; i++) {
3573                 if (strequal(builtin_forms1[i].form_name, form_name)) {
3574                         *r = builtin_forms1[i];
3575                         return WERR_OK;
3576                 }
3577         }
3578
3579         tmp_ctx = talloc_new(mem_ctx);
3580         if (tmp_ctx == NULL) {
3581                 return WERR_NOMEM;
3582         }
3583
3584         ZERO_STRUCT(hive_hnd);
3585         ZERO_STRUCT(key_hnd);
3586
3587         result = winreg_printer_openkey(tmp_ctx,
3588                                         server_info,
3589                                         &winreg_pipe,
3590                                         TOP_LEVEL_CONTROL_FORMS_KEY,
3591                                         "",
3592                                         true,
3593                                         access_mask,
3594                                         &hive_hnd,
3595                                         &key_hnd);
3596         if (!W_ERROR_IS_OK(result)) {
3597                 DEBUG(0, ("winreg_printer_getform1: Could not open key %s: %s\n",
3598                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3599                 goto done;
3600         }
3601
3602         wvalue.name = form_name;
3603
3604         /*
3605          * call QueryValue once with data == NULL to get the
3606          * needed memory size to be allocated, then allocate
3607          * data buffer and call again.
3608          */
3609         status = rpccli_winreg_QueryValue(winreg_pipe,
3610                                           tmp_ctx,
3611                                           &key_hnd,
3612                                           &wvalue,
3613                                           &type_in,
3614                                           NULL,
3615                                           &data_in_size,
3616                                           &value_len,
3617                                           &result);
3618         if (!NT_STATUS_IS_OK(status)) {
3619                 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3620                           wvalue.name, nt_errstr(status)));
3621                 if (!W_ERROR_IS_OK(result)) {
3622                         goto done;
3623                 }
3624                 result = ntstatus_to_werror(status);
3625                 goto done;
3626         }
3627
3628         data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
3629         if (data_in == NULL) {
3630                 result = WERR_NOMEM;
3631                 goto done;
3632         }
3633         value_len = 0;
3634
3635         status = rpccli_winreg_QueryValue(winreg_pipe,
3636                                           tmp_ctx,
3637                                           &key_hnd,
3638                                           &wvalue,
3639                                           &type_in,
3640                                           data_in,
3641                                           &data_in_size,
3642                                           &value_len,
3643                                           &result);
3644         if (!NT_STATUS_IS_OK(status)) {
3645                 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3646                           wvalue.name, nt_errstr(status)));
3647                 if (!W_ERROR_IS_OK(result)) {
3648                         goto done;
3649                 }
3650                 result = ntstatus_to_werror(status);
3651                 goto done;
3652         }
3653
3654         r->form_name = talloc_strdup(mem_ctx, form_name);
3655         if (r->form_name == NULL) {
3656                 result = WERR_NOMEM;
3657                 goto done;
3658         }
3659
3660         r->size.width  = IVAL(data_in,  0);
3661         r->size.height = IVAL(data_in,  4);
3662         r->area.left   = IVAL(data_in,  8);
3663         r->area.top    = IVAL(data_in, 12);
3664         r->area.right  = IVAL(data_in, 16);
3665         r->area.bottom = IVAL(data_in, 20);
3666         /* skip index    IVAL(data_in, 24)));*/
3667         r->flags       = IVAL(data_in, 28);
3668
3669         result = WERR_OK;
3670 done:
3671         if (winreg_pipe != NULL) {
3672                 if (is_valid_policy_hnd(&key_hnd)) {
3673                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3674                 }
3675                 if (is_valid_policy_hnd(&hive_hnd)) {
3676                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3677                 }
3678         }
3679
3680         TALLOC_FREE(tmp_ctx);
3681         return result;
3682 }
3683
3684 WERROR winreg_add_driver(TALLOC_CTX *mem_ctx,
3685                          struct auth_serversupplied_info *server_info,
3686                          struct spoolss_AddDriverInfoCtr *r,
3687                          const char **driver_name,
3688                          uint32_t *driver_version)
3689 {
3690         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3691         struct rpc_pipe_client *winreg_pipe = NULL;
3692         struct policy_handle hive_hnd, key_hnd;
3693         struct spoolss_DriverInfo8 info8;
3694         TALLOC_CTX *tmp_ctx = NULL;
3695         WERROR result;
3696
3697         ZERO_STRUCT(hive_hnd);
3698         ZERO_STRUCT(key_hnd);
3699         ZERO_STRUCT(info8);
3700
3701         if (!driver_info_ctr_to_info8(r, &info8)) {
3702                 result = WERR_INVALID_PARAMETER;
3703                 goto done;
3704         }
3705
3706         tmp_ctx = talloc_new(mem_ctx);
3707         if (tmp_ctx == NULL) {
3708                 return WERR_NOMEM;
3709         }
3710
3711         result = winreg_printer_opendriver(tmp_ctx,
3712                                            server_info,
3713                                            info8.driver_name,
3714                                            info8.architecture,
3715                                            info8.version,
3716                                            access_mask, true,
3717                                            &winreg_pipe,
3718                                            &hive_hnd,
3719                                            &key_hnd);
3720         if (!W_ERROR_IS_OK(result)) {
3721                 DEBUG(0, ("winreg_add_driver: "
3722                           "Could not open driver key (%s,%s,%d): %s\n",
3723                           info8.driver_name, info8.architecture,
3724                           info8.version, win_errstr(result)));
3725                 goto done;
3726         }
3727
3728         /* TODO: "Attributes" ? */
3729
3730         result = winreg_printer_write_dword(tmp_ctx, winreg_pipe,
3731                                             &key_hnd, "Version",
3732                                             info8.version);
3733         if (!W_ERROR_IS_OK(result)) {
3734                 goto done;
3735         }
3736
3737         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3738                                          &key_hnd, "Driver",
3739                                          info8.driver_path);
3740         if (!W_ERROR_IS_OK(result)) {
3741                 goto done;
3742         }
3743
3744         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3745                                          &key_hnd, "Data File",
3746                                          info8.data_file);
3747         if (!W_ERROR_IS_OK(result)) {
3748                 goto done;
3749         }
3750
3751         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3752                                          &key_hnd, "Configuration File",
3753                                          info8.config_file);
3754         if (!W_ERROR_IS_OK(result)) {
3755                 goto done;
3756         }
3757
3758         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3759                                          &key_hnd, "Help File",
3760                                          info8.help_file);
3761         if (!W_ERROR_IS_OK(result)) {
3762                 goto done;
3763         }
3764
3765         result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
3766                                                &key_hnd, "Dependent Files",
3767                                                info8.dependent_files);
3768         if (!W_ERROR_IS_OK(result)) {
3769                 goto done;
3770         }
3771
3772         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3773                                          &key_hnd, "Monitor",
3774                                          info8.monitor_name);
3775         if (!W_ERROR_IS_OK(result)) {
3776                 goto done;
3777         }
3778
3779         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3780                                          &key_hnd, "Datatype",
3781                                          info8.default_datatype);
3782         if (!W_ERROR_IS_OK(result)) {
3783                 goto done;
3784         }
3785
3786         result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
3787                                                &key_hnd, "Previous Names",
3788                                                info8.previous_names);
3789         if (!W_ERROR_IS_OK(result)) {
3790                 goto done;
3791         }
3792
3793         result = winreg_printer_write_date(tmp_ctx, winreg_pipe,
3794                                            &key_hnd, "DriverDate",
3795                                            info8.driver_date);
3796         if (!W_ERROR_IS_OK(result)) {
3797                 goto done;
3798         }
3799
3800         result = winreg_printer_write_ver(tmp_ctx, winreg_pipe,
3801                                           &key_hnd, "DriverVersion",
3802                                           info8.driver_version);
3803         if (!W_ERROR_IS_OK(result)) {
3804                 goto done;
3805         }
3806
3807         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3808                                          &key_hnd, "Manufacturer",
3809                                          info8.manufacturer_name);
3810         if (!W_ERROR_IS_OK(result)) {
3811                 goto done;
3812         }
3813
3814         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3815                                          &key_hnd, "OEM URL",
3816                                          info8.manufacturer_url);
3817         if (!W_ERROR_IS_OK(result)) {
3818                 goto done;
3819         }
3820
3821         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3822                                          &key_hnd, "HardwareID",
3823                                          info8.hardware_id);
3824         if (!W_ERROR_IS_OK(result)) {
3825                 goto done;
3826         }
3827
3828         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3829                                          &key_hnd, "Provider",
3830                                          info8.provider);
3831         if (!W_ERROR_IS_OK(result)) {
3832                 goto done;
3833         }
3834
3835         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3836                                          &key_hnd, "Print Processor",
3837                                          info8.print_processor);
3838         if (!W_ERROR_IS_OK(result)) {
3839                 goto done;
3840         }
3841
3842         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3843                                          &key_hnd, "VendorSetup",
3844                                          info8.vendor_setup);
3845         if (!W_ERROR_IS_OK(result)) {
3846                 goto done;
3847         }
3848
3849         result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
3850                                                &key_hnd, "Color Profiles",
3851                                                info8.color_profiles);
3852         if (!W_ERROR_IS_OK(result)) {
3853                 goto done;
3854         }
3855
3856         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3857                                          &key_hnd, "InfPath",
3858                                          info8.inf_path);
3859         if (!W_ERROR_IS_OK(result)) {
3860                 goto done;
3861         }
3862
3863         result = winreg_printer_write_dword(tmp_ctx, winreg_pipe, &key_hnd,
3864                                             "PrinterDriverAttributes",
3865                                             info8.printer_driver_attributes);
3866         if (!W_ERROR_IS_OK(result)) {
3867                 goto done;
3868         }
3869
3870         result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
3871                                                &key_hnd, "CoreDependencies",
3872                                                info8.core_driver_dependencies);
3873         if (!W_ERROR_IS_OK(result)) {
3874                 goto done;
3875         }
3876
3877         result = winreg_printer_write_date(tmp_ctx, winreg_pipe,
3878                                            &key_hnd, "MinInboxDriverVerDate",
3879                                            info8.min_inbox_driver_ver_date);
3880         if (!W_ERROR_IS_OK(result)) {
3881                 goto done;
3882         }
3883
3884         result = winreg_printer_write_ver(tmp_ctx, winreg_pipe, &key_hnd,
3885                                           "MinInboxDriverVerVersion",
3886                                           info8.min_inbox_driver_ver_version);
3887         if (!W_ERROR_IS_OK(result)) {
3888                 goto done;
3889         }
3890
3891         *driver_name = info8.driver_name;
3892         *driver_version = info8.version;
3893         result = WERR_OK;
3894 done:
3895         if (winreg_pipe != NULL) {
3896                 if (is_valid_policy_hnd(&key_hnd)) {
3897                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3898                 }
3899                 if (is_valid_policy_hnd(&hive_hnd)) {
3900                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3901                 }
3902         }
3903
3904         TALLOC_FREE(tmp_ctx);
3905         return result;
3906 }
3907
3908 WERROR winreg_get_driver(TALLOC_CTX *mem_ctx,
3909                          struct auth_serversupplied_info *server_info,
3910                          const char *architecture,
3911                          const char *driver_name,
3912                          uint32_t driver_version,
3913                          struct spoolss_DriverInfo8 **_info8)
3914 {
3915         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3916         struct rpc_pipe_client *winreg_pipe = NULL;
3917         struct policy_handle hive_hnd, key_hnd;
3918         struct spoolss_DriverInfo8 i8, *info8;
3919         struct spoolss_PrinterEnumValues *enum_values = NULL;
3920         struct spoolss_PrinterEnumValues *v;
3921         uint32_t num_values = 0;
3922         TALLOC_CTX *tmp_ctx;
3923         WERROR result;
3924         uint32_t i;
3925
3926         ZERO_STRUCT(hive_hnd);
3927         ZERO_STRUCT(key_hnd);
3928         ZERO_STRUCT(i8);
3929
3930         tmp_ctx = talloc_new(mem_ctx);
3931         if (tmp_ctx == NULL) {
3932                 return WERR_NOMEM;
3933         }
3934
3935         if (driver_version == DRIVER_ANY_VERSION) {
3936                 /* look for Win2k first and then for NT4 */
3937                 result = winreg_printer_opendriver(tmp_ctx,
3938                                                    server_info,
3939                                                    driver_name,
3940                                                    architecture,
3941                                                    3,
3942                                                    access_mask, false,
3943                                                    &winreg_pipe,
3944                                                    &hive_hnd,
3945                                                    &key_hnd);
3946                 if (!W_ERROR_IS_OK(result)) {
3947                         result = winreg_printer_opendriver(tmp_ctx,
3948                                                            server_info,
3949                                                            driver_name,
3950                                                            architecture,
3951                                                            2,
3952                                                            access_mask, false,
3953                                                            &winreg_pipe,
3954                                                            &hive_hnd,
3955                                                            &key_hnd);
3956                 }
3957         } else {
3958                 /* ok normal case */
3959                 result = winreg_printer_opendriver(tmp_ctx,
3960                                                    server_info,
3961                                                    driver_name,
3962                                                    architecture,
3963                                                    driver_version,
3964                                                    access_mask, false,
3965                                                    &winreg_pipe,
3966                                                    &hive_hnd,
3967                                                    &key_hnd);
3968         }
3969         if (!W_ERROR_IS_OK(result)) {
3970                 DEBUG(5, ("winreg_get_driver: "
3971                           "Could not open driver key (%s,%s,%d): %s\n",
3972                           driver_name, architecture,
3973                           driver_version, win_errstr(result)));
3974                 goto done;
3975         }
3976
3977         result = winreg_printer_enumvalues(tmp_ctx,
3978                                            winreg_pipe,
3979                                            &key_hnd,
3980                                            &num_values,
3981                                            &enum_values);
3982         if (!W_ERROR_IS_OK(result)) {
3983                 DEBUG(0, ("winreg_get_driver: "
3984                           "Could not enumerate values for (%s,%s,%d): %s\n",
3985                           driver_name, architecture,
3986                           driver_version, win_errstr(result)));
3987                 goto done;
3988         }
3989
3990         info8 = talloc_zero(tmp_ctx, struct spoolss_DriverInfo8);
3991         if (info8 == NULL) {
3992                 result = WERR_NOMEM;
3993                 goto done;
3994         }
3995
3996         info8->driver_name = talloc_strdup(info8, driver_name);
3997         if (info8->driver_name == NULL) {
3998                 result = WERR_NOMEM;
3999                 goto done;
4000         }
4001
4002         info8->architecture = talloc_strdup(info8, architecture);
4003         if (info8->architecture == NULL) {
4004                 result = WERR_NOMEM;
4005                 goto done;
4006         }
4007
4008         info8->config_file = EMPTY_STRING;
4009         info8->data_file = EMPTY_STRING;
4010         info8->default_datatype = EMPTY_STRING;
4011         info8->driver_path = EMPTY_STRING;
4012         info8->hardware_id = EMPTY_STRING;
4013         info8->help_file = EMPTY_STRING;
4014         info8->inf_path = EMPTY_STRING;
4015         info8->manufacturer_name = EMPTY_STRING;
4016         info8->manufacturer_url = EMPTY_STRING;
4017         info8->monitor_name = EMPTY_STRING;
4018         info8->print_processor = EMPTY_STRING;
4019         info8->provider = EMPTY_STRING;
4020         info8->vendor_setup = EMPTY_STRING;
4021
4022         info8->color_profiles = empty_string_array;
4023         info8->core_driver_dependencies = EMPTY_STRING_ARRAY;
4024         info8->dependent_files = EMPTY_STRING_ARRAY;
4025         info8->previous_names = EMPTY_STRING_ARRAY;
4026
4027         result = WERR_OK;
4028
4029         for (i = 0; i < num_values; i++) {
4030                 const char *tmp_str;
4031
4032                 v = &enum_values[i];
4033
4034                 result = winreg_enumval_to_dword(info8, v,
4035                                                  "Version",
4036                                                  &info8->version);
4037                 CHECK_ERROR(result);
4038
4039                 result = winreg_enumval_to_sz(info8, v,
4040                                               "Driver",
4041                                               &info8->driver_path);
4042                 CHECK_ERROR(result);
4043
4044                 result = winreg_enumval_to_sz(info8, v,
4045                                               "Data File",
4046                                               &info8->data_file);
4047                 CHECK_ERROR(result);
4048
4049                 result = winreg_enumval_to_sz(info8, v,
4050                                               "Configuration File",
4051                                               &info8->config_file);
4052                 CHECK_ERROR(result);
4053
4054                 result = winreg_enumval_to_sz(info8, v,
4055                                               "Help File",
4056                                               &info8->help_file);
4057                 CHECK_ERROR(result);
4058
4059                 result = winreg_enumval_to_multi_sz(info8, v,
4060                                                     "Dependent Files",
4061                                                     &info8->dependent_files);
4062                 CHECK_ERROR(result);
4063
4064                 result = winreg_enumval_to_sz(info8, v,
4065                                               "Monitor",
4066                                               &info8->monitor_name);
4067                 CHECK_ERROR(result);
4068
4069                 result = winreg_enumval_to_sz(info8, v,
4070                                               "Datatype",
4071                                               &info8->default_datatype);
4072                 CHECK_ERROR(result);
4073
4074                 result = winreg_enumval_to_multi_sz(info8, v,
4075                                                     "Previous Names",
4076                                                     &info8->previous_names);
4077                 CHECK_ERROR(result);
4078
4079                 result = winreg_enumval_to_sz(info8, v,
4080                                               "DriverDate",
4081                                               &tmp_str);
4082                 if (W_ERROR_IS_OK(result)) {
4083                         result = winreg_printer_date_to_NTTIME(tmp_str,
4084                                                 &info8->driver_date);
4085                 }
4086                 CHECK_ERROR(result);
4087
4088                 result = winreg_enumval_to_sz(info8, v,
4089                                               "DriverVersion",
4090                                               &tmp_str);
4091                 if (W_ERROR_IS_OK(result)) {
4092                         result = winreg_printer_ver_to_dword(tmp_str,
4093                                                 &info8->driver_version);
4094                 }
4095                 CHECK_ERROR(result);
4096
4097                 result = winreg_enumval_to_sz(info8, v,
4098                                               "Manufacturer",
4099                                               &info8->manufacturer_name);
4100                 CHECK_ERROR(result);
4101
4102                 result = winreg_enumval_to_sz(info8, v,
4103                                               "OEM URL",
4104                                               &info8->manufacturer_url);
4105                 CHECK_ERROR(result);
4106
4107                 result = winreg_enumval_to_sz(info8, v,
4108                                               "HardwareID",
4109                                               &info8->hardware_id);
4110                 CHECK_ERROR(result);
4111
4112                 result = winreg_enumval_to_sz(info8, v,
4113                                               "Provider",
4114                                               &info8->provider);
4115                 CHECK_ERROR(result);
4116
4117                 result = winreg_enumval_to_sz(info8, v,
4118                                               "Print Processor",
4119                                               &info8->print_processor);
4120                 CHECK_ERROR(result);
4121
4122                 result = winreg_enumval_to_sz(info8, v,
4123                                               "VendorSetup",
4124                                               &info8->vendor_setup);
4125                 CHECK_ERROR(result);
4126
4127                 result = winreg_enumval_to_multi_sz(info8, v,
4128                                                     "Color Profiles",
4129                                                     &info8->color_profiles);
4130                 CHECK_ERROR(result);
4131
4132                 result = winreg_enumval_to_sz(info8, v,
4133                                               "InfPath",
4134                                               &info8->inf_path);
4135                 CHECK_ERROR(result);
4136
4137                 result = winreg_enumval_to_dword(info8, v,
4138                                                  "PrinterDriverAttributes",
4139                                                  &info8->printer_driver_attributes);
4140                 CHECK_ERROR(result);
4141
4142                 result = winreg_enumval_to_multi_sz(info8, v,
4143                                                     "CoreDependencies",
4144                                                     &info8->core_driver_dependencies);
4145                 CHECK_ERROR(result);
4146
4147                 result = winreg_enumval_to_sz(info8, v,
4148                                               "MinInboxDriverVerDate",
4149                                               &tmp_str);
4150                 if (W_ERROR_IS_OK(result)) {
4151                         result = winreg_printer_date_to_NTTIME(tmp_str,
4152                                         &info8->min_inbox_driver_ver_date);
4153                 }
4154                 CHECK_ERROR(result);
4155
4156                 result = winreg_enumval_to_sz(info8, v,
4157                                               "MinInboxDriverVerVersion",
4158                                               &tmp_str);
4159                 if (W_ERROR_IS_OK(result)) {
4160                         result = winreg_printer_ver_to_dword(tmp_str,
4161                                         &info8->min_inbox_driver_ver_version);
4162                 }
4163                 CHECK_ERROR(result);
4164         }
4165
4166         if (!W_ERROR_IS_OK(result)) {
4167                 DEBUG(0, ("winreg_enumval_to_TYPE() failed "
4168                           "for %s: %s\n", v->value_name,
4169                           win_errstr(result)));
4170                 goto done;
4171         }
4172
4173         *_info8 = talloc_steal(mem_ctx, info8);
4174         result = WERR_OK;
4175 done:
4176         if (winreg_pipe != NULL) {
4177                 if (is_valid_policy_hnd(&key_hnd)) {
4178                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
4179                 }
4180                 if (is_valid_policy_hnd(&hive_hnd)) {
4181                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
4182                 }
4183         }
4184
4185         TALLOC_FREE(tmp_ctx);
4186         return result;
4187 }
4188
4189 WERROR winreg_del_driver(TALLOC_CTX *mem_ctx,
4190                          struct auth_serversupplied_info *server_info,
4191                          struct spoolss_DriverInfo8 *info8,
4192                          uint32_t version)
4193 {
4194         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
4195         struct rpc_pipe_client *winreg_pipe = NULL;
4196         struct policy_handle hive_hnd, key_hnd;
4197         TALLOC_CTX *tmp_ctx;
4198         char *key_name;
4199         WERROR result;
4200
4201         ZERO_STRUCT(hive_hnd);
4202         ZERO_STRUCT(key_hnd);
4203
4204         tmp_ctx = talloc_new(mem_ctx);
4205         if (tmp_ctx == NULL) {
4206                 return WERR_NOMEM;
4207         }
4208
4209         /* test that the key exists */
4210         result = winreg_printer_opendriver(tmp_ctx,
4211                                            server_info,
4212                                            info8->driver_name,
4213                                            info8->architecture,
4214                                            version,
4215                                            access_mask, false,
4216                                            &winreg_pipe,
4217                                            &hive_hnd,
4218                                            &key_hnd);
4219         if (!W_ERROR_IS_OK(result)) {
4220                 /* key doesn't exist */
4221                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
4222                         result = WERR_OK;
4223                         goto done;
4224                 }
4225
4226                 DEBUG(5, ("winreg_del_driver: "
4227                           "Could not open driver (%s,%s,%u): %s\n",
4228                           info8->driver_name, info8->architecture,
4229                           version, win_errstr(result)));
4230                 goto done;
4231         }
4232
4233
4234         if (is_valid_policy_hnd(&key_hnd)) {
4235                 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
4236         }
4237
4238         key_name = talloc_asprintf(tmp_ctx,
4239                                    "%s\\Environments\\%s\\Drivers\\Version-%u",
4240                                    TOP_LEVEL_CONTROL_KEY,
4241                                    info8->architecture, version);
4242         if (key_name == NULL) {
4243                 result = WERR_NOMEM;
4244                 goto done;
4245         }
4246
4247         result = winreg_printer_delete_subkeys(tmp_ctx,
4248                                                winreg_pipe,
4249                                                &hive_hnd,
4250                                                access_mask,
4251                                                key_name);
4252         if (!W_ERROR_IS_OK(result)) {
4253                 DEBUG(0, ("winreg_del_driver: "
4254                           "Could not open driver (%s,%s,%u): %s\n",
4255                           info8->driver_name, info8->architecture,
4256                           version, win_errstr(result)));
4257                 goto done;
4258         }
4259
4260         result = WERR_OK;
4261 done:
4262         if (winreg_pipe != NULL) {
4263                 if (is_valid_policy_hnd(&key_hnd)) {
4264                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
4265                 }
4266                 if (is_valid_policy_hnd(&hive_hnd)) {
4267                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
4268                 }
4269         }
4270
4271         TALLOC_FREE(tmp_ctx);
4272         return result;
4273 }
4274
4275 WERROR winreg_get_driver_list(TALLOC_CTX *mem_ctx,
4276                               struct auth_serversupplied_info *server_info,
4277                               const char *architecture,
4278                               uint32_t version,
4279                               uint32_t *num_drivers,
4280                               const char ***drivers_p)
4281 {
4282         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
4283         struct rpc_pipe_client *winreg_pipe = NULL;
4284         struct policy_handle hive_hnd, key_hnd;
4285         const char **drivers;
4286         TALLOC_CTX *tmp_ctx;
4287         WERROR result;
4288
4289         *num_drivers = 0;
4290         *drivers_p = NULL;
4291
4292         ZERO_STRUCT(hive_hnd);
4293         ZERO_STRUCT(key_hnd);
4294
4295         tmp_ctx = talloc_new(mem_ctx);
4296         if (tmp_ctx == NULL) {
4297                 return WERR_NOMEM;
4298         }
4299
4300         /* use NULL for the driver name so we open the key that is
4301          * parent of all drivers for this architecture and version */
4302         result = winreg_printer_opendriver(tmp_ctx,
4303                                            server_info,
4304                                            NULL,
4305                                            architecture,
4306                                            version,
4307                                            access_mask, false,
4308                                            &winreg_pipe,
4309                                            &hive_hnd,
4310                                            &key_hnd);
4311         if (!W_ERROR_IS_OK(result)) {
4312                 DEBUG(5, ("winreg_get_driver_list: "
4313                           "Could not open key (%s,%u): %s\n",
4314                           architecture, version, win_errstr(result)));
4315                 result = WERR_OK;
4316                 goto done;
4317         }
4318
4319         result = winreg_printer_enumkeys(tmp_ctx,
4320                                          winreg_pipe,
4321                                          &key_hnd,
4322                                          num_drivers,
4323                                          &drivers);
4324         if (!W_ERROR_IS_OK(result)) {
4325                 DEBUG(0, ("winreg_get_driver_list: "
4326                           "Could not enumerate drivers for (%s,%u): %s\n",
4327                           architecture, version, win_errstr(result)));
4328                 goto done;
4329         }
4330
4331         *drivers_p = talloc_steal(mem_ctx, drivers);
4332
4333         result = WERR_OK;
4334 done:
4335         if (winreg_pipe != NULL) {
4336                 if (is_valid_policy_hnd(&key_hnd)) {
4337                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
4338                 }
4339                 if (is_valid_policy_hnd(&hive_hnd)) {
4340                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
4341                 }
4342         }
4343
4344         TALLOC_FREE(tmp_ctx);
4345         return result;
4346 }
4347