Skip to content

Commit

Permalink
Add support for specifying permissions in cupsFileOpen.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Sep 4, 2024
1 parent 80f7ad1 commit a95ddb3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Changes in CUPS v2.5b1 (TBA)
- Added Docker support (Issue #929)
- Added a systemd slice to the systemd services included with the scheduler
- Added localizations for deprecated IPP attributes/options (Issue #1020)
- Added support for specifying permissions with the `cupsFileOpen` API.
- Updated CUPS to require TLS support - OpenSSL, GNUTLS and LibreSSL are
supported.
- Updated CUPS to require ZLIB.
Expand Down
26 changes: 19 additions & 7 deletions cups/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct _cups_file_s // CUPS file structure...

static ssize_t cups_compress(cups_file_t *fp, const char *buf, size_t bytes);
static ssize_t cups_fill(cups_file_t *fp);
static int cups_open(const char *filename, int mode);
static int cups_open(const char *filename, int oflag, int mode);
static ssize_t cups_read(cups_file_t *fp, char *buf, size_t bytes);
static ssize_t cups_write(cups_file_t *fp, const char *buf, size_t bytes);

Expand Down Expand Up @@ -909,6 +909,9 @@ cupsFileNumber(cups_file_t *fp) // I - CUPS file
// supplied which enables Flate compression of the file. Compression is
// not supported for the "a" (append) mode.
//
// When opening for writing ("w") or append ("a"), an optional 'm###' suffix
// can be used to set the permissions of the opened file.
//
// When opening a socket connection, the filename is a string of the form
// "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
// connection as needed, generally preferring IPv6 connections when there is
Expand All @@ -926,6 +929,8 @@ cupsFileOpen(const char *filename, // I - Name of file
char hostname[1024], // Hostname
*portname; // Port "name" (number or service)
http_addrlist_t *addrlist; // Host address list
int perm = 0664; // Permissions for write/append
const char *ptr; // Pointer into mode string


DEBUG_printf("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename, mode);
Expand All @@ -936,24 +941,30 @@ cupsFileOpen(const char *filename, // I - Name of file
(*mode == 'a' && isdigit(mode[1] & 255)))
return (NULL);

if ((ptr = strchr(mode, 'm')) != NULL && ptr[1] >= '0' && ptr[1] <= '7')
{
// Get permissions from mode string...
perm = (int)strtol(mode + 1, NULL, 8);
}

// Open the file...
switch (*mode)
{
case 'a' : // Append file
fd = cups_open(filename, O_WRONLY | O_CREAT | O_APPEND | O_LARGEFILE | O_BINARY);
fd = cups_open(filename, O_WRONLY | O_CREAT | O_APPEND | O_LARGEFILE | O_BINARY, perm);
break;

case 'r' : // Read file
fd = open(filename, O_RDONLY | O_LARGEFILE | O_BINARY, 0);
break;

case 'w' : // Write file
fd = cups_open(filename, O_WRONLY | O_LARGEFILE | O_BINARY);
fd = cups_open(filename, O_WRONLY | O_LARGEFILE | O_BINARY, perm);
if (fd < 0 && errno == ENOENT)
{
fd = cups_open(filename, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE | O_BINARY);
fd = cups_open(filename, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE | O_BINARY, perm);
if (fd < 0 && errno == EEXIST)
fd = cups_open(filename, O_WRONLY | O_LARGEFILE | O_BINARY);
fd = cups_open(filename, O_WRONLY | O_LARGEFILE | O_BINARY, perm);
}

if (fd >= 0)
Expand Down Expand Up @@ -2181,7 +2192,8 @@ cups_fill(cups_file_t *fp) // I - CUPS file

static int // O - File descriptor or -1 otherwise
cups_open(const char *filename, // I - Filename
int mode) // I - Open mode
int oflag, // I - Open flags
int mode) // I - Open permissions
{
int fd; // File descriptor
struct stat fileinfo; // File information
Expand All @@ -2191,7 +2203,7 @@ cups_open(const char *filename, // I - Filename


// Open the file...
if ((fd = open(filename, mode, 0666)) < 0)
if ((fd = open(filename, oflag, mode)) < 0)
return (-1);

// Then verify that the file descriptor doesn't point to a directory or hard-linked file.
Expand Down

0 comments on commit a95ddb3

Please sign in to comment.