Coverage Report

Created: 2025-11-24 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libcups/cups/globals.c
Line
Count
Source
1
//
2
// Global variable access routines for CUPS.
3
//
4
// Copyright © 2021-2025 by OpenPrinting.
5
// Copyright © 2007-2019 by Apple Inc.
6
// Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7
//
8
// Licensed under Apache License v2.0.  See the file "LICENSE" for more
9
// information.
10
//
11
12
#include "cups-private.h"
13
#ifndef _WIN32
14
#  include <pwd.h>
15
#endif // !_WIN32
16
17
18
//
19
// Local globals...
20
//
21
22
#ifdef DEBUG
23
static int    cups_global_index = 0;
24
          // Next thread number
25
#endif // DEBUG
26
static cups_thread_key_t cups_globals_key = CUPS_THREADKEY_INITIALIZER;
27
          // Thread local storage key
28
#ifndef _WIN32
29
static pthread_once_t cups_globals_key_once = PTHREAD_ONCE_INIT;
30
          // One-time initialization object
31
#endif // !_WIN32
32
static cups_mutex_t cups_global_mutex = CUPS_MUTEX_INITIALIZER;
33
          // Global critical section
34
35
36
//
37
// Local functions...
38
//
39
40
#ifdef _WIN32
41
static void   cups_fix_path(char *path);
42
#endif // _WIN32
43
static _cups_globals_t  *cups_globals_alloc(void);
44
static void   cups_globals_free(_cups_globals_t *g);
45
#ifndef _WIN32
46
static void   cups_globals_init(void);
47
#endif // !_WIN32
48
49
50
//
51
// '_cupsGlobalLock()' - Lock the global mutex.
52
//
53
54
void
55
_cupsGlobalLock(void)
56
0
{
57
0
  cupsMutexLock(&cups_global_mutex);
58
0
}
59
60
61
//
62
// '_cupsGlobals()' - Return a pointer to thread local storage
63
//
64
65
_cups_globals_t *     // O - Pointer to global data
66
_cupsGlobals(void)
67
691k
{
68
691k
  _cups_globals_t *cg;      // Pointer to global data
69
70
71
691k
#ifndef _WIN32
72
  // Initialize the global data exactly once...
73
691k
  pthread_once(&cups_globals_key_once, cups_globals_init);
74
691k
#endif // !_WIN32
75
76
  // See if we have allocated the data yet...
77
691k
  if ((cg = (_cups_globals_t *)cupsThreadGetData(cups_globals_key)) == NULL)
78
1
  {
79
    // No, allocate memory as set the pointer for the key...
80
1
    if ((cg = cups_globals_alloc()) != NULL)
81
1
      cupsThreadSetData(cups_globals_key, cg);
82
1
  }
83
84
  // Return the pointer to the data...
85
691k
  return (cg);
86
691k
}
87
88
89
//
90
// '_cupsGlobalUnlock()' - Unlock the global mutex.
91
//
92
93
void
94
_cupsGlobalUnlock(void)
95
0
{
96
0
  cupsMutexUnlock(&cups_global_mutex);
97
0
}
98
99
100
#ifdef _WIN32
101
//
102
// 'DllMain()' - Main entry for library.
103
//
104
// @private@
105
//
106
107
BOOL WINAPI       // O - Success/failure
108
DllMain(HINSTANCE hinst,    // I - DLL module handle
109
        DWORD     reason,   // I - Reason
110
        LPVOID    reserved)   // I - Unused
111
{
112
  _cups_globals_t *cg;      // Global data
113
114
115
  (void)hinst;
116
  (void)reserved;
117
118
  switch (reason)
119
  {
120
    case DLL_PROCESS_ATTACH :   // Called on library initialization
121
        InitializeCriticalSection(&cups_global_mutex);
122
123
        if ((cups_globals_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
124
          return (FALSE);
125
        break;
126
127
    case DLL_THREAD_DETACH :    // Called when a thread terminates
128
        if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL)
129
          cups_globals_free(cg);
130
        break;
131
132
    case DLL_PROCESS_DETACH :   // Called when library is unloaded
133
        if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL)
134
          cups_globals_free(cg);
135
136
        TlsFree(cups_globals_key);
137
        DeleteCriticalSection(&cups_global_mutex);
138
        break;
139
140
    default:
141
        break;
142
  }
143
144
  return (TRUE);
145
}
146
#endif // _WIN32
147
148
149
//
150
// 'cups_globals_alloc()' - Allocate and initialize global data.
151
//
152
153
static _cups_globals_t *    // O - Pointer to global data
154
cups_globals_alloc(void)
155
1
{
156
1
  const char  *cups_userconfig = getenv("CUPS_USERCONFIG");
157
          // Location of user config files
158
1
  _cups_globals_t *cg = calloc(1, sizeof(_cups_globals_t));
159
          // Pointer to global data
160
#ifdef _WIN32
161
  HKEY    key;      // Registry key
162
  DWORD   size;     // Size of string
163
  const char  *userprofile = getenv("USERPROFILE");
164
          // User profile (home) directory
165
  char    userconfig[1024], // User configuration directory
166
    *userptr;   // Pointer into user config
167
  static char installdir[1024] = "",  // Install directory
168
    sysconfig[1024] = ""; // Server configuration directory
169
#endif // _WIN32
170
171
172
1
  if (!cg)
173
0
    return (NULL);
174
175
  // Clear the global storage and set the default encryption and password callback values...
176
1
  memset(cg, 0, sizeof(_cups_globals_t));
177
1
  cg->encryption     = (http_encryption_t)-1;
178
1
  cg->password_cb    = (cups_password_cb_t)_cupsGetPassword;
179
1
  cg->trust_first    = -1;
180
1
  cg->any_root       = -1;
181
1
  cg->expired_certs  = -1;
182
1
  cg->validate_certs = -1;
183
184
#ifdef DEBUG
185
  // Friendly thread ID for debugging...
186
  cg->thread_id = ++ cups_global_index;
187
#endif // DEBUG
188
189
  // Then set directories as appropriate...
190
#ifdef _WIN32
191
  if (!installdir[0])
192
  {
193
    // Open the registry...
194
    cupsCopyString(installdir, "C:/Program Files/cups.org", sizeof(installdir));
195
196
    if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ, &key))
197
    {
198
      // Grab the installation directory...
199
      char  *ptr;     // Pointer into installdir
200
201
      size = sizeof(installdir);
202
      RegQueryValueExA(key, "installdir", NULL, NULL, installdir, &size);
203
      RegCloseKey(key);
204
205
      for (ptr = installdir; *ptr;)
206
      {
207
        if (*ptr == '\\')
208
        {
209
          if (ptr[1])
210
            *ptr++ = '/';
211
          else
212
            *ptr = '\0';    // Strip trailing "\"
213
        }
214
        else if (*ptr == '/' && !ptr[1])
215
        {
216
          *ptr = '\0';      // Strip trailing "/"
217
        }
218
        else
219
        {
220
          ptr ++;
221
  }
222
      }
223
    }
224
225
    snprintf(sysconfig, sizeof(sysconfig), "%s/conf", installdir);
226
  }
227
228
  if ((cg->datadir = getenv("CUPS_DATADIR")) == NULL)
229
    cg->datadir = installdir;
230
231
  if ((cg->sysconfig = getenv("CUPS_SERVERROOT")) == NULL)
232
    cg->sysconfig = sysconfig;
233
234
  if (cups_userconfig)
235
    cupsCopyString(userconfig, cups_userconfig, sizeof(userconfig));
236
  else if (userprofile)
237
    snprintf(userconfig, sizeof(userconfig), "%s/AppData/Local/cups", userprofile);
238
  else
239
    cupsCopyString(userconfig, "C:/cups", sizeof(userconfig));
240
241
  for (userptr = userconfig; *userptr; userptr ++)
242
  {
243
    // Convert back slashes to forward slashes
244
    if (*userptr == '\\')
245
      *userptr = '/';
246
  }
247
248
  cg->userconfig = strdup(userconfig);
249
250
#else
251
1
  const char  *home = getenv("HOME"); // HOME environment variable
252
1
  char    homedir[1024],    // Home directory from account
253
1
    temp[1024];   // Temporary directory string
254
1
#  ifndef __APPLE__
255
1
  const char  *snap_common = getenv("SNAP_COMMON"),
256
1
    *xdg_config_home = getenv("XDG_CONFIG_HOME");
257
          // Environment variables
258
1
#  endif // !__APPLE__
259
260
#  ifdef HAVE_GETEUID
261
  if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
262
#  else
263
1
  if (!getuid())
264
1
#  endif // HAVE_GETEUID
265
1
  {
266
    // When running setuid/setgid, don't allow environment variables to override
267
    // the system directories...
268
1
    cg->datadir   = CUPS_DATADIR;
269
1
    cg->sysconfig = CUPS_SERVERROOT;
270
271
1
    cups_userconfig = NULL;
272
1
  }
273
0
  else
274
0
  {
275
    // Allow directories to be overridden by environment variables.
276
0
    if ((cg->datadir = getenv("CUPS_DATADIR")) == NULL)
277
0
      cg->datadir = CUPS_DATADIR;
278
279
0
    if ((cg->sysconfig = getenv("CUPS_SYSCONFIG")) == NULL)
280
0
    {
281
0
      if ((cg->sysconfig = getenv("CUPS_SERVERROOT")) == NULL)
282
0
  cg->sysconfig = CUPS_SERVERROOT;
283
0
    }
284
0
  }
285
286
1
  if (!getuid())
287
1
  {
288
    // When running as root, make "userconfig" the same as "sysconfig"...
289
1
    cg->userconfig = strdup(cg->sysconfig);
290
1
    return (cg);
291
1
  }
292
0
  else if (cups_userconfig)
293
0
  {
294
    // Use the value of the CUPS_USERCONFIG environment variable...
295
0
    cg->userconfig = strdup(cups_userconfig);
296
0
    return (cg);
297
0
  }
298
299
  // Find the user configuration directory relative to the home directory...
300
#  ifdef __APPLE__
301
  if (!home)
302
#else
303
0
  if (!home && !xdg_config_home)
304
0
#  endif // __APPLE__
305
0
  {
306
0
    struct passwd pw;   // User info
307
0
    struct passwd *result;  // Auxiliary pointer
308
309
0
    getpwuid_r(getuid(), &pw, cg->pw_buf, PW_BUF_SIZE, &result);
310
0
    if (result)
311
0
    {
312
0
      cupsCopyString(homedir, pw.pw_dir, sizeof(homedir));
313
0
      home = homedir;
314
0
    }
315
0
  }
316
317
#  ifdef __APPLE__
318
  if (home)
319
  {
320
    // macOS uses ~/Library/Application Support/FOO
321
    snprintf(temp, sizeof(temp), "%s/Library/Application Support/cups", home);
322
  }
323
  else
324
  {
325
    // Something went wrong, use temporary directory...
326
    snprintf(temp, sizeof(temp), "/private/tmp/cups%u", (unsigned)getuid());
327
  }
328
329
#  else
330
0
  if (snap_common)
331
0
  {
332
    // Snaps use $SNAP_COMMON/FOO
333
0
    snprintf(temp, sizeof(temp), "%s/cups", snap_common);
334
0
  }
335
0
  else if (xdg_config_home)
336
0
  {
337
    // XDG uses $XDG_CONFIG_HOME/FOO
338
0
    snprintf(temp, sizeof(temp), "%s/cups", xdg_config_home);
339
0
  }
340
0
  else if (home)
341
0
  {
342
    // Use ~/.cups if it exists, otherwise ~/.config/cups (XDG standard)
343
0
    snprintf(temp, sizeof(temp), "%s/.cups", home);
344
0
    if (access(temp, 0))
345
0
      snprintf(temp, sizeof(temp), "%s/.config/cups", home);
346
0
  }
347
0
  else
348
0
  {
349
    // Something went wrong, use temporary directory...
350
0
    snprintf(temp, sizeof(temp), "/tmp/cups%u", (unsigned)getuid());
351
0
  }
352
0
#  endif // __APPLE__
353
354
  // Can't use _cupsStrAlloc since it causes a loop with debug logging enabled
355
0
  cg->userconfig = strdup(temp);
356
0
#endif // _WIN32
357
358
0
  return (cg);
359
1
}
360
361
362
//
363
// 'cups_globals_free()' - Free global data.
364
//
365
366
static void
367
cups_globals_free(_cups_globals_t *cg)  // I - Pointer to global data
368
0
{
369
0
  _cups_buffer_t  *buffer,  // Current read/write buffer
370
0
      *next;    // Next buffer
371
372
373
0
  if (cg->last_status_message)
374
0
    _cupsStrFree(cg->last_status_message);
375
376
0
  for (buffer = cg->cups_buffers; buffer; buffer = next)
377
0
  {
378
0
    next = buffer->next;
379
0
    free(buffer);
380
0
  }
381
382
0
  cupsArrayDelete(cg->leg_size_lut);
383
0
  cupsArrayDelete(cg->ppd_size_lut);
384
0
  cupsArrayDelete(cg->pwg_size_lut);
385
386
0
  httpClose(cg->http);
387
388
0
  _httpFreeCredentials(cg->credentials);
389
390
0
  cupsFileClose(cg->stdio_files[0]);
391
0
  cupsFileClose(cg->stdio_files[1]);
392
0
  cupsFileClose(cg->stdio_files[2]);
393
394
0
  cupsArrayDelete(cg->browse_domains);
395
0
  cupsArrayDelete(cg->filter_location_array);
396
0
  if (cg->filter_location_regex)
397
0
  {
398
0
    regfree(cg->filter_location_regex);
399
0
    free(cg->filter_location_regex);
400
0
  }
401
402
0
  free(cg->userconfig);
403
0
  free(cg->raster_error.start);
404
0
  free(cg);
405
0
}
406
407
408
#ifndef _WIN32
409
//
410
// 'cups_globals_init()' - Initialize environment variables.
411
//
412
413
static void
414
cups_globals_init(void)
415
1
{
416
  // Register the global data for this thread...
417
1
  pthread_key_create(&cups_globals_key, (void (*)(void *))cups_globals_free);
418
1
}
419
#endif // !_WIN32