s3-spoolss: Fixed the DeviceMode handling in winreg.
[ira/wip.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, NULL, &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, NULL, &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, NULL, 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, NULL, v->data, array)) {
1136                 return WERR_NOMEM;
1137         }
1138
1139         return WERR_OK;
1140 }
1141
1142 static WERROR winreg_enumval_to_blob(TALLOC_CTX *mem_ctx,
1143                                      struct spoolss_PrinterEnumValues *v,
1144                                      const char *valuename,
1145                                      DATA_BLOB *blob)
1146 {
1147         /* just return if it is not the one we are looking for */
1148         if (strcmp(valuename, v->value_name) != 0) {
1149                 return WERR_NOT_FOUND;
1150         }
1151
1152         if (v->type != REG_BINARY) {
1153                 return WERR_INVALID_DATATYPE;
1154         }
1155
1156         blob->data = v->data->data;
1157         blob->length = v->data_length;
1158
1159         return WERR_OK;
1160 }
1161
1162 static WERROR winreg_printer_write_date(TALLOC_CTX *mem_ctx,
1163                                         struct rpc_pipe_client *pipe_handle,
1164                                         struct policy_handle *key_handle,
1165                                         const char *value,
1166                                         NTTIME data)
1167 {
1168         struct winreg_String wvalue;
1169         DATA_BLOB blob;
1170         WERROR result = WERR_OK;
1171         NTSTATUS status;
1172         const char *str;
1173         struct tm *tm;
1174         time_t t;
1175
1176         t = nt_time_to_unix(data);
1177         tm = localtime(&t);
1178         str = talloc_asprintf(mem_ctx, "%02d/%02d/%04d",
1179                               tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
1180         if (!str) {
1181                 return WERR_NOMEM;
1182         }
1183
1184         wvalue.name = value;
1185         if (!push_reg_sz(mem_ctx, NULL, &blob, str)) {
1186                 return WERR_NOMEM;
1187         }
1188         status = rpccli_winreg_SetValue(pipe_handle,
1189                                         mem_ctx,
1190                                         key_handle,
1191                                         wvalue,
1192                                         REG_SZ,
1193                                         blob.data,
1194                                         blob.length,
1195                                         &result);
1196         if (!NT_STATUS_IS_OK(status)) {
1197                 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
1198                         wvalue.name, win_errstr(result)));
1199                 if (!W_ERROR_IS_OK(result)) {
1200                         result = ntstatus_to_werror(status);
1201                 }
1202         }
1203
1204         return result;
1205 }
1206
1207 static WERROR winreg_printer_date_to_NTTIME(const char *str, NTTIME *data)
1208 {
1209         struct tm tm;
1210         time_t t;
1211
1212         ZERO_STRUCT(tm);
1213
1214         if (sscanf(str, "%d/%d/%d",
1215                    &tm.tm_mon, &tm.tm_mday, &tm.tm_year) != 3) {
1216                 return WERR_INVALID_PARAMETER;
1217         }
1218         tm.tm_mon -= 1;
1219         tm.tm_year -= 1900;
1220         tm.tm_isdst = -1;
1221
1222         t = mktime(&tm);
1223         unix_to_nt_time(data, t);
1224
1225         return WERR_OK;
1226 }
1227
1228 static WERROR winreg_printer_write_ver(TALLOC_CTX *mem_ctx,
1229                                        struct rpc_pipe_client *pipe_handle,
1230                                        struct policy_handle *key_handle,
1231                                        const char *value,
1232                                        uint64_t data)
1233 {
1234         struct winreg_String wvalue;
1235         DATA_BLOB blob;
1236         WERROR result = WERR_OK;
1237         NTSTATUS status;
1238         char *str;
1239
1240         /* FIXME: check format is right,
1241          *      this needs to be something like: 6.1.7600.16385 */
1242         str = talloc_asprintf(mem_ctx, "%u.%u.%u.%u",
1243                               (unsigned)((data >> 48) & 0xFFFF),
1244                               (unsigned)((data >> 32) & 0xFFFF),
1245                               (unsigned)((data >> 16) & 0xFFFF),
1246                               (unsigned)(data & 0xFFFF));
1247         if (!str) {
1248                 return WERR_NOMEM;
1249         }
1250
1251         wvalue.name = value;
1252         if (!push_reg_sz(mem_ctx, NULL, &blob, str)) {
1253                 return WERR_NOMEM;
1254         }
1255         status = rpccli_winreg_SetValue(pipe_handle,
1256                                         mem_ctx,
1257                                         key_handle,
1258                                         wvalue,
1259                                         REG_SZ,
1260                                         blob.data,
1261                                         blob.length,
1262                                         &result);
1263         if (!NT_STATUS_IS_OK(status)) {
1264                 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
1265                         wvalue.name, win_errstr(result)));
1266                 if (!W_ERROR_IS_OK(result)) {
1267                         result = ntstatus_to_werror(status);
1268                 }
1269         }
1270
1271         return result;
1272 }
1273
1274 static WERROR winreg_printer_ver_to_dword(const char *str, uint64_t *data)
1275 {
1276         unsigned int v1, v2, v3, v4;
1277
1278         if (sscanf(str, "%u.%u.%u.%u", &v1, &v2, &v3, &v4) != 4) {
1279                 return WERR_INVALID_PARAMETER;
1280         }
1281
1282         *data = ((uint64_t)(v1 & 0xFFFF) << 48) +
1283                 ((uint64_t)(v2 & 0xFFFF) << 32) +
1284                 ((uint64_t)(v3 & 0xFFFF) << 16) +
1285                 (uint64_t)(v2 & 0xFFFF);
1286
1287         return WERR_OK;
1288 }
1289
1290 /********************************************************************
1291  Public winreg function for spoolss
1292 ********************************************************************/
1293
1294 WERROR winreg_create_printer(TALLOC_CTX *mem_ctx,
1295                              struct auth_serversupplied_info *server_info,
1296                              const char *servername,
1297                              const char *sharename)
1298 {
1299         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1300         struct rpc_pipe_client *winreg_pipe = NULL;
1301         struct policy_handle hive_hnd, key_hnd;
1302         struct spoolss_SetPrinterInfo2 *info2;
1303         struct security_descriptor *secdesc;
1304         struct winreg_String wkey, wkeyclass;
1305         const char *path;
1306         const char *subkeys[] = { "DsDriver", "DsSpooler", "PrinterDriverData" };
1307         uint32_t i, count = ARRAY_SIZE(subkeys);
1308         uint32_t info2_mask = 0;
1309         WERROR result = WERR_OK;
1310         TALLOC_CTX *tmp_ctx;
1311
1312         tmp_ctx = talloc_new(mem_ctx);
1313         if (tmp_ctx == NULL) {
1314                 return WERR_NOMEM;
1315         }
1316
1317         path = winreg_printer_data_keyname(tmp_ctx, sharename);
1318         if (path == NULL) {
1319                 TALLOC_FREE(tmp_ctx);
1320                 return WERR_NOMEM;
1321         }
1322
1323         ZERO_STRUCT(hive_hnd);
1324         ZERO_STRUCT(key_hnd);
1325
1326         result = winreg_printer_openkey(tmp_ctx,
1327                                         server_info,
1328                                         &winreg_pipe,
1329                                         path,
1330                                         "",
1331                                         false,
1332                                         access_mask,
1333                                         &hive_hnd,
1334                                         &key_hnd);
1335         if (W_ERROR_IS_OK(result)) {
1336                 DEBUG(2, ("winreg_create_printer: Skipping, %s already exists\n", path));
1337                 goto done;
1338         } else if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1339                 DEBUG(2, ("winreg_create_printer: Creating default values in %s\n", path));
1340         } else if (!W_ERROR_IS_OK(result)) {
1341                 DEBUG(0, ("winreg_create_printer: Could not open key %s: %s\n",
1342                         path, win_errstr(result)));
1343                 goto done;
1344         }
1345
1346         /* Create the main key */
1347         result = winreg_printer_openkey(tmp_ctx,
1348                                         server_info,
1349                                         &winreg_pipe,
1350                                         path,
1351                                         "",
1352                                         true,
1353                                         access_mask,
1354                                         &hive_hnd,
1355                                         &key_hnd);
1356         if (!W_ERROR_IS_OK(result)) {
1357                 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
1358                         path, win_errstr(result)));
1359                 goto done;
1360         }
1361
1362         if (is_valid_policy_hnd(&key_hnd)) {
1363                 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1364         }
1365
1366         /* Create subkeys */
1367         for (i = 0; i < count; i++) {
1368                 NTSTATUS status;
1369                 enum winreg_CreateAction action = REG_ACTION_NONE;
1370
1371                 ZERO_STRUCT(key_hnd);
1372                 ZERO_STRUCT(wkey);
1373
1374                 wkey.name = talloc_asprintf(tmp_ctx, "%s\\%s", path, subkeys[i]);
1375                 if (wkey.name == NULL) {
1376                         result = WERR_NOMEM;
1377                         goto done;
1378                 }
1379
1380                 ZERO_STRUCT(wkeyclass);
1381                 wkeyclass.name = "";
1382
1383                 status = rpccli_winreg_CreateKey(winreg_pipe,
1384                                                  tmp_ctx,
1385                                                  &hive_hnd,
1386                                                  wkey,
1387                                                  wkeyclass,
1388                                                  0,
1389                                                  access_mask,
1390                                                  NULL,
1391                                                  &key_hnd,
1392                                                  &action,
1393                                                  &result);
1394                 if (!NT_STATUS_IS_OK(status)) {
1395                         DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
1396                                 wkey.name, win_errstr(result)));
1397                         if (!W_ERROR_IS_OK(result)) {
1398                                 result = ntstatus_to_werror(status);
1399                         }
1400                         goto done;
1401                 }
1402
1403                 if (is_valid_policy_hnd(&key_hnd)) {
1404                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1405                 }
1406         }
1407         info2 = talloc_zero(tmp_ctx, struct spoolss_SetPrinterInfo2);
1408         if (info2 == NULL) {
1409                 result = WERR_NOMEM;
1410                 goto done;
1411         }
1412
1413         if (servername != NULL) {
1414                 info2->printername = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
1415                                                      servername, sharename);
1416         } else {
1417                 info2->printername = sharename;
1418         }
1419         if (info2->printername == NULL) {
1420                 result = WERR_NOMEM;
1421                 goto done;
1422         }
1423         info2_mask |= SPOOLSS_PRINTER_INFO_PRINTERNAME;
1424
1425         info2->sharename = sharename;
1426         info2_mask |= SPOOLSS_PRINTER_INFO_SHARENAME;
1427
1428         info2->portname = SAMBA_PRINTER_PORT_NAME;
1429         info2_mask |= SPOOLSS_PRINTER_INFO_PORTNAME;
1430
1431         info2->printprocessor = "winprint";
1432         info2_mask |= SPOOLSS_PRINTER_INFO_PRINTPROCESSOR;
1433
1434         info2->datatype = "RAW";
1435         info2_mask |= SPOOLSS_PRINTER_INFO_DATATYPE;
1436
1437         info2->comment = "";
1438         info2_mask |= SPOOLSS_PRINTER_INFO_COMMENT;
1439
1440         info2->attributes = PRINTER_ATTRIBUTE_SAMBA;
1441         info2_mask |= SPOOLSS_PRINTER_INFO_ATTRIBUTES;
1442
1443         info2->starttime = 0; /* Minutes since 12:00am GMT */
1444         info2_mask |= SPOOLSS_PRINTER_INFO_STARTTIME;
1445
1446         info2->untiltime = 0; /* Minutes since 12:00am GMT */
1447         info2_mask |= SPOOLSS_PRINTER_INFO_UNTILTIME;
1448
1449         info2->priority = 1;
1450         info2_mask |= SPOOLSS_PRINTER_INFO_PRIORITY;
1451
1452         info2->defaultpriority = 1;
1453         info2_mask |= SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY;
1454
1455         result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1456         if (!W_ERROR_IS_OK(result)) {
1457                 goto done;
1458         }
1459         info2_mask |= SPOOLSS_PRINTER_INFO_SECDESC;
1460
1461         /*
1462          * Don't write a default Device Mode to the registry! The Device Mode is
1463          * only written to disk with a SetPrinter level 2 or 8.
1464          */
1465
1466         result = winreg_update_printer(tmp_ctx,
1467                                        server_info,
1468                                        sharename,
1469                                        info2_mask,
1470                                        info2,
1471                                        NULL,
1472                                        secdesc);
1473
1474 done:
1475         if (winreg_pipe != NULL) {
1476                 if (is_valid_policy_hnd(&key_hnd)) {
1477                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1478                 }
1479                 if (is_valid_policy_hnd(&hive_hnd)) {
1480                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1481                 }
1482         }
1483
1484         talloc_free(tmp_ctx);
1485         return result;
1486 }
1487
1488 WERROR winreg_update_printer(TALLOC_CTX *mem_ctx,
1489                              struct auth_serversupplied_info *server_info,
1490                              const char *sharename,
1491                              uint32_t info2_mask,
1492                              struct spoolss_SetPrinterInfo2 *info2,
1493                              struct spoolss_DeviceMode *devmode,
1494                              struct security_descriptor *secdesc)
1495 {
1496         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1497         struct rpc_pipe_client *winreg_pipe = NULL;
1498         struct policy_handle hive_hnd, key_hnd;
1499         int snum = lp_servicenumber(sharename);
1500         enum ndr_err_code ndr_err;
1501         DATA_BLOB blob;
1502         char *path;
1503         WERROR result = WERR_OK;
1504         TALLOC_CTX *tmp_ctx;
1505
1506         tmp_ctx = talloc_new(mem_ctx);
1507         if (tmp_ctx == NULL) {
1508                 return WERR_NOMEM;
1509         }
1510
1511         path = winreg_printer_data_keyname(tmp_ctx, sharename);
1512         if (path == NULL) {
1513                 TALLOC_FREE(tmp_ctx);
1514                 return WERR_NOMEM;
1515         }
1516
1517         ZERO_STRUCT(hive_hnd);
1518         ZERO_STRUCT(key_hnd);
1519
1520         result = winreg_printer_openkey(tmp_ctx,
1521                                         server_info,
1522                                         &winreg_pipe,
1523                                         path,
1524                                         "",
1525                                         true,
1526                                         access_mask,
1527                                         &hive_hnd,
1528                                         &key_hnd);
1529         if (!W_ERROR_IS_OK(result)) {
1530                 DEBUG(0, ("winreg_update_printer: Could not open key %s: %s\n",
1531                         path, win_errstr(result)));
1532                 goto done;
1533         }
1534
1535         if (info2_mask & SPOOLSS_PRINTER_INFO_ATTRIBUTES) {
1536                 result = winreg_printer_write_dword(tmp_ctx,
1537                                                     winreg_pipe,
1538                                                     &key_hnd,
1539                                                     "Attributes",
1540                                                     info2->attributes);
1541                 if (!W_ERROR_IS_OK(result)) {
1542                         goto done;
1543                 }
1544         }
1545
1546 #if 0
1547         if (info2_mask & SPOOLSS_PRINTER_INFO_AVERAGEPPM) {
1548                 result = winreg_printer_write_dword(tmp_ctx,
1549                                                     winreg_pipe,
1550                                                     &key_hnd,
1551                                                     "AveragePpm",
1552                                                     info2->attributes);
1553                 if (!W_ERROR_IS_OK(result)) {
1554                         goto done;
1555                 }
1556         }
1557 #endif
1558
1559         if (info2_mask & SPOOLSS_PRINTER_INFO_COMMENT) {
1560                 result = winreg_printer_write_sz(tmp_ctx,
1561                                                  winreg_pipe,
1562                                                  &key_hnd,
1563                                                  "Description",
1564                                                  info2->comment);
1565                 if (!W_ERROR_IS_OK(result)) {
1566                         goto done;
1567                 }
1568         }
1569
1570         if (info2_mask & SPOOLSS_PRINTER_INFO_DATATYPE) {
1571                 result = winreg_printer_write_sz(tmp_ctx,
1572                                                  winreg_pipe,
1573                                                  &key_hnd,
1574                                                  "Datatype",
1575                                                  info2->datatype);
1576                 if (!W_ERROR_IS_OK(result)) {
1577                         goto done;
1578                 }
1579         }
1580
1581         if (info2_mask & SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY) {
1582                 result = winreg_printer_write_dword(tmp_ctx,
1583                                                     winreg_pipe,
1584                                                     &key_hnd,
1585                                                     "Default Priority",
1586                                                     info2->defaultpriority);
1587                 if (!W_ERROR_IS_OK(result)) {
1588                         goto done;
1589                 }
1590         }
1591
1592         if (info2_mask & SPOOLSS_PRINTER_INFO_DEVMODE) {
1593                 /*
1594                  * Some client drivers freak out if there is a NULL devmode
1595                  * (probably the driver is not checking before accessing
1596                  * the devmode pointer)   --jerry
1597                  */
1598                 if (devmode == NULL && lp_default_devmode(snum) && info2 != NULL) {
1599                         result = spoolss_create_default_devmode(tmp_ctx,
1600                                                                 info2->printername,
1601                                                                 &devmode);
1602                         if (!W_ERROR_IS_OK(result)) {
1603                                 goto done;
1604                         }
1605                 }
1606                 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, NULL, devmode,
1607                                 (ndr_push_flags_fn_t) ndr_push_spoolss_DeviceMode);
1608                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1609                         DEBUG(0, ("winreg_update_printer: Failed to marshall device mode\n"));
1610                         result = WERR_NOMEM;
1611                         goto done;
1612                 }
1613
1614                 result = winreg_printer_write_binary(tmp_ctx,
1615                                                      winreg_pipe,
1616                                                      &key_hnd,
1617                                                      "Default DevMode",
1618                                                      blob);
1619                 if (!W_ERROR_IS_OK(result)) {
1620                         goto done;
1621                 }
1622         }
1623
1624         if (info2_mask & SPOOLSS_PRINTER_INFO_DRIVERNAME) {
1625                 result = winreg_printer_write_sz(tmp_ctx,
1626                                                  winreg_pipe,
1627                                                  &key_hnd,
1628                                                  "Printer Driver",
1629                                                  info2->drivername);
1630                 if (!W_ERROR_IS_OK(result)) {
1631                         goto done;
1632                 }
1633         }
1634
1635         if (info2_mask & SPOOLSS_PRINTER_INFO_LOCATION) {
1636                 result = winreg_printer_write_sz(tmp_ctx,
1637                                                  winreg_pipe,
1638                                                  &key_hnd,
1639                                                  "Location",
1640                                                  info2->location);
1641                 if (!W_ERROR_IS_OK(result)) {
1642                         goto done;
1643                 }
1644         }
1645
1646         if (info2_mask & SPOOLSS_PRINTER_INFO_PARAMETERS) {
1647                 result = winreg_printer_write_sz(tmp_ctx,
1648                                                  winreg_pipe,
1649                                                  &key_hnd,
1650                                                  "Parameters",
1651                                                  info2->parameters);
1652                 if (!W_ERROR_IS_OK(result)) {
1653                         goto done;
1654                 }
1655         }
1656
1657         if (info2_mask & SPOOLSS_PRINTER_INFO_PORTNAME) {
1658                 result = winreg_printer_write_sz(tmp_ctx,
1659                                                  winreg_pipe,
1660                                                  &key_hnd,
1661                                                  "Port",
1662                                                  info2->portname);
1663                 if (!W_ERROR_IS_OK(result)) {
1664                         goto done;
1665                 }
1666         }
1667
1668         if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTERNAME) {
1669                 /*
1670                  * in addprinter: no servername and the printer is the name
1671                  * in setprinter: servername is \\server
1672                  *                and printer is \\server\\printer
1673                  *
1674                  * Samba manages only local printers.
1675                  * we currently don't support things like i
1676                  * path=\\other_server\printer
1677                  *
1678                  * We only store the printername, not \\server\printername
1679                  */
1680                 const char *p = strrchr(info2->printername, '\\');
1681                 if (p == NULL) {
1682                         p = info2->printername;
1683                 } else {
1684                         p++;
1685                 }
1686                 result = winreg_printer_write_sz(tmp_ctx,
1687                                                  winreg_pipe,
1688                                                  &key_hnd,
1689                                                  "Name",
1690                                                  p);
1691                 if (!W_ERROR_IS_OK(result)) {
1692                         goto done;
1693                 }
1694         }
1695
1696         if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTPROCESSOR) {
1697                 result = winreg_printer_write_sz(tmp_ctx,
1698                                                  winreg_pipe,
1699                                                  &key_hnd,
1700                                                  "Print Processor",
1701                                                  info2->printprocessor);
1702                 if (!W_ERROR_IS_OK(result)) {
1703                         goto done;
1704                 }
1705         }
1706
1707         if (info2_mask & SPOOLSS_PRINTER_INFO_PRIORITY) {
1708                 result = winreg_printer_write_dword(tmp_ctx,
1709                                                     winreg_pipe,
1710                                                     &key_hnd,
1711                                                     "Priority",
1712                                                     info2->priority);
1713                 if (!W_ERROR_IS_OK(result)) {
1714                         goto done;
1715                 }
1716         }
1717
1718         if (info2_mask & SPOOLSS_PRINTER_INFO_SECDESC) {
1719                 /*
1720                  * We need a security descriptor, if it isn't specified by
1721                  * AddPrinter{Ex} then create a default descriptor.
1722                  */
1723                 if (secdesc == NULL) {
1724                         result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1725                         if (!W_ERROR_IS_OK(result)) {
1726                                 goto done;
1727                         }
1728                 }
1729                 result = winreg_set_printer_secdesc(tmp_ctx,
1730                                                     server_info,
1731                                                     sharename,
1732                                                     secdesc);
1733                 if (!W_ERROR_IS_OK(result)) {
1734                         goto done;
1735                 }
1736         }
1737
1738         if (info2_mask & SPOOLSS_PRINTER_INFO_SEPFILE) {
1739                 result = winreg_printer_write_sz(tmp_ctx,
1740                                                  winreg_pipe,
1741                                                  &key_hnd,
1742                                                  "Separator File",
1743                                                  info2->sepfile);
1744                 if (!W_ERROR_IS_OK(result)) {
1745                         goto done;
1746                 }
1747         }
1748
1749         if (info2_mask & SPOOLSS_PRINTER_INFO_SHARENAME) {
1750                 result = winreg_printer_write_sz(tmp_ctx,
1751                                                  winreg_pipe,
1752                                                  &key_hnd,
1753                                                  "Share Name",
1754                                                  info2->sharename);
1755                 if (!W_ERROR_IS_OK(result)) {
1756                         goto done;
1757                 }
1758         }
1759
1760         if (info2_mask & SPOOLSS_PRINTER_INFO_STARTTIME) {
1761                 result = winreg_printer_write_dword(tmp_ctx,
1762                                                     winreg_pipe,
1763                                                     &key_hnd,
1764                                                     "StartTime",
1765                                                     info2->starttime);
1766                 if (!W_ERROR_IS_OK(result)) {
1767                         goto done;
1768                 }
1769         }
1770
1771         if (info2_mask & SPOOLSS_PRINTER_INFO_STATUS) {
1772                 result = winreg_printer_write_dword(tmp_ctx,
1773                                                     winreg_pipe,
1774                                                     &key_hnd,
1775                                                     "Status",
1776                                                     info2->status);
1777                 if (!W_ERROR_IS_OK(result)) {
1778                         goto done;
1779                 }
1780         }
1781
1782         if (info2_mask & SPOOLSS_PRINTER_INFO_UNTILTIME) {
1783                 result = winreg_printer_write_dword(tmp_ctx,
1784                                                     winreg_pipe,
1785                                                     &key_hnd,
1786                                                     "UntilTime",
1787                                                     info2->untiltime);
1788                 if (!W_ERROR_IS_OK(result)) {
1789                         goto done;
1790                 }
1791         }
1792
1793         result = winreg_printer_write_dword(tmp_ctx,
1794                                             winreg_pipe,
1795                                             &key_hnd,
1796                                             "ChangeID",
1797                                             winreg_printer_rev_changeid());
1798         if (!W_ERROR_IS_OK(result)) {
1799                 goto done;
1800         }
1801
1802         result = WERR_OK;
1803 done:
1804         if (winreg_pipe != NULL) {
1805                 if (is_valid_policy_hnd(&key_hnd)) {
1806                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1807                 }
1808                 if (is_valid_policy_hnd(&hive_hnd)) {
1809                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1810                 }
1811         }
1812
1813         TALLOC_FREE(tmp_ctx);
1814         return result;
1815 }
1816
1817 WERROR winreg_get_printer(TALLOC_CTX *mem_ctx,
1818                           struct auth_serversupplied_info *server_info,
1819                           const char *servername,
1820                           const char *printer,
1821                           struct spoolss_PrinterInfo2 **pinfo2)
1822 {
1823         struct spoolss_PrinterInfo2 *info2;
1824         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1825         struct rpc_pipe_client *winreg_pipe = NULL;
1826         struct policy_handle hive_hnd, key_hnd;
1827         struct spoolss_PrinterEnumValues *enum_values = NULL;
1828         struct spoolss_PrinterEnumValues *v;
1829         enum ndr_err_code ndr_err;
1830         DATA_BLOB blob;
1831         int snum = lp_servicenumber(printer);
1832         uint32_t num_values = 0;
1833         uint32_t i;
1834         char *path;
1835         WERROR result = WERR_OK;
1836         TALLOC_CTX *tmp_ctx;
1837
1838         tmp_ctx = talloc_new(mem_ctx);
1839         if (tmp_ctx == NULL) {
1840                 return WERR_NOMEM;
1841         }
1842
1843         path = winreg_printer_data_keyname(tmp_ctx, printer);
1844         if (path == NULL) {
1845                 TALLOC_FREE(tmp_ctx);
1846                 return WERR_NOMEM;
1847         }
1848
1849         result = winreg_printer_openkey(tmp_ctx,
1850                                         server_info,
1851                                         &winreg_pipe,
1852                                         path,
1853                                         "",
1854                                         false,
1855                                         access_mask,
1856                                         &hive_hnd,
1857                                         &key_hnd);
1858         if (!W_ERROR_IS_OK(result)) {
1859                 DEBUG(0, ("winreg_get_printer: Could not open key %s: %s\n",
1860                           path, win_errstr(result)));
1861                 goto done;
1862         }
1863
1864         result = winreg_printer_enumvalues(tmp_ctx,
1865                                            winreg_pipe,
1866                                            &key_hnd,
1867                                            &num_values,
1868                                            &enum_values);
1869         if (!W_ERROR_IS_OK(result)) {
1870                 DEBUG(0, ("winreg_get_printer: Could not enumerate values in %s: %s\n",
1871                           path, win_errstr(result)));
1872                 goto done;
1873         }
1874
1875         info2 = talloc_zero(tmp_ctx, struct spoolss_PrinterInfo2);
1876         if (info2 == NULL) {
1877                 result = WERR_NOMEM;
1878                 goto done;
1879         }
1880
1881         info2->servername     = EMPTY_STRING;
1882         info2->printername    = EMPTY_STRING;
1883         info2->sharename      = EMPTY_STRING;
1884         info2->portname       = EMPTY_STRING;
1885         info2->drivername     = EMPTY_STRING;
1886         info2->comment        = EMPTY_STRING;
1887         info2->location       = EMPTY_STRING;
1888         info2->sepfile        = EMPTY_STRING;
1889         info2->printprocessor = EMPTY_STRING;
1890         info2->datatype       = EMPTY_STRING;
1891         info2->parameters     = EMPTY_STRING;
1892
1893         if (servername != NULL && servername[0] != '\0') {
1894                 info2->servername = talloc_asprintf(info2, "\\\\%s", servername);
1895                 if (info2->servername == NULL) {
1896                         result = WERR_NOMEM;
1897                         goto done;
1898                 }
1899         }
1900
1901         for (i = 0; i < num_values; i++) {
1902                 v = &enum_values[i];
1903
1904                 result = winreg_enumval_to_sz(info2,
1905                                               v,
1906                                               "Name",
1907                                               &info2->printername);
1908                 CHECK_ERROR(result);
1909
1910                 result = winreg_enumval_to_sz(info2,
1911                                               v,
1912                                               "Share Name",
1913                                               &info2->sharename);
1914                 CHECK_ERROR(result);
1915
1916                 result = winreg_enumval_to_sz(info2,
1917                                               v,
1918                                               "Port",
1919                                               &info2->portname);
1920                 CHECK_ERROR(result);
1921
1922                 result = winreg_enumval_to_sz(info2,
1923                                               v,
1924                                               "Description",
1925                                               &info2->comment);
1926                 CHECK_ERROR(result);
1927
1928                 result = winreg_enumval_to_sz(info2,
1929                                               v,
1930                                               "Location",
1931                                               &info2->location);
1932                 CHECK_ERROR(result);
1933
1934                 result = winreg_enumval_to_sz(info2,
1935                                               v,
1936                                               "Separator File",
1937                                               &info2->sepfile);
1938                 CHECK_ERROR(result);
1939
1940                 result = winreg_enumval_to_sz(info2,
1941                                               v,
1942                                               "Print Processor",
1943                                               &info2->printprocessor);
1944                 CHECK_ERROR(result);
1945
1946                 result = winreg_enumval_to_sz(info2,
1947                                               v,
1948                                               "Datatype",
1949                                               &info2->datatype);
1950                 CHECK_ERROR(result);
1951
1952                 result = winreg_enumval_to_sz(info2,
1953                                               v,
1954                                               "Parameters",
1955                                               &info2->parameters);
1956                 CHECK_ERROR(result);
1957
1958                 result = winreg_enumval_to_sz(info2,
1959                                               v,
1960                                               "Printer Driver",
1961                                               &info2->drivername);
1962                 CHECK_ERROR(result);
1963
1964                 result = winreg_enumval_to_dword(info2,
1965                                                  v,
1966                                                  "Attributes",
1967                                                  &info2->attributes);
1968                 CHECK_ERROR(result);
1969
1970                 result = winreg_enumval_to_dword(info2,
1971                                                  v,
1972                                                  "Priority",
1973                                                  &info2->priority);
1974                 CHECK_ERROR(result);
1975
1976                 result = winreg_enumval_to_dword(info2,
1977                                                  v,
1978                                                  "Default Priority",
1979                                                  &info2->defaultpriority);
1980                 CHECK_ERROR(result);
1981
1982                 result = winreg_enumval_to_dword(info2,
1983                                                  v,
1984                                                  "StartTime",
1985                                                  &info2->starttime);
1986                 CHECK_ERROR(result);
1987
1988                 result = winreg_enumval_to_dword(info2,
1989                                                  v,
1990                                                  "UntilTime",
1991                                                  &info2->untiltime);
1992                 CHECK_ERROR(result);
1993
1994                 result = winreg_enumval_to_dword(info2,
1995                                                  v,
1996                                                  "Status",
1997                                                  &info2->status);
1998                 CHECK_ERROR(result);
1999
2000                 result = winreg_enumval_to_dword(info2,
2001                                                  v,
2002                                                  "StartTime",
2003                                                  &info2->starttime);
2004                 CHECK_ERROR(result);
2005         }
2006
2007         if (!W_ERROR_IS_OK(result)) {
2008                 DEBUG(0, ("winreg_get_printer: winreg_enumval_to_TYPE() failed "
2009                                         "for %s: %s\n",
2010                                         v->value_name,
2011                                         win_errstr(result)));
2012                 goto done;
2013         }
2014
2015         /* Create the printername */
2016         if (info2->servername[0] != '\0') {
2017                 if (lp_force_printername(snum)) {
2018                         const char *p = talloc_asprintf(info2, "%s\\%s",
2019                                                         info2->servername,
2020                                                         info2->sharename);
2021                                 if (p == NULL) {
2022                                         result = WERR_NOMEM;
2023                                         goto done;
2024                                 }
2025                                 info2->printername = p;
2026                 } else {
2027                         char *p = talloc_asprintf(info2, "%s\\%s",
2028                                                   info2->servername,
2029                                                   info2->printername);
2030                         if (p == NULL) {
2031                                 result = WERR_NOMEM;
2032                                 goto done;
2033                         }
2034                         info2->printername = p;
2035                 }
2036         }
2037
2038         /* Construct the Device Mode */
2039         result = winreg_printer_query_binary(tmp_ctx,
2040                                              winreg_pipe,
2041                                              &key_hnd,
2042                                              "Default DevMode",
2043                                              &blob);
2044         if (W_ERROR_IS_OK(result)) {
2045                 info2->devmode = talloc_zero(info2, struct spoolss_DeviceMode);
2046                 if (info2->devmode == NULL) {
2047                         result = WERR_NOMEM;
2048                         goto done;
2049                 }
2050                 ndr_err = ndr_pull_struct_blob(&blob,
2051                                                info2->devmode,
2052                                                NULL,
2053                                                info2->devmode,
2054                                                (ndr_pull_flags_fn_t) ndr_pull_spoolss_DeviceMode);
2055                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2056                         DEBUG(0, ("winreg_get_printer: Failed to unmarshall device mode\n"));
2057                         result = WERR_NOMEM;
2058                         goto done;
2059                 }
2060         }
2061
2062         if (info2->devmode == NULL && lp_default_devmode(snum)) {
2063                 result = spoolss_create_default_devmode(info2,
2064                                                         info2->printername,
2065                                                         &info2->devmode);
2066                 if (!W_ERROR_IS_OK(result)) {
2067                         goto done;
2068                 }
2069         }
2070
2071         if (info2->devmode != NULL) {
2072                 info2->devmode->devicename = talloc_strdup(info2->devmode,
2073                                                            info2->printername);
2074                 if (info2->devmode->devicename == NULL) {
2075                         DEBUG(0, ("winreg_get_printer: Failed to set devicename\n"));
2076                         result = WERR_NOMEM;
2077                         goto done;
2078                 }
2079         }
2080
2081         result = winreg_get_printer_secdesc(info2,
2082                                             server_info,
2083                                             printer,
2084                                             &info2->secdesc);
2085         if (!W_ERROR_IS_OK(result)) {
2086                 goto done;
2087         }
2088
2089         if (pinfo2) {
2090                 *pinfo2 = talloc_move(mem_ctx, &info2);
2091         }
2092
2093         result = WERR_OK;
2094 done:
2095         if (winreg_pipe != NULL) {
2096                 if (is_valid_policy_hnd(&key_hnd)) {
2097                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2098                 }
2099                 if (is_valid_policy_hnd(&hive_hnd)) {
2100                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2101                 }
2102         }
2103
2104         TALLOC_FREE(tmp_ctx);
2105         return result;
2106 }
2107
2108 WERROR winreg_get_printer_secdesc(TALLOC_CTX *mem_ctx,
2109                                   struct auth_serversupplied_info *server_info,
2110                                   const char *sharename,
2111                                   struct spoolss_security_descriptor **psecdesc)
2112 {
2113         struct spoolss_security_descriptor *secdesc;
2114         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2115         struct rpc_pipe_client *winreg_pipe = NULL;
2116         struct policy_handle hive_hnd, key_hnd;
2117         enum ndr_err_code ndr_err;
2118         const char *path;
2119         DATA_BLOB blob;
2120         TALLOC_CTX *tmp_ctx;
2121         WERROR result;
2122
2123         tmp_ctx = talloc_new(mem_ctx);
2124         if (tmp_ctx == NULL) {
2125                 return WERR_NOMEM;
2126         }
2127
2128         path = winreg_printer_data_keyname(tmp_ctx, sharename);
2129         if (path == NULL) {
2130                 talloc_free(tmp_ctx);
2131                 return WERR_NOMEM;
2132         }
2133
2134         ZERO_STRUCT(hive_hnd);
2135         ZERO_STRUCT(key_hnd);
2136
2137         result = winreg_printer_openkey(tmp_ctx,
2138                                         server_info,
2139                                         &winreg_pipe,
2140                                         path,
2141                                         "",
2142                                         false,
2143                                         access_mask,
2144                                         &hive_hnd,
2145                                         &key_hnd);
2146         if (!W_ERROR_IS_OK(result)) {
2147                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2148                         goto create_default;
2149                 }
2150                 goto done;
2151         }
2152
2153         result = winreg_printer_query_binary(tmp_ctx,
2154                                              winreg_pipe,
2155                                              &key_hnd,
2156                                              "Security",
2157                                              &blob);
2158         if (!W_ERROR_IS_OK(result)) {
2159                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2160                         goto create_default;
2161                 }
2162                 goto done;
2163         }
2164
2165         secdesc = talloc_zero(tmp_ctx, struct spoolss_security_descriptor);
2166         if (secdesc == NULL) {
2167                 result = WERR_NOMEM;
2168                 goto done;
2169         }
2170         ndr_err = ndr_pull_struct_blob(&blob,
2171                                        secdesc,
2172                                        NULL,
2173                                        secdesc,
2174                                        (ndr_pull_flags_fn_t) ndr_pull_security_descriptor);
2175         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2176                 DEBUG(0, ("winreg_get_secdesc: Failed to unmarshall security descriptor\n"));
2177                 result = WERR_NOMEM;
2178                 goto done;
2179         }
2180
2181         if (psecdesc) {
2182                 *psecdesc = talloc_move(mem_ctx, &secdesc);
2183         }
2184
2185         result = WERR_OK;
2186         goto done;
2187
2188 create_default:
2189         result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
2190         if (!W_ERROR_IS_OK(result)) {
2191                 return result;
2192         }
2193
2194         /* If security descriptor is owned by S-1-1-0 and winbindd is up,
2195            this security descriptor has been created when winbindd was
2196            down.  Take ownership of security descriptor. */
2197         if (sid_equal(secdesc->owner_sid, &global_sid_World)) {
2198                 DOM_SID owner_sid;
2199
2200                 /* Change sd owner to workgroup administrator */
2201
2202                 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
2203                         struct spoolss_security_descriptor *new_secdesc;
2204                         size_t size;
2205
2206                         /* Create new sd */
2207                         sid_append_rid(&owner_sid, DOMAIN_USER_RID_ADMIN);
2208
2209                         new_secdesc = make_sec_desc(tmp_ctx,
2210                                                     secdesc->revision,
2211                                                     secdesc->type,
2212                                                     &owner_sid,
2213                                                     secdesc->group_sid,
2214                                                     secdesc->sacl,
2215                                                     secdesc->dacl,
2216                                                     &size);
2217
2218                         if (new_secdesc == NULL) {
2219                                 result = WERR_NOMEM;
2220                                 goto done;
2221                         }
2222
2223                         /* Swap with other one */
2224                         secdesc = new_secdesc;
2225                 }
2226         }
2227
2228         ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, NULL, secdesc,
2229                         (ndr_push_flags_fn_t) ndr_push_security_descriptor);
2230         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2231                 DEBUG(0, ("winreg_set_secdesc: Failed to marshall security descriptor\n"));
2232                 result = WERR_NOMEM;
2233                 goto done;
2234         }
2235
2236         result = winreg_printer_write_binary(tmp_ctx,
2237                                              winreg_pipe,
2238                                              &key_hnd,
2239                                              "Security",
2240                                              blob);
2241         if (!W_ERROR_IS_OK(result)) {
2242                 return result;
2243         }
2244
2245         if (psecdesc) {
2246                 *psecdesc = talloc_move(mem_ctx, &secdesc);
2247         }
2248
2249         result = WERR_OK;
2250 done:
2251         if (winreg_pipe != NULL) {
2252                 if (is_valid_policy_hnd(&key_hnd)) {
2253                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2254                 }
2255                 if (is_valid_policy_hnd(&hive_hnd)) {
2256                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2257                 }
2258         }
2259
2260         talloc_free(tmp_ctx);
2261         return result;
2262 }
2263
2264 WERROR winreg_set_printer_secdesc(TALLOC_CTX *mem_ctx,
2265                                   struct auth_serversupplied_info *server_info,
2266                                   const char *sharename,
2267                                   const struct spoolss_security_descriptor *secdesc)
2268 {
2269         const struct spoolss_security_descriptor *new_secdesc = secdesc;
2270         struct spoolss_security_descriptor *old_secdesc;
2271         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2272         struct rpc_pipe_client *winreg_pipe = NULL;
2273         struct policy_handle hive_hnd, key_hnd;
2274         enum ndr_err_code ndr_err;
2275         const char *path;
2276         DATA_BLOB blob;
2277         TALLOC_CTX *tmp_ctx;
2278         WERROR result;
2279
2280         tmp_ctx = talloc_new(mem_ctx);
2281         if (tmp_ctx == NULL) {
2282                 return WERR_NOMEM;
2283         }
2284
2285         path = winreg_printer_data_keyname(tmp_ctx, sharename);
2286         if (path == NULL) {
2287                 talloc_free(tmp_ctx);
2288                 return WERR_NOMEM;
2289         }
2290
2291         /*
2292          * The old owner and group sids of the security descriptor are not
2293          * present when new ACEs are added or removed by changing printer
2294          * permissions through NT.  If they are NULL in the new security
2295          * descriptor then copy them over from the old one.
2296          */
2297         if (!secdesc->owner_sid || !secdesc->group_sid) {
2298                 DOM_SID *owner_sid, *group_sid;
2299                 SEC_ACL *dacl, *sacl;
2300                 size_t size;
2301
2302                 result = winreg_get_printer_secdesc(tmp_ctx,
2303                                                     server_info,
2304                                                     sharename,
2305                                                     &old_secdesc);
2306                 if (!W_ERROR_IS_OK(result)) {
2307                         talloc_free(tmp_ctx);
2308                         return result;
2309                 }
2310
2311                 /* Pick out correct owner and group sids */
2312                 owner_sid = secdesc->owner_sid ?
2313                             secdesc->owner_sid :
2314                             old_secdesc->owner_sid;
2315
2316                 group_sid = secdesc->group_sid ?
2317                             secdesc->group_sid :
2318                             old_secdesc->group_sid;
2319
2320                 dacl = secdesc->dacl ?
2321                        secdesc->dacl :
2322                        old_secdesc->dacl;
2323
2324                 sacl = secdesc->sacl ?
2325                        secdesc->sacl :
2326                        old_secdesc->sacl;
2327
2328                 /* Make a deep copy of the security descriptor */
2329                 new_secdesc = make_sec_desc(tmp_ctx,
2330                                             secdesc->revision,
2331                                             secdesc->type,
2332                                             owner_sid,
2333                                             group_sid,
2334                                             sacl,
2335                                             dacl,
2336                                             &size);
2337                 if (new_secdesc == NULL) {
2338                         talloc_free(tmp_ctx);
2339                         return WERR_NOMEM;
2340                 }
2341         }
2342
2343         ZERO_STRUCT(hive_hnd);
2344         ZERO_STRUCT(key_hnd);
2345
2346         result = winreg_printer_openkey(tmp_ctx,
2347                                         server_info,
2348                                         &winreg_pipe,
2349                                         path,
2350                                         "",
2351                                         false,
2352                                         access_mask,
2353                                         &hive_hnd,
2354                                         &key_hnd);
2355         if (!W_ERROR_IS_OK(result)) {
2356                 goto done;
2357         }
2358
2359         ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, NULL, new_secdesc,
2360                         (ndr_push_flags_fn_t) ndr_push_security_descriptor);
2361         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2362                 DEBUG(0, ("winreg_set_secdesc: Failed to marshall security descriptor\n"));
2363                 result = WERR_NOMEM;
2364                 goto done;
2365         }
2366
2367         result = winreg_printer_write_binary(tmp_ctx,
2368                                              winreg_pipe,
2369                                              &key_hnd,
2370                                              "Security",
2371                                              blob);
2372
2373 done:
2374         if (winreg_pipe != NULL) {
2375                 if (is_valid_policy_hnd(&key_hnd)) {
2376                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2377                 }
2378                 if (is_valid_policy_hnd(&hive_hnd)) {
2379                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2380                 }
2381         }
2382
2383         talloc_free(tmp_ctx);
2384         return result;
2385 }
2386
2387 /* Set printer data over the winreg pipe. */
2388 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
2389                                  struct auth_serversupplied_info *server_info,
2390                                  const char *printer,
2391                                  const char *key,
2392                                  const char *value,
2393                                  enum winreg_Type type,
2394                                  uint8_t *data,
2395                                  uint32_t data_size)
2396 {
2397         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2398         struct rpc_pipe_client *winreg_pipe = NULL;
2399         struct policy_handle hive_hnd, key_hnd;
2400         struct winreg_String wvalue;
2401         char *path;
2402         WERROR result = WERR_OK;
2403         NTSTATUS status;
2404         TALLOC_CTX *tmp_ctx;
2405
2406         tmp_ctx = talloc_new(mem_ctx);
2407         if (tmp_ctx == NULL) {
2408                 return WERR_NOMEM;
2409         }
2410
2411         path = winreg_printer_data_keyname(tmp_ctx, printer);
2412         if (path == NULL) {
2413                 TALLOC_FREE(tmp_ctx);
2414                 return WERR_NOMEM;
2415         }
2416
2417         ZERO_STRUCT(hive_hnd);
2418         ZERO_STRUCT(key_hnd);
2419
2420         DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
2421                         key, value, access_mask, printer));
2422         result = winreg_printer_openkey(tmp_ctx,
2423                                         server_info,
2424                                         &winreg_pipe,
2425                                         path,
2426                                         key,
2427                                         true,
2428                                         access_mask,
2429                                         &hive_hnd,
2430                                         &key_hnd);
2431         if (!W_ERROR_IS_OK(result)) {
2432                 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
2433                           key, win_errstr(result)));
2434                 goto done;
2435         }
2436
2437         wvalue.name = value;
2438         status = rpccli_winreg_SetValue(winreg_pipe,
2439                                         tmp_ctx,
2440                                         &key_hnd,
2441                                         wvalue,
2442                                         type,
2443                                         data,
2444                                         data_size,
2445                                         &result);
2446         if (!NT_STATUS_IS_OK(status)) {
2447                 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
2448                           value, nt_errstr(status)));
2449                 if (!W_ERROR_IS_OK(result)) {
2450                         goto done;
2451                 }
2452                 result = ntstatus_to_werror(status);
2453                 goto done;
2454         }
2455
2456         result = WERR_OK;
2457 done:
2458         if (winreg_pipe != NULL) {
2459                 if (is_valid_policy_hnd(&key_hnd)) {
2460                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2461                 }
2462                 if (is_valid_policy_hnd(&hive_hnd)) {
2463                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2464                 }
2465         }
2466
2467         TALLOC_FREE(tmp_ctx);
2468         return result;
2469 }
2470
2471 /* Get printer data over a winreg pipe. */
2472 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
2473                                  struct auth_serversupplied_info *server_info,
2474                                  const char *printer,
2475                                  const char *key,
2476                                  const char *value,
2477                                  enum winreg_Type *type,
2478                                  uint8_t **data,
2479                                  uint32_t *data_size)
2480 {
2481         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2482         struct rpc_pipe_client *winreg_pipe = NULL;
2483         struct policy_handle hive_hnd, key_hnd;
2484         struct winreg_String wvalue;
2485         enum winreg_Type type_in;
2486         char *path;
2487         uint8_t *data_in;
2488         uint32_t data_in_size = 0;
2489         uint32_t value_len = 0;
2490         WERROR result = WERR_OK;
2491         NTSTATUS status;
2492         TALLOC_CTX *tmp_ctx;
2493
2494         tmp_ctx = talloc_new(mem_ctx);
2495         if (tmp_ctx == NULL) {
2496                 return WERR_NOMEM;
2497         }
2498
2499         path = winreg_printer_data_keyname(tmp_ctx, printer);
2500         if (path == NULL) {
2501                 TALLOC_FREE(tmp_ctx);
2502                 return WERR_NOMEM;
2503         }
2504
2505         ZERO_STRUCT(hive_hnd);
2506         ZERO_STRUCT(key_hnd);
2507
2508         result = winreg_printer_openkey(tmp_ctx,
2509                                         server_info,
2510                                         &winreg_pipe,
2511                                         path,
2512                                         key,
2513                                         false,
2514                                         access_mask,
2515                                         &hive_hnd,
2516                                         &key_hnd);
2517         if (!W_ERROR_IS_OK(result)) {
2518                 DEBUG(0, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
2519                           key, win_errstr(result)));
2520                 goto done;
2521         }
2522
2523         wvalue.name = value;
2524
2525         /*
2526          * call QueryValue once with data == NULL to get the
2527          * needed memory size to be allocated, then allocate
2528          * data buffer and call again.
2529          */
2530         status = rpccli_winreg_QueryValue(winreg_pipe,
2531                                           tmp_ctx,
2532                                           &key_hnd,
2533                                           &wvalue,
2534                                           &type_in,
2535                                           NULL,
2536                                           &data_in_size,
2537                                           &value_len,
2538                                           &result);
2539         if (!NT_STATUS_IS_OK(status)) {
2540                 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2541                           value, nt_errstr(status)));
2542                 if (!W_ERROR_IS_OK(result)) {
2543                         goto done;
2544                 }
2545                 result = ntstatus_to_werror(status);
2546                 goto done;
2547         }
2548
2549         data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
2550         if (data_in == NULL) {
2551                 result = WERR_NOMEM;
2552                 goto done;
2553         }
2554         value_len = 0;
2555
2556         status = rpccli_winreg_QueryValue(winreg_pipe,
2557                                           tmp_ctx,
2558                                           &key_hnd,
2559                                           &wvalue,
2560                                           &type_in,
2561                                           data_in,
2562                                           &data_in_size,
2563                                           &value_len,
2564                                           &result);
2565         if (!NT_STATUS_IS_OK(status)) {
2566                 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2567                           value, nt_errstr(status)));
2568                 if (!W_ERROR_IS_OK(result)) {
2569                         result = ntstatus_to_werror(status);
2570                 }
2571                 goto done;
2572         }
2573
2574         *type = type_in;
2575         *data_size = data_in_size;
2576         if (data_in_size) {
2577                 *data = talloc_move(mem_ctx, &data_in);
2578         }
2579
2580         result = WERR_OK;
2581 done:
2582         if (winreg_pipe != NULL) {
2583                 if (is_valid_policy_hnd(&key_hnd)) {
2584                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2585                 }
2586                 if (is_valid_policy_hnd(&hive_hnd)) {
2587                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2588                 }
2589         }
2590
2591         TALLOC_FREE(tmp_ctx);
2592         return result;
2593 }
2594
2595 /* Enumerate on the values of a given key and provide the data. */
2596 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
2597                                   struct auth_serversupplied_info *server_info,
2598                                   const char *printer,
2599                                   const char *key,
2600                                   uint32_t *pnum_values,
2601                                   struct spoolss_PrinterEnumValues **penum_values)
2602 {
2603         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2604         struct rpc_pipe_client *winreg_pipe = NULL;
2605         struct policy_handle hive_hnd, key_hnd;
2606
2607         struct spoolss_PrinterEnumValues *enum_values = NULL;
2608         uint32_t num_values = 0;
2609         char *path;
2610         WERROR result = WERR_OK;
2611
2612         TALLOC_CTX *tmp_ctx;
2613
2614         tmp_ctx = talloc_new(mem_ctx);
2615         if (tmp_ctx == NULL) {
2616                 return WERR_NOMEM;
2617         }
2618
2619         path = winreg_printer_data_keyname(tmp_ctx, printer);
2620         if (path == NULL) {
2621                 TALLOC_FREE(tmp_ctx);
2622                 return WERR_NOMEM;
2623         }
2624
2625         result = winreg_printer_openkey(tmp_ctx,
2626                                         server_info,
2627                                         &winreg_pipe,
2628                                         path,
2629                                         key,
2630                                         false,
2631                                         access_mask,
2632                                         &hive_hnd,
2633                                         &key_hnd);
2634         if (!W_ERROR_IS_OK(result)) {
2635                 DEBUG(0, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
2636                           key, win_errstr(result)));
2637                 goto done;
2638         }
2639
2640         result = winreg_printer_enumvalues(tmp_ctx,
2641                                            winreg_pipe,
2642                                            &key_hnd,
2643                                            &num_values,
2644                                            &enum_values);
2645         if (!W_ERROR_IS_OK(result)) {
2646                 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2647                           key, win_errstr(result)));
2648                 goto done;
2649         }
2650
2651         *pnum_values = num_values;
2652         if (penum_values) {
2653                 *penum_values = talloc_move(mem_ctx, &enum_values);
2654         }
2655
2656         result = WERR_OK;
2657 done:
2658         if (winreg_pipe != NULL) {
2659                 if (is_valid_policy_hnd(&key_hnd)) {
2660                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2661                 }
2662                 if (is_valid_policy_hnd(&hive_hnd)) {
2663                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2664                 }
2665         }
2666
2667         TALLOC_FREE(tmp_ctx);
2668         return result;
2669 }
2670
2671 /* Delete printer data over a winreg pipe. */
2672 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
2673                                     struct auth_serversupplied_info *server_info,
2674                                     const char *printer,
2675                                     const char *key,
2676                                     const char *value)
2677 {
2678         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2679         struct rpc_pipe_client *winreg_pipe = NULL;
2680         struct policy_handle hive_hnd, key_hnd;
2681         struct winreg_String wvalue;
2682         char *path;
2683         WERROR result = WERR_OK;
2684         NTSTATUS status;
2685
2686         TALLOC_CTX *tmp_ctx;
2687
2688         tmp_ctx = talloc_new(mem_ctx);
2689         if (tmp_ctx == NULL) {
2690                 return WERR_NOMEM;
2691         }
2692
2693         path = winreg_printer_data_keyname(tmp_ctx, printer);
2694         if (path == NULL) {
2695                 TALLOC_FREE(tmp_ctx);
2696                 return WERR_NOMEM;
2697         }
2698
2699         ZERO_STRUCT(hive_hnd);
2700         ZERO_STRUCT(key_hnd);
2701
2702         result = winreg_printer_openkey(tmp_ctx,
2703                                         server_info,
2704                                         &winreg_pipe,
2705                                         path,
2706                                         key,
2707                                         false,
2708                                         access_mask,
2709                                         &hive_hnd,
2710                                         &key_hnd);
2711         if (!W_ERROR_IS_OK(result)) {
2712                 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
2713                           key, win_errstr(result)));
2714                 goto done;
2715         }
2716
2717         wvalue.name = value;
2718         status = rpccli_winreg_DeleteValue(winreg_pipe,
2719                                            tmp_ctx,
2720                                            &key_hnd,
2721                                            wvalue,
2722                                            &result);
2723         if (!NT_STATUS_IS_OK(status)) {
2724                 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
2725                           value, nt_errstr(status)));
2726                 if (!W_ERROR_IS_OK(result)) {
2727                         goto done;
2728                 }
2729                 result = ntstatus_to_werror(status);
2730                 goto done;
2731         }
2732
2733         result = WERR_OK;
2734 done:
2735         if (winreg_pipe != NULL) {
2736                 if (is_valid_policy_hnd(&key_hnd)) {
2737                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2738                 }
2739                 if (is_valid_policy_hnd(&hive_hnd)) {
2740                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2741                 }
2742         }
2743
2744         TALLOC_FREE(tmp_ctx);
2745         return result;
2746 }
2747
2748 /* Enumerate on the subkeys of a given key and provide the data. */
2749 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
2750                                struct auth_serversupplied_info *server_info,
2751                                const char *printer,
2752                                const char *key,
2753                                uint32_t *pnum_subkeys,
2754                                const char ***psubkeys)
2755 {
2756         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2757         struct rpc_pipe_client *winreg_pipe = NULL;
2758         struct policy_handle hive_hnd, key_hnd;
2759         char *path;
2760         const char **subkeys = NULL;
2761         uint32_t num_subkeys = -1;
2762
2763         WERROR result = WERR_OK;
2764
2765         TALLOC_CTX *tmp_ctx;
2766
2767         tmp_ctx = talloc_new(mem_ctx);
2768         if (tmp_ctx == NULL) {
2769                 return WERR_NOMEM;
2770         }
2771
2772         path = winreg_printer_data_keyname(tmp_ctx, printer);
2773         if (path == NULL) {
2774                 TALLOC_FREE(tmp_ctx);
2775                 return WERR_NOMEM;
2776         }
2777
2778         ZERO_STRUCT(hive_hnd);
2779         ZERO_STRUCT(key_hnd);
2780
2781         result = winreg_printer_openkey(tmp_ctx,
2782                                         server_info,
2783                                         &winreg_pipe,
2784                                         path,
2785                                         key,
2786                                         false,
2787                                         access_mask,
2788                                         &hive_hnd,
2789                                         &key_hnd);
2790         if (!W_ERROR_IS_OK(result)) {
2791                 DEBUG(0, ("winreg_enum_printer_key: Could not open key %s: %s\n",
2792          &nb