]>
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",
115 if (NumSystemGroups
> 1)
118 * Set POSIX ACLs for the root certificate so that all system
119 * groups can access it...
122 int j
; /* Looping var */
124 # ifdef HAVE_MBR_UID_TO_UUID
126 * On MacOS X, ACLs use UUIDs instead of GIDs...
129 acl
= acl_init(NumSystemGroups
- 1);
131 for (i
= 1; i
< NumSystemGroups
; i
++)
134 * Add each group ID to the ACL...
137 for (j
= 0; j
< i
; j
++)
138 if (SystemGroupIDs
[j
] == SystemGroupIDs
[i
])
142 continue; /* Skip duplicate groups */
144 acl_create_entry(&acl
, &entry
);
145 acl_get_permset(entry
, &permset
);
146 acl_add_perm(permset
, ACL_READ_DATA
);
147 acl_set_tag_type(entry
, ACL_EXTENDED_ALLOW
);
148 mbr_gid_to_uuid((gid_t
)SystemGroupIDs
[i
], group
);
149 acl_set_qualifier(entry
, &group
);
150 acl_set_permset(entry
, permset
);
155 * POSIX ACLs need permissions for owner, group, other, and mask
156 * in addition to the rest of the system groups...
159 acl
= acl_init(NumSystemGroups
+ 3);
162 acl_create_entry(&acl
, &entry
);
163 acl_get_permset(entry
, &permset
);
164 acl_add_perm(permset
, ACL_READ
);
165 acl_set_tag_type(entry
, ACL_USER_OBJ
);
166 acl_set_permset(entry
, permset
);
169 acl_create_entry(&acl
, &entry
);
170 acl_get_permset(entry
, &permset
);
171 acl_add_perm(permset
, ACL_READ
);
172 acl_set_tag_type(entry
, ACL_GROUP_OBJ
);
173 acl_set_permset(entry
, permset
);
176 acl_create_entry(&acl
, &entry
);
177 acl_get_permset(entry
, &permset
);
178 acl_add_perm(permset
, 0);
179 acl_set_tag_type(entry
, ACL_OTHER
);
180 acl_set_permset(entry
, permset
);
183 acl_create_entry(&acl
, &entry
);
184 acl_get_permset(entry
, &permset
);
185 acl_add_perm(permset
, ACL_READ
);
186 acl_set_tag_type(entry
, ACL_MASK
);
187 acl_set_permset(entry
, permset
);
189 for (i
= 1; i
< NumSystemGroups
; i
++)
192 * Add each group ID to the ACL...
195 for (j
= 0; j
< i
; j
++)
196 if (SystemGroupIDs
[j
] == SystemGroupIDs
[i
])
200 continue; /* Skip duplicate groups */
202 acl_create_entry(&acl
, &entry
);
203 acl_get_permset(entry
, &permset
);
204 acl_add_perm(permset
, ACL_READ
);
205 acl_set_tag_type(entry
, ACL_GROUP
);
206 acl_set_qualifier(entry
, SystemGroupIDs
+ i
);
207 acl_set_permset(entry
, permset
);
212 char *text
, *textptr
; /* Temporary string */
214 cupsdLogMessage(CUPSD_LOG_ERROR
, "ACL did not validate: %s",
216 text
= acl_to_text(acl
, NULL
);
217 for (textptr
= strchr(text
, '\n');
219 textptr
= strchr(textptr
+ 1, '\n'))
222 cupsdLogMessage(CUPSD_LOG_ERROR
, "ACL: %s", text
);
225 # endif /* HAVE_MBR_UID_TO_UUID */
227 if (acl_set_fd(fd
, acl
))
229 if (errno
!= EOPNOTSUPP
|| !acls_not_supported
)
230 cupsdLogMessage(CUPSD_LOG_ERROR
,
231 "Unable to set ACLs on root certificate \"%s\" - %s",
232 filename
, strerror(errno
));
234 if (errno
== EOPNOTSUPP
)
235 acls_not_supported
= 1;
240 #endif /* HAVE_ACL_INIT */
242 RootCertTime
= time(NULL
);
251 fchown(fd
, User
, Group
);
254 DEBUG_printf(("ADD pid=%d, username=%s, cert=%s\n", pid
, username
,
257 write(fd
, cert
->certificate
, strlen(cert
->certificate
));
261 * Insert the certificate at the front of the list...
270 * 'cupsdDeleteCert()' - Delete a single certificate.
274 cupsdDeleteCert(int pid
) /* I - Process ID */
276 cupsd_cert_t
*cert
, /* Current certificate */
277 *prev
; /* Previous certificate */
278 char filename
[1024]; /* Certificate file */
281 for (prev
= NULL
, cert
= Certs
; cert
!= NULL
; prev
= cert
, cert
= cert
->next
)
282 if (cert
->pid
== pid
)
285 * Remove this certificate from the list...
288 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
289 "cupsdDeleteCert: Removing certificate for PID %d", pid
);
291 DEBUG_printf(("DELETE pid=%d, username=%s, cert=%s\n", cert
->pid
,
292 cert
->username
, cert
->certificate
));
297 prev
->next
= cert
->next
;
302 * Delete the file and return...
305 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, pid
);
306 if (unlink(filename
))
307 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to remove %s!", filename
);
315 * 'cupsdDeleteAllCerts()' - Delete all certificates...
319 cupsdDeleteAllCerts(void)
321 cupsd_cert_t
*cert
, /* Current certificate */
322 *next
; /* Next certificate */
323 char filename
[1024]; /* Certificate file */
327 * Loop through each certificate, deleting them...
330 for (cert
= Certs
; cert
!= NULL
; cert
= next
)
336 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, cert
->pid
);
337 if (unlink(filename
))
338 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to remove %s!", filename
);
354 * 'cupsdFindCert()' - Find a certificate.
357 cupsd_cert_t
* /* O - Matching certificate or NULL */
358 cupsdFindCert(const char *certificate
) /* I - Certificate */
360 cupsd_cert_t
*cert
; /* Current certificate */
363 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert(certificate=%s)",
365 for (cert
= Certs
; cert
!= NULL
; cert
= cert
->next
)
366 if (!ctcompare(certificate
, cert
->certificate
))
368 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert: Returning %s...",
373 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert: Certificate not found!");
380 * 'cupsdInitCerts()' - Initialize the certificate "system" and root
387 #ifndef HAVE_ARC4RANDOM
388 cups_file_t
*fp
; /* /dev/random file */
392 * Initialize the random number generator using the random device or
393 * the current time, as available...
396 if ((fp
= cupsFileOpen("/dev/urandom", "rb")) == NULL
)
398 struct timeval tod
; /* Time of day */
401 * Get the time in usecs and use it as the initial seed...
404 gettimeofday(&tod
, NULL
);
406 CUPS_SRAND((unsigned)(tod
.tv_sec
+ tod
.tv_usec
));
410 unsigned seed
; /* Seed for random number generator */
413 * Read 4 random characters from the random device and use
414 * them as the seed...
417 seed
= (unsigned)cupsFileGetChar(fp
);
418 seed
= (seed
<< 8) | (unsigned)cupsFileGetChar(fp
);
419 seed
= (seed
<< 8) | (unsigned)cupsFileGetChar(fp
);
420 CUPS_SRAND((seed
<< 8) | (unsigned)cupsFileGetChar(fp
));
424 #endif /* !HAVE_ARC4RANDOM */
427 * Create a root certificate and return...
431 cupsdAddCert(0, "root", cupsdDefaultAuthType());
436 * 'ctcompare()' - Compare two strings in constant time.
439 static int /* O - 0 on match, non-zero on non-match */
440 ctcompare(const char *a
, /* I - First string */
441 const char *b
) /* I - Second string */
443 int result
= 0; /* Result */