]>
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_DEBUG
, "cupsdAddCert: Adding certificate for PID %d", pid
);
49 * Allocate memory for the certificate...
52 if ((cert
= calloc(sizeof(cupsd_cert_t
), 1)) == NULL
)
56 * Fill in the certificate information...
61 strlcpy(cert
->username
, username
, sizeof(cert
->username
));
63 for (i
= 0; i
< 32; i
++)
64 cert
->certificate
[i
] = hex
[CUPS_RAND() & 15];
67 * Save the certificate to a file readable only by the User and Group
68 * (or root and SystemGroup for PID == 0)...
71 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, pid
);
74 if ((fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0400)) < 0)
76 cupsdLogMessage(CUPSD_LOG_ERROR
,
77 "Unable to create certificate file %s - %s",
78 filename
, strerror(errno
));
86 acl_t acl
; /* ACL information */
87 acl_entry_t entry
; /* ACL entry */
88 acl_permset_t permset
; /* Permissions */
89 # ifdef HAVE_MBR_UID_TO_UUID
90 uuid_t group
; /* Group ID */
91 # endif /* HAVE_MBR_UID_TO_UUID */
92 static int acls_not_supported
= 0;
94 #endif /* HAVE_ACL_INIT */
102 fchown(fd
, RunUser
, SystemGroupIDs
[0]);
104 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddCert: NumSystemGroups=%d",
108 if (NumSystemGroups
> 1)
111 * Set POSIX ACLs for the root certificate so that all system
112 * groups can access it...
115 int j
; /* Looping var */
117 # ifdef HAVE_MBR_UID_TO_UUID
119 * On MacOS X, ACLs use UUIDs instead of GIDs...
122 acl
= acl_init(NumSystemGroups
- 1);
124 for (i
= 1; i
< NumSystemGroups
; i
++)
127 * Add each group ID to the ACL...
130 for (j
= 0; j
< i
; j
++)
131 if (SystemGroupIDs
[j
] == SystemGroupIDs
[i
])
135 continue; /* Skip duplicate groups */
137 acl_create_entry(&acl
, &entry
);
138 acl_get_permset(entry
, &permset
);
139 acl_add_perm(permset
, ACL_READ_DATA
);
140 acl_set_tag_type(entry
, ACL_EXTENDED_ALLOW
);
141 mbr_gid_to_uuid((gid_t
)SystemGroupIDs
[i
], group
);
142 acl_set_qualifier(entry
, &group
);
143 acl_set_permset(entry
, permset
);
148 * POSIX ACLs need permissions for owner, group, other, and mask
149 * in addition to the rest of the system groups...
152 acl
= acl_init(NumSystemGroups
+ 3);
155 acl_create_entry(&acl
, &entry
);
156 acl_get_permset(entry
, &permset
);
157 acl_add_perm(permset
, ACL_READ
);
158 acl_set_tag_type(entry
, ACL_USER_OBJ
);
159 acl_set_permset(entry
, permset
);
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_GROUP_OBJ
);
166 acl_set_permset(entry
, permset
);
169 acl_create_entry(&acl
, &entry
);
170 acl_get_permset(entry
, &permset
);
171 acl_add_perm(permset
, 0);
172 acl_set_tag_type(entry
, ACL_OTHER
);
173 acl_set_permset(entry
, permset
);
176 acl_create_entry(&acl
, &entry
);
177 acl_get_permset(entry
, &permset
);
178 acl_add_perm(permset
, ACL_READ
);
179 acl_set_tag_type(entry
, ACL_MASK
);
180 acl_set_permset(entry
, permset
);
182 for (i
= 1; i
< NumSystemGroups
; i
++)
185 * Add each group ID to the ACL...
188 for (j
= 0; j
< i
; j
++)
189 if (SystemGroupIDs
[j
] == SystemGroupIDs
[i
])
193 continue; /* Skip duplicate groups */
195 acl_create_entry(&acl
, &entry
);
196 acl_get_permset(entry
, &permset
);
197 acl_add_perm(permset
, ACL_READ
);
198 acl_set_tag_type(entry
, ACL_GROUP
);
199 acl_set_qualifier(entry
, SystemGroupIDs
+ i
);
200 acl_set_permset(entry
, permset
);
205 char *text
, *textptr
; /* Temporary string */
207 cupsdLogMessage(CUPSD_LOG_ERROR
, "ACL did not validate: %s",
209 text
= acl_to_text(acl
, NULL
);
210 for (textptr
= strchr(text
, '\n');
212 textptr
= strchr(textptr
+ 1, '\n'))
215 cupsdLogMessage(CUPSD_LOG_ERROR
, "ACL: %s", text
);
218 # endif /* HAVE_MBR_UID_TO_UUID */
220 if (acl_set_fd(fd
, acl
))
222 if (errno
!= EOPNOTSUPP
|| !acls_not_supported
)
223 cupsdLogMessage(CUPSD_LOG_ERROR
,
224 "Unable to set ACLs on root certificate \"%s\" - %s",
225 filename
, strerror(errno
));
227 if (errno
== EOPNOTSUPP
)
228 acls_not_supported
= 1;
233 #endif /* HAVE_ACL_INIT */
235 RootCertTime
= time(NULL
);
244 fchown(fd
, User
, Group
);
247 DEBUG_printf(("ADD pid=%d, username=%s, cert=%s\n", pid
, username
,
250 write(fd
, cert
->certificate
, strlen(cert
->certificate
));
254 * Insert the certificate at the front of the list...
263 * 'cupsdDeleteCert()' - Delete a single certificate.
267 cupsdDeleteCert(int pid
) /* I - Process ID */
269 cupsd_cert_t
*cert
, /* Current certificate */
270 *prev
; /* Previous certificate */
271 char filename
[1024]; /* Certificate file */
274 for (prev
= NULL
, cert
= Certs
; cert
!= NULL
; prev
= cert
, cert
= cert
->next
)
275 if (cert
->pid
== pid
)
278 * Remove this certificate from the list...
281 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
282 "cupsdDeleteCert: Removing certificate for PID %d", pid
);
284 DEBUG_printf(("DELETE pid=%d, username=%s, cert=%s\n", cert
->pid
,
285 cert
->username
, cert
->certificate
));
290 prev
->next
= cert
->next
;
295 * Delete the file and return...
298 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, pid
);
299 if (unlink(filename
))
300 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to remove %s!", filename
);
308 * 'cupsdDeleteAllCerts()' - Delete all certificates...
312 cupsdDeleteAllCerts(void)
314 cupsd_cert_t
*cert
, /* Current certificate */
315 *next
; /* Next certificate */
316 char filename
[1024]; /* Certificate file */
320 * Loop through each certificate, deleting them...
323 for (cert
= Certs
; cert
!= NULL
; cert
= next
)
329 snprintf(filename
, sizeof(filename
), "%s/certs/%d", StateDir
, cert
->pid
);
330 if (unlink(filename
))
331 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to remove %s!", filename
);
347 * 'cupsdFindCert()' - Find a certificate.
350 cupsd_cert_t
* /* O - Matching certificate or NULL */
351 cupsdFindCert(const char *certificate
) /* I - Certificate */
353 cupsd_cert_t
*cert
; /* Current certificate */
356 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert(certificate=%s)",
358 for (cert
= Certs
; cert
!= NULL
; cert
= cert
->next
)
359 if (!_cups_strcasecmp(certificate
, cert
->certificate
))
361 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert: Returning %s...",
366 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindCert: Certificate not found!");
373 * 'cupsdInitCerts()' - Initialize the certificate "system" and root
380 #ifndef HAVE_ARC4RANDOM
381 cups_file_t
*fp
; /* /dev/random file */
385 * Initialize the random number generator using the random device or
386 * the current time, as available...
389 if ((fp
= cupsFileOpen("/dev/urandom", "rb")) == NULL
)
391 struct timeval tod
; /* Time of day */
394 * Get the time in usecs and use it as the initial seed...
397 gettimeofday(&tod
, NULL
);
399 CUPS_SRAND((unsigned)(tod
.tv_sec
+ tod
.tv_usec
));
403 unsigned seed
; /* Seed for random number generator */
406 * Read 4 random characters from the random device and use
407 * them as the seed...
410 seed
= (unsigned)cupsFileGetChar(fp
);
411 seed
= (seed
<< 8) | (unsigned)cupsFileGetChar(fp
);
412 seed
= (seed
<< 8) | (unsigned)cupsFileGetChar(fp
);
413 CUPS_SRAND((seed
<< 8) | (unsigned)cupsFileGetChar(fp
));
417 #endif /* !HAVE_ARC4RANDOM */
420 * Create a root certificate and return...
424 cupsdAddCert(0, "root", cupsdDefaultAuthType());