s3:smbd/open: use Builtin_Administrators as owner of files (if possible)
[kai/samba.git] / source3 / printing / nt_printing_os2.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-2000,
5  *  Copyright (C) Jean François Micouleau      1998-2000.
6  *  Copyright (C) Gerald Carter                2002-2005.
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 "printing/nt_printing_os2.h"
24
25 /****************************************************************************
26  ***************************************************************************/
27
28 static char *win_driver;
29 static char *os2_driver;
30
31 static const char *get_win_driver(void)
32 {
33         if (win_driver == NULL) {
34                 return "";
35         }
36         return win_driver;
37 }
38
39 static const char *get_os2_driver(void)
40 {
41         if (os2_driver == NULL) {
42                 return "";
43         }
44         return os2_driver;
45 }
46
47 static bool set_driver_mapping(const char *from, const char *to)
48 {
49         SAFE_FREE(win_driver);
50         SAFE_FREE(os2_driver);
51
52         win_driver = SMB_STRDUP(from);
53         os2_driver = SMB_STRDUP(to);
54
55         if (win_driver == NULL || os2_driver == NULL) {
56                 SAFE_FREE(win_driver);
57                 SAFE_FREE(os2_driver);
58                 return false;
59         }
60         return true;
61 }
62
63 /**
64  * @internal
65  *
66  * @brief Map a Windows driver to a OS/2 driver.
67  *
68  * @param[in]  mem_ctx  The memory context to use.
69  *
70  * @param[in,out] pdrivername The drivername of Windows to remap.
71  *
72  * @return              WERR_OK on success, a corresponding WERROR on failure.
73  */
74 WERROR spoolss_map_to_os2_driver(TALLOC_CTX *mem_ctx, const char **pdrivername)
75 {
76         const char *mapfile = lp_os2_driver_map(talloc_tos());
77         char **lines = NULL;
78         const char *drivername;
79         int numlines = 0;
80         int i;
81
82         if (pdrivername == NULL || *pdrivername == NULL || *pdrivername[0] == '\0') {
83                 return WERR_INVALID_PARAMETER;
84         }
85
86         drivername = *pdrivername;
87
88         if (mapfile[0] == '\0') {
89                 return WERR_BADFILE;
90         }
91
92         if (strequal(drivername, get_win_driver())) {
93                 DEBUG(3,("Mapped Windows driver %s to OS/2 driver %s\n",
94                         drivername, get_os2_driver()));
95                 drivername = talloc_strdup(mem_ctx, get_os2_driver());
96                 if (drivername == NULL) {
97                         return WERR_NOMEM;
98                 }
99                 *pdrivername = drivername;
100                 return WERR_OK;
101         }
102
103         lines = file_lines_load(mapfile, &numlines, 0, NULL);
104         if (numlines == 0 || lines == NULL) {
105                 DEBUG(0,("No entries in OS/2 driver map %s\n", mapfile));
106                 TALLOC_FREE(lines);
107                 return WERR_EMPTY;
108         }
109
110         DEBUG(4,("Scanning OS/2 driver map %s\n",mapfile));
111
112         for( i = 0; i < numlines; i++) {
113                 char *nt_name = lines[i];
114                 char *os2_name = strchr(nt_name, '=');
115
116                 if (os2_name == NULL) {
117                         continue;
118                 }
119
120                 *os2_name++ = '\0';
121
122                 while (isspace(*nt_name)) {
123                         nt_name++;
124                 }
125
126                 if (*nt_name == '\0' || strchr("#;", *nt_name)) {
127                         continue;
128                 }
129
130                 {
131                         int l = strlen(nt_name);
132                         while (l && isspace(nt_name[l - 1])) {
133                                 nt_name[l - 1] = 0;
134                                 l--;
135                         }
136                 }
137
138                 while (isspace(*os2_name)) {
139                         os2_name++;
140                 }
141
142                 {
143                         int l = strlen(os2_name);
144                         while (l && isspace(os2_name[l-1])) {
145                                 os2_name[l-1] = 0;
146                                 l--;
147                         }
148                 }
149
150                 if (strequal(nt_name, drivername)) {
151                         DEBUG(3,("Mapped Windows driver %s to OS/2 driver %s\n",drivername,os2_name));
152                         set_driver_mapping(drivername, os2_name);
153                         drivername = talloc_strdup(mem_ctx, os2_name);
154                         TALLOC_FREE(lines);
155                         if (drivername == NULL) {
156                                 return WERR_NOMEM;
157                         }
158                         *pdrivername = drivername;
159                         return WERR_OK;
160                 }
161         }
162
163         TALLOC_FREE(lines);
164         return WERR_OK;
165 }