]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cert.c
4 * Authentication certificate routines for the CUPS scheduler.
6 * Copyright 2007-2014 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 */
30 * 'cupsdAddCert()' - Add a certificate.
34 cupsdAddCert(int pid
, /* I - Process ID */
35 const char *username
, /* I - Username */
36 int type
) /* I - AuthType for username */
38 int i
; /* Looping var */
39 cupsd_cert_t
*cert
; /* Current certificate */
40 int fd
; /* Certificate file */
41 char filename
[1024]; /* Certificate filename */
42 static const char hex
[] = "0123456789ABCDEF";
43 /* Hex constants... */
46 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
47 "cupsdAddCert: Adding certificate for PID %d", pid
);
50 * Allocate memory for the certificate...
53 if ((cert
= calloc(sizeof(cupsd_cert_t
), 1)) == NULL
)
57 * Fill in the certificate information...
62 strlcpy(cert
->username
, username
, sizeof(cert
->username
));
64 for (i
= 0; i
< 32; i
++)
65 cert
->certificate
[i
] = hex
[CUPS_RAND() & 15];
68 * Save the certificate to a file readable only by the User and Group
69 * (or root and SystemGroup for PID == 0)...
72 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, pid
);
75 if ((fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0400)) < 0)
77 cupsdLogMessage(CUPSD_LOG_ERROR
,
78 "Unable to create certificate file %s - %s",
79 filename
, strerror(errno
));
87 acl_t acl
; /* ACL information */
88 acl_entry_t entry
; /* ACL entry */
89 acl_permset_t permset
; /* Permissions */
90 # ifdef HAVE_MBR_UID_TO_UUID
91 uuid_t group
; /* Group ID */
92 # endif /* HAVE_MBR_UID_TO_UUID */
93 static int acls_not_supported
= 0;
95 #endif /* HAVE_ACL_INIT */
103 fchown(fd
, RunUser
, SystemGroupIDs
[0]);
105 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddCert: NumSystemGroups=%d",
109 if (NumSystemGroups
> 1)
112 * Set POSIX ACLs for the root certificate so that all system
113 * groups can access it...
116 int j
; /* Looping var */
118 # ifdef HAVE_MBR_UID_TO_UUID
120 * On MacOS X, ACLs use UUIDs instead of GIDs...
123 acl
= acl_init(NumSystemGroups
- 1);
125 for (i
= 1; i
< NumSystemGroups
; i
++)
128 * Add each group ID to the ACL...
131 for (j
= 0; j
< i
; j
++)
132 if (SystemGroupIDs
[j
] == SystemGroupIDs
[i
])
136 continue; /* Skip duplicate groups */
138 acl_create_entry(&acl
, &entry
);
139 acl_get_permset(entry
, &permset
);
140 acl_add_perm(permset
, ACL_READ_DATA
);
141 acl_set_tag_type(entry
, ACL_EXTENDED_ALLOW
);
142 mbr_gid_to_uuid((gid_t
)SystemGroupIDs
[i
], group
);
143 acl_set_qualifier(entry
, &group
);
144 acl_set_permset(entry
, permset
);
149 * POSIX ACLs need permissions for owner, group, other, and mask
150 * in addition to the rest of the system groups...
153 acl
= acl_init(NumSystemGroups
+ 3);
156 acl_create_entry(&acl
, &entry
);
157 acl_get_permset(entry
, &permset
);
158 acl_add_perm(permset
, ACL_READ
);
159 acl_set_tag_type(entry
, ACL_USER_OBJ
);
160 acl_set_permset(entry
, permset
);
163 acl_create_entry(&acl
, &entry
);
164 acl_get_permset(entry
, &permset
);
165 acl_add_perm(permset
, ACL_READ
);
166 acl_set_tag_type(entry
, ACL_GROUP_OBJ
);
167 acl_set_permset(entry
, permset
);
170 acl_create_entry(&acl
, &entry
);
171 acl_get_permset(entry
, &permset
);
172 acl_add_perm(permset
, 0);
173 acl_set_tag_type(entry
, ACL_OTHER
);
174 acl_set_permset(entry
, permset
);
177 acl_create_entry(&acl
, &entry
);
178 acl_get_permset(entry
, &permset
);
179 acl_add_perm(permset
, ACL_READ
);
180 acl_set_tag_type(entry
, ACL_MASK
);
181 acl_set_permset(entry
, permset
);
183 for (i
= 1; i
< NumSystemGroups
; i
++)
186 * Add each group ID to the ACL...
189 for (j
= 0; j
< i
; j
++)
190 if (SystemGroupIDs
[j
] == SystemGroupIDs
[i
])
194 continue; /* Skip duplicate groups */
196 acl_create_entry(&acl
, &entry
);
197 acl_get_permset(entry
, &permset
);
198 acl_add_perm(permset
, ACL_READ
);
199 acl_set_tag_type(entry
, ACL_GROUP
);
200 acl_set_qualifier(entry
, SystemGroupIDs
+ i
);
201 acl_set_permset(entry
, permset
);
206 char *text
, *textptr
; /* Temporary string */
208 cupsdLogMessage(CUPSD_LOG_ERROR
, "ACL did not validate: %s",
210 text
= acl_to_text(acl
, NULL
);
211 for (textptr
= strchr(text
, '\n');
213 textptr
= strchr(textptr
+ 1, '\n'))
216 cupsdLogMessage(CUPSD_LOG_ERROR
, "ACL: %s", text
);
219 # endif /* HAVE_MBR_UID_TO_UUID */
221 if (acl_set_fd(fd
, acl
))
223 if (errno
!= EOPNOTSUPP
|| !acls_not_supported
)
224 cupsdLogMessage(CUPSD_LOG_ERROR
,
225 "Unable to set ACLs on root certificate \"%s\" - %s",
226 filename
, strerror(errno
));
228 if (errno
== EOPNOTSUPP
)
229 acls_not_supported
= 1;
234 #endif /* HAVE_ACL_INIT */
236 RootCertTime
= time(NULL
);
245 fchown(fd
, User
, Group
);
248 DEBUG_printf(("ADD pid=%d, username=%s, cert=%s\n", pid
, username
,
251 write(fd
, cert
->certificate
, strlen(cert
->certificate
));
255 * Insert the certificate at the front of the list...
264 * 'cupsdDeleteCert()' - Delete a single certificate.
268 cupsdDeleteCert(int pid
) /* I - Process ID */
270 cupsd_cert_t
*cert
, /* Current certificate */
271 *prev
; /* Previous certificate */
272 char filename
[1024]; /* Certificate file */
275 for (prev
= NULL
, cert
= Certs
; cert
!= NULL
; prev
= cert
, cert
= cert
->next
)
276 if (cert
->pid
== pid
)
279 * Remove this certificate from the list...
282 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
283 "cupsdDeleteCert: Removing certificate for PID %d", pid
);
285 DEBUG_printf(("DELETE pid=%d, username=%s, cert=%s\n", cert
->pid
,
286 cert
->username
, cert
->certificate
));
291 prev
->next
= cert
->next
;
296 * Delete the file and return...
299 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, pid
);
300 if (unlink(filename
))
301 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to remove %s!", filename
);
309 * 'cupsdDeleteAllCerts()' - Delete all certificates...
313 cupsdDeleteAllCerts(void)
315 cupsd_cert_t
*cert
, /* Current certificate */
316 *next
; /* Next certificate */
317 char filename
[1024]; /* Certificate file */
321 * Loop through each certificate, deleting them...
324 for (cert
= Certs
; cert
!= NULL
; cert
= next
)
330 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, cert
->pid
);
331 if (unlink(filename
))
332 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to remove %s!", filename
);
348 * 'cupsdFindCert()' - Find a certificate.
351 cupsd_cert_t
* /* O - Matching certificate or NULL */
352 cupsdFindCert(const char *certificate
) /* I - Certificate */
354 cupsd_cert_t
*cert
; /* Current certificate */
357 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert(certificate=%s)",
359 for (cert
= Certs
; cert
!= NULL
; cert
= cert
->next
)
360 if (!_cups_strcasecmp(certificate
, cert
->certificate
))
362 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert: Returning %s...",
367 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert: Certificate not found!");
374 * 'cupsdInitCerts()' - Initialize the certificate "system" and root
381 #ifndef HAVE_ARC4RANDOM
382 cups_file_t
*fp
; /* /dev/random file */
386 * Initialize the random number generator using the random device or
387 * the current time, as available...
390 if ((fp
= cupsFileOpen("/dev/urandom", "rb")) == NULL
)
392 struct timeval tod
; /* Time of day */
395 * Get the time in usecs and use it as the initial seed...
398 gettimeofday(&tod
, NULL
);
400 CUPS_SRAND((unsigned)(tod
.tv_sec
+ tod
.tv_usec
));
404 unsigned seed
; /* Seed for random number generator */
407 * Read 4 random characters from the random device and use
408 * them as the seed...
411 seed
= (unsigned)cupsFileGetChar(fp
);
412 seed
= (seed
<< 8) | (unsigned)cupsFileGetChar(fp
);
413 seed
= (seed
<< 8) | (unsigned)cupsFileGetChar(fp
);
414 CUPS_SRAND((seed
<< 8) | (unsigned)cupsFileGetChar(fp
));
418 #endif /* !HAVE_ARC4RANDOM */
421 * Create a root certificate and return...
425 cupsdAddCert(0, "root", cupsdDefaultAuthType());