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