| 1 | /*
|
|---|
| 2 | Unix SMB/Netbios implementation.
|
|---|
| 3 | Version 3.0
|
|---|
| 4 | printing backend routines
|
|---|
| 5 | Copyright (C) Andrew Tridgell 1992-2000
|
|---|
| 6 | Copyright (C) Jeremy Allison 2002
|
|---|
| 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 "system/filesys.h"
|
|---|
| 24 | #include "printing.h"
|
|---|
| 25 | #include "util_tdb.h"
|
|---|
| 26 |
|
|---|
| 27 | static struct tdb_print_db *print_db_head;
|
|---|
| 28 |
|
|---|
| 29 | /****************************************************************************
|
|---|
| 30 | Function to find or create the printer specific job tdb given a printername.
|
|---|
| 31 | Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
|
|---|
| 32 | ****************************************************************************/
|
|---|
| 33 |
|
|---|
| 34 | struct tdb_print_db *get_print_db_byname(const char *printername)
|
|---|
| 35 | {
|
|---|
| 36 | struct tdb_print_db *p = NULL, *last_entry = NULL;
|
|---|
| 37 | int num_open = 0;
|
|---|
| 38 | char *printdb_path = NULL;
|
|---|
| 39 | bool done_become_root = False;
|
|---|
| 40 |
|
|---|
| 41 | SMB_ASSERT(printername != NULL);
|
|---|
| 42 |
|
|---|
| 43 | for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
|
|---|
| 44 | /* Ensure the list terminates... JRA. */
|
|---|
| 45 | SMB_ASSERT(p->next != print_db_head);
|
|---|
| 46 |
|
|---|
| 47 | if (p->tdb && strequal(p->printer_name, printername)) {
|
|---|
| 48 | DLIST_PROMOTE(print_db_head, p);
|
|---|
| 49 | p->ref_count++;
|
|---|
| 50 | return p;
|
|---|
| 51 | }
|
|---|
| 52 | num_open++;
|
|---|
| 53 | last_entry = p;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /* Not found. */
|
|---|
| 57 | if (num_open >= MAX_PRINT_DBS_OPEN) {
|
|---|
| 58 | /* Try and recycle the last entry. */
|
|---|
| 59 | if (print_db_head && last_entry) {
|
|---|
| 60 | DLIST_PROMOTE(print_db_head, last_entry);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | for (p = print_db_head; p; p = p->next) {
|
|---|
| 64 | if (p->ref_count)
|
|---|
| 65 | continue;
|
|---|
| 66 | if (p->tdb) {
|
|---|
| 67 | if (tdb_close(print_db_head->tdb)) {
|
|---|
| 68 | DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
|
|---|
| 69 | print_db_head->printer_name ));
|
|---|
| 70 | return NULL;
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | p->tdb = NULL;
|
|---|
| 74 | p->ref_count = 0;
|
|---|
| 75 | memset(p->printer_name, '\0', sizeof(p->printer_name));
|
|---|
| 76 | break;
|
|---|
| 77 | }
|
|---|
| 78 | if (p && print_db_head) {
|
|---|
| 79 | DLIST_PROMOTE(print_db_head, p);
|
|---|
| 80 | p = print_db_head;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if (!p) {
|
|---|
| 85 | /* Create one. */
|
|---|
| 86 | p = SMB_MALLOC_P(struct tdb_print_db);
|
|---|
| 87 | if (!p) {
|
|---|
| 88 | DEBUG(0,("get_print_db: malloc fail !\n"));
|
|---|
| 89 | return NULL;
|
|---|
| 90 | }
|
|---|
| 91 | ZERO_STRUCTP(p);
|
|---|
| 92 | DLIST_ADD(print_db_head, p);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | if (asprintf(&printdb_path, "%s%s.tdb",
|
|---|
| 96 | cache_path("printing/"),
|
|---|
| 97 | printername) < 0) {
|
|---|
| 98 | DLIST_REMOVE(print_db_head, p);
|
|---|
| 99 | SAFE_FREE(p);
|
|---|
| 100 | return NULL;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | if (geteuid() != sec_initial_uid()) {
|
|---|
| 104 | become_root();
|
|---|
| 105 | done_become_root = True;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | p->tdb = tdb_open_log(printdb_path, 5000, TDB_DEFAULT, O_RDWR|O_CREAT,
|
|---|
| 109 | 0600);
|
|---|
| 110 |
|
|---|
| 111 | if (done_become_root)
|
|---|
| 112 | unbecome_root();
|
|---|
| 113 |
|
|---|
| 114 | if (!p->tdb) {
|
|---|
| 115 | DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
|
|---|
| 116 | printdb_path ));
|
|---|
| 117 | DLIST_REMOVE(print_db_head, p);
|
|---|
| 118 | SAFE_FREE(printdb_path);
|
|---|
| 119 | SAFE_FREE(p);
|
|---|
| 120 | return NULL;
|
|---|
| 121 | }
|
|---|
| 122 | SAFE_FREE(printdb_path);
|
|---|
| 123 | fstrcpy(p->printer_name, printername);
|
|---|
| 124 | p->ref_count++;
|
|---|
| 125 | return p;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /***************************************************************************
|
|---|
| 129 | Remove a reference count.
|
|---|
| 130 | ****************************************************************************/
|
|---|
| 131 |
|
|---|
| 132 | void release_print_db( struct tdb_print_db *pdb)
|
|---|
| 133 | {
|
|---|
| 134 | pdb->ref_count--;
|
|---|
| 135 | SMB_ASSERT(pdb->ref_count >= 0);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /***************************************************************************
|
|---|
| 139 | Close all open print db entries.
|
|---|
| 140 | ****************************************************************************/
|
|---|
| 141 |
|
|---|
| 142 | void close_all_print_db(void)
|
|---|
| 143 | {
|
|---|
| 144 | struct tdb_print_db *p = NULL, *next_p = NULL;
|
|---|
| 145 |
|
|---|
| 146 | for (p = print_db_head; p; p = next_p) {
|
|---|
| 147 | next_p = p->next;
|
|---|
| 148 |
|
|---|
| 149 | if (p->tdb)
|
|---|
| 150 | tdb_close(p->tdb);
|
|---|
| 151 | DLIST_REMOVE(print_db_head, p);
|
|---|
| 152 | ZERO_STRUCTP(p);
|
|---|
| 153 | SAFE_FREE(p);
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 | /****************************************************************************
|
|---|
| 159 | Fetch and clean the pid_t record list for all pids interested in notify
|
|---|
| 160 | messages. data needs freeing on exit.
|
|---|
| 161 | ****************************************************************************/
|
|---|
| 162 |
|
|---|
| 163 | struct TDB_DATA get_printer_notify_pid_list(struct tdb_context *tdb, const char *printer_name, bool cleanlist)
|
|---|
| 164 | {
|
|---|
| 165 | TDB_DATA data;
|
|---|
| 166 | size_t i;
|
|---|
| 167 |
|
|---|
| 168 | ZERO_STRUCT(data);
|
|---|
| 169 |
|
|---|
| 170 | data = tdb_fetch_bystring( tdb, NOTIFY_PID_LIST_KEY );
|
|---|
| 171 |
|
|---|
| 172 | if (!data.dptr) {
|
|---|
| 173 | ZERO_STRUCT(data);
|
|---|
| 174 | return data;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | if (data.dsize % 8) {
|
|---|
| 178 | DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n", printer_name ));
|
|---|
| 179 | tdb_delete_bystring(tdb, NOTIFY_PID_LIST_KEY );
|
|---|
| 180 | SAFE_FREE(data.dptr);
|
|---|
| 181 | ZERO_STRUCT(data);
|
|---|
| 182 | return data;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | if (!cleanlist)
|
|---|
| 186 | return data;
|
|---|
| 187 |
|
|---|
| 188 | /*
|
|---|
| 189 | * Weed out all dead entries.
|
|---|
| 190 | */
|
|---|
| 191 |
|
|---|
| 192 | for( i = 0; i < data.dsize; i += 8) {
|
|---|
| 193 | pid_t pid = (pid_t)IVAL(data.dptr, i);
|
|---|
| 194 |
|
|---|
| 195 | if (pid == sys_getpid())
|
|---|
| 196 | continue;
|
|---|
| 197 |
|
|---|
| 198 | /* Entry is dead if process doesn't exist or refcount is zero. */
|
|---|
| 199 |
|
|---|
| 200 | while ((i < data.dsize) && ((IVAL(data.dptr, i + 4) == 0) || !process_exists_by_pid(pid))) {
|
|---|
| 201 |
|
|---|
| 202 | /* Refcount == zero is a logic error and should never happen. */
|
|---|
| 203 | if (IVAL(data.dptr, i + 4) == 0) {
|
|---|
| 204 | DEBUG(0,("get_printer_notify_pid_list: Refcount == 0 for pid = %u printer %s !\n",
|
|---|
| 205 | (unsigned int)pid, printer_name ));
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | if (data.dsize - i > 8)
|
|---|
| 209 | memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
|
|---|
| 210 | data.dsize -= 8;
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | return data;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|