2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Prototypes for lib directory of Bacula
39 ATTR *new_attr(JCR *jcr);
40 void free_attr(ATTR *attr);
41 int unpack_attributes_record(JCR *jcr, int32_t stream, char *rec, int32_t reclen, ATTR *attr);
42 void build_attr_output_fnames(JCR *jcr, ATTR *attr);
43 void print_ls_output(JCR *jcr, ATTR *attr);
46 void base64_init (void);
47 int to_base64 (intmax_t value, char *where);
48 int from_base64 (intmax_t *value, char *where);
49 int bin_to_base64 (char *buf, int buflen, char *bin, int binlen,
53 char *bstrncpy (char *dest, const char *src, int maxlen);
54 char *bstrncpy (char *dest, POOL_MEM &src, int maxlen);
55 char *bstrncat (char *dest, const char *src, int maxlen);
56 char *bstrncat (char *dest, POOL_MEM &src, int maxlen);
57 bool bstrcmp (const char *s1, const char *s2);
58 int cstrlen (const char *str);
59 void *b_malloc (const char *file, int line, size_t size);
61 void *bmalloc (size_t size);
63 void bfree (void *buf);
64 void *brealloc (void *buf, size_t size);
65 void *bcalloc (size_t size1, size_t size2);
66 int bsnprintf (char *str, int32_t size, const char *format, ...);
67 int bvsnprintf (char *str, int32_t size, const char *format, va_list ap);
68 int pool_sprintf (char *pool_buf, const char *fmt, ...);
69 void create_pid_file (char *dir, const char *progname, int port);
70 int delete_pid_file (char *dir, const char *progname, int port);
71 void drop (char *uid, char *gid, bool keep_readall_caps);
72 int bmicrosleep (int32_t sec, int32_t usec);
73 char *bfgets (char *s, int size, FILE *fd);
74 void make_unique_filename (POOLMEM **name, int Id, char *what);
76 long long int strtoll (const char *ptr, char **endptr, int base);
78 void read_state_file(char *dir, const char *progname, int port);
79 int b_strerror(int errnum, char *buf, size_t bufsiz);
80 char *escape_filename(const char *file_path);
81 int Zdeflate(char *in, int in_len, char *out, int &out_len);
82 int Zinflate(char *in, int in_len, char *out, int &out_len);
85 int32_t bnet_recv (BSOCK *bsock);
86 bool bnet_send (BSOCK *bsock);
87 bool bnet_fsend (BSOCK *bs, const char *fmt, ...);
88 bool bnet_set_buffer_size (BSOCK *bs, uint32_t size, int rw);
89 bool bnet_sig (BSOCK *bs, int sig);
90 bool bnet_tls_server (TLS_CONTEXT *ctx, BSOCK *bsock,
92 bool bnet_tls_client (TLS_CONTEXT *ctx, BSOCK *bsock,
94 BSOCK * bnet_connect (JCR *jcr, int retry_interval,
95 utime_t max_retry_time, utime_t heart_beat,
96 const char *name, char *host, char *service,
97 int port, int verbose);
98 void bnet_close (BSOCK *bsock);
99 BSOCK * init_bsock (JCR *jcr, int sockfd, const char *who, const char *ip,
100 int port, struct sockaddr *client_addr);
103 #define socklen_t int
106 int bnet_get_peer (BSOCK *bs, char *buf, socklen_t buflen);
107 BSOCK * dup_bsock (BSOCK *bsock);
108 void term_bsock (BSOCK *bsock);
109 const char *bnet_strerror (BSOCK *bsock);
110 const char *bnet_sig_to_ascii (BSOCK *bsock);
111 int bnet_wait_data (BSOCK *bsock, int sec);
112 int bnet_wait_data_intr (BSOCK *bsock, int sec);
113 bool is_bnet_stop (BSOCK *bsock);
114 int is_bnet_error (BSOCK *bsock);
115 void bnet_suppress_error_messages(BSOCK *bsock, bool flag);
116 dlist *bnet_host2ipaddrs(const char *host, int family, const char **errstr);
117 int bnet_set_blocking (BSOCK *sock);
118 int bnet_set_nonblocking (BSOCK *sock);
119 void bnet_restore_blocking (BSOCK *sock, int flags);
122 int bget_msg(BSOCK *sock);
125 BPIPE * open_bpipe(char *prog, int wait, const char *mode);
126 int close_wpipe(BPIPE *bpipe);
127 int close_bpipe(BPIPE *bpipe);
130 bool cram_md5_respond(BSOCK *bs, const char *password, int *tls_remote_need, int *compatible);
131 bool cram_md5_challenge(BSOCK *bs, const char *password, int tls_local_need, int compatible);
132 void hmac_md5(uint8_t* text, int text_len, uint8_t* key, int key_len, uint8_t *hmac);
136 uint32_t bcrc32(uint8_t *buf, int len);
139 int init_crypto (void);
140 int cleanup_crypto (void);
141 DIGEST * crypto_digest_new (JCR *jcr, crypto_digest_t type);
142 bool crypto_digest_update (DIGEST *digest, const uint8_t *data, uint32_t length);
143 bool crypto_digest_finalize (DIGEST *digest, uint8_t *dest, uint32_t *length);
144 void crypto_digest_free (DIGEST *digest);
145 SIGNATURE * crypto_sign_new (JCR *jcr);
146 crypto_error_t crypto_sign_get_digest (SIGNATURE *sig, X509_KEYPAIR *keypair,
147 crypto_digest_t &algorithm, DIGEST **digest);
148 crypto_error_t crypto_sign_verify (SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST *digest);
149 int crypto_sign_add_signer (SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair);
150 int crypto_sign_encode (SIGNATURE *sig, uint8_t *dest, uint32_t *length);
151 SIGNATURE * crypto_sign_decode (JCR *jcr, const uint8_t *sigData, uint32_t length);
152 void crypto_sign_free (SIGNATURE *sig);
153 CRYPTO_SESSION * crypto_session_new (crypto_cipher_t cipher, alist *pubkeys);
154 void crypto_session_free (CRYPTO_SESSION *cs);
155 bool crypto_session_encode (CRYPTO_SESSION *cs, uint8_t *dest, uint32_t *length);
156 crypto_error_t crypto_session_decode (const uint8_t *data, uint32_t length, alist *keypairs, CRYPTO_SESSION **session);
157 CRYPTO_SESSION * crypto_session_decode (const uint8_t *data, uint32_t length);
158 CIPHER_CONTEXT * crypto_cipher_new (CRYPTO_SESSION *cs, bool encrypt, uint32_t *blocksize);
159 bool crypto_cipher_update (CIPHER_CONTEXT *cipher_ctx, const uint8_t *data, uint32_t length, const uint8_t *dest, uint32_t *written);
160 bool crypto_cipher_finalize (CIPHER_CONTEXT *cipher_ctx, uint8_t *dest, uint32_t *written);
161 void crypto_cipher_free (CIPHER_CONTEXT *cipher_ctx);
162 X509_KEYPAIR * crypto_keypair_new (void);
163 X509_KEYPAIR * crypto_keypair_dup (X509_KEYPAIR *keypair);
164 int crypto_keypair_load_cert (X509_KEYPAIR *keypair, const char *file);
165 bool crypto_keypair_has_key (const char *file);
166 int crypto_keypair_load_key (X509_KEYPAIR *keypair, const char *file, CRYPTO_PEM_PASSWD_CB *pem_callback, const void *pem_userdata);
167 void crypto_keypair_free (X509_KEYPAIR *keypair);
168 int crypto_default_pem_callback (char *buf, int size, const void *userdata);
169 const char * crypto_digest_name (DIGEST *digest);
170 crypto_digest_t crypto_digest_stream_type (int stream);
171 const char * crypto_strerror (crypto_error_t error);
174 void daemon_start ();
177 uint64_t str_to_uint64(char *str);
178 int64_t str_to_int64(char *str);
179 #define str_to_int32(str) ((int32_t)str_to_int64(str))
180 char * edit_uint64_with_commas (uint64_t val, char *buf);
181 char * edit_uint64_with_suffix (uint64_t val, char *buf);
182 char * add_commas (char *val, char *buf);
183 char * edit_uint64 (uint64_t val, char *buf);
184 char * edit_int64 (int64_t val, char *buf);
185 char * edit_int64_with_commas (int64_t val, char *buf);
186 bool duration_to_utime (char *str, utime_t *value);
187 bool size_to_uint64(char *str, int str_len, uint64_t *rtn_value);
188 bool speed_to_uint64(char *str, int str_len, uint64_t *rtn_value);
189 char *edit_utime (utime_t val, char *buf, int buf_len);
190 bool is_a_number (const char *num);
191 bool is_a_number_list (const char *n);
192 bool is_an_integer (const char *n);
193 bool is_name_valid (const char *name, POOLMEM **msg);
195 /* jcr.c (most definitions are in src/jcr.h) */
196 void init_last_jobs_list();
197 void term_last_jobs_list();
198 void lock_last_jobs_list();
199 void unlock_last_jobs_list();
200 bool read_last_jobs_list(int fd, uint64_t addr);
201 uint64_t write_last_jobs_list(int fd, uint64_t addr);
202 void write_state_file(char *dir, const char *progname, int port);
203 void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr,void *), void *ctx);
206 JCR *jcr_walk_start();
207 JCR *jcr_walk_next(JCR *prev_jcr);
208 void jcr_walk_end(JCR *jcr);
210 JCR *get_jcr_from_tsd();
211 void set_jcr_in_tsd(JCR *jcr);
212 void remove_jcr_from_tsd(JCR *jcr);
213 uint32_t get_jobid_from_tsd();
214 uint32_t get_jobid_from_tid(pthread_t tid);
218 LEX * lex_close_file (LEX *lf);
219 LEX * lex_open_file (LEX *lf, const char *fname, LEX_ERROR_HANDLER *scan_error);
220 int lex_get_char (LEX *lf);
221 void lex_unget_char (LEX *lf);
222 const char * lex_tok_to_str (int token);
223 int lex_get_token (LEX *lf, int expect);
224 void lex_set_default_error_handler (LEX *lf);
225 int lex_set_error_handler_error_type (LEX *lf, int err_type);
228 void my_name_is (int argc, char *argv[], const char *name);
229 void init_msg (JCR *jcr, MSGS *msg);
230 void term_msg (void);
231 void close_msg (JCR *jcr);
232 void add_msg_dest (MSGS *msg, int dest, int type, char *where, char *dest_code);
233 void rem_msg_dest (MSGS *msg, int dest, int type, char *where);
234 void Jmsg (JCR *jcr, int type, utime_t mtime, const char *fmt, ...);
235 void dispatch_message (JCR *jcr, int type, utime_t mtime, char *buf);
236 void init_console_msg (const char *wd);
237 void free_msgs_res (MSGS *msgs);
238 void dequeue_messages (JCR *jcr);
239 void set_trace (int trace_flag);
240 void set_db_type (const char *name);
241 void register_message_callback(void msg_callback(int type, char *msg));
244 void bnet_thread_server(dlist *addr, int max_clients, workq_t *client_wq,
245 void *handle_client_request(void *bsock));
246 void bnet_stop_thread_server(pthread_t tid);
247 void bnet_server (int port, void handle_client_request(BSOCK *bsock));
248 int net_connect (int port);
249 BSOCK * bnet_bind (int port);
250 BSOCK * bnet_accept (BSOCK *bsock, char *who);
253 typedef int (EVENT_HANDLER)(JCR *jcr, const char *event);
254 //EVENT_HANDLER *generate_daemon_event;
255 int generate_daemon_event(JCR *jcr, const char *event);
258 void init_signals (void terminate(int sig));
259 void init_stack_dump (void);
262 void strip_leading_space (char *str);
263 void strip_trailing_junk (char *str);
264 void strip_trailing_newline (char *str);
266 void strip_trailing_slashes (char *dir);
267 bool skip_spaces (char **msg);
268 bool skip_nonspaces (char **msg);
269 int fstrsch (const char *a, const char *b);
270 char *next_arg(char **s);
271 int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc,
272 char **argk, char **argv, int max_args);
273 int parse_args_only(POOLMEM *cmd, POOLMEM **args, int *argc,
274 char **argk, char **argv, int max_args);
275 void split_path_and_filename(const char *fname, POOLMEM **path,
276 int *pnl, POOLMEM **file, int *fnl);
277 int bsscanf(const char *buf, const char *fmt, ...);
281 TLS_CONTEXT *new_tls_context (const char *ca_certfile,
282 const char *ca_certdir,
283 const char *certfile,
285 CRYPTO_PEM_PASSWD_CB *pem_callback,
286 const void *pem_userdata,
289 void free_tls_context (TLS_CONTEXT *ctx);
291 bool tls_postconnect_verify_host(JCR *jcr, TLS_CONNECTION *tls,
293 bool tls_postconnect_verify_cn(JCR *jcr, TLS_CONNECTION *tls,
295 TLS_CONNECTION *new_tls_connection (TLS_CONTEXT *ctx, int fd);
296 bool tls_bsock_accept (BSOCK *bsock);
297 int tls_bsock_writen (BSOCK *bsock, char *ptr, int32_t nbytes);
298 int tls_bsock_readn (BSOCK *bsock, char *ptr, int32_t nbytes);
299 #endif /* HAVE_TLS */
300 bool tls_bsock_connect (BSOCK *bsock);
301 void tls_bsock_shutdown (BSOCK *bsock);
302 void free_tls_connection (TLS_CONNECTION *tls);
303 bool get_tls_require (TLS_CONTEXT *ctx);
304 bool get_tls_enable (TLS_CONTEXT *ctx);
310 typedef char *(*job_code_callback_t)(JCR *, const char *);
313 bool is_buf_zero (char *buf, int len);
314 void lcase (char *str);
315 void bash_spaces (char *str);
316 void bash_spaces (POOL_MEM &pm);
317 void unbash_spaces (char *str);
318 void unbash_spaces (POOL_MEM &pm);
319 char * encode_time (utime_t time, char *buf);
320 char * encode_mode (mode_t mode, char *buf);
321 int do_shell_expansion (char *name, int name_len);
322 void jobstatus_to_ascii (int JobStatus, char *msg, int maxlen);
323 void jobstatus_to_ascii_gui (int JobStatus, char *msg, int maxlen);
324 int run_program (char *prog, int wait, POOLMEM *&results);
325 int run_program_full_output (char *prog, int wait, POOLMEM *&results);
326 char * action_on_purge_to_string(int aop, POOL_MEM &ret);
327 const char * job_type_to_str (int type);
328 const char * job_status_to_str (int stat);
329 const char * job_level_to_str (int level);
330 const char * volume_status_to_str (const char *status);
331 void make_session_key (char *key, char *seed, int mode);
332 void encode_session_key (char *encode, char *session, char *key, int maxlen);
333 void decode_session_key (char *decode, char *session, char *key, int maxlen);
334 POOLMEM * edit_job_codes (JCR *jcr, char *omsg, char *imsg, const char *to, job_code_callback_t job_code_callback = NULL);
335 void set_working_directory (char *wd);
336 const char * last_path_separator (const char *str);
340 int start_watchdog(void);
341 int stop_watchdog(void);
342 watchdog_t *new_watchdog(void);
343 bool register_watchdog(watchdog_t *wd);
344 bool unregister_watchdog(watchdog_t *wd);
348 btimer_t *start_child_timer(JCR *jcr, pid_t pid, uint32_t wait);
349 void stop_child_timer(btimer_t *wid);
350 btimer_t *start_thread_timer(JCR *jcr, pthread_t tid, uint32_t wait);
351 void stop_thread_timer(btimer_t *wid);
352 btimer_t *start_bsock_timer(BSOCK *bs, uint32_t wait);
353 void stop_bsock_timer(btimer_t *wid);
355 #endif /* __LIBPROTOS_H */