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