]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cert.c
4 * Authentication certificate routines for the CUPS scheduler.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * Include necessary headers...
23 # ifdef HAVE_MEMBERSHIP_H
24 # include <membership.h>
25 # endif /* HAVE_MEMBERSHIP_H */
26 #endif /* HAVE_ACL_INIT */
33 static int ctcompare(const char *a
, const char *b
);
37 * 'cupsdAddCert()' - Add a certificate.
41 cupsdAddCert(int pid
, /* I - Process ID */
42 const char *username
, /* I - Username */
43 int type
) /* I - AuthType for username */
45 int i
; /* Looping var */
46 cupsd_cert_t
*cert
; /* Current certificate */
47 int fd
; /* Certificate file */
48 char filename
[1024]; /* Certificate filename */
49 static const char hex
[] = "0123456789ABCDEF";
50 /* Hex constants... */
53 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAddCert: Adding certificate for PID %d", pid
);
56 * Allocate memory for the certificate...
59 if ((cert
= calloc(sizeof(cupsd_cert_t
), 1)) == NULL
)
63 * Fill in the certificate information...
68 strlcpy(cert
->username
, username
, sizeof(cert
->username
));
70 for (i
= 0; i
< 32; i
++)
71 cert
->certificate
[i
] = hex
[CUPS_RAND() & 15];
74 * Save the certificate to a file readable only by the User and Group
75 * (or root and SystemGroup for PID == 0)...
78 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, pid
);
81 if ((fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0400)) < 0)
83 cupsdLogMessage(CUPSD_LOG_ERROR
,
84 "Unable to create certificate file %s - %s",
85 filename
, strerror(errno
));
93 acl_t acl
; /* ACL information */
94 acl_entry_t entry
; /* ACL entry */
95 acl_permset_t permset
; /* Permissions */
96 # ifdef HAVE_MBR_UID_TO_UUID
97 uuid_t group
; /* Group ID */
98 # endif /* HAVE_MBR_UID_TO_UUID */
99 static int acls_not_supported
= 0;
101 #endif /* HAVE_ACL_INIT */
105 * Root certificate...
109 fchown(fd
, RunUser
, SystemGroupIDs
[0]);
111 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddCert: NumSystemGroups=%d", NumSystemGroups
);
114 if (NumSystemGroups
> 1)
117 * Set POSIX ACLs for the root certificate so that all system
118 * groups can access it...
121 int j
; /* Looping var */
123 # ifdef HAVE_MBR_UID_TO_UUID
125 * On MacOS X, ACLs use UUIDs instead of GIDs...
128 acl
= acl_init(NumSystemGroups
- 1);
130 for (i
= 1; i
< NumSystemGroups
; i
++)
133 * Add each group ID to the ACL...
136 for (j
= 0; j
< i
; j
++)
137 if (SystemGroupIDs
[j
] == SystemGroupIDs
[i
])
141 continue; /* Skip duplicate groups */
143 acl_create_entry(&acl
, &entry
);
144 acl_get_permset(entry
, &permset
);
145 acl_add_perm(permset
, ACL_READ_DATA
);
146 acl_set_tag_type(entry
, ACL_EXTENDED_ALLOW
);
147 mbr_gid_to_uuid((gid_t
)SystemGroupIDs
[i
], group
);
148 acl_set_qualifier(entry
, &group
);
149 acl_set_permset(entry
, permset
);
154 * POSIX ACLs need permissions for owner, group, other, and mask
155 * in addition to the rest of the system groups...
158 acl
= acl_init(NumSystemGroups
+ 3);
161 acl_create_entry(&acl
, &entry
);
162 acl_get_permset(entry
, &permset
);
163 acl_add_perm(permset
, ACL_READ
);
164 acl_set_tag_type(entry
, ACL_USER_OBJ
);
165 acl_set_permset(entry
, permset
);
168 acl_create_entry(&acl
, &entry
);
169 acl_get_permset(entry
, &permset
);
170 acl_add_perm(permset
, ACL_READ
);
171 acl_set_tag_type(entry
, ACL_GROUP_OBJ
);
172 acl_set_permset(entry
, permset
);
175 acl_create_entry(&acl
, &entry
);
176 acl_get_permset(entry
, &permset
);
177 acl_add_perm(permset
, 0);
178 acl_set_tag_type(entry
, ACL_OTHER
);
179 acl_set_permset(entry
, permset
);
182 acl_create_entry(&acl
, &entry
);
183 acl_get_permset(entry
, &permset
);
184 acl_add_perm(permset
, ACL_READ
);
185 acl_set_tag_type(entry
, ACL_MASK
);
186 acl_set_permset(entry
, permset
);
188 for (i
= 1; i
< NumSystemGroups
; i
++)
191 * Add each group ID to the ACL...
194 for (j
= 0; j
< i
; j
++)
195 if (SystemGroupIDs
[j
] == SystemGroupIDs
[i
])
199 continue; /* Skip duplicate groups */
201 acl_create_entry(&acl
, &entry
);
202 acl_get_permset(entry
, &permset
);
203 acl_add_perm(permset
, ACL_READ
);
204 acl_set_tag_type(entry
, ACL_GROUP
);
205 acl_set_qualifier(entry
, SystemGroupIDs
+ i
);
206 acl_set_permset(entry
, permset
);
211 char *text
, *textptr
; /* Temporary string */
213 cupsdLogMessage(CUPSD_LOG_ERROR
, "ACL did not validate: %s",
215 text
= acl_to_text(acl
, NULL
);
216 for (textptr
= strchr(text
, '\n');
218 textptr
= strchr(textptr
+ 1, '\n'))
221 cupsdLogMessage(CUPSD_LOG_ERROR
, "ACL: %s", text
);
224 # endif /* HAVE_MBR_UID_TO_UUID */
226 if (acl_set_fd(fd
, acl
))
228 if (errno
!= EOPNOTSUPP
|| !acls_not_supported
)
229 cupsdLogMessage(CUPSD_LOG_ERROR
,
230 "Unable to set ACLs on root certificate \"%s\" - %s",
231 filename
, strerror(errno
));
233 if (errno
== EOPNOTSUPP
)
234 acls_not_supported
= 1;
239 #endif /* HAVE_ACL_INIT */
241 RootCertTime
= time(NULL
);
250 fchown(fd
, User
, Group
);
253 DEBUG_printf(("ADD pid=%d, username=%s, cert=%s\n", pid
, username
,
256 write(fd
, cert
->certificate
, strlen(cert
->certificate
));
260 * Insert the certificate at the front of the list...
269 * 'cupsdDeleteCert()' - Delete a single certificate.
273 cupsdDeleteCert(int pid
) /* I - Process ID */
275 cupsd_cert_t
*cert
, /* Current certificate */
276 *prev
; /* Previous certificate */
277 char filename
[1024]; /* Certificate file */
280 for (prev
= NULL
, cert
= Certs
; cert
!= NULL
; prev
= cert
, cert
= cert
->next
)
281 if (cert
->pid
== pid
)
284 * Remove this certificate from the list...
287 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeleteCert: Removing certificate for PID %d.", pid
);
289 DEBUG_printf(("DELETE pid=%d, username=%s, cert=%s\n", cert
->pid
,
290 cert
->username
, cert
->certificate
));
295 prev
->next
= cert
->next
;
300 * Delete the file and return...
303 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, pid
);
304 if (unlink(filename
))
305 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to remove %s!", filename
);
313 * 'cupsdDeleteAllCerts()' - Delete all certificates...
317 cupsdDeleteAllCerts(void)
319 cupsd_cert_t
*cert
, /* Current certificate */
320 *next
; /* Next certificate */
321 char filename
[1024]; /* Certificate file */
325 * Loop through each certificate, deleting them...
328 for (cert
= Certs
; cert
!= NULL
; cert
= next
)
334 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, cert
->pid
);
335 if (unlink(filename
))
336 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to remove %s!", filename
);
352 * 'cupsdFindCert()' - Find a certificate.
355 cupsd_cert_t
* /* O - Matching certificate or NULL */
356 cupsdFindCert(const char *certificate
) /* I - Certificate */
358 cupsd_cert_t
*cert
; /* Current certificate */
361 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert(certificate=%s)", certificate
);
362 for (cert
= Certs
; cert
!= NULL
; cert
= cert
->next
)
363 if (!ctcompare(certificate
, cert
->certificate
))
365 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert: Returning \"%s\".", cert
->username
);
369 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert: Certificate not found.");
376 * 'cupsdInitCerts()' - Initialize the certificate "system" and root
383 #ifndef HAVE_ARC4RANDOM
384 cups_file_t
*fp
; /* /dev/random file */
388 * Initialize the random number generator using the random device or
389 * the current time, as available...
392 if ((fp
= cupsFileOpen("/dev/urandom", "rb")) == NULL
)
394 struct timeval tod
; /* Time of day */
397 * Get the time in usecs and use it as the initial seed...
400 gettimeofday(&tod
, NULL
);
402 CUPS_SRAND((unsigned)(tod
.tv_sec
+ tod
.tv_usec
));
406 unsigned seed
; /* Seed for random number generator */
409 * Read 4 random characters from the random device and use
410 * them as the seed...
413 seed
= (unsigned)cupsFileGetChar(fp
);
414 seed
= (seed
<< 8) | (unsigned)cupsFileGetChar(fp
);
415 seed
= (seed
<< 8) | (unsigned)cupsFileGetChar(fp
);
416 CUPS_SRAND((seed
<< 8) | (unsigned)cupsFileGetChar(fp
));
420 #endif /* !HAVE_ARC4RANDOM */
423 * Create a root certificate and return...
427 cupsdAddCert(0, "root", cupsdDefaultAuthType());
432 * 'ctcompare()' - Compare two strings in constant time.
435 static int /* O - 0 on match, non-zero on non-match */
436 ctcompare(const char *a
, /* I - First string */
437 const char *b
) /* I - Second string */
439 int result
= 0; /* Result */