Coverage Report

Created: 2025-12-31 06:20

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