s3-spoolss: Added winreg helper functions to write registry values.
[sfrench/samba-autobuild/.git] / source3 / rpc_server / srv_spoolss_util.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *
4  *  SPOOLSS RPC Pipe server / winreg client routines
5  *
6  *  Copyright (c) 2010      Andreas Schneider <asn@samba.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "srv_spoolss_util.h"
24 #include "../librpc/gen_ndr/srv_winreg.h"
25 #include "../librpc/gen_ndr/cli_winreg.h"
26
27 #define TOP_LEVEL_PRINT_KEY "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print"
28 #define TOP_LEVEL_PRINT_PRINTERS_KEY TOP_LEVEL_PRINT_KEY "\\Printers"
29 #define TOP_LEVEL_CONTROL_KEY "SYSTEM\\CurrentControlSet\\Control\\Print"
30 #define TOP_LEVEL_CONTROL_FORMS_KEY TOP_LEVEL_CONTROL_KEY "\\Forms"
31
32 /*        FLAGS,                NAME,                              with,   height,   left, top, right, bottom */
33 static const struct spoolss_FormInfo1 builtin_forms1[] = {
34         { SPOOLSS_FORM_BUILTIN, "Letter",                         {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
35         { SPOOLSS_FORM_BUILTIN, "Letter Small",                   {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
36         { SPOOLSS_FORM_BUILTIN, "Tabloid",                        {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
37         { SPOOLSS_FORM_BUILTIN, "Ledger",                         {0x696b8,0x44368}, {0x0,0x0,0x696b8,0x44368} },
38         { SPOOLSS_FORM_BUILTIN, "Legal",                          {0x34b5c,0x56d10}, {0x0,0x0,0x34b5c,0x56d10} },
39         { SPOOLSS_FORM_BUILTIN, "Statement",                      {0x221b4,0x34b5c}, {0x0,0x0,0x221b4,0x34b5c} },
40         { SPOOLSS_FORM_BUILTIN, "Executive",                      {0x2cf56,0x411cc}, {0x0,0x0,0x2cf56,0x411cc} },
41         { SPOOLSS_FORM_BUILTIN, "A3",                             {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
42         { SPOOLSS_FORM_BUILTIN, "A4",                             {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
43         { SPOOLSS_FORM_BUILTIN, "A4 Small",                       {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
44         { SPOOLSS_FORM_BUILTIN, "A5",                             {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
45         { SPOOLSS_FORM_BUILTIN, "B4 (JIS)",                       {0x3ebe8,0x58de0}, {0x0,0x0,0x3ebe8,0x58de0} },
46         { SPOOLSS_FORM_BUILTIN, "B5 (JIS)",                       {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
47         { SPOOLSS_FORM_BUILTIN, "Folio",                          {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
48         { SPOOLSS_FORM_BUILTIN, "Quarto",                         {0x347d8,0x43238}, {0x0,0x0,0x347d8,0x43238} },
49         { SPOOLSS_FORM_BUILTIN, "10x14",                          {0x3e030,0x56d10}, {0x0,0x0,0x3e030,0x56d10} },
50         { SPOOLSS_FORM_BUILTIN, "11x17",                          {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
51         { SPOOLSS_FORM_BUILTIN, "Note",                           {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
52         { SPOOLSS_FORM_BUILTIN, "Envelope #9",                    {0x18079,0x37091}, {0x0,0x0,0x18079,0x37091} },
53         { SPOOLSS_FORM_BUILTIN, "Envelope #10",                   {0x19947,0x3ae94}, {0x0,0x0,0x19947,0x3ae94} },
54         { SPOOLSS_FORM_BUILTIN, "Envelope #11",                   {0x1be7c,0x40565}, {0x0,0x0,0x1be7c,0x40565} },
55         { SPOOLSS_FORM_BUILTIN, "Envelope #12",                   {0x1d74a,0x44368}, {0x0,0x0,0x1d74a,0x44368} },
56         { SPOOLSS_FORM_BUILTIN, "Envelope #14",                   {0x1f018,0x47504}, {0x0,0x0,0x1f018,0x47504} },
57         { SPOOLSS_FORM_BUILTIN, "C size sheet",                   {0x696b8,0x886d0}, {0x0,0x0,0x696b8,0x886d0} },
58         { SPOOLSS_FORM_BUILTIN, "D size sheet",                   {0x886d0,0xd2d70}, {0x0,0x0,0x886d0,0xd2d70} },
59         { SPOOLSS_FORM_BUILTIN, "E size sheet",                   {0xd2d70,0x110da0},{0x0,0x0,0xd2d70,0x110da0} },
60         { SPOOLSS_FORM_BUILTIN, "Envelope DL",                    {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
61         { SPOOLSS_FORM_BUILTIN, "Envelope C5",                    {0x278d0,0x37e88}, {0x0,0x0,0x278d0,0x37e88} },
62         { SPOOLSS_FORM_BUILTIN, "Envelope C3",                    {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
63         { SPOOLSS_FORM_BUILTIN, "Envelope C4",                    {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
64         { SPOOLSS_FORM_BUILTIN, "Envelope C6",                    {0x1bd50,0x278d0}, {0x0,0x0,0x1bd50,0x278d0} },
65         { SPOOLSS_FORM_BUILTIN, "Envelope C65",                   {0x1bd50,0x37e88}, {0x0,0x0,0x1bd50,0x37e88} },
66         { SPOOLSS_FORM_BUILTIN, "Envelope B4",                    {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
67         { SPOOLSS_FORM_BUILTIN, "Envelope B5",                    {0x2af80,0x3d090}, {0x0,0x0,0x2af80,0x3d090} },
68         { SPOOLSS_FORM_BUILTIN, "Envelope B6",                    {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
69         { SPOOLSS_FORM_BUILTIN, "Envelope",                       {0x1adb0,0x38270}, {0x0,0x0,0x1adb0,0x38270} },
70         { SPOOLSS_FORM_BUILTIN, "Envelope Monarch",               {0x18079,0x2e824}, {0x0,0x0,0x18079,0x2e824} },
71         { SPOOLSS_FORM_BUILTIN, "6 3/4 Envelope",                 {0x167ab,0x284ec}, {0x0,0x0,0x167ab,0x284ec} },
72         { SPOOLSS_FORM_BUILTIN, "US Std Fanfold",                 {0x5c3e1,0x44368}, {0x0,0x0,0x5c3e1,0x44368} },
73         { SPOOLSS_FORM_BUILTIN, "German Std Fanfold",             {0x34b5c,0x4a6a0}, {0x0,0x0,0x34b5c,0x4a6a0} },
74         { SPOOLSS_FORM_BUILTIN, "German Legal Fanfold",           {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
75         { SPOOLSS_FORM_BUILTIN, "B4 (ISO)",                       {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
76         { SPOOLSS_FORM_BUILTIN, "Japanese Postcard",              {0x186a0,0x24220}, {0x0,0x0,0x186a0,0x24220} },
77         { SPOOLSS_FORM_BUILTIN, "9x11",                           {0x37cf8,0x44368}, {0x0,0x0,0x37cf8,0x44368} },
78         { SPOOLSS_FORM_BUILTIN, "10x11",                          {0x3e030,0x44368}, {0x0,0x0,0x3e030,0x44368} },
79         { SPOOLSS_FORM_BUILTIN, "15x11",                          {0x5d048,0x44368}, {0x0,0x0,0x5d048,0x44368} },
80         { SPOOLSS_FORM_BUILTIN, "Envelope Invite",                {0x35b60,0x35b60}, {0x0,0x0,0x35b60,0x35b60} },
81         { SPOOLSS_FORM_BUILTIN, "Reserved48",                     {0x1,0x1},         {0x0,0x0,0x1,0x1} },
82         { SPOOLSS_FORM_BUILTIN, "Reserved49",                     {0x1,0x1},         {0x0,0x0,0x1,0x1} },
83         { SPOOLSS_FORM_BUILTIN, "Letter Extra",                   {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
84         { SPOOLSS_FORM_BUILTIN, "Legal Extra",                    {0x3ae94,0x5d048}, {0x0,0x0,0x3ae94,0x5d048} },
85         { SPOOLSS_FORM_BUILTIN, "Tabloid Extra",                  {0x4a6a0,0x6f9f0}, {0x0,0x0,0x4a6a0,0x6f9f0} },
86         { SPOOLSS_FORM_BUILTIN, "A4 Extra",                       {0x397c2,0x4eb16}, {0x0,0x0,0x397c2,0x4eb16} },
87         { SPOOLSS_FORM_BUILTIN, "Letter Transverse",              {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
88         { SPOOLSS_FORM_BUILTIN, "A4 Transverse",                  {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
89         { SPOOLSS_FORM_BUILTIN, "Letter Extra Transverse",        {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
90         { SPOOLSS_FORM_BUILTIN, "Super A",                        {0x376b8,0x56ea0}, {0x0,0x0,0x376b8,0x56ea0} },
91         { SPOOLSS_FORM_BUILTIN, "Super B",                        {0x4a768,0x76e58}, {0x0,0x0,0x4a768,0x76e58} },
92         { SPOOLSS_FORM_BUILTIN, "Letter Plus",                    {0x34b5c,0x4eb16}, {0x0,0x0,0x34b5c,0x4eb16} },
93         { SPOOLSS_FORM_BUILTIN, "A4 Plus",                        {0x33450,0x50910}, {0x0,0x0,0x33450,0x50910} },
94         { SPOOLSS_FORM_BUILTIN, "A5 Transverse",                  {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
95         { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Transverse",            {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
96         { SPOOLSS_FORM_BUILTIN, "A3 Extra",                       {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
97         { SPOOLSS_FORM_BUILTIN, "A5 Extra",                       {0x2a7b0,0x395f8}, {0x0,0x0,0x2a7b0,0x395f8} },
98         { SPOOLSS_FORM_BUILTIN, "B5 (ISO) Extra",                 {0x31128,0x43620}, {0x0,0x0,0x31128,0x43620} },
99         { SPOOLSS_FORM_BUILTIN, "A2",                             {0x668a0,0x91050}, {0x0,0x0,0x668a0,0x91050} },
100         { SPOOLSS_FORM_BUILTIN, "A3 Transverse",                  {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
101         { SPOOLSS_FORM_BUILTIN, "A3 Extra Transverse",            {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
102         { SPOOLSS_FORM_BUILTIN, "Japanese Double Postcard",       {0x30d40,0x24220}, {0x0,0x0,0x30d40,0x24220} },
103         { SPOOLSS_FORM_BUILTIN, "A6",                             {0x19a28,0x24220}, {0x0,0x0,0x19a28,0x24220} },
104         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #2",      {0x3a980,0x510e0}, {0x0,0x0,0x3a980,0x510e0} },
105         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #3",      {0x34bc0,0x43a08}, {0x0,0x0,0x34bc0,0x43a08} },
106         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #3",      {0x1d4c0,0x395f8}, {0x0,0x0,0x1d4c0,0x395f8} },
107         { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #4",      {0x15f90,0x320c8}, {0x0,0x0,0x15f90,0x320c8} },
108         { SPOOLSS_FORM_BUILTIN, "Letter Rotated",                 {0x44368,0x34b5c}, {0x0,0x0,0x44368,0x34b5c} },
109         { SPOOLSS_FORM_BUILTIN, "A3 Rotated",                     {0x668a0,0x48828}, {0x0,0x0,0x668a0,0x48828} },
110         { SPOOLSS_FORM_BUILTIN, "A4 Rotated",                     {0x48828,0x33450}, {0x0,0x0,0x48828,0x33450} },
111         { SPOOLSS_FORM_BUILTIN, "A5 Rotated",                     {0x33450,0x24220}, {0x0,0x0,0x33450,0x24220} },
112         { SPOOLSS_FORM_BUILTIN, "B4 (JIS) Rotated",               {0x58de0,0x3ebe8}, {0x0,0x0,0x58de0,0x3ebe8} },
113         { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Rotated",               {0x3ebe8,0x2c6f0}, {0x0,0x0,0x3ebe8,0x2c6f0} },
114         { SPOOLSS_FORM_BUILTIN, "Japanese Postcard Rotated",      {0x24220,0x186a0}, {0x0,0x0,0x24220,0x186a0} },
115         { SPOOLSS_FORM_BUILTIN, "Double Japan Postcard Rotated",  {0x24220,0x30d40}, {0x0,0x0,0x24220,0x30d40} },
116         { SPOOLSS_FORM_BUILTIN, "A6 Rotated",                     {0x24220,0x19a28}, {0x0,0x0,0x24220,0x19a28} },
117         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #2 Rotated", {0x510e0,0x3a980}, {0x0,0x0,0x510e0,0x3a980} },
118         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #3 Rotated", {0x43a08,0x34bc0}, {0x0,0x0,0x43a08,0x34bc0} },
119         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #3 Rotated", {0x395f8,0x1d4c0}, {0x0,0x0,0x395f8,0x1d4c0} },
120         { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #4 Rotated", {0x320c8,0x15f90}, {0x0,0x0,0x320c8,0x15f90} },
121         { SPOOLSS_FORM_BUILTIN, "B6 (JIS)",                       {0x1f400,0x2c6f0}, {0x0,0x0,0x1f400,0x2c6f0} },
122         { SPOOLSS_FORM_BUILTIN, "B6 (JIS) Rotated",               {0x2c6f0,0x1f400}, {0x0,0x0,0x2c6f0,0x1f400} },
123         { SPOOLSS_FORM_BUILTIN, "12x11",                          {0x4a724,0x443e1}, {0x0,0x0,0x4a724,0x443e1} },
124         { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4",          {0x19a28,0x395f8}, {0x0,0x0,0x19a28,0x395f8} },
125         { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4 Rotated",  {0x395f8,0x19a28}, {0x0,0x0,0x395f8,0x19a28} },
126         { SPOOLSS_FORM_BUILTIN, "PRC 16K",                        {0x2de60,0x3f7a0}, {0x0,0x0,0x2de60,0x3f7a0} },
127         { SPOOLSS_FORM_BUILTIN, "PRC 32K",                        {0x1fbd0,0x2cec0}, {0x0,0x0,0x1fbd0,0x2cec0} },
128         { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big)",                   {0x222e0,0x318f8}, {0x0,0x0,0x222e0,0x318f8} },
129         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1",                {0x18e70,0x28488}, {0x0,0x0,0x18e70,0x28488} },
130         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2",                {0x18e70,0x2af80}, {0x0,0x0,0x18e70,0x2af80} },
131         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3",                {0x1e848,0x2af80}, {0x0,0x0,0x1e848,0x2af80} },
132         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4",                {0x1adb0,0x32c80}, {0x0,0x0,0x1adb0,0x32c80} },
133         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5",                {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
134         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6",                {0x1d4c0,0x38270}, {0x0,0x0,0x1d4c0,0x38270} },
135         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7",                {0x27100,0x38270}, {0x0,0x0,0x27100,0x38270} },
136         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8",                {0x1d4c0,0x4b708}, {0x0,0x0,0x1d4c0,0x4b708} },
137         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9",                {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
138         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10",               {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
139         { SPOOLSS_FORM_BUILTIN, "PRC 16K Rotated",                {0x3f7a0,0x2de60}, {0x0,0x0,0x3f7a0,0x2de60} },
140         { SPOOLSS_FORM_BUILTIN, "PRC 32K Rotated",                {0x2cec0,0x1fbd0}, {0x0,0x0,0x2cec0,0x1fbd0} },
141         { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big) Rotated",           {0x318f8,0x222e0}, {0x0,0x0,0x318f8,0x222e0} },
142         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1 Rotated",        {0x28488,0x18e70}, {0x0,0x0,0x28488,0x18e70} },
143         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2 Rotated",        {0x2af80,0x18e70}, {0x0,0x0,0x2af80,0x18e70} },
144         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3 Rotated",        {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
145         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4 Rotated",        {0x32c80,0x1adb0}, {0x0,0x0,0x32c80,0x1adb0} },
146         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5 Rotated",        {0x35b60,0x1adb0}, {0x0,0x0,0x35b60,0x1adb0} },
147         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6 Rotated",        {0x38270,0x1d4c0}, {0x0,0x0,0x38270,0x1d4c0} },
148         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7 Rotated",        {0x38270,0x27100}, {0x0,0x0,0x38270,0x27100} },
149         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8 Rotated",        {0x4b708,0x1d4c0}, {0x0,0x0,0x4b708,0x1d4c0} },
150         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9 Rotated",        {0x4f1a0,0x37e88}, {0x0,0x0,0x4f1a0,0x37e88} },
151         { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10 Rotated",       {0x6fd10,0x4f1a0}, {0x0,0x0,0x6fd10,0x4f1a0} }
152 };
153
154 /********************************************************************
155  static helper functions
156 ********************************************************************/
157
158 /**
159  * @internal
160  *
161  * @brief Connect to the interal winreg server and open the given printer key.
162  *
163  * The function will create the needed subkeys if they don't exist.
164  *
165  * @param[in]  mem_ctx       The memory context to use.
166  *
167  * @param[in]  server_info   The supplied server info.
168  *
169  * @param[out] winreg_pipe   A pointer for the winreg rpc client pipe.
170  *
171  * @param[in]  path          The path to the key to open.
172  *
173  * @param[in]  key           The key to open.
174  *
175  * @param[in]  create_key    Set to true if the key should be created if it
176  *                           doesn't exist.
177  *
178  * @param[in]  access_mask   The access mask to open the key.
179  *
180  * @param[out] hive_handle   A policy handle for the opened hive.
181  *
182  * @param[out] key_handle    A policy handle for the opened key.
183  *
184  * @return                   WERR_OK on success, the corresponding DOS error
185  *                           code if something gone wrong.
186  */
187 static WERROR winreg_printer_openkey(TALLOC_CTX *mem_ctx,
188                               struct auth_serversupplied_info *server_info,
189                               struct rpc_pipe_client **winreg_pipe,
190                               const char *path,
191                               const char *key,
192                               bool create_key,
193                               uint32_t access_mask,
194                               struct policy_handle *hive_handle,
195                               struct policy_handle *key_handle)
196 {
197         struct rpc_pipe_client *pipe_handle;
198         struct winreg_String wkey, wkeyclass;
199         char *keyname;
200         NTSTATUS status;
201         WERROR result = WERR_OK;
202
203         /* create winreg connection */
204         status = rpc_pipe_open_internal(mem_ctx,
205                                         &ndr_table_winreg.syntax_id,
206                                         rpc_winreg_dispatch,
207                                         server_info,
208                                         &pipe_handle);
209         if (!NT_STATUS_IS_OK(status)) {
210                 DEBUG(0, ("winreg_printer_openkey: Could not connect to winreg_pipe: %s\n",
211                           nt_errstr(status)));
212                 return ntstatus_to_werror(status);
213         }
214
215         status = rpccli_winreg_OpenHKLM(pipe_handle,
216                                         mem_ctx,
217                                         NULL,
218                                         access_mask,
219                                         hive_handle,
220                                         &result);
221         if (!NT_STATUS_IS_OK(status)) {
222                 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
223                           nt_errstr(status)));
224                 talloc_free(pipe_handle);
225                 if (!W_ERROR_IS_OK(result)) {
226                         return result;
227                 }
228                 return ntstatus_to_werror(status);
229         }
230
231         if (key && *key) {
232                 keyname = talloc_asprintf(mem_ctx, "%s\\%s", path, key);
233         } else {
234                 keyname = talloc_strdup(mem_ctx, path);
235         }
236         if (keyname == NULL) {
237                 talloc_free(pipe_handle);
238                 return WERR_NOMEM;
239         }
240
241         ZERO_STRUCT(wkey);
242         wkey.name = keyname;
243
244         if (create_key) {
245                 enum winreg_CreateAction action = REG_ACTION_NONE;
246
247                 ZERO_STRUCT(wkeyclass);
248                 wkeyclass.name = "";
249
250                 status = rpccli_winreg_CreateKey(pipe_handle,
251                                                  mem_ctx,
252                                                  hive_handle,
253                                                  wkey,
254                                                  wkeyclass,
255                                                  0,
256                                                  access_mask,
257                                                  NULL,
258                                                  key_handle,
259                                                  &action,
260                                                  &result);
261                 switch (action) {
262                         case REG_ACTION_NONE:
263                                 DEBUG(8, ("winreg_printer_openkey:createkey did nothing -- huh?\n"));
264                                 break;
265                         case REG_CREATED_NEW_KEY:
266                                 DEBUG(8, ("winreg_printer_openkey: createkey created %s\n", keyname));
267                                 break;
268                         case REG_OPENED_EXISTING_KEY:
269                                 DEBUG(8, ("winreg_printer_openkey: createkey opened existing %s\n", keyname));
270                                 break;
271                 }
272         } else {
273                 status = rpccli_winreg_OpenKey(pipe_handle,
274                                                mem_ctx,
275                                                hive_handle,
276                                                wkey,
277                                                0,
278                                                access_mask,
279                                                key_handle,
280                                                &result);
281         }
282         if (!NT_STATUS_IS_OK(status)) {
283                 talloc_free(pipe_handle);
284                 if (!W_ERROR_IS_OK(result)) {
285                         return result;
286                 }
287                 return ntstatus_to_werror(status);
288         }
289
290         *winreg_pipe = pipe_handle;
291
292         return WERR_OK;
293 }
294
295 /**
296  * @brief Create the registry keyname for the given printer.
297  *
298  * @param[in]  mem_ctx  The memory context to use.
299  *
300  * @param[in]  printer  The name of the printer to get the registry key.
301  *
302  * @return     The registry key or NULL on error.
303  */
304 static char *winreg_printer_data_keyname(TALLOC_CTX *mem_ctx, const char *printer) {
305         return talloc_asprintf(mem_ctx, "%s\\%s", TOP_LEVEL_PRINT_PRINTERS_KEY, printer);
306 }
307
308 /**
309  * @internal
310  *
311  * @brief Enumerate values of an opened key handle and retrieve the data.
312  *
313  * @param[in]  mem_ctx  The memory context to use.
314  *
315  * @param[in]  pipe_handle The pipe handle for the rpc connection.
316  *
317  * @param[in]  key_hnd  The opened key handle.
318  *
319  * @param[out] pnum_values A pointer to store he number of values found.
320  *
321  * @param[out] pnum_values A pointer to store the number of values we found.
322  *
323  * @return                   WERR_OK on success, the corresponding DOS error
324  *                           code if something gone wrong.
325  */
326 static WERROR winreg_printer_enumvalues(TALLOC_CTX *mem_ctx,
327                                         struct rpc_pipe_client *pipe_handle,
328                                         struct policy_handle *key_hnd,
329                                         uint32_t *pnum_values,
330                                         struct spoolss_PrinterEnumValues **penum_values)
331 {
332         TALLOC_CTX *tmp_ctx;
333         uint32_t num_subkeys, max_subkeylen, max_classlen;
334         uint32_t num_values, max_valnamelen, max_valbufsize;
335         uint32_t secdescsize;
336         uint32_t i;
337         NTTIME last_changed_time;
338         struct winreg_String classname;
339
340         struct spoolss_PrinterEnumValues *enum_values;
341
342         WERROR result = WERR_OK;
343         NTSTATUS status;
344
345         tmp_ctx = talloc_new(mem_ctx);
346         if (tmp_ctx == NULL) {
347                 return WERR_NOMEM;
348         }
349
350         ZERO_STRUCT(classname);
351
352         status = rpccli_winreg_QueryInfoKey(pipe_handle,
353                                             tmp_ctx,
354                                             key_hnd,
355                                             &classname,
356                                             &num_subkeys,
357                                             &max_subkeylen,
358                                             &max_classlen,
359                                             &num_values,
360                                             &max_valnamelen,
361                                             &max_valbufsize,
362                                             &secdescsize,
363                                             &last_changed_time,
364                                             &result);
365         if (!NT_STATUS_IS_OK(status)) {
366                 DEBUG(0, ("winreg_printer_enumvalues: Could not query info: %s\n",
367                           nt_errstr(status)));
368                 if (!W_ERROR_IS_OK(result)) {
369                         goto error;
370                 }
371                 result = ntstatus_to_werror(status);
372                 goto error;
373         }
374
375         if (num_values == 0) {
376                 *pnum_values = 0;
377                 TALLOC_FREE(tmp_ctx);
378                 return WERR_OK;
379         }
380
381         enum_values = TALLOC_ARRAY(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
382         if (enum_values == NULL) {
383                 result = WERR_NOMEM;
384                 goto error;
385         }
386
387         for (i = 0; i < num_values; i++) {
388                 struct spoolss_PrinterEnumValues val;
389                 struct winreg_ValNameBuf name_buf;
390                 enum winreg_Type type = REG_NONE;
391                 uint8_t *data = NULL;
392                 uint32_t data_size;
393                 uint32_t length;
394                 char n = '\0';;
395
396                 name_buf.name = &n;
397                 name_buf.size = max_valnamelen + 2;
398                 name_buf.length = 0;
399
400                 data_size = max_valbufsize;
401                 data = (uint8_t *) TALLOC(tmp_ctx, data_size);
402                 length = 0;
403
404                 status = rpccli_winreg_EnumValue(pipe_handle,
405                                                  tmp_ctx,
406                                                  key_hnd,
407                                                  i,
408                                                  &name_buf,
409                                                  &type,
410                                                  data,
411                                                  &data_size,
412                                                  &length,
413                                                  &result);
414                 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
415                         result = WERR_OK;
416                         status = NT_STATUS_OK;
417                         break;
418                 }
419
420                 if (!NT_STATUS_IS_OK(status)) {
421                         DEBUG(0, ("winreg_printer_enumvalues: Could not enumerate values: %s\n",
422                                   nt_errstr(status)));
423                         if (!W_ERROR_IS_OK(result)) {
424                                 goto error;
425                         }
426                         result = ntstatus_to_werror(status);
427                         goto error;
428                 }
429
430                 if (name_buf.name == NULL) {
431                         result = WERR_INVALID_PARAMETER;
432                         goto error;
433                 }
434
435                 val.value_name = talloc_strdup(enum_values, name_buf.name);
436                 if (val.value_name == NULL) {
437                         result = WERR_NOMEM;
438                         goto error;
439                 }
440                 val.value_name_len = strlen_m_term(val.value_name) * 2;
441
442                 val.type = type;
443                 val.data_length = data_size;
444                 val.data = NULL;
445                 if (val.data_length) {
446                         val.data = talloc(enum_values, DATA_BLOB);
447                         if (val.data == NULL) {
448                                 result = WERR_NOMEM;
449                                 goto error;
450                         }
451                         *val.data = data_blob_talloc(enum_values, data, data_size);
452                 }
453
454                 enum_values[i] = val;
455         }
456
457         *pnum_values = num_values;
458         if (penum_values) {
459                 *penum_values = talloc_move(mem_ctx, &enum_values);
460         }
461
462         result = WERR_OK;
463
464  error:
465         TALLOC_FREE(tmp_ctx);
466         return result;
467 }
468
469 /**
470  * @internal
471  *
472  * @brief Enumerate subkeys of an opened key handle and get the names.
473  *
474  * @param[in]  mem_ctx  The memory context to use.
475  *
476  * @param[in]  pipe_handle The pipe handle for the rpc connection.
477  *
478  * @param[in]  key_hnd  The opened key handle.
479  *
480  * @param[in]  pnum_subkeys A pointer to store the number of found subkeys.
481  *
482  * @param[in]  psubkeys A pointer to an array to store the found names of
483  *                      subkeys.
484  *
485  * @return                   WERR_OK on success, the corresponding DOS error
486  *                           code if something gone wrong.
487  */
488 static WERROR winreg_printer_enumkeys(TALLOC_CTX *mem_ctx,
489                                       struct rpc_pipe_client *pipe_handle,
490                                       struct policy_handle *key_hnd,
491                                       uint32_t *pnum_subkeys,
492                                       const char ***psubkeys)
493 {
494         TALLOC_CTX *tmp_ctx;
495         const char **subkeys;
496         uint32_t num_subkeys, max_subkeylen, max_classlen;
497         uint32_t num_values, max_valnamelen, max_valbufsize;
498         uint32_t i;
499         NTTIME last_changed_time;
500         uint32_t secdescsize;
501         struct winreg_String classname;
502         WERROR result = WERR_OK;
503         NTSTATUS status;
504
505         tmp_ctx = talloc_new(mem_ctx);
506         if (tmp_ctx == NULL) {
507                 return WERR_NOMEM;
508         }
509
510         ZERO_STRUCT(classname);
511
512         status = rpccli_winreg_QueryInfoKey(pipe_handle,
513                                             tmp_ctx,
514                                             key_hnd,
515                                             &classname,
516                                             &num_subkeys,
517                                             &max_subkeylen,
518                                             &max_classlen,
519                                             &num_values,
520                                             &max_valnamelen,
521                                             &max_valbufsize,
522                                             &secdescsize,
523                                             &last_changed_time,
524                                             &result);
525         if (!NT_STATUS_IS_OK(status)) {
526                 DEBUG(0, ("winreg_printer_enumkeys: Could not query info: %s\n",
527                           nt_errstr(status)));
528                 if (!W_ERROR_IS_OK(result)) {
529                         goto error;
530                 }
531                 result = ntstatus_to_werror(status);
532                 goto error;
533         }
534
535         subkeys = talloc_zero_array(tmp_ctx, const char *, num_subkeys + 2);
536         if (subkeys == NULL) {
537                 result = WERR_NOMEM;
538                 goto error;
539         }
540
541         if (num_subkeys == 0) {
542                 subkeys[0] = talloc_strdup(subkeys, "");
543                 if (subkeys[0] == NULL) {
544                         result = WERR_NOMEM;
545                         goto error;
546                 }
547                 *pnum_subkeys = 0;
548                 if (psubkeys) {
549                         *psubkeys = talloc_move(mem_ctx, &subkeys);
550                 }
551
552                 TALLOC_FREE(tmp_ctx);
553                 return WERR_OK;
554         }
555
556         for (i = 0; i < num_subkeys; i++) {
557                 char c = '\0';
558                 char n = '\0';
559                 char *name = NULL;
560                 struct winreg_StringBuf class_buf;
561                 struct winreg_StringBuf name_buf;
562                 NTTIME modtime;
563
564                 class_buf.name = &c;
565                 class_buf.size = max_classlen + 2;
566                 class_buf.length = 0;
567
568                 name_buf.name = &n;
569                 name_buf.size = max_subkeylen + 2;
570                 name_buf.length = 0;
571
572                 ZERO_STRUCT(modtime);
573
574                 status = rpccli_winreg_EnumKey(pipe_handle,
575                                                tmp_ctx,
576                                                key_hnd,
577                                                i,
578                                                &name_buf,
579                                                &class_buf,
580                                                &modtime,
581                                                &result);
582                 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
583                         result = WERR_OK;
584                         status = NT_STATUS_OK;
585                         break;
586                 }
587
588                 if (!NT_STATUS_IS_OK(status)) {
589                         DEBUG(0, ("winreg_printer_enumkeys: Could not enumerate keys: %s\n",
590                                   nt_errstr(status)));
591                         if (!W_ERROR_IS_OK(result)) {
592                                 goto error;
593                         }
594                         result = ntstatus_to_werror(status);
595                         goto error;
596                 }
597
598                 if (name_buf.name == NULL) {
599                         result = WERR_INVALID_PARAMETER;
600                         goto error;
601                 }
602
603                 name = talloc_strdup(subkeys, name_buf.name);
604                 if (name == NULL) {
605                         result = WERR_NOMEM;
606                         goto error;
607                 }
608
609                 subkeys[i] = name;
610         }
611
612         *pnum_subkeys = num_subkeys;
613         if (psubkeys) {
614                 *psubkeys = talloc_move(mem_ctx, &subkeys);
615         }
616
617  error:
618         TALLOC_FREE(tmp_ctx);
619         return result;
620 }
621
622 /**
623  * @internal
624  *
625  * @brief A function to delete a key and its subkeys recurively.
626  *
627  * @param[in]  mem_ctx  The memory context to use.
628  *
629  * @param[in]  pipe_handle The pipe handle for the rpc connection.
630  *
631  * @param[in]  hive_handle A opened hive handle to the key.
632  *
633  * @param[in]  access_mask The access mask to access the key.
634  *
635  * @param[in]  key      The key to delete
636  *
637  * @return              WERR_OK on success, the corresponding DOS error
638  *                      code if something gone wrong.
639  */
640 static WERROR winreg_printer_delete_subkeys(TALLOC_CTX *mem_ctx,
641                                             struct rpc_pipe_client *pipe_handle,
642                                             struct policy_handle *hive_handle,
643                                             uint32_t access_mask,
644                                             const char *key)
645 {
646         const char **subkeys = NULL;
647         uint32_t num_subkeys = 0;
648         struct policy_handle key_hnd;
649         struct winreg_String wkey;
650         WERROR result = WERR_OK;
651         NTSTATUS status;
652         uint32_t i;
653
654         ZERO_STRUCT(key_hnd);
655         wkey.name = key;
656
657         DEBUG(2, ("winreg_printer_delete_subkeys: delete key %s\n", key));
658         /* open the key */
659         status = rpccli_winreg_OpenKey(pipe_handle,
660                                        mem_ctx,
661                                        hive_handle,
662                                        wkey,
663                                        0,
664                                        access_mask,
665                                        &key_hnd,
666                                        &result);
667         if (!NT_STATUS_IS_OK(status)) {
668                 DEBUG(0, ("winreg_printer_delete_subkeys: Could not open key %s: %s\n",
669                           wkey.name, nt_errstr(status)));
670                 if (!W_ERROR_IS_OK(result)) {
671                         return result;
672                 }
673                 return ntstatus_to_werror(status);
674         }
675
676         result = winreg_printer_enumkeys(mem_ctx,
677                                          pipe_handle,
678                                          &key_hnd,
679                                          &num_subkeys,
680                                          &subkeys);
681         if (!W_ERROR_IS_OK(result)) {
682                 goto done;
683         }
684
685         for (i = 0; i < num_subkeys; i++) {
686                 /* create key + subkey */
687                 char *subkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkeys[i]);
688                 if (subkey == NULL) {
689                         goto done;
690                 }
691
692                 DEBUG(2, ("winreg_printer_delete_subkeys: delete subkey %s\n", subkey));
693                 result = winreg_printer_delete_subkeys(mem_ctx,
694                                                        pipe_handle,
695                                                        hive_handle,
696                                                        access_mask,
697                                                        subkey);
698                 if (!W_ERROR_IS_OK(result)) {
699                         goto done;
700                 }
701         }
702
703         if (is_valid_policy_hnd(&key_hnd)) {
704                 rpccli_winreg_CloseKey(pipe_handle, mem_ctx, &key_hnd, NULL);
705         }
706
707         wkey.name = key;
708
709         status = rpccli_winreg_DeleteKey(pipe_handle,
710                                          mem_ctx,
711                                          hive_handle,
712                                          wkey,
713                                          &result);
714
715 done:
716         if (is_valid_policy_hnd(&key_hnd)) {
717                 rpccli_winreg_CloseKey(pipe_handle, mem_ctx, &key_hnd, NULL);
718         }
719
720         return result;
721 }
722
723 static WERROR winreg_printer_write_sz(TALLOC_CTX *mem_ctx,
724                                       struct rpc_pipe_client *pipe_handle,
725                                       struct policy_handle *key_handle,
726                                       const char *value,
727                                       const char *data)
728 {
729         struct winreg_String wvalue;
730         DATA_BLOB blob;
731         WERROR result = WERR_OK;
732         NTSTATUS status;
733
734         wvalue.name = value;
735         if (data == NULL) {
736                 blob = data_blob_string_const("");
737         } else {
738                 if (!push_reg_sz(mem_ctx, NULL, &blob, data)) {
739                         DEBUG(0, ("winreg_printer_write_sz: Could not marshall string %s for %s\n",
740                                 data, wvalue.name));
741                         return WERR_NOMEM;
742                 }
743         }
744         status = rpccli_winreg_SetValue(pipe_handle,
745                                         mem_ctx,
746                                         key_handle,
747                                         wvalue,
748                                         REG_SZ,
749                                         blob.data,
750                                         blob.length,
751                                         &result);
752         if (!NT_STATUS_IS_OK(status)) {
753                 DEBUG(0, ("winreg_printer_write_sz: Could not set value %s: %s\n",
754                         wvalue.name, win_errstr(result)));
755                 if (!W_ERROR_IS_OK(result)) {
756                         result = ntstatus_to_werror(status);
757                 }
758         }
759
760         return result;
761 }
762
763 static WERROR winreg_printer_write_dword(TALLOC_CTX *mem_ctx,
764                                          struct rpc_pipe_client *pipe_handle,
765                                          struct policy_handle *key_handle,
766                                          const char *value,
767                                          uint32_t data)
768 {
769         struct winreg_String wvalue;
770         DATA_BLOB blob;
771         WERROR result = WERR_OK;
772         NTSTATUS status;
773
774         wvalue.name = value;
775         blob = data_blob_talloc(mem_ctx, NULL, 4);
776         SIVAL(blob.data, 0, data);
777
778         status = rpccli_winreg_SetValue(pipe_handle,
779                                         mem_ctx,
780                                         key_handle,
781                                         wvalue,
782                                         REG_DWORD,
783                                         blob.data,
784                                         blob.length,
785                                         &result);
786         if (!NT_STATUS_IS_OK(status)) {
787                 DEBUG(0, ("winreg_printer_write_dword: Could not set value %s: %s\n",
788                         wvalue.name, win_errstr(result)));
789                 if (!W_ERROR_IS_OK(result)) {
790                         result = ntstatus_to_werror(status);
791                 }
792         }
793
794         return result;
795 }
796
797 static WERROR winreg_printer_write_binary(TALLOC_CTX *mem_ctx,
798                                           struct rpc_pipe_client *pipe_handle,
799                                           struct policy_handle *key_handle,
800                                           const char *value,
801                                           DATA_BLOB blob)
802 {
803         struct winreg_String wvalue;
804         WERROR result = WERR_OK;
805         NTSTATUS status;
806
807         wvalue.name = value;
808         status = rpccli_winreg_SetValue(pipe_handle,
809                                         mem_ctx,
810                                         key_handle,
811                                         wvalue,
812                                         REG_BINARY,
813                                         blob.data,
814                                         blob.length,
815                                         &result);
816         if (!NT_STATUS_IS_OK(status)) {
817                 DEBUG(0, ("winreg_printer_write_binary: Could not set value %s: %s\n",
818                         wvalue.name, win_errstr(result)));
819                 if (!W_ERROR_IS_OK(result)) {
820                         result = ntstatus_to_werror(status);
821                 }
822         }
823
824         return result;
825 }
826
827 /********************************************************************
828  Public winreg function for spoolss
829 ********************************************************************/
830
831 /* Set printer data over the winreg pipe. */
832 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
833                                  struct auth_serversupplied_info *server_info,
834                                  const char *printer,
835                                  const char *key,
836                                  const char *value,
837                                  enum winreg_Type type,
838                                  uint8_t *data,
839                                  uint32_t data_size)
840 {
841         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
842         struct rpc_pipe_client *winreg_pipe = NULL;
843         struct policy_handle hive_hnd, key_hnd;
844         struct winreg_String wvalue;
845         char *path;
846         WERROR result = WERR_OK;
847         NTSTATUS status;
848         TALLOC_CTX *tmp_ctx;
849
850         tmp_ctx = talloc_new(mem_ctx);
851         if (tmp_ctx == NULL) {
852                 return WERR_NOMEM;
853         }
854
855         path = winreg_printer_data_keyname(tmp_ctx, printer);
856         if (path == NULL) {
857                 TALLOC_FREE(tmp_ctx);
858                 return WERR_NOMEM;
859         }
860
861         ZERO_STRUCT(hive_hnd);
862         ZERO_STRUCT(key_hnd);
863
864         DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
865                         key, value, access_mask, printer));
866         result = winreg_printer_openkey(tmp_ctx,
867                                         server_info,
868                                         &winreg_pipe,
869                                         path,
870                                         key,
871                                         true,
872                                         access_mask,
873                                         &hive_hnd,
874                                         &key_hnd);
875         if (!W_ERROR_IS_OK(result)) {
876                 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
877                           key, win_errstr(result)));
878                 goto done;
879         }
880
881         wvalue.name = value;
882         status = rpccli_winreg_SetValue(winreg_pipe,
883                                         tmp_ctx,
884                                         &key_hnd,
885                                         wvalue,
886                                         type,
887                                         data,
888                                         data_size,
889                                         &result);
890         if (!NT_STATUS_IS_OK(status)) {
891                 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
892                           value, nt_errstr(status)));
893                 if (!W_ERROR_IS_OK(result)) {
894                         goto done;
895                 }
896                 result = ntstatus_to_werror(status);
897                 goto done;
898         }
899
900         result = WERR_OK;
901 done:
902         if (winreg_pipe != NULL) {
903                 if (is_valid_policy_hnd(&key_hnd)) {
904                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
905                 }
906                 if (is_valid_policy_hnd(&hive_hnd)) {
907                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
908                 }
909         }
910
911         TALLOC_FREE(tmp_ctx);
912         return result;
913 }
914
915 /* Get printer data over a winreg pipe. */
916 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
917                                  struct auth_serversupplied_info *server_info,
918                                  const char *printer,
919                                  const char *key,
920                                  const char *value,
921                                  enum winreg_Type *type,
922                                  uint8_t **data,
923                                  uint32_t *data_size)
924 {
925         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
926         struct rpc_pipe_client *winreg_pipe = NULL;
927         struct policy_handle hive_hnd, key_hnd;
928         struct winreg_String wvalue;
929         enum winreg_Type type_in;
930         char *path;
931         uint8_t *data_in;
932         uint32_t data_in_size = 0;
933         uint32_t value_len = 0;
934         WERROR result = WERR_OK;
935         NTSTATUS status;
936         TALLOC_CTX *tmp_ctx;
937
938         tmp_ctx = talloc_new(mem_ctx);
939         if (tmp_ctx == NULL) {
940                 return WERR_NOMEM;
941         }
942
943         path = winreg_printer_data_keyname(tmp_ctx, printer);
944         if (path == NULL) {
945                 TALLOC_FREE(tmp_ctx);
946                 return WERR_NOMEM;
947         }
948
949         ZERO_STRUCT(hive_hnd);
950         ZERO_STRUCT(key_hnd);
951
952         result = winreg_printer_openkey(tmp_ctx,
953                                         server_info,
954                                         &winreg_pipe,
955                                         path,
956                                         key,
957                                         false,
958                                         access_mask,
959                                         &hive_hnd,
960                                         &key_hnd);
961         if (!W_ERROR_IS_OK(result)) {
962                 DEBUG(0, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
963                           key, win_errstr(result)));
964                 goto done;
965         }
966
967         wvalue.name = value;
968
969         /*
970          * call QueryValue once with data == NULL to get the
971          * needed memory size to be allocated, then allocate
972          * data buffer and call again.
973          */
974         status = rpccli_winreg_QueryValue(winreg_pipe,
975                                           tmp_ctx,
976                                           &key_hnd,
977                                           &wvalue,
978                                           &type_in,
979                                           NULL,
980                                           &data_in_size,
981                                           &value_len,
982                                           &result);
983         if (!NT_STATUS_IS_OK(status)) {
984                 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
985                           value, 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         data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
994         if (data_in == NULL) {
995                 result = WERR_NOMEM;
996                 goto done;
997         }
998         value_len = 0;
999
1000         status = rpccli_winreg_QueryValue(winreg_pipe,
1001                                           tmp_ctx,
1002                                           &key_hnd,
1003                                           &wvalue,
1004                                           &type_in,
1005                                           data_in,
1006                                           &data_in_size,
1007                                           &value_len,
1008                                           &result);
1009         if (!NT_STATUS_IS_OK(status)) {
1010                 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
1011                           value, nt_errstr(status)));
1012                 if (!W_ERROR_IS_OK(result)) {
1013                         result = ntstatus_to_werror(status);
1014                 }
1015                 goto done;
1016         }
1017
1018         *type = type_in;
1019         *data_size = data_in_size;
1020         if (data_in_size) {
1021                 *data = talloc_move(mem_ctx, &data_in);
1022         }
1023
1024         result = WERR_OK;
1025 done:
1026         if (winreg_pipe != NULL) {
1027                 if (is_valid_policy_hnd(&key_hnd)) {
1028                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1029                 }
1030                 if (is_valid_policy_hnd(&hive_hnd)) {
1031                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1032                 }
1033         }
1034
1035         TALLOC_FREE(tmp_ctx);
1036         return result;
1037 }
1038
1039 /* Enumerate on the values of a given key and provide the data. */
1040 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
1041                                   struct auth_serversupplied_info *server_info,
1042                                   const char *printer,
1043                                   const char *key,
1044                                   uint32_t *pnum_values,
1045                                   struct spoolss_PrinterEnumValues **penum_values)
1046 {
1047         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1048         struct rpc_pipe_client *winreg_pipe = NULL;
1049         struct policy_handle hive_hnd, key_hnd;
1050
1051         struct spoolss_PrinterEnumValues *enum_values = NULL;
1052         uint32_t num_values = 0;
1053         char *path;
1054         WERROR result = WERR_OK;
1055
1056         TALLOC_CTX *tmp_ctx;
1057
1058         tmp_ctx = talloc_new(mem_ctx);
1059         if (tmp_ctx == NULL) {
1060                 return WERR_NOMEM;
1061         }
1062
1063         path = winreg_printer_data_keyname(tmp_ctx, printer);
1064         if (path == NULL) {
1065                 TALLOC_FREE(tmp_ctx);
1066                 return WERR_NOMEM;
1067         }
1068
1069         result = winreg_printer_openkey(tmp_ctx,
1070                                         server_info,
1071                                         &winreg_pipe,
1072                                         path,
1073                                         key,
1074                                         false,
1075                                         access_mask,
1076                                         &hive_hnd,
1077                                         &key_hnd);
1078         if (!W_ERROR_IS_OK(result)) {
1079                 DEBUG(0, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
1080                           key, win_errstr(result)));
1081                 goto done;
1082         }
1083
1084         result = winreg_printer_enumvalues(tmp_ctx,
1085                                            winreg_pipe,
1086                                            &key_hnd,
1087                                            &num_values,
1088                                            &enum_values);
1089         if (!W_ERROR_IS_OK(result)) {
1090                 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
1091                           key, win_errstr(result)));
1092                 goto done;
1093         }
1094
1095         *pnum_values = num_values;
1096         if (penum_values) {
1097                 *penum_values = talloc_move(mem_ctx, &enum_values);
1098         }
1099
1100         result = WERR_OK;
1101 done:
1102         if (winreg_pipe != NULL) {
1103                 if (is_valid_policy_hnd(&key_hnd)) {
1104                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1105                 }
1106                 if (is_valid_policy_hnd(&hive_hnd)) {
1107                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1108                 }
1109         }
1110
1111         TALLOC_FREE(tmp_ctx);
1112         return result;
1113 }
1114
1115 /* Delete printer data over a winreg pipe. */
1116 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
1117                                     struct auth_serversupplied_info *server_info,
1118                                     const char *printer,
1119                                     const char *key,
1120                                     const char *value)
1121 {
1122         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1123         struct rpc_pipe_client *winreg_pipe = NULL;
1124         struct policy_handle hive_hnd, key_hnd;
1125         struct winreg_String wvalue;
1126         char *path;
1127         WERROR result = WERR_OK;
1128         NTSTATUS status;
1129
1130         TALLOC_CTX *tmp_ctx;
1131
1132         tmp_ctx = talloc_new(mem_ctx);
1133         if (tmp_ctx == NULL) {
1134                 return WERR_NOMEM;
1135         }
1136
1137         path = winreg_printer_data_keyname(tmp_ctx, printer);
1138         if (path == NULL) {
1139                 TALLOC_FREE(tmp_ctx);
1140                 return WERR_NOMEM;
1141         }
1142
1143         ZERO_STRUCT(hive_hnd);
1144         ZERO_STRUCT(key_hnd);
1145
1146         result = winreg_printer_openkey(tmp_ctx,
1147                                         server_info,
1148                                         &winreg_pipe,
1149                                         path,
1150                                         key,
1151                                         false,
1152                                         access_mask,
1153                                         &hive_hnd,
1154                                         &key_hnd);
1155         if (!W_ERROR_IS_OK(result)) {
1156                 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
1157                           key, win_errstr(result)));
1158                 goto done;
1159         }
1160
1161         wvalue.name = value;
1162         status = rpccli_winreg_DeleteValue(winreg_pipe,
1163                                            tmp_ctx,
1164                                            &key_hnd,
1165                                            wvalue,
1166                                            &result);
1167         if (!NT_STATUS_IS_OK(status)) {
1168                 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
1169                           value, nt_errstr(status)));
1170                 if (!W_ERROR_IS_OK(result)) {
1171                         goto done;
1172                 }
1173                 result = ntstatus_to_werror(status);
1174                 goto done;
1175         }
1176
1177         result = WERR_OK;
1178 done:
1179         if (winreg_pipe != NULL) {
1180                 if (is_valid_policy_hnd(&key_hnd)) {
1181                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1182                 }
1183                 if (is_valid_policy_hnd(&hive_hnd)) {
1184                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1185                 }
1186         }
1187
1188         TALLOC_FREE(tmp_ctx);
1189         return result;
1190 }
1191
1192 /* Enumerate on the subkeys of a given key and provide the data. */
1193 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
1194                                struct auth_serversupplied_info *server_info,
1195                                const char *printer,
1196                                const char *key,
1197                                uint32_t *pnum_subkeys,
1198                                const char ***psubkeys)
1199 {
1200         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1201         struct rpc_pipe_client *winreg_pipe = NULL;
1202         struct policy_handle hive_hnd, key_hnd;
1203         char *path;
1204         const char **subkeys = NULL;
1205         uint32_t num_subkeys = -1;
1206
1207         WERROR result = WERR_OK;
1208
1209         TALLOC_CTX *tmp_ctx;
1210
1211         tmp_ctx = talloc_new(mem_ctx);
1212         if (tmp_ctx == NULL) {
1213                 return WERR_NOMEM;
1214         }
1215
1216         path = winreg_printer_data_keyname(tmp_ctx, printer);
1217         if (path == NULL) {
1218                 TALLOC_FREE(tmp_ctx);
1219                 return WERR_NOMEM;
1220         }
1221
1222         ZERO_STRUCT(hive_hnd);
1223         ZERO_STRUCT(key_hnd);
1224
1225         result = winreg_printer_openkey(tmp_ctx,
1226                                         server_info,
1227                                         &winreg_pipe,
1228                                         path,
1229                                         key,
1230                                         false,
1231                                         access_mask,
1232                                         &hive_hnd,
1233                                         &key_hnd);
1234         if (!W_ERROR_IS_OK(result)) {
1235                 DEBUG(0, ("winreg_enum_printer_key: Could not open key %s: %s\n",
1236                           key, win_errstr(result)));
1237                 goto done;
1238         }
1239
1240         result = winreg_printer_enumkeys(tmp_ctx,
1241                                          winreg_pipe,
1242                                          &key_hnd,
1243                                          &num_subkeys,
1244                                          &subkeys);
1245         if (!W_ERROR_IS_OK(result)) {
1246                 DEBUG(0, ("winreg_enum_printer_key: Could not enumerate subkeys in %s: %s\n",
1247                           key, win_errstr(result)));
1248                 goto done;
1249         }
1250
1251         *pnum_subkeys = num_subkeys;
1252         if (psubkeys) {
1253                 *psubkeys = talloc_move(mem_ctx, &subkeys);
1254         }
1255
1256         result = WERR_OK;
1257 done:
1258         if (winreg_pipe != NULL) {
1259                 if (is_valid_policy_hnd(&key_hnd)) {
1260                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1261                 }
1262                 if (is_valid_policy_hnd(&hive_hnd)) {
1263                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1264                 }
1265         }
1266
1267         TALLOC_FREE(tmp_ctx);
1268         return result;
1269 }
1270
1271 /* Delete a key with subkeys of a given printer. */
1272 WERROR winreg_delete_printer_key(TALLOC_CTX *mem_ctx,
1273                                  struct auth_serversupplied_info *server_info,
1274                                  const char *printer,
1275                                  const char *key)
1276 {
1277         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1278         struct rpc_pipe_client *winreg_pipe = NULL;
1279         struct policy_handle hive_hnd, key_hnd;
1280         char *keyname;
1281         char *path;
1282         WERROR result;
1283         TALLOC_CTX *tmp_ctx;
1284
1285         tmp_ctx = talloc_new(mem_ctx);
1286         if (tmp_ctx == NULL) {
1287                 return WERR_NOMEM;
1288         }
1289
1290         path = winreg_printer_data_keyname(tmp_ctx, printer);
1291         if (path == NULL) {
1292                 TALLOC_FREE(tmp_ctx);
1293                 return WERR_NOMEM;
1294         }
1295
1296         result = winreg_printer_openkey(tmp_ctx,
1297                                         server_info,
1298                                         &winreg_pipe,
1299                                         path,
1300                                         key,
1301                                         false,
1302                                         access_mask,
1303                                         &hive_hnd,
1304                                         &key_hnd);
1305         if (!W_ERROR_IS_OK(result)) {
1306                 /* key doesn't exist */
1307                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1308                         result = WERR_OK;
1309                         goto done;
1310                 }
1311
1312                 DEBUG(0, ("winreg_delete_printer_key: Could not open key %s: %s\n",
1313                           key, win_errstr(result)));
1314                 goto done;
1315         }
1316
1317         if (is_valid_policy_hnd(&key_hnd)) {
1318                 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1319         }
1320
1321         keyname = talloc_asprintf(tmp_ctx,
1322                                   "%s\\%s",
1323                                   path,
1324                                   key);
1325         if (keyname == NULL) {
1326                 result = WERR_NOMEM;
1327                 goto done;
1328         }
1329
1330         result = winreg_printer_delete_subkeys(tmp_ctx,
1331                                                winreg_pipe,
1332                                                &hive_hnd,
1333                                                access_mask,
1334                                                keyname);
1335         if (!W_ERROR_IS_OK(result)) {
1336                 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
1337                           key, win_errstr(result)));
1338                 goto done;
1339         }
1340
1341 done:
1342         if (winreg_pipe != NULL) {
1343                 if (is_valid_policy_hnd(&key_hnd)) {
1344                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1345                 }
1346                 if (is_valid_policy_hnd(&hive_hnd)) {
1347                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1348                 }
1349         }
1350
1351         TALLOC_FREE(tmp_ctx);
1352         return result;
1353 }
1354
1355 /*
1356  * The special behaviour of the spoolss forms is documented at the website:
1357  *
1358  * Managing Win32 Printserver Forms
1359  * http://unixwiz.net/techtips/winspooler-forms.html
1360  */
1361
1362 WERROR winreg_printer_addform1(TALLOC_CTX *mem_ctx,
1363                                struct auth_serversupplied_info *server_info,
1364                                struct spoolss_AddFormInfo1 *form)
1365 {
1366         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1367         struct rpc_pipe_client *winreg_pipe = NULL;
1368         struct policy_handle hive_hnd, key_hnd;
1369         struct winreg_String wvalue;
1370         DATA_BLOB blob;
1371         uint32_t num_info = 0;
1372         union spoolss_FormInfo *info = NULL;
1373         uint32_t i;
1374         WERROR result;
1375         NTSTATUS status;
1376         TALLOC_CTX *tmp_ctx;
1377
1378         tmp_ctx = talloc_new(mem_ctx);
1379         if (tmp_ctx == NULL) {
1380                 return WERR_NOMEM;
1381         }
1382
1383         ZERO_STRUCT(hive_hnd);
1384         ZERO_STRUCT(key_hnd);
1385
1386         result = winreg_printer_openkey(tmp_ctx,
1387                                         server_info,
1388                                         &winreg_pipe,
1389                                         TOP_LEVEL_CONTROL_FORMS_KEY,
1390                                         "",
1391                                         true,
1392                                         access_mask,
1393                                         &hive_hnd,
1394                                         &key_hnd);
1395         if (!W_ERROR_IS_OK(result)) {
1396                 DEBUG(0, ("winreg_printer_addform1: Could not open key %s: %s\n",
1397                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1398                 goto done;
1399         }
1400
1401         result = winreg_printer_enumforms1(tmp_ctx, server_info, &num_info, &info);
1402         if (!W_ERROR_IS_OK(result)) {
1403                 DEBUG(0, ("winreg_printer_addform: Could not enum keys %s: %s\n",
1404                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1405                 goto done;
1406         }
1407
1408         /* If form name already exists or is builtin return ALREADY_EXISTS */
1409         for (i = 0; i < num_info; i++) {
1410                 if (strequal(info[i].info1.form_name, form->form_name)) {
1411                         result = WERR_FILE_EXISTS;
1412                         goto done;
1413                 }
1414         }
1415
1416         wvalue.name = form->form_name;
1417
1418         blob = data_blob_talloc(tmp_ctx, NULL, 32);
1419         SIVAL(blob.data,  0, form->size.width);
1420         SIVAL(blob.data,  4, form->size.height);
1421         SIVAL(blob.data,  8, form->area.left);
1422         SIVAL(blob.data, 12, form->area.top);
1423         SIVAL(blob.data, 16, form->area.right);
1424         SIVAL(blob.data, 20, form->area.bottom);
1425         SIVAL(blob.data, 24, num_info + 1); /* FIXME */
1426         SIVAL(blob.data, 28, form->flags);
1427
1428         status = rpccli_winreg_SetValue(winreg_pipe,
1429                                         tmp_ctx,
1430                                         &key_hnd,
1431                                         wvalue,
1432                                         REG_BINARY,
1433                                         blob.data,
1434                                         blob.length,
1435                                         &result);
1436         if (!NT_STATUS_IS_OK(status)) {
1437                 DEBUG(0, ("winreg_printer_addform1: Could not set value %s: %s\n",
1438                           wvalue.name, nt_errstr(status)));
1439                 if (!W_ERROR_IS_OK(result)) {
1440                         goto done;
1441                 }
1442                 result = ntstatus_to_werror(status);
1443                 goto done;
1444         }
1445
1446         result = WERR_OK;
1447 done:
1448         if (winreg_pipe != NULL) {
1449                 if (is_valid_policy_hnd(&key_hnd)) {
1450                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1451                 }
1452                 if (is_valid_policy_hnd(&hive_hnd)) {
1453                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1454                 }
1455         }
1456
1457         TALLOC_FREE(info);
1458         TALLOC_FREE(tmp_ctx);
1459         return result;
1460 }
1461
1462 WERROR winreg_printer_enumforms1(TALLOC_CTX *mem_ctx,
1463                                  struct auth_serversupplied_info *server_info,
1464                                  uint32_t *pnum_info,
1465                                  union spoolss_FormInfo **pinfo)
1466 {
1467         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1468         struct rpc_pipe_client *winreg_pipe = NULL;
1469         struct policy_handle hive_hnd, key_hnd;
1470         union spoolss_FormInfo *info;
1471         struct spoolss_PrinterEnumValues *enum_values = NULL;
1472         uint32_t num_values = 0;
1473         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
1474         uint32_t i;
1475         WERROR result;
1476         TALLOC_CTX *tmp_ctx;
1477
1478         tmp_ctx = talloc_new(mem_ctx);
1479         if (tmp_ctx == NULL) {
1480                 return WERR_NOMEM;
1481         }
1482
1483         ZERO_STRUCT(hive_hnd);
1484         ZERO_STRUCT(key_hnd);
1485
1486         result = winreg_printer_openkey(tmp_ctx,
1487                                         server_info,
1488                                         &winreg_pipe,
1489                                         TOP_LEVEL_CONTROL_FORMS_KEY,
1490                                         "",
1491                                         true,
1492                                         access_mask,
1493                                         &hive_hnd,
1494                                         &key_hnd);
1495         if (!W_ERROR_IS_OK(result)) {
1496                 /* key doesn't exist */
1497                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1498                         result = WERR_OK;
1499                         goto done;
1500                 }
1501
1502                 DEBUG(0, ("winreg_printer_enumforms1: Could not open key %s: %s\n",
1503                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1504                 goto done;
1505         }
1506
1507         result = winreg_printer_enumvalues(tmp_ctx,
1508                                            winreg_pipe,
1509                                            &key_hnd,
1510                                            &num_values,
1511                                            &enum_values);
1512         if (!W_ERROR_IS_OK(result)) {
1513                 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
1514                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1515                 goto done;
1516         }
1517
1518         info = TALLOC_ARRAY(tmp_ctx, union spoolss_FormInfo, num_builtin + num_values);
1519         if (info == NULL) {
1520                 result = WERR_NOMEM;
1521                 goto done;
1522         }
1523
1524         /* Enumerate BUILTIN forms */
1525         for (i = 0; i < num_builtin; i++) {
1526                 info[i].info1 = builtin_forms1[i];
1527         }
1528
1529         /* Enumerate registry forms */
1530         for (i = 0; i < num_values; i++) {
1531                 union spoolss_FormInfo val;
1532
1533                 if (enum_values[i].type != REG_BINARY ||
1534                     enum_values[i].data_length != 32) {
1535                         continue;
1536                 }
1537
1538                 val.info1.form_name = talloc_strdup(info, enum_values[i].value_name);
1539                 if (val.info1.form_name == NULL) {
1540                         result = WERR_NOMEM;
1541                         goto done;
1542                 }
1543
1544                 val.info1.size.width  = IVAL(enum_values[i].data->data,  0);
1545                 val.info1.size.height = IVAL(enum_values[i].data->data,  4);
1546                 val.info1.area.left   = IVAL(enum_values[i].data->data,  8);
1547                 val.info1.area.top    = IVAL(enum_values[i].data->data, 12);
1548                 val.info1.area.right  = IVAL(enum_values[i].data->data, 16);
1549                 val.info1.area.bottom = IVAL(enum_values[i].data->data, 20);
1550                 /* skip form index      IVAL(enum_values[i].data->data, 24)));*/
1551                 val.info1.flags       = IVAL(enum_values[i].data->data, 28);
1552
1553                 info[i + num_builtin] = val;
1554         }
1555
1556         *pnum_info = num_builtin + num_values;
1557         if (pinfo) {
1558                 *pinfo = talloc_move(mem_ctx, &info);
1559         }
1560
1561 done:
1562         if (winreg_pipe != NULL) {
1563                 if (is_valid_policy_hnd(&key_hnd)) {
1564                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1565                 }
1566                 if (is_valid_policy_hnd(&hive_hnd)) {
1567                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1568                 }
1569         }
1570
1571         TALLOC_FREE(enum_values);
1572         TALLOC_FREE(tmp_ctx);
1573         return result;
1574 }
1575
1576 WERROR winreg_printer_deleteform1(TALLOC_CTX *mem_ctx,
1577                                   struct auth_serversupplied_info *server_info,
1578                                   const char *form_name)
1579 {
1580         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1581         struct rpc_pipe_client *winreg_pipe = NULL;
1582         struct policy_handle hive_hnd, key_hnd;
1583         struct winreg_String wvalue;
1584         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
1585         uint32_t i;
1586         WERROR result = WERR_OK;
1587         NTSTATUS status;
1588         TALLOC_CTX *tmp_ctx;
1589
1590         for (i = 0; i < num_builtin; i++) {
1591                 if (strequal(builtin_forms1[i].form_name, form_name)) {
1592                         return WERR_INVALID_PARAMETER;
1593                 }
1594         }
1595
1596         tmp_ctx = talloc_new(mem_ctx);
1597         if (tmp_ctx == NULL) {
1598                 return WERR_NOMEM;
1599         }
1600
1601         ZERO_STRUCT(hive_hnd);
1602         ZERO_STRUCT(key_hnd);
1603
1604         result = winreg_printer_openkey(tmp_ctx,
1605                                         server_info,
1606                                         &winreg_pipe,
1607                                         TOP_LEVEL_CONTROL_FORMS_KEY,
1608                                         "",
1609                                         false,
1610                                         access_mask,
1611                                         &hive_hnd,
1612                                         &key_hnd);
1613         if (!W_ERROR_IS_OK(result)) {
1614                 DEBUG(0, ("winreg_printer_deleteform1: Could not open key %s: %s\n",
1615                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1616                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1617                         result = WERR_INVALID_FORM_NAME;
1618                 }
1619                 goto done;
1620         }
1621
1622         wvalue.name = form_name;
1623         status = rpccli_winreg_DeleteValue(winreg_pipe,
1624                                            tmp_ctx,
1625                                            &key_hnd,
1626                                            wvalue,
1627                                            &result);
1628         if (!NT_STATUS_IS_OK(status)) {
1629                 /* If the value doesn't exist, return WERR_INVALID_FORM_NAME */
1630                 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1631                         result = WERR_INVALID_FORM_NAME;
1632                         goto done;
1633                 }
1634                 DEBUG(0, ("winreg_printer_delteform1: Could not delete value %s: %s\n",
1635                           wvalue.name, nt_errstr(status)));
1636                 if (!W_ERROR_IS_OK(result)) {
1637                         goto done;
1638                 }
1639                 result = ntstatus_to_werror(status);
1640                 goto done;
1641         }
1642
1643         result = WERR_OK;
1644 done:
1645         if (winreg_pipe != NULL) {
1646                 if (is_valid_policy_hnd(&key_hnd)) {
1647                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1648                 }
1649                 if (is_valid_policy_hnd(&hive_hnd)) {
1650                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1651                 }
1652         }
1653
1654         TALLOC_FREE(tmp_ctx);
1655         return result;
1656 }
1657
1658 WERROR winreg_printer_setform1(TALLOC_CTX *mem_ctx,
1659                                struct auth_serversupplied_info *server_info,
1660                                const char *form_name,
1661                                struct spoolss_AddFormInfo1 *form)
1662 {
1663         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1664         struct rpc_pipe_client *winreg_pipe = NULL;
1665         struct policy_handle hive_hnd, key_hnd;
1666         struct winreg_String wvalue;
1667         DATA_BLOB blob;
1668         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
1669         uint32_t i;
1670         WERROR result;
1671         NTSTATUS status;
1672         TALLOC_CTX *tmp_ctx = NULL;
1673
1674         for (i = 0; i < num_builtin; i++) {
1675                 if (strequal(builtin_forms1[i].form_name, form->form_name)) {
1676                         result = WERR_INVALID_PARAM;
1677                         goto done;
1678                 }
1679         }
1680
1681         tmp_ctx = talloc_new(mem_ctx);
1682         if (tmp_ctx == NULL) {
1683                 return WERR_NOMEM;
1684         }
1685
1686         ZERO_STRUCT(hive_hnd);
1687         ZERO_STRUCT(key_hnd);
1688
1689         result = winreg_printer_openkey(tmp_ctx,
1690                                         server_info,
1691                                         &winreg_pipe,
1692                                         TOP_LEVEL_CONTROL_FORMS_KEY,
1693                                         "",
1694                                         true,
1695                                         access_mask,
1696                                         &hive_hnd,
1697                                         &key_hnd);
1698         if (!W_ERROR_IS_OK(result)) {
1699                 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
1700                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1701                 goto done;
1702         }
1703
1704         /* If form_name != form->form_name then we renamed the form */
1705         if (strequal(form_name, form->form_name)) {
1706                 result = winreg_printer_deleteform1(tmp_ctx, server_info, form_name);
1707                 if (!W_ERROR_IS_OK(result)) {
1708                         DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
1709                                   TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1710                         goto done;
1711                 }
1712         }
1713
1714         wvalue.name = form->form_name;
1715
1716         blob = data_blob_talloc(tmp_ctx, NULL, 32);
1717         SIVAL(blob.data,  0, form->size.width);
1718         SIVAL(blob.data,  4, form->size.height);
1719         SIVAL(blob.data,  8, form->area.left);
1720         SIVAL(blob.data, 12, form->area.top);
1721         SIVAL(blob.data, 16, form->area.right);
1722         SIVAL(blob.data, 20, form->area.bottom);
1723         SIVAL(blob.data, 24, 42);
1724         SIVAL(blob.data, 28, form->flags);
1725
1726         status = rpccli_winreg_SetValue(winreg_pipe,
1727                                         tmp_ctx,
1728                                         &key_hnd,
1729                                         wvalue,
1730                                         REG_BINARY,
1731                                         blob.data,
1732                                         blob.length,
1733                                         &result);
1734         if (!NT_STATUS_IS_OK(status)) {
1735                 DEBUG(0, ("winreg_printer_setform1: Could not set value %s: %s\n",
1736                           wvalue.name, nt_errstr(status)));
1737                 if (!W_ERROR_IS_OK(result)) {
1738                         goto done;
1739                 }
1740                 result = ntstatus_to_werror(status);
1741                 goto done;
1742         }
1743
1744         result = WERR_OK;
1745 done:
1746         if (winreg_pipe != NULL) {
1747                 if (is_valid_policy_hnd(&key_hnd)) {
1748                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1749                 }
1750                 if (is_valid_policy_hnd(&hive_hnd)) {
1751                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1752                 }
1753         }
1754
1755         TALLOC_FREE(tmp_ctx);
1756         return result;
1757 }
1758
1759 WERROR winreg_printer_getform1(TALLOC_CTX *mem_ctx,
1760                                struct auth_serversupplied_info *server_info,
1761                                const char *form_name,
1762                                struct spoolss_FormInfo1 *r)
1763 {
1764         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1765         struct rpc_pipe_client *winreg_pipe = NULL;
1766         struct policy_handle hive_hnd, key_hnd;
1767         struct winreg_String wvalue;
1768         enum winreg_Type type_in;
1769         uint8_t *data_in;
1770         uint32_t data_in_size = 0;
1771         uint32_t value_len = 0;
1772         uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
1773         uint32_t i;
1774         WERROR result;
1775         NTSTATUS status;
1776         TALLOC_CTX *tmp_ctx;
1777
1778         /* check builtin forms first */
1779         for (i = 0; i < num_builtin; i++) {
1780                 if (strequal(builtin_forms1[i].form_name, form_name)) {
1781                         *r = builtin_forms1[i];
1782                         return WERR_OK;
1783                 }
1784         }
1785
1786         tmp_ctx = talloc_new(mem_ctx);
1787         if (tmp_ctx == NULL) {
1788                 return WERR_NOMEM;
1789         }
1790
1791         ZERO_STRUCT(hive_hnd);
1792         ZERO_STRUCT(key_hnd);
1793
1794         result = winreg_printer_openkey(tmp_ctx,
1795                                         server_info,
1796                                         &winreg_pipe,
1797                                         TOP_LEVEL_CONTROL_FORMS_KEY,
1798                                         "",
1799                                         true,
1800                                         access_mask,
1801                                         &hive_hnd,
1802                                         &key_hnd);
1803         if (!W_ERROR_IS_OK(result)) {
1804                 DEBUG(0, ("winreg_printer_getform1: Could not open key %s: %s\n",
1805                           TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1806                 goto done;
1807         }
1808
1809         wvalue.name = form_name;
1810
1811         /*
1812          * call QueryValue once with data == NULL to get the
1813          * needed memory size to be allocated, then allocate
1814          * data buffer and call again.
1815          */
1816         status = rpccli_winreg_QueryValue(winreg_pipe,
1817                                           tmp_ctx,
1818                                           &key_hnd,
1819                                           &wvalue,
1820                                           &type_in,
1821                                           NULL,
1822                                           &data_in_size,
1823                                           &value_len,
1824                                           &result);
1825         if (!NT_STATUS_IS_OK(status)) {
1826                 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
1827                           wvalue.name, nt_errstr(status)));
1828                 if (!W_ERROR_IS_OK(result)) {
1829                         goto done;
1830                 }
1831                 result = ntstatus_to_werror(status);
1832                 goto done;
1833         }
1834
1835         data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
1836         if (data_in == NULL) {
1837                 result = WERR_NOMEM;
1838                 goto done;
1839         }
1840         value_len = 0;
1841
1842         status = rpccli_winreg_QueryValue(winreg_pipe,
1843                                           tmp_ctx,
1844                                           &key_hnd,
1845                                           &wvalue,
1846                                           &type_in,
1847                                           data_in,
1848                                           &data_in_size,
1849                                           &value_len,
1850                                           &result);
1851         if (!NT_STATUS_IS_OK(status)) {
1852                 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
1853                           wvalue.name, nt_errstr(status)));
1854                 if (!W_ERROR_IS_OK(result)) {
1855                         goto done;
1856                 }
1857                 result = ntstatus_to_werror(status);
1858                 goto done;
1859         }
1860
1861         r->form_name = talloc_strdup(mem_ctx, form_name);
1862         if (r->form_name == NULL) {
1863                 result = WERR_NOMEM;
1864                 goto done;
1865         }
1866
1867         r->size.width  = IVAL(data_in,  0);
1868         r->size.height = IVAL(data_in,  4);
1869         r->area.left   = IVAL(data_in,  8);
1870         r->area.top    = IVAL(data_in, 12);
1871         r->area.right  = IVAL(data_in, 16);
1872         r->area.bottom = IVAL(data_in, 20);
1873         /* skip index    IVAL(data_in, 24)));*/
1874         r->flags       = IVAL(data_in, 28);
1875
1876         result = WERR_OK;
1877 done:
1878         if (winreg_pipe != NULL) {
1879                 if (is_valid_policy_hnd(&key_hnd)) {
1880                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1881                 }
1882                 if (is_valid_policy_hnd(&hive_hnd)) {
1883                         rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1884                 }
1885         }
1886
1887         TALLOC_FREE(tmp_ctx);
1888         return result;
1889 }