/src/samba/source3/printing/rap_jobid.c
Line | Count | Source |
1 | | /* |
2 | | * Maintain rap vs spoolss jobids |
3 | | * |
4 | | * This program is free software; you can redistribute it and/or modify |
5 | | * it under the terms of the GNU General Public License as published by |
6 | | * the Free Software Foundation; either version 3 of the License, or |
7 | | * (at your option) any later version. |
8 | | * |
9 | | * This program is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU General Public License |
15 | | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | /* |
19 | | the printing backend revolves around a tdb database that stores the |
20 | | SMB view of the print queue |
21 | | |
22 | | The key for this database is a jobid - a internally generated number that |
23 | | uniquely identifies a print job |
24 | | |
25 | | reading the print queue involves two steps: |
26 | | - possibly running lpq and updating the internal database from that |
27 | | - reading entries from the database |
28 | | |
29 | | jobids are assigned when a job starts spooling. |
30 | | */ |
31 | | |
32 | | #include "rap_jobid.h" |
33 | | #include <tdb.h> |
34 | | #include "source3/include/util_tdb.h" |
35 | | #include "lib/util/string_wrappers.h" |
36 | | |
37 | | static TDB_CONTEXT *rap_tdb; |
38 | | static uint16_t next_rap_jobid; |
39 | | struct rap_jobid_key { |
40 | | fstring sharename; |
41 | | uint32_t jobid; |
42 | | }; |
43 | | |
44 | | /*************************************************************************** |
45 | | Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32 |
46 | | bit RPC jobids.... JRA. |
47 | | ***************************************************************************/ |
48 | | |
49 | | uint16_t pjobid_to_rap(const char* sharename, uint32_t jobid) |
50 | 0 | { |
51 | 0 | uint16_t rap_jobid; |
52 | 0 | TDB_DATA data, key; |
53 | 0 | struct rap_jobid_key jinfo; |
54 | 0 | uint8_t buf[2]; |
55 | |
|
56 | 0 | DEBUG(10,("pjobid_to_rap: called.\n")); |
57 | |
|
58 | 0 | if (!rap_tdb) { |
59 | | /* Create the in-memory tdb. */ |
60 | 0 | rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644); |
61 | 0 | if (!rap_tdb) |
62 | 0 | return 0; |
63 | 0 | } |
64 | | |
65 | 0 | ZERO_STRUCT( jinfo ); |
66 | 0 | fstrcpy( jinfo.sharename, sharename ); |
67 | 0 | jinfo.jobid = jobid; |
68 | 0 | key.dptr = (uint8_t *)&jinfo; |
69 | 0 | key.dsize = sizeof(jinfo); |
70 | |
|
71 | 0 | data = tdb_fetch(rap_tdb, key); |
72 | 0 | if (data.dptr && data.dsize == sizeof(uint16_t)) { |
73 | 0 | rap_jobid = SVAL(data.dptr, 0); |
74 | 0 | SAFE_FREE(data.dptr); |
75 | 0 | DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n", |
76 | 0 | (unsigned int)jobid, (unsigned int)rap_jobid)); |
77 | 0 | return rap_jobid; |
78 | 0 | } |
79 | 0 | SAFE_FREE(data.dptr); |
80 | | /* Not found - create and store mapping. */ |
81 | 0 | rap_jobid = ++next_rap_jobid; |
82 | 0 | if (rap_jobid == 0) |
83 | 0 | rap_jobid = ++next_rap_jobid; |
84 | 0 | SSVAL(buf,0,rap_jobid); |
85 | 0 | data.dptr = buf; |
86 | 0 | data.dsize = sizeof(rap_jobid); |
87 | 0 | tdb_store(rap_tdb, key, data, TDB_REPLACE); |
88 | 0 | tdb_store(rap_tdb, data, key, TDB_REPLACE); |
89 | |
|
90 | 0 | DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n", |
91 | 0 | (unsigned int)jobid, (unsigned int)rap_jobid)); |
92 | 0 | return rap_jobid; |
93 | 0 | } |
94 | | |
95 | | bool rap_to_pjobid(uint16_t rap_jobid, fstring sharename, uint32_t *pjobid) |
96 | 0 | { |
97 | 0 | TDB_DATA data, key; |
98 | 0 | uint8_t buf[2]; |
99 | |
|
100 | 0 | DEBUG(10,("rap_to_pjobid called.\n")); |
101 | |
|
102 | 0 | if (!rap_tdb) |
103 | 0 | return False; |
104 | | |
105 | 0 | SSVAL(buf,0,rap_jobid); |
106 | 0 | key.dptr = buf; |
107 | 0 | key.dsize = sizeof(rap_jobid); |
108 | 0 | data = tdb_fetch(rap_tdb, key); |
109 | 0 | if ( data.dptr && data.dsize == sizeof(struct rap_jobid_key) ) |
110 | 0 | { |
111 | 0 | struct rap_jobid_key *jinfo = (struct rap_jobid_key*)data.dptr; |
112 | 0 | if (sharename != NULL) { |
113 | 0 | fstrcpy( sharename, jinfo->sharename ); |
114 | 0 | } |
115 | 0 | *pjobid = jinfo->jobid; |
116 | 0 | DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n", |
117 | 0 | (unsigned int)*pjobid, (unsigned int)rap_jobid)); |
118 | 0 | SAFE_FREE(data.dptr); |
119 | 0 | return True; |
120 | 0 | } |
121 | | |
122 | 0 | DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n", |
123 | 0 | (unsigned int)rap_jobid)); |
124 | 0 | SAFE_FREE(data.dptr); |
125 | 0 | return False; |
126 | 0 | } |
127 | | |
128 | | void rap_jobid_delete(const char* sharename, uint32_t jobid) |
129 | 0 | { |
130 | 0 | TDB_DATA key, data; |
131 | 0 | uint16_t rap_jobid; |
132 | 0 | struct rap_jobid_key jinfo; |
133 | 0 | uint8_t buf[2]; |
134 | |
|
135 | 0 | DEBUG(10,("rap_jobid_delete: called.\n")); |
136 | |
|
137 | 0 | if (!rap_tdb) |
138 | 0 | return; |
139 | | |
140 | 0 | ZERO_STRUCT( jinfo ); |
141 | 0 | fstrcpy( jinfo.sharename, sharename ); |
142 | 0 | jinfo.jobid = jobid; |
143 | 0 | key.dptr = (uint8_t *)&jinfo; |
144 | 0 | key.dsize = sizeof(jinfo); |
145 | |
|
146 | 0 | data = tdb_fetch(rap_tdb, key); |
147 | 0 | if (!data.dptr || (data.dsize != sizeof(uint16_t))) { |
148 | 0 | DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n", |
149 | 0 | (unsigned int)jobid )); |
150 | 0 | SAFE_FREE(data.dptr); |
151 | 0 | return; |
152 | 0 | } |
153 | | |
154 | 0 | DEBUG(10,("rap_jobid_delete: deleting jobid %u\n", |
155 | 0 | (unsigned int)jobid )); |
156 | |
|
157 | 0 | rap_jobid = SVAL(data.dptr, 0); |
158 | 0 | SAFE_FREE(data.dptr); |
159 | 0 | SSVAL(buf,0,rap_jobid); |
160 | 0 | data.dptr = buf; |
161 | 0 | data.dsize = sizeof(rap_jobid); |
162 | 0 | tdb_delete(rap_tdb, key); |
163 | 0 | tdb_delete(rap_tdb, data); |
164 | 0 | } |