1 /*
2  * Main header file for the CUPS scheduler.
3  *
4  * Copyright © 2007-2018 by Apple Inc.
5  * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
6  *
7  * Licensed under Apache License v2.0.  See the file "LICENSE" for more
8  * information.
9  */
10 
11 
12 /*
13  * Include necessary headers.
14  */
15 
16 #include <cups/cups-private.h>
17 #include <cups/file-private.h>
18 #include <cups/ppd-private.h>
19 
20 #include <limits.h>
21 #include <time.h>
22 #include <signal.h>
23 #include <fcntl.h>
24 #include <math.h>
25 #include <sys/types.h>
26 #include <sys/time.h>
27 #include <sys/stat.h>
28 #include <sys/wait.h>
29 
30 #ifdef _WIN32
31 #  include <direct.h>
32 #else
33 #  include <unistd.h>
34 #endif /* _WIN32 */
35 
36 #include "mime.h"
37 
38 #if defined(HAVE_CDSASSL)
39 #  include <CoreFoundation/CoreFoundation.h>
40 #endif /* HAVE_CDSASSL */
41 
42 
43 /*
44  * Some OS's don't have hstrerror(), most notably Solaris...
45  */
46 
47 #ifndef HAVE_HSTRERROR
48 #  ifdef hstrerror
49 #    undef hstrerror
50 #  endif /* hstrerror */
51 #  define hstrerror cups_hstrerror
52 
53 extern const char *cups_hstrerror(int);
54 #endif /* !HAVE_HSTRERROR */
55 
56 
57 /*
58  * Common constants.
59  */
60 
61 #ifndef FALSE
62 #  define FALSE		0
63 #  define TRUE		(!FALSE)
64 #endif /* !FALSE */
65 
66 
67 /*
68  * Implementation limits...
69  */
70 
71 #define MAX_ENV			100	/* Maximum number of environment strings */
72 #define MAX_USERPASS		33	/* Maximum size of username/password */
73 #define MAX_FILTERS		20	/* Maximum number of filters */
74 #define MAX_SYSTEM_GROUPS	32	/* Maximum number of system groups */
75 
76 
77 /*
78  * Defaults...
79  */
80 
81 #define DEFAULT_HISTORY		INT_MAX	/* Preserve job history? */
82 #define DEFAULT_FILES		86400	/* Preserve job files? */
83 #define DEFAULT_TIMEOUT		300	/* Timeout during requests/updates */
84 #define DEFAULT_KEEPALIVE	30	/* Timeout between requests */
85 
86 
87 /*
88  * Global variable macros...
89  */
90 
91 #ifdef _MAIN_C_
92 #  define VAR
93 #  define VALUE(x) =x
94 #  define VALUE2(x,y) ={x,y}
95 #else
96 #  define VAR      extern
97 #  define VALUE(x)
98 #  define VALUE2(x,y)
99 #endif /* _MAIN_C */
100 
101 
102 /*
103  * Other stuff for the scheduler...
104  */
105 
106 #include "sysman.h"
107 #include "statbuf.h"
108 #include "cert.h"
109 #include "auth.h"
110 #include "client.h"
111 #include "policy.h"
112 #include "printers.h"
113 #include "classes.h"
114 #include "job.h"
115 #include "colorman.h"
116 #include "conf.h"
117 #include "banners.h"
118 #include "dirsvc.h"
119 #include "network.h"
120 #include "subscriptions.h"
121 
122 
123 /*
124  * Reload types...
125  */
126 
127 #define RELOAD_NONE	0		/* No reload needed */
128 #define RELOAD_ALL	1		/* Reload everything */
129 #define RELOAD_CUPSD	2		/* Reload only cupsd.conf */
130 
131 
132 /*
133  * Select callback function type...
134  */
135 
136 typedef void (*cupsd_selfunc_t)(void *data);
137 
138 
139 /*
140  * Globals...
141  */
142 
143 VAR int			TestConfigFile	VALUE(0);
144 					/* Test the cupsd.conf file? */
145 VAR int			MaxFDs		VALUE(0);
146 					/* Maximum number of files */
147 
148 VAR time_t		ReloadTime	VALUE(0);
149 					/* Time of reload request... */
150 VAR int			NeedReload	VALUE(RELOAD_ALL),
151 					/* Need to load configuration? */
152 			DoingShutdown	VALUE(0);
153 					/* Shutting down the scheduler? */
154 VAR void		*DefaultProfile	VALUE(0);
155 					/* Default security profile */
156 
157 #ifdef HAVE_ONDEMAND
158 VAR int			OnDemand	VALUE(0);
159 					/* Launched on demand */
160 #endif /* HAVE_ONDEMAND */
161 
162 
163 /*
164  * Prototypes...
165  */
166 
167 /* env.c */
168 extern void		cupsdInitEnv(void);
169 extern int		cupsdLoadEnv(char *envp[], int envmax);
170 extern void		cupsdSetEnv(const char *name, const char *value);
171 extern void		cupsdSetEnvf(const char *name, const char *value, ...) _CUPS_FORMAT(2, 3);
172 extern void		cupsdUpdateEnv(void);
173 
174 /* file.c */
175 extern void		cupsdCleanFiles(const char *path, const char *pattern);
176 extern int		cupsdCloseCreatedConfFile(cups_file_t *fp,
177 			                          const char *filename);
178 extern void		cupsdClosePipe(int *fds);
179 extern cups_file_t	*cupsdCreateConfFile(const char *filename, mode_t mode);
180 extern cups_file_t	*cupsdOpenConfFile(const char *filename);
181 extern int		cupsdOpenPipe(int *fds);
182 extern int		cupsdRemoveFile(const char *filename);
183 extern int		cupsdUnlinkOrRemoveFile(const char *filename);
184 
185 /* main.c */
186 extern int		cupsdAddString(cups_array_t **a, const char *s);
187 extern void		cupsdCheckProcess(void);
188 extern void		cupsdClearString(char **s);
189 extern void		cupsdFreeStrings(cups_array_t **a);
190 extern void		cupsdHoldSignals(void);
191 extern char		*cupsdMakeUUID(const char *name, int number,
192 				       char *buffer, size_t bufsize);
193 extern void		cupsdReleaseSignals(void);
194 extern void		cupsdSetString(char **s, const char *v);
195 extern void		cupsdSetStringf(char **s, const char *f, ...)
196 			__attribute__ ((__format__ (__printf__, 2, 3)));
197 
198 /* process.c */
199 extern void		*cupsdCreateProfile(int job_id, int allow_networking);
200 extern void		cupsdDestroyProfile(void *profile);
201 extern int		cupsdEndProcess(int pid, int force);
202 extern const char	*cupsdFinishProcess(int pid, char *name, size_t namelen, int *job_id);
203 extern int		cupsdStartProcess(const char *command, char *argv[],
204 					  char *envp[], int infd, int outfd,
205 					  int errfd, int backfd, int sidefd,
206 					  int root, void *profile,
207 					  cupsd_job_t *job, int *pid);
208 
209 /* select.c */
210 extern int		cupsdAddSelect(int fd, cupsd_selfunc_t read_cb,
211 			               cupsd_selfunc_t write_cb, void *data);
212 extern int		cupsdDoSelect(long timeout);
213 #ifdef CUPSD_IS_SELECTING
214 extern int		cupsdIsSelecting(int fd);
215 #endif /* CUPSD_IS_SELECTING */
216 extern void		cupsdRemoveSelect(int fd);
217 extern void		cupsdStartSelect(void);
218 extern void		cupsdStopSelect(void);
219 
220 /* server.c */
221 extern void		cupsdStartServer(void);
222 extern void		cupsdStopServer(void);
223