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