s3-spoolss: Added a winreg_del_driver function.
[kamenim/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 /*        FLAGS,                NAME,                              with,   height,   left, top, right, bottom */
33 static const struct spoolss_FormInfo1 builtin_forms1[] = {
34         { SPOOLSS_FORM_BUILTIN, "Letter",                         {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
35         { SPOOLSS_FORM_BUILTIN, "Letter Small",                   {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
36         { SPOOLSS_FORM_BUILTIN, "Tabloid",                        {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
37         { SPOOLSS_FORM_BUILTIN, "Ledger",                         {0x696b8,0x44368}, {0x0,0x0,0x696b8,0x44368} },
38         { SPOOLSS_FORM_BUILTIN, "Legal",                          {0x34b5c,0x56d10}, {0x0,0x0,0x34b5c,0x56d10} },
39         { SPOOLSS_FORM_BUILTIN, "Statement",                      {0x221b4,0x34b5c}, {0x0,0x0,0x221b4,0x34b5c} },
40         { SPOOLSS_FORM_BUILTIN, "Executive",                      {0x2cf56,0x411cc}, {0x0,0x0,0x2cf56,0x411cc} },
41         { SPOOLSS_FORM_BUILTIN, "A3",                             {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
42         { SPOOLSS_FORM_BUILTIN, "A4",                             {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
43         { SPOOLSS_FORM_BUILTIN, "A4 Small",                       {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
44         { SPOOLSS_FORM_BUILTIN, "A5",                             {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
45         { SPOOLSS_FORM_BUILTIN, "B4 (JIS)",                       {0x3ebe8,0x58de0}, {0x0,0x0,0x3ebe8,0x58de0} },
46         { SPOOLSS_FORM_BUILTIN, "B5 (JIS)",                       {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
47         { SPOOLSS_FORM_BUILTIN, "Folio",                          {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
48         { SPOOLSS_FORM_BUILTIN, "Quarto",                         {0x347d8,0x43238}, {0x0,0x0,0x347d8,0x43238} },
49         { SPOOLSS_FORM_BUILTIN, "10x14",                          {0x3e030,0x56d10}, {0x0,0x0,0x3e030,0x56d10} },
50         { SPOOLSS_FORM_BUILTIN, "11x17",                          {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
51         { SPOOLSS_FORM_BUILTIN, "Note",                           {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
52         { SPOOLSS_FORM_BUILTIN, "Envelope #9",                    {0x18079,0x37091}, {0x0,0x0,0x18079,0x37091} },
53         { SPOOLSS_FORM_BUILTIN, "Envelope #10",                   {0x19947,0x3ae94}, {0x0,0x0,0x19947,0x3ae94} },
54         { SPOOLSS_FORM_BUILTIN, "Envelope #11",                   {0x1be7c,0x40565}, {0x0,0x0,0x1be7c,0x40565} },
55         { SPOOLSS_FORM_BUILTIN, "Envelope #12",                   {0x1d74a,0x44368}, {0x0,0x0,0x1d74a,0x44368} },
56         { SPOOLSS_FORM_BUILTIN, "Envelope #14",                   {0x1f018,0x47504}, {0x0,0x0,0x1f018,0x47504} },
57         { SPOOLSS_FORM_BUILTIN, "C size sheet",                   {0x696b8,0x886d0}, {0x0,0x0,0x696b8,0x886d0} },
58         { SPOOLSS_FORM_BUILTIN, "D size sheet",                   {0x886d0,0xd2d70}, {0x0,0x0,0x886d0,0xd2d70} },
59         { SPOOLSS_FORM_BUILTIN, "E size sheet",                   {0xd2d70,0x110da0},{0x0,0x0,0xd2d70,0x110da0} },
60         { SPOOLSS_FORM_BUILTIN, "Envelope DL",                    {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
61         { SPOOLSS_FORM_BUILTIN, "Envelope C5",                    {0x278d0,0x37e88}, {0x0,0x0,0x278d0,0x37e88} },
62         { SPOOLSS_FORM_BUILTIN, "Envelope C3",                    {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
63         { SPOOLSS_FORM_BUILTIN, "Envelope C4",                    {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
64         { SPOOLSS_FORM_BUILTIN, "Envelope C6",                    {0x1bd50,0x278d0}, {0x0,0x0,0x1bd50,0x278d0} },
65         { SPOOLSS_FORM_BUILTIN, "Envelope C65",                   {0x1bd50,0x37e88}, {0x0,0x0,0x1bd50,0x37e88} },
66         { SPOOLSS_FORM_BUILTIN, "Envelope B4",                    {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
67         { SPOOLSS_FORM_BUILTIN, "Envelope B5",                    {0x2af80,0x3d090}, {0x0,0x0,0x2af80,0x3d090} },
68         { SPOOLSS_FORM_BUILTIN, "Envelope B6",                    {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
69         { SPOOLSS_FORM_BUILTIN, "Envelope",                       {0x1adb0,0x38270}, {0x0,0x0,0x1adb0,0x38270} },
70         { SPOOLSS_FORM_BUILTIN, "Envelope Monarch",               {0x18079,0x2e824}, {0x0,0x0,0x18079,0x2e824} },
71         { SPOOLSS_FORM_BUILTIN, "6 3/4 Envelope",                 {0x167ab,0x284ec}, {0x0,0x0,0x167ab,0x284ec} },
72         { SPOOLSS_FORM_BUILTIN, "US Std Fanfold",                 {0x5c3e1,0x44368}, {0x0,0x0,0x5c3e1,0x44368} },
73         { SPOOLSS_FORM_BUILTIN, "German Std Fanfold",             {0x34b5c,0x4a6a0}, {0x0,0x0,0x34b5c,0x4a6a0} },
74         { SPOOLSS_FORM_BUILTIN, "German Legal Fanfold",           {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
75         { SPOOLSS_FORM_BUILTIN, "B4 (ISO)",                       {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
76         { SPOOLSS_FORM_BUILTIN, "Japanese Postcard",              {0x186a0,0x24220}, {0x0,0x0,0x186a0,0x24220} },
77         { SPOOLSS_FORM_BUILTIN, "9x11",                           {0x37cf8,0x44368}, {0x0,0x0,0x37cf8,0x44368} },
78         { SPOOLSS_FORM_BUILTIN, "10x11",                          {0x3e030,0x44368}, {0x0,0x0,0x3e030,0x44368} },
79         { SPOOLSS_FORM_BUILTIN, "15x11",                          {0x5d048,0x44368}, {0x0,0x0,0x5d048,0x44368} },
80         { SPOOLSS_FORM_BUILTIN, "Envelope Invite",                {0x35b60,0x35b60}, {0x0,0x0,0x35b60,0x35b60} },
81         { SPOOLSS_FORM_BUILTIN, "Reserved48",                     {0x1,0x1},         {0x0,0x0,0x1,0x1} },
82         { SPOOLSS_FORM_BUILTIN, "Reserved49",                     {0x1,0x1},         {0x0,0x0,0x1,0x1} },
83         { SPOOLSS_FORM_BUILTIN, "Letter Extra",                   {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
84         { SPOOLSS_FORM_BUILTIN, "Legal Extra",                    {0x3ae94,0x5d048}, {0x0,0x0,0x3ae94,0x5d048} },
85         { SPOOLSS_FORM_BUILTIN, "Tabloid Extra",                  {0x4a6a0,0x6f9f0}, {0x0,0x0,0x4a6a0,0x6f9f0} },
86         { SPOOLSS_FORM_BUILTIN, "A4 Extra",                       {0x397c2,0x4eb16}, {0x0,0x0,0x397c2,0x4eb16} },
87         { SPOOLSS_FORM_BUILTIN, "Letter Transverse",              {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
88         { SPOOLSS_FORM_BUILTIN, "A4 Transverse",                  {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
89         { SPOOLSS_FORM_BUILTIN, "Letter Extra Transverse",        {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
90         { SPOOLSS_FORM_BUILTIN, "Super A",                        {0x376b8,0x56ea0}, {0x0,0x0,0x376b8,0x56ea0} },
91         { SPOOLSS_FORM_BUILTIN, "Super B",                        {0x4a768,0x76e58}, {0x0,0x0,0x4a768,0x76e58} },
92         { SPOOLSS_FORM_BUILTIN, "Letter Plus",                    {0x34b5c,0x4eb16}, {0x0,0x0,0x34b5c,0x4eb16} },
93         { SPOOLSS_FORM_BUILTIN, "A4 Plus",                        {0x33450,0x50910}, {0x0,0x0,0x33450,0x50910} },
94         { SPOOLSS_FORM_BUILTIN, "A5 Transverse",                  {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
95         { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Transverse",            {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
96         { SPOOLSS_FORM_BUILTIN, "A3 Extra",                       {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
97         { SPOOLSS_FORM_BUILTIN, "A5 Extra",                       {0x2a7b0,0x395f8}, {0x0,0x0,0x2a7b0,0x395f8} },
98         { SPOOLSS_FORM_BUILTIN, "B5 (ISO) Extra",                 {0x31128,0x43620}, {0x0,0x0,0x31128,0x43620} },
99         { SPOOLSS_FORM_BUILTIN, "A2",                             {0x668a0,0x91050}, {0x0,0x0,0x668a0,0x91050} },
100         { SPOOLSS_FORM_BUILTIN, "A3 Transverse",                  {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
101         { SPOOLSS_FORM_BUILTIN, "A3 Extra Transverse",            {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
102         { SPOOLSS_FORM_BUILTIN, "Japanese Double Postcard",       {0x30d40,0x24220}, {0x0,0x0,0x30d40,0x24220} },
103         { SPOOLSS_FORM_BUILTIN, "A6",                             {0x19a28,0x24220}, {0x0,0x0,0x19a28,0x24220} },
104         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #2",      {0x3a980,0x510e0}, {0x0,0x0,0x3a980,0x510e0} },
105         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #3",      {0x34bc0,0x43a08}, {0x0,0x0,0x34bc0,0x43a08} },
106         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #3",      {0x1d4c0,0x395f8}, {0x0,0x0,0x1d4c0,0x395f8} },
107         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #4",      {0x15f90,0x320c8}, {0x0,0x0,0x15f90,0x320c8} },
108         { SPOOLSS_FORM_BUILTIN, "Letter Rotated",                 {0x44368,0x34b5c}, {0x0,0x0,0x44368,0x34b5c} },
109         { SPOOLSS_FORM_BUILTIN, "A3 Rotated",                     {0x668a0,0x48828}, {0x0,0x0,0x668a0,0x48828} },
110         { SPOOLSS_FORM_BUILTIN, "A4 Rotated",                     {0x48828,0x33450}, {0x0,0x0,0x48828,0x33450} },
111         { SPOOLSS_FORM_BUILTIN, "A5 Rotated",                     {0x33450,0x24220}, {0x0,0x0,0x33450,0x24220} },
112         { SPOOLSS_FORM_BUILTIN, "B4 (JIS) Rotated",               {0x58de0,0x3ebe8}, {0x0,0x0,0x58de0,0x3ebe8} },
113         { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Rotated",               {0x3ebe8,0x2c6f0}, {0x0,0x0,0x3ebe8,0x2c6f0} },
114         { SPOOLSS_FORM_BUILTIN, "Japanese Postcard Rotated",      {0x24220,0x186a0}, {0x0,0x0,0x24220,0x186a0} },
115         { SPOOLSS_FORM_BUILTIN, "Double Japan Postcard Rotated",  {0x24220,0x30d40}, {0x0,0x0,0x24220,0x30d40} },
116         { SPOOLSS_FORM_BUILTIN, "A6 Rotated",                     {0x24220,0x19a28}, {0x0,0x0,0x24220,0x19a28} },
117         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #2 Rotated", {0x510e0,0x3a980}, {0x0,0x0,0x510e0,0x3a980} },
118         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #3 Rotated", {0x43a08,0x34bc0}, {0x0,0x0,0x43a08,0x34bc0} },
119         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #3 Rotated", {0x395f8,0x1d4c0}, {0x0,0x0,0x395f8,0x1d4c0} },
120         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #4 Rotated", {0x320c8,0x15f90}, {0x0,0x0,0x320c8,0x15f90} },
121         { SPOOLSS_FORM_BUILTIN, "B6 (JIS)",                       {0x1f400,0x2c6f0}, {0x0,0x0,0x1f400,0x2c6f0} },
122         { SPOOLSS_FORM_BUILTIN, "B6 (JIS) Rotated",               {0x2c6f0,0x1f400}, {0x0,0x0,0x2c6f0,0x1f400} },
123         { SPOOLSS_FORM_BUILTIN, "12x11",                          {0x4a724,0x443e1}, {0x0,0x0,0x4a724,0x443e1} },
124         { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4",          {0x19a28,0x395f8}, {0x0,0x0,0x19a28,0x395f8} },
125         { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4 Rotated",  {0x395f8,0x19a28}, {0x0,0x0,0x395f8,0x19a28} },
126         { SPOOLSS_FORM_BUILTIN, "PRC 16K",                        {0x2de60,0x3f7a0}, {0x0,0x0,0x2de60,0x3f7a0} },
127         { SPOOLSS_FORM_BUILTIN, "PRC 32K",                        {0x1fbd0,0x2cec0}, {0x0,0x0,0x1fbd0,0x2cec0} },
128         { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big)",                   {0x222e0,0x318f8}, {0x0,0x0,0x222e0,0x318f8} },
129         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1",                {0x18e70,0x28488}, {0x0,0x0,0x18e70,0x28488} },
130         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2",                {0x18e70,0x2af80}, {0x0,0x0,0x18e70,0x2af80} },
131         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3",                {0x1e848,0x2af80}, {0x0,0x0,0x1e848,0x2af80} },
132         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4",                {0x1adb0,0x32c80}, {0x0,0x0,0x1adb0,0x32c80} },
133         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5",                {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
134         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6",                {0x1d4c0,0x38270}, {0x0,0x0,0x1d4c0,0x38270} },
135         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7",                {0x27100,0x38270}, {0x0,0x0,0x27100,0x38270} },
136         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8",                {0x1d4c0,0x4b708}, {0x0,0x0,0x1d4c0,0x4b708} },
137         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9",                {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
138         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10",               {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
139         { SPOOLSS_FORM_BUILTIN, "PRC 16K Rotated",                {0x3f7a0,0x2de60}, {0x0,0x0,0x3f7a0,0x2de60} },
140         { SPOOLSS_FORM_BUILTIN, "PRC 32K Rotated",                {0x2cec0,0x1fbd0}, {0x0,0x0,0x2cec0,0x1fbd0} },
141         { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big) Rotated",           {0x318f8,0x222e0}, {0x0,0x0,0x318f8,0x222e0} },
142         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1 Rotated",        {0x28488,0x18e70}, {0x0,0x0,0x28488,0x18e70} },
143         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2 Rotated",        {0x2af80,0x18e70}, {0x0,0x0,0x2af80,0x18e70} },
144         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3 Rotated",        {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
145         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4 Rotated",        {0x32c80,0x1adb0}, {0x0,0x0,0x32c80,0x1adb0} },
146         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5 Rotated",        {0x35b60,0x1adb0}, {0x0,0x0,0x35b60,0x1adb0} },
147         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6 Rotated",        {0x38270,0x1d4c0}, {0x0,0x0,0x38270,0x1d4c0} },
148         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7 Rotated",        {0x38270,0x27100}, {0x0,0x0,0x38270,0x27100} },
149         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8 Rotated",        {0x4b708,0x1d4c0}, {0x0,0x0,0x4b708,0x1d4c0} },
150         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9 Rotated",        {0x4f1a0,0x37e88}, {0x0,0x0,0x4f1a0,0x37e88} },
151         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10 Rotated",       {0x6fd10,0x4f1a0}, {0x0,0x0,0x6fd10,0x4f1a0} }
152 };
153
154 /********************************************************************
155  static helper functions
156 ********************************************************************/
157
158 /****************************************************************************
159  Update the changeid time.
160 ****************************************************************************/
161 /**
162  * @internal
163  *
164  * @brief Update the ChangeID time of a printer.
165  *
166  * This is SO NASTY as some drivers need this to change, others need it
167  * static. This value will change every second, and I must hope that this
168  * is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
169  * UTAH ! JRA.
170  *
171  * @return              The ChangeID.
172  */
173 static uint32_t winreg_printer_rev_changeid(void)
174 {
175         struct timeval tv;
176
177         get_process_uptime(&tv);
178
179 #if 1   /* JERRY */
180         /* Return changeid as msec since spooler restart */
181         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
182 #else
183         /*
184          * This setting seems to work well but is too untested
185          * to replace the above calculation.  Left in for experiementation
186          * of the reader            --jerry (Tue Mar 12 09:15:05 CST 2002)
187          */
188         return tv.tv_sec * 10 + tv.tv_usec / 100000;
189 #endif
190 }
191
192 /**
193  * @internal
194  *
195  * @brief Connect to the interal winreg server and open the given printer key.
196  *
197  * The function will create the needed subkeys if they don't exist.
198  *
199  * @param[in]  mem_ctx       The memory context to use.
200  *
201  * @param[in]  server_info   The supplied server info.
202  *
203  * @param[out] winreg_pipe   A pointer for the winreg rpc client pipe.
204  *
205  * @param[in]  path          The path to the key to open.
206  *
207  * @param[in]  key           The key to open.
208  *
209  * @param[in]  create_key    Set to true if the key should be created if it
210  *                           doesn't exist.
211  *
212  * @param[in]  access_mask   The access mask to open the key.
213  *
214  * @param[out] hive_handle   A policy handle for the opened hive.
215  *
216  * @param[out] key_handle    A policy handle for the opened key.
217  *
218  * @return                   WERR_OK on success, the corresponding DOS error
219  *                           code if something gone wrong.
220  */
221 static WERROR winreg_printer_openkey(TALLOC_CTX *mem_ctx,
222                               struct auth_serversupplied_info *server_info,
223                               struct rpc_pipe_client **winreg_pipe,
224                               const char *path,
225                               const char *key,
226                               bool create_key,
227                               uint32_t access_mask,
228                               struct policy_handle *hive_handle,
229                               struct policy_handle *key_handle)
230 {
231         struct rpc_pipe_client *pipe_handle;
232         struct winreg_String wkey, wkeyclass;
233         char *keyname;
234         NTSTATUS status;
235         WERROR result = WERR_OK;
236
237         /* create winreg connection */
238         status = rpc_pipe_open_internal(mem_ctx,
239                                         &ndr_table_winreg.syntax_id,
240                                         rpc_winreg_dispatch,
241                                         server_info,
242                                         &pipe_handle);
243         if (!NT_STATUS_IS_OK(status)) {
244                 DEBUG(0, ("winreg_printer_openkey: Could not connect to winreg_pipe: %s\n",
245                           nt_errstr(status)));
246                 return ntstatus_to_werror(status);
247         }
248
249         status = rpccli_winreg_OpenHKLM(pipe_handle,
250                                         mem_ctx,
251                                         NULL,
252                                         access_mask,
253                                         hive_handle,
254                                         &result);
255         if (!NT_STATUS_IS_OK(status)) {
256                 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
257                           nt_errstr(status)));
258                 talloc_free(pipe_handle);
259                 if (!W_ERROR_IS_OK(result)) {
260                         return result;
261                 }
262                 return ntstatus_to_werror(status);
263         }
264
265         if (key && *key) {
266                 keyname = talloc_asprintf(mem_ctx, "%s\\%s", path, key);
267         } else {
268                 keyname = talloc_strdup(mem_ctx, path);
269         }
270         if (keyname == NULL) {
271                 talloc_free(pipe_handle);
272                 return WERR_NOMEM;
273         }
274
275         ZERO_STRUCT(wkey);
276         wkey.name = keyname;
277
278         if (create_key) {
279                 enum winreg_CreateAction action = REG_ACTION_NONE;
280
281                 ZERO_STRUCT(wkeyclass);
282                 wkeyclass.name = "";
283
284                 status = rpccli_winreg_CreateKey(pipe_handle,
285                                                  mem_ctx,
286                                                  hive_handle,
287                                                  wkey,
288                                                  wkeyclass,
289                                                  0,
290                                                  access_mask,
291                                                  NULL,
292                                                  key_handle,
293                                                  &action,
294                                                  &result);
295                 switch (action) {
296                         case REG_ACTION_NONE:
297                                 DEBUG(8, ("winreg_printer_openkey:createkey did nothing -- huh?\n"));
298                                 break;
299                         case REG_CREATED_NEW_KEY:
300                                 DEBUG(8, ("winreg_printer_openkey: createkey created %s\n", keyname));
301                                 break;
302                         case REG_OPENED_EXISTING_KEY:
303                                 DEBUG(8, ("winreg_printer_openkey: createkey opened existing %s\n", keyname));
304                                 break;
305                 }
306         } else {
307                 status = rpccli_winreg_OpenKey(pipe_handle,
308                                                mem_ctx,
309                                                hive_handle,
310                                                wkey,
311                                                0,
312                                                access_mask,
313                                                key_handle,
314                                                &result);
315         }
316         if (!NT_STATUS_IS_OK(status)) {
317                 talloc_free(pipe_handle);
318                 if (!W_ERROR_IS_OK(result)) {
319                         return result;
320                 }
321                 return ntstatus_to_werror(status);
322         }
323
324         *winreg_pipe = pipe_handle;
325
326         return WERR_OK;
327 }
328
329 /**
330  * @brief Create the registry keyname for the given printer.
331  *
332  * @param[in]  mem_ctx  The memory context to use.
333  *
334  * @param[in]  printer  The name of the printer to get the registry key.
335  *
336  * @return     The registry key or NULL on error.
337  */
338 static char *winreg_printer_data_keyname(TALLOC_CTX *mem_ctx, const char *printer) {
339         return talloc_asprintf(mem_ctx, "%s\\%s", TOP_LEVEL_PRINT_PRINTERS_KEY, printer);
340 }
341
342 /**
343  * @internal
344  *
345  * @brief Enumerate values of an opened key handle and retrieve the data.
346  *
347  * @param[in]  mem_ctx  The memory context to use.
348  *
349  * @param[in]  pipe_handle The pipe handle for the rpc connection.
350  *
351  * @param[in]  key_hnd  The opened key handle.
352  *
353  * @param[out] pnum_values A pointer to store he number of values found.
354  *
355  * @param[out] pnum_values A pointer to store the number of values we found.
356  *
357  * @return                   WERR_OK on success, the corresponding DOS error
358  *                           code if something gone wrong.
359  */
360 static WERROR winreg_printer_enumvalues(TALLOC_CTX *mem_ctx,
361                                         struct rpc_pipe_client *pipe_handle,
362                                         struct policy_handle *key_hnd,
363                                         uint32_t *pnum_values,
364                                         struct spoolss_PrinterEnumValues **penum_values)
365 {
366         TALLOC_CTX *tmp_ctx;
367         uint32_t num_subkeys, max_subkeylen, max_classlen;
368         uint32_t num_values, max_valnamelen, max_valbufsize;
369         uint32_t secdescsize;
370         uint32_t i;
371         NTTIME last_changed_time;
372         struct winreg_String classname;
373
374         struct spoolss_PrinterEnumValues *enum_values;
375
376         WERROR result = WERR_OK;
377         NTSTATUS status;
378
379         tmp_ctx = talloc_new(mem_ctx);
380         if (tmp_ctx == NULL) {
381                 return WERR_NOMEM;
382         }
383
384         ZERO_STRUCT(classname);
385
386         status = rpccli_winreg_QueryInfoKey(pipe_handle,
387                                             tmp_ctx,
388                                             key_hnd,
389                                             &classname,
390                                             &num_subkeys,
391                                             &max_subkeylen,
392                                             &max_classlen,
393                                             &num_values,
394                                             &max_valnamelen,
395                                             &max_valbufsize,
396                                             &secdescsize,
397                                             &last_changed_time,
398                                             &result);
399         if (!NT_STATUS_IS_OK(status)) {
400                 DEBUG(0, ("winreg_printer_enumvalues: Could not query info: %s\n",
401                           nt_errstr(status)));
402                 if (!W_ERROR_IS_OK(result)) {
403                         goto error;
404                 }
405                 result = ntstatus_to_werror(status);
406                 goto error;
407         }
408
409         if (num_values == 0) {
410                 *pnum_values = 0;
411                 TALLOC_FREE(tmp_ctx);
412                 return WERR_OK;
413         }
414
415         enum_values = TALLOC_ARRAY(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
416         if (enum_values == NULL) {
417                 result = WERR_NOMEM;
418                 goto error;
419         }
420
421         for (i = 0; i < num_values; i++) {
422                 struct spoolss_PrinterEnumValues val;
423                 struct winreg_ValNameBuf name_buf;
424                 enum winreg_Type type = REG_NONE;
425                 uint8_t *data = NULL;
426                 uint32_t data_size;
427                 uint32_t length;
428                 char n = '\0';;
429
430                 name_buf.name = &n;
431                 name_buf.size = max_valnamelen + 2;
432                 name_buf.length = 0;
433
434                 data_size = max_valbufsize;
435                 data = (uint8_t *) TALLOC(tmp_ctx, data_size);
436                 length = 0;
437
438                 status = rpccli_winreg_EnumValue(pipe_handle,
439                                                  tmp_ctx,
440                                                  key_hnd,
441                                                  i,
442                                                  &name_buf,
443                                                  &type,
444                                                  data,
445                                                  &data_size,
446                                                  &length,
447                                                  &result);
448                 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
449                         result = WERR_OK;
450                         status = NT_STATUS_OK;
451                         break;
452                 }
453
454                 if (!NT_STATUS_IS_OK(status)) {
455                         DEBUG(0, ("winreg_printer_enumvalues: Could not enumerate values: %s\n",
456                                   nt_errstr(status)));
457                         if (!W_ERROR_IS_OK(result)) {
458                                 goto error;
459                         }
460                         result = ntstatus_to_werror(status);
461                         goto error;
462                 }
463
464                 if (name_buf.name == NULL) {
465                         result = WERR_INVALID_PARAMETER;
466                         goto error;
467                 }
468
469                 val.value_name = talloc_strdup(enum_values, name_buf.name);
470                 if (val.value_name == NULL) {
471                         result = WERR_NOMEM;
472                         goto error;
473                 }
474                 val.value_name_len = strlen_m_term(val.value_name) * 2;
475
476                 val.type = type;
477                 val.data_length = data_size;
478                 val.data = NULL;
479                 if (val.data_length) {
480                         val.data = talloc(enum_values, DATA_BLOB);
481                         if (val.data == NULL) {
482                                 result = WERR_NOMEM;
483                                 goto error;
484                         }
485                         *val.data = data_blob_talloc(enum_values, data, data_size);
486                 }
487
488                 enum_values[i] = val;
489         }
490
491         *pnum_values = num_values;
492         if (penum_values) {
493                 *penum_values = talloc_move(mem_ctx, &enum_values);
494         }
495
496         result = WERR_OK;
497
498  error:
499         TALLOC_FREE(tmp_ctx);
500         return result;
501 }
502
503 /**
504  * @internal
505  *
506  * @brief Enumerate subkeys of an opened key handle and get the names.
507  *
508  * @param[in]  mem_ctx  The memory context to use.
509  *
510  * @param[in]  pipe_handle The pipe handle for the rpc connection.
511  *
512  * @param[in]  key_hnd  The opened key handle.
513  *
514  * @param[in]  pnum_subkeys A pointer to store the number of found subkeys.
515  *
516  * @param[in]  psubkeys A pointer to an array to store the found names of
517  *                      subkeys.
518  *
519  * @return                   WERR_OK on success, the corresponding DOS error
520  *                           code if something gone wrong.
521  */
522 static WERROR winreg_printer_enumkeys(TALLOC_CTX *mem_ctx,
523                                       struct rpc_pipe_client *pipe_handle,
524                                       struct policy_handle *key_hnd,
525                                       uint32_t *pnum_subkeys,
526                                       const char ***psubkeys)
527 {
528         TALLOC_CTX *tmp_ctx;
529         const char **subkeys;
530         uint32_t num_subkeys, max_subkeylen, max_classlen;
531         uint32_t num_values, max_valnamelen, max_valbufsize;
532         uint32_t i;
533         NTTIME last_changed_time;
534         uint32_t secdescsize;
535         struct winreg_String classname;
536         WERROR result = WERR_OK;
537         NTSTATUS status;
538
539         tmp_ctx = talloc_new(mem_ctx);
540         if (tmp_ctx == NULL) {
541                 return WERR_NOMEM;
542         }
543
544         ZERO_STRUCT(classname);
545
546         status = rpccli_winreg_QueryInfoKey(pipe_handle,
547                                             tmp_ctx,
548                                             key_hnd,
549                                             &classname,
550                                             &num_subkeys,
551                                             &max_subkeylen,
552                                             &max_classlen,
553                                             &num_values,
554                                             &max_valnamelen,
555                                             &max_valbufsize,
556                                             &secdescsize,
557                                             &last_changed_time,
558                                             &result);
559         if (!NT_STATUS_IS_OK(status)) {
560                 DEBUG(0, ("winreg_printer_enumkeys: Could not query info: %s\n",
561                           nt_errstr(status)));
562                 if (!W_ERROR_IS_OK(result)) {
563                         goto error;
564                 }
565                 result = ntstatus_to_werror(status);
566                 goto error;
567         }
568
569         subkeys = talloc_zero_array(tmp_ctx, const char *, num_subkeys + 2);
570         if (subkeys == NULL) {
571                 result = WERR_NOMEM;
572                 goto error;
573         }
574
575         if (num_subkeys == 0) {
576                 subkeys[0] = talloc_strdup(subkeys, "");
577                 if (subkeys[0] == NULL) {
578                         result = WERR_NOMEM;
579                         goto error;
580                 }
581                 *pnum_subkeys = 0;
582                 if (psubkeys) {
583                         *psubkeys = talloc_move(mem_ctx, &subkeys);
584                 }
585
586                 TALLOC_FREE(tmp_ctx);
587                 return WERR_OK;
588         }
589
590         for (i = 0; i < num_subkeys; i++) {
591                 char c = '\0';
592                 char n = '\0';
593                 char *name = NULL;
594                 struct winreg_StringBuf class_buf;
595                 struct winreg_StringBuf name_buf;
596                 NTTIME modtime;
597
598                 class_buf.name = &c;
599                 class_buf.size = max_classlen + 2;
600                 class_buf.length = 0;
601
602                 name_buf.name = &n;
603                 name_buf.size = max_subkeylen + 2;
604                 name_buf.length = 0;
605
606                 ZERO_STRUCT(modtime);
607
608                 status = rpccli_winreg_EnumKey(pipe_handle,
609                                                tmp_ctx,
610                                                key_hnd,
611                                                i,
612                                                &name_buf,
613                                                &class_buf,
614                                                &modtime,
615                                                &result);
616                 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
617                         result = WERR_OK;
618                         status = NT_STATUS_OK;
619                         break;
620                 }
621
622                 if (!NT_STATUS_IS_OK(status)) {
623                         DEBUG(0, ("winreg_printer_enumkeys: Could not enumerate keys: %s\n",
624                                   nt_errstr(status)));
625                         if (!W_ERROR_IS_OK(result)) {
626                                 goto error;
627                         }
628                         result = ntstatus_to_werror(status);
629                         goto error;
630                 }
631
632                 if (name_buf.name == NULL) {
633                         result = WERR_INVALID_PARAMETER;
634                         goto error;
635                 }
636
637                 name = talloc_strdup(subkeys, name_buf.name);
638                 if (name == NULL) {
639                         result = WERR_NOMEM;
640                         goto error;
641                 }
642
643                 subkeys[i] = name;
644         }
645
646         *pnum_subkeys = num_subkeys;
647         if (psubkeys) {
648                 *psubkeys = talloc_move(mem_ctx, &subkeys);
649         }
650
651  error:
652         TALLOC_FREE(tmp_ctx);
653         return result;
654 }
655
656 /**
657  * @internal
658  *
659  * @brief A function to delete a key and its subkeys recurively.
660  *
661  * @param[in]  mem_ctx  The memory context to use.
662  *
663  * @param[in]  pipe_handle The pipe handle for the rpc connection.
664  *
665  * @param[in]  hive_handle A opened hive handle to the key.
666  *
667  * @param[in]  access_mask The access mask to access the key.
668  *
669  * @param[in]  key      The key to delete
670  *
671  * @return              WERR_OK on success, the corresponding DOS error
672  *                      code if something gone wrong.
673  */
674 static WERROR winreg_printer_delete_subkeys(TALLOC_CTX *mem_ctx,
675                                             struct rpc_pipe_client *pipe_handle,
676                                             struct policy_handle *hive_handle,
677                                             uint32_t access_mask,
678                                             const char *key)
679 {
680         const char **subkeys = NULL;
681         uint32_t num_subkeys = 0;
682         struct policy_handle key_hnd;
683         struct winreg_String wkey;
684         WERROR result = WERR_OK;
685         NTSTATUS status;
686         uint32_t i;
687
688         ZERO_STRUCT(key_hnd);
689         wkey.name = key;
690
691         DEBUG(2, ("winreg_printer_delete_subkeys: delete key %s\n", key));
692         /* open the key */
693         status = rpccli_winreg_OpenKey(pipe_handle,
694                                        mem_ctx,
695                                        hive_handle,
696                                        wkey,
697                                        0,
698                                        access_mask,
699                                        &key_hnd,
700                                        &result);
701         if (!NT_STATUS_IS_OK(status)) {
702                 DEBUG(0, ("winreg_printer_delete_subkeys: Could not open key %s: %s\n",
703                           wkey.name, nt_errstr(status)));
704                 if (!W_ERROR_IS_OK(result)) {
705                         return result;
706                 }
707                 return ntstatus_to_werror(status);
708         }
709
710         result = winreg_printer_enumkeys(mem_ctx,
711                                          pipe_handle,
712                                          &key_hnd,
713                                          &num_subkeys,
714                                          &subkeys);
715         if (!W_ERROR_IS_OK(result)) {
716                 goto done;
717         }
718
719         for (i = 0; i < num_subkeys; i++) {
720                 /* create key + subkey */
721                 char *subkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkeys[i]);
722                 if (subkey == NULL) {
723                         goto done;
724                 }
725
726                 DEBUG(2, ("winreg_printer_delete_subkeys: delete subkey %s\n", subkey));
727                 result = winreg_printer_delete_subkeys(mem_ctx,
728                                                        pipe_handle,
729                                                        hive_handle,
730                                                        access_mask,
731                                                        subkey);
732                 if (!W_ERROR_IS_OK(result)) {
733                         goto done;
734                 }
735         }
736
737         if (is_valid_policy_hnd(&key_hnd)) {
738                 rpccli_winreg_CloseKey(pipe_handle, mem_ctx, &key_hnd, NULL);
739         }
740
741         wkey.name = key;
742
743         status = rpccli_winreg_DeleteKey(pipe_handle,
744                                          mem_ctx,
745                                          hive_handle,
746                                          wkey,
747                                          &result);
748
749 done:
750         if (is_valid_policy_hnd(&key_hnd)) {
751                 rpccli_winreg_CloseKey(pipe_handle, mem_ctx, &key_hnd, NULL);
752         }
753
754         return result;
755 }
756
757 static WERROR winreg_printer_write_sz(TALLOC_CTX *mem_ctx,
758                                       struct rpc_pipe_client *pipe_handle,
759                                       struct policy_handle *key_handle,
760                                       const char *value,
761                                       const char *data)
762 {
763         struct winreg_String wvalue;
764         DATA_BLOB blob;
765         WERROR result = WERR_OK;
766         NTSTATUS status;
767
768         wvalue.name = value;
769         if (data == NULL) {
770                 blob = data_blob_string_const("");
771         } else {
772                 if (!push_reg_sz(mem_ctx, NULL, &blob, data)) {
773                         DEBUG(0, ("winreg_printer_write_sz: Could not marshall string %s for %s\n",
774                                 data, wvalue.name));
775                         return WERR_NOMEM;
776                 }
777         }
778         status = rpccli_winreg_SetValue(pipe_handle,
779                                         mem_ctx,
780                                         key_handle,
781                                         wvalue,
782                                         REG_SZ,
783                                         blob.data,
784                                         blob.length,
785                                         &result);
786         if (!NT_STATUS_IS_OK(status)) {
787                 DEBUG(0, ("winreg_printer_write_sz: Could not set value %s: %s\n",
788                         wvalue.name, win_errstr(result)));
789                 if (!W_ERROR_IS_OK(result)) {
790                         result = ntstatus_to_werror(status);
791                 }
792         }
793
794         return result;
795 }
796
797 static WERROR winreg_printer_write_dword(TALLOC_CTX *mem_ctx,
798                                          struct rpc_pipe_client *pipe_handle,
799                                          struct policy_handle *key_handle,
800                                          const char *value,
801                                          uint32_t data)
802 {
803         struct winreg_String wvalue;
804         DATA_BLOB blob;
805         WERROR result = WERR_OK;
806         NTSTATUS status;
807
808         wvalue.name = value;
809         blob = data_blob_talloc(mem_ctx, NULL, 4);
810         SIVAL(blob.data, 0, data);
811
812         status = rpccli_winreg_SetValue(pipe_handle,
813                                         mem_ctx,
814                                         key_handle,
815                                         wvalue,
816                                         REG_DWORD,
817                                         blob.data,
818                                         blob.length,
819                                         &result);
820         if (!NT_STATUS_IS_OK(status)) {
821                 DEBUG(0, ("winreg_printer_write_dword: Could not set value %s: %s\n",
822                         wvalue.name, win_errstr(result)));
823                 if (!W_ERROR_IS_OK(result)) {
824                         result = ntstatus_to_werror(status);
825                 }
826         }
827
828         return result;
829 }
830
831 static WERROR winreg_printer_write_binary(TALLOC_CTX *mem_ctx,
832                                           struct rpc_pipe_client *pipe_handle,
833                                           struct policy_handle *key_handle,
834                                           const char *value,
835                                           DATA_BLOB blob)
836 {
837         struct winreg_String wvalue;
838         WERROR result = WERR_OK;
839         NTSTATUS status;
840
841         wvalue.name = value;
842         status = rpccli_winreg_SetValue(pipe_handle,
843                                         mem_ctx,
844                                         key_handle,
845                                         wvalue,
846                                         REG_BINARY,
847                                         blob.data,
848                                         blob.length,
849                                         &result);
850         if (!NT_STATUS_IS_OK(status)) {
851                 DEBUG(0, ("winreg_printer_write_binary: Could not set value %s: %s\n",
852                         wvalue.name, win_errstr(result)));
853                 if (!W_ERROR_IS_OK(result)) {
854                         result = ntstatus_to_werror(status);
855                 }
856         }
857
858         return result;
859 }
860
861 static WERROR winreg_printer_query_dword(TALLOC_CTX *mem_ctx,
862                                          struct rpc_pipe_client *pipe_handle,
863                                          struct policy_handle *key_handle,
864                                          const char *value,
865                                          uint32_t *data)
866 {
867         struct winreg_String wvalue;
868         enum winreg_Type type;
869         WERROR result = WERR_OK;
870         uint32_t value_len = 0;
871         NTSTATUS status;
872         DATA_BLOB blob;
873
874         wvalue.name = value;
875         status = rpccli_winreg_QueryValue(pipe_handle,
876                                           mem_ctx,
877                                           key_handle,
878                                           &wvalue,
879                                           &type,
880                                           NULL,
881                                           (uint32_t *) &blob.length,
882                                           &value_len,
883                                           &result);
884         if (!NT_STATUS_IS_OK(status)) {
885                 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
886                           wvalue.name, nt_errstr(status)));
887                 if (!W_ERROR_IS_OK(result)) {
888                         goto done;
889                 }
890                 result = ntstatus_to_werror(status);
891                 goto done;
892         }
893
894         if (type != REG_DWORD) {
895                 result = WERR_INVALID_DATATYPE;
896                 goto done;
897         }
898
899         if (blob.length != 4) {
900                 result = WERR_INVALID_DATA;
901                 goto done;
902         }
903
904         blob.data = (uint8_t *) TALLOC(mem_ctx, blob.length);
905         if (blob.data == NULL) {
906                 result = WERR_NOMEM;
907                 goto done;
908         }
909         value_len = 0;
910
911         status = rpccli_winreg_QueryValue(pipe_handle,
912                                           mem_ctx,
913                                           key_handle,
914                                           &wvalue,
915                                           &type,
916                                           blob.data,
917                                           (uint32_t *) &blob.length,
918                                           &value_len,
919                                           &result);
920         if (!NT_STATUS_IS_OK(status)) {
921                 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
922                           wvalue.name, nt_errstr(status)));
923                 if (!W_ERROR_IS_OK(result)) {
924                         result = ntstatus_to_werror(status);
925                 }
926                 goto done;
927         }
928
929         if (data) {
930                 *data = IVAL(blob.data, 0);
931         }
932 done:
933         return result;
934 }
935
936 static WERROR winreg_printer_write_multi_sz(TALLOC_CTX *mem_ctx,
937                                             struct rpc_pipe_client *pipe_handle,
938                                             struct policy_handle *key_handle,
939                                             const char *value,
940                                             const char **data)
941 {
942         struct winreg_String wvalue;
943         DATA_BLOB blob;
944         WERROR result = WERR_OK;
945         NTSTATUS status;
946
947         wvalue.name = value;
948         if (!push_reg_multi_sz(mem_ctx, NULL, &blob, data)) {
949                 return WERR_NOMEM;
950         }
951         status = rpccli_winreg_SetValue(pipe_handle,
952                                         mem_ctx,
953                                         key_handle,
954                                         wvalue,
955                                         REG_MULTI_SZ,
956                                         blob.data,
957                                         blob.length,
958                                         &result);
959         if (!NT_STATUS_IS_OK(status)) {
960                 DEBUG(0, ("winreg_printer_write_multi_sz: Could not set value %s: %s\n",
961                         wvalue.name, win_errstr(result)));
962                 if (!W_ERROR_IS_OK(result)) {
963                         result = ntstatus_to_werror(status);
964                 }
965         }
966
967         return result;
968 }
969
970 static WERROR winreg_printer_opendriver(TALLOC_CTX *mem_ctx,
971                                         struct auth_serversupplied_info *server_info,
972                                         const char *drivername,
973                                         const char *architecture,
974                                         uint32_t version,
975                                         uint32_t access_mask,
976                                         bool create,
977                                         struct rpc_pipe_client **winreg_pipe,
978                                         struct policy_handle *hive_hnd,
979                                         struct policy_handle *key_hnd)
980 {
981         WERROR result;
982         char *key_name;
983
984         key_name = talloc_asprintf(mem_ctx, "%s\\Environments\\%s\\Drivers\\Version-%u",
985                                    TOP_LEVEL_CONTROL_KEY,
986                                    architecture, version);
987         if (!key_name) {
988                 return WERR_NOMEM;
989         }
990
991         result = winreg_printer_openkey(mem_ctx,
992                                         server_info,
993                                         winreg_pipe,
994                                         key_name,
995                                         drivername,
996                                         create,
997                                         access_mask,
998                                         hive_hnd,
999                                         key_hnd);
1000         return result;
1001 }
1002
1003 static WERROR winreg_enumval_to_dword(TALLOC_CTX *mem_ctx,
1004                                       struct spoolss_PrinterEnumValues *v,
1005                                       const char *valuename, uint32_t *dw)
1006 {
1007         /* just return if it is not the one we are looking for */
1008         if (strcmp(valuename, v->value_name) != 0) {
1009                 return WERR_NOT_FOUND;
1010         }
1011
1012         if (v->type != REG_DWORD) {
1013                 return WERR_INVALID_DATATYPE;
1014         }
1015
1016         *dw = IVAL(v->data->data, 0);
1017         return WERR_OK;
1018 }
1019
1020 static WERROR winreg_enumval_to_sz(TALLOC_CTX *mem_ctx,
1021                                    struct spoolss_PrinterEnumValues *v,
1022                                    const char *valuename, const char **_str)
1023 {
1024         /* just return if it is not the one we are looking for */
1025         if (strcmp(valuename, v->value_name) != 0) {
1026                 return WERR_NOT_FOUND;
1027         }
1028
1029         if (v->type != REG_SZ) {
1030                 return WERR_INVALID_DATATYPE;
1031         }
1032
1033         if (!pull_reg_sz(mem_ctx, NULL, v->data, _str)) {
1034                 return WERR_NOMEM;
1035         }
1036
1037         return WERR_OK;
1038 }
1039
1040 static WERROR winreg_enumval_to_multi_sz(TALLOC_CTX *mem_ctx,
1041                                          struct spoolss_PrinterEnumValues *v,
1042                                          const char *valuename,
1043                                          const char ***array)
1044 {
1045         /* just return if it is not the one we are looking for */
1046         if (strcmp(valuename, v->value_name) != 0) {
1047                 return WERR_NOT_FOUND;
1048         }
1049
1050         if (v->type != REG_MULTI_SZ) {
1051                 return WERR_INVALID_DATATYPE;
1052         }
1053
1054         if (!pull_reg_multi_sz(mem_ctx, NULL, v->data, array)) {
1055                 return WERR_NOMEM;
1056         }
1057
1058         return WERR_OK;
1059 }
1060
1061 static WERROR winreg_enumval_to_blob(TALLOC_CTX *mem_ctx,
1062                                      struct spoolss_PrinterEnumValues *v,
1063                                      const char *valuename,
1064                                      DATA_BLOB *blob)
1065 {
1066         /* just return if it is not the one we are looking for */
1067         if (strcmp(valuename, v->value_name) != 0) {
1068                 return WERR_NOT_FOUND;
1069         }
1070
1071         if (v->type != REG_BINARY) {
1072                 return WERR_INVALID_DATATYPE;
1073         }
1074
1075         blob->data = v->data->data;
1076         blob->length = v->data_length;
1077
1078         return WERR_OK;
1079 }
1080
1081 static WERROR winreg_printer_write_date(TALLOC_CTX *mem_ctx,
1082                                         struct rpc_pipe_client *pipe_handle,
1083                                         struct policy_handle *key_handle,
1084                                         const char *value,
1085                                         NTTIME data)
1086 {
1087         struct winreg_String wvalue;
1088         DATA_BLOB blob;
1089         WERROR result = WERR_OK;
1090         NTSTATUS status;
1091         const char *str;
1092         struct tm *tm;
1093         time_t t;
1094
1095         t = nt_time_to_unix(data);
1096         tm = localtime(&t);
1097         str = talloc_asprintf(mem_ctx, "%02d/%02d/%04d",
1098                               tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
1099         if (!str) {
1100                 return WERR_NOMEM;
1101         }
1102
1103         wvalue.name = value;
1104         if (!push_reg_sz(mem_ctx, NULL, &blob, str)) {
1105                 return WERR_NOMEM;
1106         }
1107         status = rpccli_winreg_SetValue(pipe_handle,
1108                                         mem_ctx,
1109                                         key_handle,
1110                                         wvalue,
1111                                         REG_SZ,
1112                                         blob.data,
1113                                         blob.length,
1114                                         &result);
1115         if (!NT_STATUS_IS_OK(status)) {
1116                 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
1117                         wvalue.name, win_errstr(result)));
1118                 if (!W_ERROR_IS_OK(result)) {
1119                         result = ntstatus_to_werror(status);
1120                 }
1121         }
1122
1123         return result;
1124 }
1125
1126 static WERROR winreg_printer_date_to_NTTIME(const char *str, NTTIME *data)
1127 {
1128         struct tm tm;
1129         time_t t;
1130
1131         ZERO_STRUCT(tm);
1132
1133         if (sscanf(str, "%d/%d/%d",
1134                    &tm.tm_mon, &tm.tm_mday, &tm.tm_year) != 3) {
1135                 return WERR_INVALID_PARAMETER;
1136         }
1137         tm.tm_mon -= 1;
1138         tm.tm_year -= 1900;
1139         tm.tm_isdst = -1;
1140
1141         t = mktime(&tm);
1142         unix_to_nt_time(data, t);
1143
1144         return WERR_OK;
1145 }
1146
1147 static WERROR winreg_printer_write_ver(TALLOC_CTX *mem_ctx,
1148                                        struct rpc_pipe_client *pipe_handle,
1149                                        struct policy_handle *key_handle,
1150                                        const char *value,
1151                                        uint64_t data)
1152 {
1153         struct winreg_String wvalue;
1154         DATA_BLOB blob;
1155         WERROR result = WERR_OK;
1156         NTSTATUS status;
1157         char *str;
1158
1159         /* FIXME: check format is right,
1160          *      this needs to be something like: 6.1.7600.16385 */
1161         str = talloc_asprintf(mem_ctx, "%u.%u.%u.%u",
1162                               (unsigned)((data >> 48) & 0xFFFF),
1163                               (unsigned)((data >> 32) & 0xFFFF),
1164                               (unsigned)((data >> 16) & 0xFFFF),
1165                               (unsigned)(data & 0xFFFF));
1166         if (!str) {
1167                 return WERR_NOMEM;
1168         }
1169
1170         wvalue.name = value;
1171         if (!push_reg_sz(mem_ctx, NULL, &blob, str)) {
1172                 return WERR_NOMEM;
1173         }
1174         status = rpccli_winreg_SetValue(pipe_handle,
1175                                         mem_ctx,
1176                                         key_handle,
1177                                         wvalue,
1178                                         REG_SZ,
1179                                         blob.data,
1180                                         blob.length,
1181                                         &result);
1182         if (!NT_STATUS_IS_OK(status)) {
1183                 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
1184                         wvalue.name, win_errstr(result)));
1185                 if (!W_ERROR_IS_OK(result)) {
1186                         result = ntstatus_to_werror(status);
1187                 }
1188         }
1189
1190         return result;
1191 }
1192
1193 static WERROR winreg_printer_ver_to_dword(const char *str, uint64_t *data)
1194 {
1195         unsigned int v1, v2, v3, v4;
1196
1197         if (sscanf(str, "%u.%u.%u.%u", &v1, &v2, &v3, &v4) != 4) {
1198                 return WERR_INVALID_PARAMETER;
1199         }
1200
1201         *data = ((uint64_t)(v1 & 0xFFFF) << 48) +
1202                 ((uint64_t)(v2 & 0xFFFF) << 32) +
1203                 ((uint64_t)(v3 & 0xFFFF) << 16) +
1204                 (uint64_t)(v2 & 0xFFFF);
1205
1206         return WERR_OK;
1207 }
1208
1209 /********************************************************************
1210  Public winreg function for spoolss
1211 ********************************************************************/
1212
1213 /* Set printer data over the winreg pipe. */
1214 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
1215                                  struct auth_serversupplied_info *server_info,
1216                                  const char *printer,
1217                                  const char *key,
1218                                  const char *value,
1219                                  enum winreg_Type type,
1220                                  uint8_t *data,
1221                                  uint32_t data_size)
1222 {
1223         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1224         struct rpc_pipe_client *winreg_pipe = NULL;
1225         struct policy_handle hive_hnd, key_hnd;
1226         struct winreg_String wvalue;
1227         char *path;
1228         WERROR result = WERR_OK;
1229         NTSTATUS status;
1230         TALLOC_CTX *tmp_ctx;
1231
1232         tmp_ctx = talloc_new(mem_ctx);
1233         if (tmp_ctx == NULL) {
1234                 return WERR_NOMEM;
1235         }
1236
1237         path = winreg_printer_data_keyname(tmp_ctx, printer);
1238         if (path == NULL) {
1239                 TALLOC_FREE(tmp_ctx);
1240                 return WERR_NOMEM;
1241         }
1242
1243         ZERO_STRUCT(hive_hnd);
1244         ZERO_STRUCT(key_hnd);
1245
1246         DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
1247                         key, value, access_mask, printer));
1248         result = winreg_printer_openkey(tmp_ctx,
1249                                         server_info,
1250                                         &winreg_pipe,
1251                                         path,
1252                                         key,
1253                                         true,
1254                                         access_mask,
1255                                         &hive_hnd,
1256                                         &key_hnd);
1257         if (!W_ERROR_IS_OK(result)) {
1258                 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
1259                           key, win_errstr(result)));
1260                 goto done;
1261         }
1262
1263         wvalue.name = value;
1264         status = rpccli_winreg_SetValue(winreg_pipe,
1265                                         tmp_ctx,
1266                                         &key_hnd,
1267                                         wvalue,
1268                                         type,
1269                                         data,
1270                                         data_size,
1271                                         &result);
1272         if (!NT_STATUS_IS_OK(status)) {
1273                 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
1274                           value, nt_errstr(status)));
1275                 if (!W_ERROR_IS_OK(result)) {
1276                         goto done;
1277                 }
1278                 result = ntstatus_to_werror(status);
1279                 goto done;
1280         }
1281
1282         result = WERR_OK;
1283 done:
1284         if (winreg_pipe != NULL) {
1285                 if (is_valid_policy_hnd(&key_hnd)) {
1286                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1287                 }
1288                 if (is_valid_policy_hnd(&hive_hnd)) {
1289                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1290                 }
1291         }
1292
1293         TALLOC_FREE(tmp_ctx);
1294         return result;
1295 }
1296
1297 /* Get printer data over a winreg pipe. */
1298 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
1299                                  struct auth_serversupplied_info *server_info,
1300                                  const char *printer,
1301                                  const char *key,
1302                                  const char *value,
1303                                  enum winreg_Type *type,
1304                                  uint8_t **data,
1305                                  uint32_t *data_size)
1306 {
1307         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1308         struct rpc_pipe_client *winreg_pipe = NULL;
1309         struct policy_handle hive_hnd, key_hnd;
1310         struct winreg_String wvalue;
1311         enum winreg_Type type_in;
1312         char *path;
1313         uint8_t *data_in;
1314         uint32_t data_in_size = 0;
1315         uint32_t value_len = 0;
1316         WERROR result = WERR_OK;
1317         NTSTATUS status;
1318         TALLOC_CTX *tmp_ctx;
1319
1320         tmp_ctx = talloc_new(mem_ctx);
1321         if (tmp_ctx == NULL) {
1322                 return WERR_NOMEM;
1323         }
1324
1325         path = winreg_printer_data_keyname(tmp_ctx, printer);
1326         if (path == NULL) {
1327                 TALLOC_FREE(tmp_ctx);
1328                 return WERR_NOMEM;
1329         }
1330
1331         ZERO_STRUCT(hive_hnd);
1332         ZERO_STRUCT(key_hnd);
1333
1334         result = winreg_printer_openkey(tmp_ctx,
1335                                         server_info,
1336                                         &winreg_pipe,
1337                                         path,
1338                                         key,
1339                                         false,
1340                                         access_mask,
1341                                         &hive_hnd,
1342                                         &key_hnd);
1343         if (!W_ERROR_IS_OK(result)) {
1344                 DEBUG(0, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
1345                           key, win_errstr(result)));
1346                 goto done;
1347         }
1348
1349         wvalue.name = value;
1350
1351         /*
1352          * call QueryValue once with data == NULL to get the
1353          * needed memory size to be allocated, then allocate
1354          * data buffer and call again.
1355          */
1356         status = rpccli_winreg_QueryValue(winreg_pipe,
1357                                           tmp_ctx,
1358                                           &key_hnd,
1359                                           &wvalue,
1360                                           &type_in,
1361                                           NULL,
1362                                           &data_in_size,
1363                                           &value_len,
1364                                           &result);
1365         if (!NT_STATUS_IS_OK(status)) {
1366                 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
1367                           value, nt_errstr(status)));
1368                 if (!W_ERROR_IS_OK(result)) {
1369                         goto done;
1370                 }
1371                 result = ntstatus_to_werror(status);
1372                 goto done;
1373         }
1374
1375         data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
1376         if (data_in == NULL) {
1377                 result = WERR_NOMEM;
1378                 goto done;
1379         }
1380         value_len = 0;
1381
1382         status = rpccli_winreg_QueryValue(winreg_pipe,
1383                                           tmp_ctx,
1384                                           &key_hnd,
1385                                           &wvalue,
1386                                           &type_in,
1387                                           data_in,
1388                                           &data_in_size,
1389                                           &value_len,
1390                                           &result);
1391         if (!NT_STATUS_IS_OK(status)) {
1392                 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
1393                           value, nt_errstr(status)));
1394                 if (!W_ERROR_IS_OK(result)) {
1395                         result = ntstatus_to_werror(status);
1396                 }
1397                 goto done;
1398         }
1399
1400         *type = type_in;
1401         *data_size = data_in_size;
1402         if (data_in_size) {
1403                 *data = talloc_move(mem_ctx, &data_in);
1404         }
1405
1406         result = WERR_OK;
1407 done:
1408         if (winreg_pipe != NULL) {
1409                 if (is_valid_policy_hnd(&key_hnd)) {
1410                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1411                 }
1412                 if (is_valid_policy_hnd(&hive_hnd)) {
1413                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1414                 }
1415         }
1416
1417         TALLOC_FREE(tmp_ctx);
1418         return result;
1419 }
1420
1421 /* Enumerate on the values of a given key and provide the data. */
1422 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
1423                                   struct auth_serversupplied_info *server_info,
1424                                   const char *printer,
1425                                   const char *key,
1426                                   uint32_t *pnum_values,
1427                                   struct spoolss_PrinterEnumValues **penum_values)
1428 {
1429         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1430         struct rpc_pipe_client *winreg_pipe = NULL;
1431         struct policy_handle hive_hnd, key_hnd;
1432
1433         struct spoolss_PrinterEnumValues *enum_values = NULL;
1434         uint32_t num_values = 0;
1435         char *path;
1436         WERROR result = WERR_OK;
1437
1438         TALLOC_CTX *tmp_ctx;
1439
1440         tmp_ctx = talloc_new(mem_ctx);
1441         if (tmp_ctx == NULL) {
1442                 return WERR_NOMEM;
1443         }
1444
1445         path = winreg_printer_data_keyname(tmp_ctx, printer);
1446         if (path == NULL) {
1447                 TALLOC_FREE(tmp_ctx);
1448                 return WERR_NOMEM;
1449         }
1450
1451         result = winreg_printer_openkey(tmp_ctx,
1452                                         server_info,
1453                                         &winreg_pipe,
1454                                         path,
1455                                         key,
1456                                         false,
1457                                         access_mask,
1458                                         &hive_hnd,
1459                                         &key_hnd);
1460         if (!W_ERROR_IS_OK(result)) {
1461                 DEBUG(0, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
1462                           key, win_errstr(result)));
1463                 goto done;
1464         }
1465
1466         result = winreg_printer_enumvalues(tmp_ctx,
1467                                            winreg_pipe,
1468                                            &key_hnd,
1469                                            &num_values,
1470                                            &enum_values);
1471         if (!W_ERROR_IS_OK(result)) {
1472                 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
1473                           key, win_errstr(result)));
1474                 goto done;
1475         }
1476
1477         *pnum_values = num_values;
1478         if (penum_values) {
1479                 *penum_values = talloc_move(mem_ctx, &enum_values);
1480         }
1481
1482         result = WERR_OK;
1483 done:
1484         if (winreg_pipe != NULL) {
1485                 if (is_valid_policy_hnd(&key_hnd)) {
1486                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1487                 }
1488                 if (is_valid_policy_hnd(&hive_hnd)) {
1489                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1490                 }
1491         }
1492
1493         TALLOC_FREE(tmp_ctx);
1494         return result;
1495 }
1496
1497 /* Delete printer data over a winreg pipe. */
1498 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
1499                                     struct auth_serversupplied_info *server_info,
1500                                     const char *printer,
1501                                     const char *key,
1502                                     const char *value)
1503 {
1504         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1505         struct rpc_pipe_client *winreg_pipe = NULL;
1506         struct policy_handle hive_hnd, key_hnd;
1507         struct winreg_String wvalue;
1508         char *path;
1509         WERROR result = WERR_OK;
1510         NTSTATUS status;
1511
1512         TALLOC_CTX *tmp_ctx;
1513
1514         tmp_ctx = talloc_new(mem_ctx);
1515         if (tmp_ctx == NULL) {
1516                 return WERR_NOMEM;
1517         }
1518
1519         path = winreg_printer_data_keyname(tmp_ctx, printer);
1520         if (path == NULL) {
1521                 TALLOC_FREE(tmp_ctx);
1522                 return WERR_NOMEM;
1523         }
1524
1525         ZERO_STRUCT(hive_hnd);
1526         ZERO_STRUCT(key_hnd);
1527
1528         result = winreg_printer_openkey(tmp_ctx,
1529                                         server_info,
1530                                         &winreg_pipe,
1531                                         path,
1532                                         key,
1533                                         false,
1534                                         access_mask,
1535                                         &hive_hnd,
1536                                         &key_hnd);
1537         if (!W_ERROR_IS_OK(result)) {
1538                 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
1539                           key, win_errstr(result)));
1540                 goto done;
1541         }
1542
1543         wvalue.name = value;
1544         status = rpccli_winreg_DeleteValue(winreg_pipe,
1545                                            tmp_ctx,
1546                                            &key_hnd,
1547                                            wvalue,
1548                                            &result);
1549         if (!NT_STATUS_IS_OK(status)) {
1550                 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
1551                           value, nt_errstr(status)));
1552                 if (!W_ERROR_IS_OK(result)) {
1553                         goto done;
1554                 }
1555                 result = ntstatus_to_werror(status);
1556                 goto done;
1557         }
1558
1559         result = WERR_OK;
1560 done:
1561         if (winreg_pipe != NULL) {
1562                 if (is_valid_policy_hnd(&key_hnd)) {
1563                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1564                 }
1565                 if (is_valid_policy_hnd(&hive_hnd)) {
1566                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1567                 }
1568         }
1569
1570         TALLOC_FREE(tmp_ctx);
1571         return result;
1572 }
1573
1574 /* Enumerate on the subkeys of a given key and provide the data. */
1575 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
1576                                struct auth_serversupplied_info *server_info,
1577                                const char *printer,
1578                                const char *key,
1579                                uint32_t *pnum_subkeys,
1580                                const char ***psubkeys)
1581 {
1582         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1583         struct rpc_pipe_client *winreg_pipe = NULL;
1584         struct policy_handle hive_hnd, key_hnd;
1585         char *path;
1586         const char **subkeys = NULL;
1587         uint32_t num_subkeys = -1;
1588
1589         WERROR result = WERR_OK;
1590
1591         TALLOC_CTX *tmp_ctx;
1592
1593         tmp_ctx = talloc_new(mem_ctx);
1594         if (tmp_ctx == NULL) {
1595                 return WERR_NOMEM;
1596         }
1597
1598         path = winreg_printer_data_keyname(tmp_ctx, printer);
1599         if (path == NULL) {
1600                 TALLOC_FREE(tmp_ctx);
1601                 return WERR_NOMEM;
1602         }
1603
1604         ZERO_STRUCT(hive_hnd);
1605         ZERO_STRUCT(key_hnd);
1606
1607         result = winreg_printer_openkey(tmp_ctx,
1608                                         server_info,
1609                                         &winreg_pipe,
1610                                         path,
1611                                         key,
1612                                         false,
1613                                         access_mask,
1614                                         &hive_hnd,
1615                                         &key_hnd);
1616         if (!W_ERROR_IS_OK(result)) {
1617                 DEBUG(0, ("winreg_enum_printer_key: Could not open key %s: %s\n",
1618                           key, win_errstr(result)));
1619                 goto done;
1620         }
1621
1622         result = winreg_printer_enumkeys(tmp_ctx,
1623                                          winreg_pipe,
1624                                          &key_hnd,
1625                                          &num_subkeys,
1626                                          &subkeys);
1627         if (!W_ERROR_IS_OK(result)) {
1628                 DEBUG(0, ("winreg_enum_printer_key: Could not enumerate subkeys in %s: %s\n",
1629                           key, win_errstr(result)));
1630                 goto done;
1631         }
1632
1633         *pnum_subkeys = num_subkeys;
1634         if (psubkeys) {
1635                 *psubkeys = talloc_move(mem_ctx, &subkeys);
1636         }
1637
1638         result = WERR_OK;
1639 done:
1640         if (winreg_pipe != NULL) {
1641                 if (is_valid_policy_hnd(&key_hnd)) {
1642                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1643                 }
1644                 if (is_valid_policy_hnd(&hive_hnd)) {
1645                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1646                 }
1647         }
1648
1649         TALLOC_FREE(tmp_ctx);
1650         return result;
1651 }
1652
1653 /* Delete a key with subkeys of a given printer. */
1654 WERROR winreg_delete_printer_key(TALLOC_CTX *mem_ctx,
1655                                  struct auth_serversupplied_info *server_info,
1656                                  const char *printer,
1657                                  const char *key)
1658 {
1659         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1660         struct rpc_pipe_client *winreg_pipe = NULL;
1661         struct policy_handle hive_hnd, key_hnd;
1662         char *keyname;
1663         char *path;
1664         WERROR result;
1665         TALLOC_CTX *tmp_ctx;
1666
1667         tmp_ctx = talloc_new(mem_ctx);
1668         if (tmp_ctx == NULL) {
1669                 return WERR_NOMEM;
1670         }
1671
1672         path = winreg_printer_data_keyname(tmp_ctx, printer);
1673         if (path == NULL) {
1674                 TALLOC_FREE(tmp_ctx);
1675                 return WERR_NOMEM;
1676         }
1677
1678         result = winreg_printer_openkey(tmp_ctx,
1679                                         server_info,
1680                                         &winreg_pipe,
1681                                         path,
1682                                         key,
1683                                         false,
1684                                         access_mask,
1685                                         &hive_hnd,
1686                                         &key_hnd);
1687         if (!W_ERROR_IS_OK(result)) {
1688                 /* key doesn't exist */
1689                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1690                         result = WERR_OK;
1691                         goto done;
1692                 }
1693
1694                 DEBUG(0, ("winreg_delete_printer_key: Could not open key %s: %s\n",
1695                           key, win_errstr(result)));
1696                 goto done;
1697         }
1698
1699         if (is_valid_policy_hnd(&key_hnd)) {
1700                 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1701         }
1702
1703         if (key == NULL || key[0] == '\0') {
1704                 keyname = path;
1705         } else {
1706                 keyname = talloc_asprintf(tmp_ctx,
1707                                           "%s\\%s",
1708                                           path,
1709                                           key);
1710                 if (keyname == NULL) {
1711                         result = WERR_NOMEM;
1712                         goto done;
1713                 }
1714         }
1715
1716         result = winreg_printer_delete_subkeys(tmp_ctx,
1717                                                winreg_pipe,
1718                                                &hive_hnd,
1719                                                access_mask,
1720                                                keyname);
1721         if (!W_ERROR_IS_OK(result)) {
1722                 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
1723                           key, win_errstr(result)));
1724                 goto done;
1725         }
1726
1727 done:
1728         if (winreg_pipe != NULL) {
1729                 if (is_valid_policy_hnd(&key_hnd)) {
1730                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1731                 }
1732                 if (is_valid_policy_hnd(&hive_hnd)) {
1733                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1734                 }
1735         }
1736
1737         TALLOC_FREE(tmp_ctx);
1738         return result;
1739 }
1740
1741 WERROR winreg_printer_update_changeid(TALLOC_CTX *mem_ctx,
1742                                       struct auth_serversupplied_info *server_info,
1743                                       const char *printer)
1744 {
1745         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1746         struct rpc_pipe_client *winreg_pipe = NULL;
1747         struct policy_handle hive_hnd, key_hnd;
1748         char *path;
1749         WERROR result;
1750         TALLOC_CTX *tmp_ctx;
1751
1752         tmp_ctx = talloc_new(mem_ctx);
1753         if (tmp_ctx == NULL) {
1754                 return WERR_NOMEM;
1755         }
1756
1757         path = winreg_printer_data_keyname(tmp_ctx, printer);
1758         if (path == NULL) {
1759                 TALLOC_FREE(tmp_ctx);
1760                 return WERR_NOMEM;
1761         }
1762
1763         ZERO_STRUCT(hive_hnd);
1764         ZERO_STRUCT(key_hnd);
1765
1766         result = winreg_printer_openkey(tmp_ctx,
1767                                         server_info,
1768                                         &winreg_pipe,
1769                                         path,
1770                                         "",
1771                                         false,
1772                                         access_mask,
1773                                         &hive_hnd,
1774                                         &key_hnd);
1775         if (!W_ERROR_IS_OK(result)) {
1776                 DEBUG(0, ("winreg_printer_update_changeid: Could not open key %s: %s\n",
1777                           path, win_errstr(result)));
1778                 goto done;
1779         }
1780
1781         result = winreg_printer_write_dword(tmp_ctx,
1782                                             winreg_pipe,
1783                                             &key_hnd,
1784                                             "ChangeID",
1785                                             winreg_printer_rev_changeid());
1786         if (!W_ERROR_IS_OK(result)) {
1787                 goto done;
1788         }
1789
1790         result = WERR_OK;
1791 done:
1792         if (winreg_pipe != NULL) {
1793                 if (is_valid_policy_hnd(&key_hnd)) {
1794                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1795                 }
1796                 if (is_valid_policy_hnd(&hive_hnd)) {
1797                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1798                 }
1799         }
1800
1801         TALLOC_FREE(tmp_ctx);
1802         return result;
1803 }
1804
1805 WERROR winreg_printer_get_changeid(TALLOC_CTX *mem_ctx,
1806                                    struct auth_serversupplied_info *server_info,
1807                                    const char *printer,
1808                                    uint32_t *pchangeid)
1809 {
1810         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1811         struct rpc_pipe_client *winreg_pipe = NULL;
1812         struct policy_handle hive_hnd, key_hnd;
1813         uint32_t changeid = 0;
1814         char *path;
1815         WERROR result;
1816         TALLOC_CTX *tmp_ctx;
1817
1818         tmp_ctx = talloc_new(mem_ctx);
1819         if (tmp_ctx == NULL) {
1820                 return WERR_NOMEM;
1821         }
1822
1823         path = winreg_printer_data_keyname(tmp_ctx, printer);
1824         if (path == NULL) {
1825                 TALLOC_FREE(tmp_ctx);
1826                 return WERR_NOMEM;
1827         }
1828
1829         ZERO_STRUCT(hive_hnd);
1830         ZERO_STRUCT(key_hnd);
1831
1832         result = winreg_printer_openkey(tmp_ctx,
1833                                         server_info,
1834                                         &winreg_pipe,
1835                                         path,
1836                                         "",
1837                                         false,
1838                                         access_mask,
1839                                         &hive_hnd,
1840                                         &key_hnd);
1841         if (!W_ERROR_IS_OK(result)) {
1842                 DEBUG(0, ("winreg_printer_get_changeid: Could not open key %s: %s\n",
1843                           path, win_errstr(result)));
1844                 goto done;
1845         }
1846
1847         DEBUG(0, ("winreg_printer_get_changeid: get changeid from %s\n", path));
1848         result = winreg_printer_query_dword(tmp_ctx,
1849                                             winreg_pipe,
1850                                             &key_hnd,
1851                                             "ChangeID",
1852                                             &changeid);
1853         if (!W_ERROR_IS_OK(result)) {
1854                 goto done;
1855         }
1856
1857         if (pchangeid) {
1858                 *pchangeid = changeid;
1859         }
1860
1861         result = WERR_OK;
1862 done:
1863         if (winreg_pipe != NULL) {
1864                 if (is_valid_policy_hnd(&key_hnd)) {
1865                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1866                 }
1867                 if (is_valid_policy_hnd(&hive_hnd)) {
1868                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1869                 }
1870         }
1871
1872         TALLOC_FREE(tmp_ctx);
1873         return result;
1874 }
1875
1876 /*
1877  * The special behaviour of the spoolss forms is documented at the website:
1878  *
1879  * Managing Win32 Printserver Forms
1880  * http://unixwiz.net/techtips/winspooler-forms.html
1881  */
1882
1883 WERROR winreg_printer_addform1(TALLOC_CTX *mem_ctx,
1884                                struct auth_serversupplied_info *server_info,
1885                                struct spoolss_AddFormInfo1 *form)
1886 {
1887         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1888         struct rpc_pipe_client *winreg_pipe = NULL;
1889         struct policy_handle hive_hnd, key_hnd;
1890         struct winreg_String wvalue;
1891         DATA_BLOB blob;
1892         uint32_t num_info = 0;
1893         union spoolss_FormInfo *info = NULL;
1894         uint32_t i;
1895         WERROR result;
1896         NTSTATUS status;
1897         TALLOC_CTX *tmp_ctx;
1898
1899         tmp_ctx = talloc_new(mem_ctx);
1900         if (tmp_ctx == NULL) {
1901                 return WERR_NOMEM;
1902         }
1903
1904         ZERO_STRUCT(hive_hnd);
1905         ZERO_STRUCT(key_hnd);
1906
1907         result = winreg_printer_openkey(tmp_ctx,
1908                                         server_info,
1909                                         &winreg_pipe,
1910                                         TOP_LEVEL_CONTROL_FORMS_KEY,
1911                                         "",
1912                                         true,
1913                                         access_mask,
1914                                         &hive_hnd,
1915                                         &key_hnd);
1916         if (!W_ERROR_IS_OK(result)) {
1917                 DEBUG(0, ("winreg_printer_addform1: Could not open key %s: %s\n",
1918                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1919                 goto done;
1920         }
1921
1922         result = winreg_printer_enumforms1(tmp_ctx, server_info, &num_info, &info);
1923         if (!W_ERROR_IS_OK(result)) {
1924                 DEBUG(0, ("winreg_printer_addform: Could not enum keys %s: %s\n",
1925                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1926                 goto done;
1927         }
1928
1929         /* If form name already exists or is builtin return ALREADY_EXISTS */
1930         for (i = 0; i < num_info; i++) {
1931                 if (strequal(info[i].info1.form_name, form->form_name)) {
1932                         result = WERR_FILE_EXISTS;
1933                         goto done;
1934                 }
1935         }
1936
1937         wvalue.name = form->form_name;
1938
1939         blob = data_blob_talloc(tmp_ctx, NULL, 32);
1940         SIVAL(blob.data,  0, form->size.width);
1941         SIVAL(blob.data,  4, form->size.height);
1942         SIVAL(blob.data,  8, form->area.left);
1943         SIVAL(blob.data, 12, form->area.top);
1944         SIVAL(blob.data, 16, form->area.right);
1945         SIVAL(blob.data, 20, form->area.bottom);
1946         SIVAL(blob.data, 24, num_info + 1); /* FIXME */
1947         SIVAL(blob.data, 28, form->flags);
1948
1949         status = rpccli_winreg_SetValue(winreg_pipe,
1950                                         tmp_ctx,
1951                                         &key_hnd,
1952                                         wvalue,
1953                                         REG_BINARY,
1954                                         blob.data,
1955                                         blob.length,
1956                                         &result);
1957         if (!NT_STATUS_IS_OK(status)) {
1958                 DEBUG(0, ("winreg_printer_addform1: Could not set value %s: %s\n",
1959                           wvalue.name, nt_errstr(status)));
1960                 if (!W_ERROR_IS_OK(result)) {
1961                         goto done;
1962                 }
1963                 result = ntstatus_to_werror(status);
1964                 goto done;
1965         }
1966
1967         result = WERR_OK;
1968 done:
1969         if (winreg_pipe != NULL) {
1970                 if (is_valid_policy_hnd(&key_hnd)) {
1971                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1972                 }
1973                 if (is_valid_policy_hnd(&hive_hnd)) {
1974                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1975                 }
1976         }
1977
1978         TALLOC_FREE(info);
1979         TALLOC_FREE(tmp_ctx);
1980         return result;
1981 }
1982
1983 WERROR winreg_printer_enumforms1(TALLOC_CTX *mem_ctx,
1984                                  struct auth_serversupplied_info *server_info,
1985                                  uint32_t *pnum_info,
1986                                  union spoolss_FormInfo **pinfo)
1987 {
1988         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1989         struct rpc_pipe_client *winreg_pipe = NULL;
1990         struct policy_handle hive_hnd, key_hnd;
1991         union spoolss_FormInfo *info;
1992         struct spoolss_PrinterEnumValues *enum_values = NULL;
1993         uint32_t num_values = 0;
1994         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
1995         uint32_t i;
1996         WERROR result;
1997         TALLOC_CTX *tmp_ctx;
1998
1999         tmp_ctx = talloc_new(mem_ctx);
2000         if (tmp_ctx == NULL) {
2001                 return WERR_NOMEM;
2002         }
2003
2004         ZERO_STRUCT(hive_hnd);
2005         ZERO_STRUCT(key_hnd);
2006
2007         result = winreg_printer_openkey(tmp_ctx,
2008                                         server_info,
2009                                         &winreg_pipe,
2010                                         TOP_LEVEL_CONTROL_FORMS_KEY,
2011                                         "",
2012                                         true,
2013                                         access_mask,
2014                                         &hive_hnd,
2015                                         &key_hnd);
2016         if (!W_ERROR_IS_OK(result)) {
2017                 /* key doesn't exist */
2018                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2019                         result = WERR_OK;
2020                         goto done;
2021                 }
2022
2023                 DEBUG(0, ("winreg_printer_enumforms1: Could not open key %s: %s\n",
2024                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2025                 goto done;
2026         }
2027
2028         result = winreg_printer_enumvalues(tmp_ctx,
2029                                            winreg_pipe,
2030                                            &key_hnd,
2031                                            &num_values,
2032                                            &enum_values);
2033         if (!W_ERROR_IS_OK(result)) {
2034                 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
2035                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2036                 goto done;
2037         }
2038
2039         info = TALLOC_ARRAY(tmp_ctx, union spoolss_FormInfo, num_builtin + num_values);
2040         if (info == NULL) {
2041                 result = WERR_NOMEM;
2042                 goto done;
2043         }
2044
2045         /* Enumerate BUILTIN forms */
2046         for (i = 0; i < num_builtin; i++) {
2047                 info[i].info1 = builtin_forms1[i];
2048         }
2049
2050         /* Enumerate registry forms */
2051         for (i = 0; i < num_values; i++) {
2052                 union spoolss_FormInfo val;
2053
2054                 if (enum_values[i].type != REG_BINARY ||
2055                     enum_values[i].data_length != 32) {
2056                         continue;
2057                 }
2058
2059                 val.info1.form_name = talloc_strdup(info, enum_values[i].value_name);
2060                 if (val.info1.form_name == NULL) {
2061                         result = WERR_NOMEM;
2062                         goto done;
2063                 }
2064
2065                 val.info1.size.width  = IVAL(enum_values[i].data->data,  0);
2066                 val.info1.size.height = IVAL(enum_values[i].data->data,  4);
2067                 val.info1.area.left   = IVAL(enum_values[i].data->data,  8);
2068                 val.info1.area.top    = IVAL(enum_values[i].data->data, 12);
2069                 val.info1.area.right  = IVAL(enum_values[i].data->data, 16);
2070                 val.info1.area.bottom = IVAL(enum_values[i].data->data, 20);
2071                 /* skip form index      IVAL(enum_values[i].data->data, 24)));*/
2072                 val.info1.flags       = IVAL(enum_values[i].data->data, 28);
2073
2074                 info[i + num_builtin] = val;
2075         }
2076
2077         *pnum_info = num_builtin + num_values;
2078         if (pinfo) {
2079                 *pinfo = talloc_move(mem_ctx, &info);
2080         }
2081
2082 done:
2083         if (winreg_pipe != NULL) {
2084                 if (is_valid_policy_hnd(&key_hnd)) {
2085                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2086                 }
2087                 if (is_valid_policy_hnd(&hive_hnd)) {
2088                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2089                 }
2090         }
2091
2092         TALLOC_FREE(enum_values);
2093         TALLOC_FREE(tmp_ctx);
2094         return result;
2095 }
2096
2097 WERROR winreg_printer_deleteform1(TALLOC_CTX *mem_ctx,
2098                                   struct auth_serversupplied_info *server_info,
2099                                   const char *form_name)
2100 {
2101         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2102         struct rpc_pipe_client *winreg_pipe = NULL;
2103         struct policy_handle hive_hnd, key_hnd;
2104         struct winreg_String wvalue;
2105         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2106         uint32_t i;
2107         WERROR result = WERR_OK;
2108         NTSTATUS status;
2109         TALLOC_CTX *tmp_ctx;
2110
2111         for (i = 0; i < num_builtin; i++) {
2112                 if (strequal(builtin_forms1[i].form_name, form_name)) {
2113                         return WERR_INVALID_PARAMETER;
2114                 }
2115         }
2116
2117         tmp_ctx = talloc_new(mem_ctx);
2118         if (tmp_ctx == NULL) {
2119                 return WERR_NOMEM;
2120         }
2121
2122         ZERO_STRUCT(hive_hnd);
2123         ZERO_STRUCT(key_hnd);
2124
2125         result = winreg_printer_openkey(tmp_ctx,
2126                                         server_info,
2127                                         &winreg_pipe,
2128                                         TOP_LEVEL_CONTROL_FORMS_KEY,
2129                                         "",
2130                                         false,
2131                                         access_mask,
2132                                         &hive_hnd,
2133                                         &key_hnd);
2134         if (!W_ERROR_IS_OK(result)) {
2135                 DEBUG(0, ("winreg_printer_deleteform1: Could not open key %s: %s\n",
2136                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2137                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2138                         result = WERR_INVALID_FORM_NAME;
2139                 }
2140                 goto done;
2141         }
2142
2143         wvalue.name = form_name;
2144         status = rpccli_winreg_DeleteValue(winreg_pipe,
2145                                            tmp_ctx,
2146                                            &key_hnd,
2147                                            wvalue,
2148                                            &result);
2149         if (!NT_STATUS_IS_OK(status)) {
2150                 /* If the value doesn't exist, return WERR_INVALID_FORM_NAME */
2151                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2152                         result = WERR_INVALID_FORM_NAME;
2153                         goto done;
2154                 }
2155                 DEBUG(0, ("winreg_printer_delteform1: Could not delete value %s: %s\n",
2156                           wvalue.name, nt_errstr(status)));
2157                 if (!W_ERROR_IS_OK(result)) {
2158                         goto done;
2159                 }
2160                 result = ntstatus_to_werror(status);
2161                 goto done;
2162         }
2163
2164         result = WERR_OK;
2165 done:
2166         if (winreg_pipe != NULL) {
2167                 if (is_valid_policy_hnd(&key_hnd)) {
2168                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2169                 }
2170                 if (is_valid_policy_hnd(&hive_hnd)) {
2171                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2172                 }
2173         }
2174
2175         TALLOC_FREE(tmp_ctx);
2176         return result;
2177 }
2178
2179 WERROR winreg_printer_setform1(TALLOC_CTX *mem_ctx,
2180                                struct auth_serversupplied_info *server_info,
2181                                const char *form_name,
2182                                struct spoolss_AddFormInfo1 *form)
2183 {
2184         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2185         struct rpc_pipe_client *winreg_pipe = NULL;
2186         struct policy_handle hive_hnd, key_hnd;
2187         struct winreg_String wvalue;
2188         DATA_BLOB blob;
2189         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2190         uint32_t i;
2191         WERROR result;
2192         NTSTATUS status;
2193         TALLOC_CTX *tmp_ctx = NULL;
2194
2195         for (i = 0; i < num_builtin; i++) {
2196                 if (strequal(builtin_forms1[i].form_name, form->form_name)) {
2197                         result = WERR_INVALID_PARAM;
2198                         goto done;
2199                 }
2200         }
2201
2202         tmp_ctx = talloc_new(mem_ctx);
2203         if (tmp_ctx == NULL) {
2204                 return WERR_NOMEM;
2205         }
2206
2207         ZERO_STRUCT(hive_hnd);
2208         ZERO_STRUCT(key_hnd);
2209
2210         result = winreg_printer_openkey(tmp_ctx,
2211                                         server_info,
2212                                         &winreg_pipe,
2213                                         TOP_LEVEL_CONTROL_FORMS_KEY,
2214                                         "",
2215                                         true,
2216                                         access_mask,
2217                                         &hive_hnd,
2218                                         &key_hnd);
2219         if (!W_ERROR_IS_OK(result)) {
2220                 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
2221                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2222                 goto done;
2223         }
2224
2225         /* If form_name != form->form_name then we renamed the form */
2226         if (strequal(form_name, form->form_name)) {
2227                 result = winreg_printer_deleteform1(tmp_ctx, server_info, form_name);
2228                 if (!W_ERROR_IS_OK(result)) {
2229                         DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
2230                                   TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2231                         goto done;
2232                 }
2233         }
2234
2235         wvalue.name = form->form_name;
2236
2237         blob = data_blob_talloc(tmp_ctx, NULL, 32);
2238         SIVAL(blob.data,  0, form->size.width);
2239         SIVAL(blob.data,  4, form->size.height);
2240         SIVAL(blob.data,  8, form->area.left);
2241         SIVAL(blob.data, 12, form->area.top);
2242         SIVAL(blob.data, 16, form->area.right);
2243         SIVAL(blob.data, 20, form->area.bottom);
2244         SIVAL(blob.data, 24, 42);
2245         SIVAL(blob.data, 28, form->flags);
2246
2247         status = rpccli_winreg_SetValue(winreg_pipe,
2248                                         tmp_ctx,
2249                                         &key_hnd,
2250                                         wvalue,
2251                                         REG_BINARY,
2252                                         blob.data,
2253                                         blob.length,
2254                                         &result);
2255         if (!NT_STATUS_IS_OK(status)) {
2256                 DEBUG(0, ("winreg_printer_setform1: Could not set value %s: %s\n",
2257                           wvalue.name, nt_errstr(status)));
2258                 if (!W_ERROR_IS_OK(result)) {
2259                         goto done;
2260                 }
2261                 result = ntstatus_to_werror(status);
2262                 goto done;
2263         }
2264
2265         result = WERR_OK;
2266 done:
2267         if (winreg_pipe != NULL) {
2268                 if (is_valid_policy_hnd(&key_hnd)) {
2269                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2270                 }
2271                 if (is_valid_policy_hnd(&hive_hnd)) {
2272                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2273                 }
2274         }
2275
2276         TALLOC_FREE(tmp_ctx);
2277         return result;
2278 }
2279
2280 WERROR winreg_printer_getform1(TALLOC_CTX *mem_ctx,
2281                                struct auth_serversupplied_info *server_info,
2282                                const char *form_name,
2283                                struct spoolss_FormInfo1 *r)
2284 {
2285         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2286         struct rpc_pipe_client *winreg_pipe = NULL;
2287         struct policy_handle hive_hnd, key_hnd;
2288         struct winreg_String wvalue;
2289         enum winreg_Type type_in;
2290         uint8_t *data_in;
2291         uint32_t data_in_size = 0;
2292         uint32_t value_len = 0;
2293         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2294         uint32_t i;
2295         WERROR result;
2296         NTSTATUS status;
2297         TALLOC_CTX *tmp_ctx;
2298
2299         /* check builtin forms first */
2300         for (i = 0; i < num_builtin; i++) {
2301                 if (strequal(builtin_forms1[i].form_name, form_name)) {
2302                         *r = builtin_forms1[i];
2303                         return WERR_OK;
2304                 }
2305         }
2306
2307         tmp_ctx = talloc_new(mem_ctx);
2308         if (tmp_ctx == NULL) {
2309                 return WERR_NOMEM;
2310         }
2311
2312         ZERO_STRUCT(hive_hnd);
2313         ZERO_STRUCT(key_hnd);
2314
2315         result = winreg_printer_openkey(tmp_ctx,
2316                                         server_info,
2317                                         &winreg_pipe,
2318                                         TOP_LEVEL_CONTROL_FORMS_KEY,
2319                                         "",
2320                                         true,
2321                                         access_mask,
2322                                         &hive_hnd,
2323                                         &key_hnd);
2324         if (!W_ERROR_IS_OK(result)) {
2325                 DEBUG(0, ("winreg_printer_getform1: Could not open key %s: %s\n",
2326                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2327                 goto done;
2328         }
2329
2330         wvalue.name = form_name;
2331
2332         /*
2333          * call QueryValue once with data == NULL to get the
2334          * needed memory size to be allocated, then allocate
2335          * data buffer and call again.
2336          */
2337         status = rpccli_winreg_QueryValue(winreg_pipe,
2338                                           tmp_ctx,
2339                                           &key_hnd,
2340                                           &wvalue,
2341                                           &type_in,
2342                                           NULL,
2343                                           &data_in_size,
2344                                           &value_len,
2345                                           &result);
2346         if (!NT_STATUS_IS_OK(status)) {
2347                 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
2348                           wvalue.name, nt_errstr(status)));
2349                 if (!W_ERROR_IS_OK(result)) {
2350                         goto done;
2351                 }
2352                 result = ntstatus_to_werror(status);
2353                 goto done;
2354         }
2355
2356         data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
2357         if (data_in == NULL) {
2358                 result = WERR_NOMEM;
2359                 goto done;
2360         }
2361         value_len = 0;
2362
2363         status = rpccli_winreg_QueryValue(winreg_pipe,
2364                                           tmp_ctx,
2365                                           &key_hnd,
2366                                           &wvalue,
2367                                           &type_in,
2368                                           data_in,
2369                                           &data_in_size,
2370                                           &value_len,
2371                                           &result);
2372         if (!NT_STATUS_IS_OK(status)) {
2373                 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
2374                           wvalue.name, nt_errstr(status)));
2375                 if (!W_ERROR_IS_OK(result)) {
2376                         goto done;
2377                 }
2378                 result = ntstatus_to_werror(status);
2379                 goto done;
2380         }
2381
2382         r->form_name = talloc_strdup(mem_ctx, form_name);
2383         if (r->form_name == NULL) {
2384                 result = WERR_NOMEM;
2385                 goto done;
2386         }
2387
2388         r->size.width  = IVAL(data_in,  0);
2389         r->size.height = IVAL(data_in,  4);
2390         r->area.left   = IVAL(data_in,  8);
2391         r->area.top    = IVAL(data_in, 12);
2392         r->area.right  = IVAL(data_in, 16);
2393         r->area.bottom = IVAL(data_in, 20);
2394         /* skip index    IVAL(data_in, 24)));*/
2395         r->flags       = IVAL(data_in, 28);
2396
2397         result = WERR_OK;
2398 done:
2399         if (winreg_pipe != NULL) {
2400                 if (is_valid_policy_hnd(&key_hnd)) {
2401                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2402                 }
2403                 if (is_valid_policy_hnd(&hive_hnd)) {
2404                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2405                 }
2406         }
2407
2408         TALLOC_FREE(tmp_ctx);
2409         return result;
2410 }
2411
2412 WERROR winreg_add_driver(TALLOC_CTX *mem_ctx,
2413                          struct auth_serversupplied_info *server_info,
2414                          struct spoolss_AddDriverInfoCtr *r,
2415                          const char **driver_name,
2416                          uint32_t *driver_version)
2417 {
2418         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2419         struct rpc_pipe_client *winreg_pipe = NULL;
2420         struct policy_handle hive_hnd, key_hnd;
2421         struct spoolss_DriverInfo8 info8;
2422         TALLOC_CTX *tmp_ctx;
2423         WERROR result;
2424
2425         ZERO_STRUCT(hive_hnd);
2426         ZERO_STRUCT(key_hnd);
2427         ZERO_STRUCT(info8);
2428
2429         if (!driver_info_ctr_to_info8(r, &info8)) {
2430                 result = WERR_INVALID_PARAMETER;
2431                 goto done;
2432         }
2433
2434         tmp_ctx = talloc_new(mem_ctx);
2435         if (tmp_ctx == NULL) {
2436                 return WERR_NOMEM;
2437         }
2438
2439         result = winreg_printer_opendriver(tmp_ctx,
2440                                            server_info,
2441                                            info8.driver_name,
2442                                            info8.architecture,
2443                                            info8.version,
2444                                            access_mask, true,
2445                                            &winreg_pipe,
2446                                            &hive_hnd,
2447                                            &key_hnd);
2448         if (!W_ERROR_IS_OK(result)) {
2449                 DEBUG(0, ("winreg_add_driver: "
2450                           "Could not open driver key (%s,%s,%d): %s\n",
2451                           info8.driver_name, info8.architecture,
2452                           info8.version, win_errstr(result)));
2453                 goto done;
2454         }
2455
2456         /* TODO: "Attributes" ? */
2457
2458         result = winreg_printer_write_dword(tmp_ctx, winreg_pipe,
2459                                             &key_hnd, "Version",
2460                                             info8.version);
2461         if (!W_ERROR_IS_OK(result)) {
2462                 goto done;
2463         }
2464
2465         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2466                                          &key_hnd, "Driver",
2467                                          info8.driver_path);
2468         if (!W_ERROR_IS_OK(result)) {
2469                 goto done;
2470         }
2471
2472         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2473                                          &key_hnd, "Data File",
2474                                          info8.data_file);
2475         if (!W_ERROR_IS_OK(result)) {
2476                 goto done;
2477         }
2478
2479         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2480                                          &key_hnd, "Configuration File",
2481                                          info8.config_file);
2482         if (!W_ERROR_IS_OK(result)) {
2483                 goto done;
2484         }
2485
2486         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2487                                          &key_hnd, "Help File",
2488                                          info8.help_file);
2489         if (!W_ERROR_IS_OK(result)) {
2490                 goto done;
2491         }
2492
2493         result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
2494                                                &key_hnd, "Dependent Files",
2495                                                info8.dependent_files);
2496         if (!W_ERROR_IS_OK(result)) {
2497                 goto done;
2498         }
2499
2500         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2501                                          &key_hnd, "Monitor",
2502                                          info8.monitor_name);
2503         if (!W_ERROR_IS_OK(result)) {
2504                 goto done;
2505         }
2506
2507         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2508                                          &key_hnd, "Datatype",
2509                                          info8.default_datatype);
2510         if (!W_ERROR_IS_OK(result)) {
2511                 goto done;
2512         }
2513
2514         result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
2515                                                &key_hnd, "Previous Names",
2516                                                info8.previous_names);
2517         if (!W_ERROR_IS_OK(result)) {
2518                 goto done;
2519         }
2520
2521         result = winreg_printer_write_date(tmp_ctx, winreg_pipe,
2522                                            &key_hnd, "DriverDate",
2523                                            info8.driver_date);
2524         if (!W_ERROR_IS_OK(result)) {
2525                 goto done;
2526         }
2527
2528         result = winreg_printer_write_ver(tmp_ctx, winreg_pipe,
2529                                           &key_hnd, "DriverVersion",
2530                                           info8.driver_version);
2531         if (!W_ERROR_IS_OK(result)) {
2532                 goto done;
2533         }
2534
2535         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2536                                          &key_hnd, "Manufacturer",
2537                                          info8.manufacturer_name);
2538         if (!W_ERROR_IS_OK(result)) {
2539                 goto done;
2540         }
2541
2542         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2543                                          &key_hnd, "OEM URL",
2544                                          info8.manufacturer_url);
2545         if (!W_ERROR_IS_OK(result)) {
2546                 goto done;
2547         }
2548
2549         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2550                                          &key_hnd, "HardwareID",
2551                                          info8.hardware_id);
2552         if (!W_ERROR_IS_OK(result)) {
2553                 goto done;
2554         }
2555
2556         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2557                                          &key_hnd, "Provider",
2558                                          info8.provider);
2559         if (!W_ERROR_IS_OK(result)) {
2560                 goto done;
2561         }
2562
2563         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2564                                          &key_hnd, "Print Processor",
2565                                          info8.print_processor);
2566         if (!W_ERROR_IS_OK(result)) {
2567                 goto done;
2568         }
2569
2570         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2571                                          &key_hnd, "VendorSetup",
2572                                          info8.vendor_setup);
2573         if (!W_ERROR_IS_OK(result)) {
2574                 goto done;
2575         }
2576
2577         result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
2578                                                &key_hnd, "Color Profiles",
2579                                                info8.color_profiles);
2580         if (!W_ERROR_IS_OK(result)) {
2581                 goto done;
2582         }
2583
2584         result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
2585                                          &key_hnd, "InfPath",
2586                                          info8.inf_path);
2587         if (!W_ERROR_IS_OK(result)) {
2588                 goto done;
2589         }
2590
2591         result = winreg_printer_write_dword(tmp_ctx, winreg_pipe, &key_hnd,
2592                                             "PrinterDriverAttributes",
2593                                             info8.printer_driver_attributes);
2594         if (!W_ERROR_IS_OK(result)) {
2595                 goto done;
2596         }
2597
2598         result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
2599                                                &key_hnd, "CoreDependencies",
2600                                                info8.core_driver_dependencies);
2601         if (!W_ERROR_IS_OK(result)) {
2602                 goto done;
2603         }
2604
2605         result = winreg_printer_write_date(tmp_ctx, winreg_pipe,
2606                                            &key_hnd, "MinInboxDriverVerDate",
2607                                            info8.min_inbox_driver_ver_date);
2608         if (!W_ERROR_IS_OK(result)) {
2609                 goto done;
2610         }
2611
2612         result = winreg_printer_write_ver(tmp_ctx, winreg_pipe, &key_hnd,
2613                                           "MinInboxDriverVerVersion",
2614                                           info8.min_inbox_driver_ver_version);
2615         if (!W_ERROR_IS_OK(result)) {
2616                 goto done;
2617         }
2618
2619         *driver_name = info8.driver_name;
2620         *driver_version = info8.version;
2621         result = WERR_OK;
2622 done:
2623         if (winreg_pipe != NULL) {
2624                 if (is_valid_policy_hnd(&key_hnd)) {
2625                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2626                 }
2627                 if (is_valid_policy_hnd(&hive_hnd)) {
2628                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2629                 }
2630         }
2631
2632         TALLOC_FREE(tmp_ctx);
2633         return result;
2634 }
2635
2636 WERROR winreg_get_driver(TALLOC_CTX *mem_ctx,
2637                          struct auth_serversupplied_info *server_info,
2638                          const char *architecture,
2639                          const char *driver_name,
2640                          uint32_t driver_version,
2641                          struct spoolss_DriverInfo8 **_info8)
2642 {
2643         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2644         struct rpc_pipe_client *winreg_pipe = NULL;
2645         struct policy_handle hive_hnd, key_hnd;
2646         struct spoolss_DriverInfo8 i8, *info8;
2647         struct spoolss_PrinterEnumValues *enum_values = NULL;
2648         struct spoolss_PrinterEnumValues *v;
2649         uint32_t num_values = 0;
2650         TALLOC_CTX *tmp_ctx;
2651         WERROR result;
2652         uint32_t i;
2653
2654         ZERO_STRUCT(hive_hnd);
2655         ZERO_STRUCT(key_hnd);
2656         ZERO_STRUCT(i8);
2657
2658         tmp_ctx = talloc_new(mem_ctx);
2659         if (tmp_ctx == NULL) {
2660                 return WERR_NOMEM;
2661         }
2662
2663         if (driver_version == DRIVER_ANY_VERSION) {
2664                 /* look for Win2k first and then for NT4 */
2665                 result = winreg_printer_opendriver(tmp_ctx,
2666                                                    server_info,
2667                                                    driver_name,
2668                                                    architecture,
2669                                                    3,
2670                                                    access_mask, false,
2671                                                    &winreg_pipe,
2672                                                    &hive_hnd,
2673                                                    &key_hnd);
2674                 if (!W_ERROR_IS_OK(result)) {
2675                         result = winreg_printer_opendriver(tmp_ctx,
2676                                                            server_info,
2677                                                            driver_name,
2678                                                            architecture,
2679                                                            2,
2680                                                            access_mask, false,
2681                                                            &winreg_pipe,
2682                                                            &hive_hnd,
2683                                                            &key_hnd);
2684                 }
2685         } else {
2686                 /* ok normal case */
2687                 result = winreg_printer_opendriver(tmp_ctx,
2688                                                    server_info,
2689                                                    driver_name,
2690                                                    architecture,
2691                                                    driver_version,
2692                                                    access_mask, false,
2693                                                    &winreg_pipe,
2694                                                    &hive_hnd,
2695                                                    &key_hnd);
2696         }
2697         if (!W_ERROR_IS_OK(result)) {
2698                 DEBUG(5, ("winreg_get_driver: "
2699                           "Could not open driver key (%s,%s,%d): %s\n",
2700                           driver_name, architecture,
2701                           driver_version, win_errstr(result)));
2702                 goto done;
2703         }
2704
2705         result = winreg_printer_enumvalues(tmp_ctx,
2706                                            winreg_pipe,
2707                                            &key_hnd,
2708                                            &num_values,
2709                                            &enum_values);
2710         if (!W_ERROR_IS_OK(result)) {
2711                 DEBUG(0, ("winreg_get_driver: "
2712                           "Could not enumerate values for (%s,%s,%d): %s\n",
2713                           driver_name, architecture,
2714                           driver_version, win_errstr(result)));
2715                 goto done;
2716         }
2717
2718         info8 = talloc_zero(tmp_ctx, struct spoolss_DriverInfo8);
2719         if (!info8) {
2720                 result = WERR_NOMEM;
2721                 goto done;
2722         }
2723
2724         result = WERR_OK;
2725
2726         for (i = 0; i < num_values; i++) {
2727                 const char *tmp_str;
2728
2729                 v = &enum_values[i];
2730
2731 #define CHECK_ERROR(result) \
2732         if (W_ERROR_IS_OK(result)) continue; \
2733         if (W_ERROR_EQUAL(result, WERR_NOT_FOUND)) result = WERR_OK; \
2734         if (!W_ERROR_IS_OK(result)) break
2735
2736                 result = winreg_enumval_to_dword(info8, v,
2737                                                  "Version",
2738                                                  &info8->version);
2739                 CHECK_ERROR(result);
2740
2741                 result = winreg_enumval_to_sz(info8, v,
2742                                               "Driver",
2743                                               &info8->driver_path);
2744                 CHECK_ERROR(result);
2745
2746                 result = winreg_enumval_to_sz(info8, v,
2747                                               "Data File",
2748                                               &info8->data_file);
2749                 CHECK_ERROR(result);
2750
2751                 result = winreg_enumval_to_sz(info8, v,
2752                                               "Configuration File",
2753                                               &info8->config_file);
2754                 CHECK_ERROR(result);
2755
2756                 result = winreg_enumval_to_sz(info8, v,
2757                                               "Help File",
2758                                               &info8->help_file);
2759                 CHECK_ERROR(result);
2760
2761                 result = winreg_enumval_to_multi_sz(info8, v,
2762                                                     "Dependent Files",
2763                                                     &info8->dependent_files);
2764                 CHECK_ERROR(result);
2765
2766                 result = winreg_enumval_to_sz(info8, v,
2767                                               "Monitor",
2768                                               &info8->monitor_name);
2769                 CHECK_ERROR(result);
2770
2771                 result = winreg_enumval_to_sz(info8, v,
2772                                               "Datatype",
2773                                               &info8->default_datatype);
2774                 CHECK_ERROR(result);
2775
2776                 result = winreg_enumval_to_multi_sz(info8, v,
2777                                                     "Previous Names",
2778                                                     &info8->previous_names);
2779                 CHECK_ERROR(result);
2780
2781                 result = winreg_enumval_to_sz(info8, v,
2782                                               "DriverDate",
2783                                               &tmp_str);
2784                 if (W_ERROR_IS_OK(result)) {
2785                         result = winreg_printer_date_to_NTTIME(tmp_str,
2786                                                 &info8->driver_date);
2787                 }
2788                 CHECK_ERROR(result);
2789
2790                 result = winreg_enumval_to_sz(info8, v,
2791                                               "DriverVersion",
2792                                               &tmp_str);
2793                 if (W_ERROR_IS_OK(result)) {
2794                         result = winreg_printer_ver_to_dword(tmp_str,
2795                                                 &info8->driver_version);
2796                 }
2797                 CHECK_ERROR(result);
2798
2799                 result = winreg_enumval_to_sz(info8, v,
2800                                               "Manufacturer",
2801                                               &info8->manufacturer_name);
2802                 CHECK_ERROR(result);
2803
2804                 result = winreg_enumval_to_sz(info8, v,
2805                                               "OEM URL",
2806                                               &info8->manufacturer_url);
2807                 CHECK_ERROR(result);
2808
2809                 result = winreg_enumval_to_sz(info8, v,
2810                                               "HardwareID",
2811                                               &info8->hardware_id);
2812                 CHECK_ERROR(result);
2813
2814                 result = winreg_enumval_to_sz(info8, v,
2815                                               "Provider",
2816                                               &info8->provider);
2817                 CHECK_ERROR(result);
2818
2819                 result = winreg_enumval_to_sz(info8, v,
2820                                               "Print Processor",
2821                                               &info8->print_processor);
2822                 CHECK_ERROR(result);
2823
2824                 result = winreg_enumval_to_sz(info8, v,
2825                                               "VendorSetup",
2826                                               &info8->vendor_setup);
2827                 CHECK_ERROR(result);
2828
2829                 result = winreg_enumval_to_multi_sz(info8, v,
2830                                                     "Color Profiles",
2831                                                     &info8->color_profiles);
2832                 CHECK_ERROR(result);
2833
2834                 result = winreg_enumval_to_sz(info8, v,
2835                                               "InfPath",
2836                                               &info8->inf_path);
2837                 CHECK_ERROR(result);
2838
2839                 result = winreg_enumval_to_dword(info8, v,
2840                                                  "PrinterDriverAttributes",
2841                                                  &info8->printer_driver_attributes);
2842                 CHECK_ERROR(result);
2843
2844                 result = winreg_enumval_to_multi_sz(info8, v,
2845                                                     "CoreDependencies",
2846                                                     &info8->core_driver_dependencies);
2847                 CHECK_ERROR(result);
2848
2849                 result = winreg_enumval_to_sz(info8, v,
2850                                               "MinInboxDriverVerDate",
2851                                               &tmp_str);
2852                 if (W_ERROR_IS_OK(result)) {
2853                         result = winreg_printer_date_to_NTTIME(tmp_str,
2854                                         &info8->min_inbox_driver_ver_date);
2855                 }
2856                 CHECK_ERROR(result);
2857
2858                 result = winreg_enumval_to_sz(info8, v,
2859                                               "MinInboxDriverVerVersion",
2860                                               &tmp_str);
2861                 if (W_ERROR_IS_OK(result)) {
2862                         result = winreg_printer_ver_to_dword(tmp_str,
2863                                         &info8->min_inbox_driver_ver_version);
2864                 }
2865                 CHECK_ERROR(result);
2866         }
2867
2868         if (!W_ERROR_IS_OK(result)) {
2869                 DEBUG(0, ("winreg_enumval_to_TYPE() failed "
2870                           "for %s: %s\n", v->value_name,
2871                           win_errstr(result)));
2872                 goto done;
2873         }
2874
2875         *_info8 = talloc_steal(mem_ctx, info8);
2876         result = WERR_OK;
2877 done:
2878         if (winreg_pipe != NULL) {
2879                 if (is_valid_policy_hnd(&key_hnd)) {
2880                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2881                 }
2882                 if (is_valid_policy_hnd(&hive_hnd)) {
2883                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2884                 }
2885         }
2886
2887         TALLOC_FREE(tmp_ctx);
2888         return result;
2889 }
2890
2891 WERROR winreg_del_driver(TALLOC_CTX *mem_ctx,
2892                          struct auth_serversupplied_info *server_info,
2893                          struct spoolss_DriverInfo8 *info8,
2894                          uint32_t version)
2895 {
2896         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2897         struct rpc_pipe_client *winreg_pipe = NULL;
2898         struct policy_handle hive_hnd, key_hnd;
2899         TALLOC_CTX *tmp_ctx;
2900         char *key_name;
2901         WERROR result;
2902
2903         ZERO_STRUCT(hive_hnd);
2904         ZERO_STRUCT(key_hnd);
2905
2906         tmp_ctx = talloc_new(mem_ctx);
2907         if (tmp_ctx == NULL) {
2908                 return WERR_NOMEM;
2909         }
2910
2911         /* test that the key exists */
2912         result = winreg_printer_opendriver(tmp_ctx,
2913                                            server_info,
2914                                            info8->driver_name,
2915                                            info8->architecture,
2916                                            version,
2917                                            access_mask, false,
2918                                            &winreg_pipe,
2919                                            &hive_hnd,
2920                                            &key_hnd);
2921         if (!W_ERROR_IS_OK(result)) {
2922                 /* key doesn't exist */
2923                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2924                         result = WERR_OK;
2925                         goto done;
2926                 }
2927
2928                 DEBUG(5, ("winreg_del_driver: "
2929                           "Could not open driver (%s,%s,%u): %s\n",
2930                           info8->driver_name, info8->architecture,
2931                           version, win_errstr(result)));
2932                 goto done;
2933         }
2934
2935
2936         if (is_valid_policy_hnd(&key_hnd)) {
2937                 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2938         }
2939
2940         key_name = talloc_asprintf(tmp_ctx,
2941                                    "%s\\Environments\\%s\\Drivers\\Version-%u",
2942                                    TOP_LEVEL_CONTROL_KEY,
2943                                    info8->architecture, version);
2944         if (key_name == NULL) {
2945                 result = WERR_NOMEM;
2946                 goto done;
2947         }
2948
2949         result = winreg_printer_delete_subkeys(tmp_ctx,
2950                                                winreg_pipe,
2951                                                &hive_hnd,
2952                                                access_mask,
2953                                                key_name);
2954         if (!W_ERROR_IS_OK(result)) {
2955                 DEBUG(0, ("winreg_del_driver: "
2956                           "Could not open driver (%s,%s,%u): %s\n",
2957                           info8->driver_name, info8->architecture,
2958                           version, win_errstr(result)));
2959                 goto done;
2960         }
2961
2962         result = WERR_OK;
2963 done:
2964         if (winreg_pipe != NULL) {
2965                 if (is_valid_policy_hnd(&key_hnd)) {
2966                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2967                 }
2968                 if (is_valid_policy_hnd(&hive_hnd)) {
2969                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2970                 }
2971         }
2972
2973         TALLOC_FREE(tmp_ctx);
2974         return result;
2975 }