2 * $Id: cnid_metad.c,v 1.8 2009-04-21 08:55:44 franklahm Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYING.
10 cnid_dbd metadaemon to start up cnid_dbd upon request from afpd.
14 1. afpd -------> cnid_metad
16 via UNIX domain socket
17 2. cnid_metad -------> cnid_dbd
20 3. cnid_metad -------> cnid_dbd
24 4. afpd -------> cnid_dbd
30 #endif /* HAVE_CONFIG_H */
38 #endif /* HAVE_UNISTD_H */
39 #include <sys/param.h>
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
46 #ifdef HAVE_SYS_TIME_H
49 #ifdef HAVE_SYS_WAIT_H
57 #include <sys/socket.h>
60 #include <sys/ioctl.h>
63 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
64 #endif /* ! WEXITSTATUS */
66 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
67 #endif /* ! WIFEXITED */
69 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
73 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
76 #define WTERMSIG(status) ((status) & 0x7f)
80 #define fork aTaC_fork
83 /* functions for username and group */
90 #define USE_SETRESUID 1
91 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
92 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
97 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
98 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
102 #include <atalk/logger.h>
103 #include <atalk/cnid_dbd_private.h>
105 #include "db_param.h"
108 #define DBHOME ".AppleDB"
113 volatile sig_atomic_t alarmed = 0;
115 #define MAXSPAWN 3 /* Max times respawned in.. */
117 #define DEFAULTHOST "localhost"
118 #define DEFAULTPORT 4700
119 #define TESTTIME 22 /* this much seconds apfd client tries to
120 * to reconnect every 5 secondes, catch it
126 time_t tm; /* When respawned last */
127 int count; /* Times respawned in the last TESTTIME secondes */
129 int control_fd; /* file descriptor to child cnid_dbd process */
132 static struct server srv[MAXVOLS];
134 /* Default logging config: log to syslog with level log_note */
135 static char *logconfig = "default log_note";
137 static struct server *test_usockfn(char *dir)
140 for (i = 0; i < MAXVOLS; i++) {
141 if (srv[i].name && !strcmp(srv[i].name, dir)) {
148 /* -------------------- */
149 static int send_cred(int socket, int fd)
154 struct cmsghdr *cmsgp = NULL;
159 size = CMSG_SPACE(sizeof fd);
162 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
166 memset(&msgh,0,sizeof (msgh));
169 msgh.msg_name = NULL;
170 msgh.msg_namelen = 0;
175 iov[0].iov_base = &er;
176 iov[0].iov_len = sizeof(er);
178 msgh.msg_control = buf;
179 msgh.msg_controllen = size;
181 cmsgp = CMSG_FIRSTHDR(&msgh);
182 cmsgp->cmsg_level = SOL_SOCKET;
183 cmsgp->cmsg_type = SCM_RIGHTS;
184 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
186 *((int *)CMSG_DATA(cmsgp)) = fd;
187 msgh.msg_controllen = cmsgp->cmsg_len;
190 ret = sendmsg(socket,&msgh, 0);
191 } while ( ret == -1 && errno == EINTR );
193 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
201 /* -------------------- */
202 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
212 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: dbdir: '%s', UNIX socket file: '%s'",
215 up = test_usockfn(dbdir);
217 /* we already have a process, send our fd */
218 if (send_cred(up->control_fd, rqstfd) < 0) {
225 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet. Starting one ...");
229 /* find an empty slot */
230 for (i = 0; i < MAXVOLS; i++) {
236 up->name = strdup(dbdir);
241 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
246 /* we have a slot but no process, check for respawn too fast */
247 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
249 (up->count > MAXSPAWN) ) /* ...and already tried to fork too often */
250 return -1; /* just exit, dont sleep, because we might have work to do for another client */
252 if ( t >= (up->tm + TESTTIME) ) { /* "reset" timer and count */
260 Create socketpair for comm between parent and child.
261 We use it to pass fds from connecting afpd processes to our
262 cnid_dbd child via fd passing.
264 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
265 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
269 if ((pid = fork()) < 0) {
270 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
276 * Child. Close descriptors and start the daemon. If it fails
277 * just log it. The client process will fail connecting
284 for (i = 0; i < MAXVOLS; i++) {
285 if (srv[i].pid && up != &srv[i]) {
286 close(srv[i].control_fd);
290 sprintf(buf1, "%i", sv[1]);
291 sprintf(buf2, "%i", rqstfd);
293 if (up->count == MAXSPAWN) {
294 /* there's a pb with the db inform child
295 * it will run recover, delete the db whatever
297 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
298 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, logconfig, NULL);
301 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, logconfig, NULL);
304 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
313 up->control_fd = sv[0];
317 /* ------------------ */
318 static int set_dbdir(char *dbdir, int len)
325 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
326 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
330 if (dbdir[len - 1] != '/') {
334 strcpy(dbdir + len, DBHOME);
335 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
336 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
342 /* ------------------ */
343 uid_t user_to_uid ( username )
346 struct passwd *this_passwd;
348 /* check for anything */
349 if ( !username || strlen ( username ) < 1 ) return 0;
351 /* grab the /etc/passwd record relating to username */
352 this_passwd = getpwnam ( username );
354 /* return false if there is no structure returned */
355 if (this_passwd == NULL) return 0;
357 /* return proper uid */
358 return this_passwd->pw_uid;
362 /* ------------------ */
363 gid_t group_to_gid ( group )
366 struct group *this_group;
368 /* check for anything */
369 if ( !group || strlen ( group ) < 1 ) return 0;
371 /* grab the /etc/groups record relating to group */
372 this_group = getgrnam ( group );
374 /* return false if there is no structure returned */
375 if (this_group == NULL) return 0;
377 /* return proper gid */
378 return this_group->gr_gid;
382 /* ------------------ */
383 void catch_alarm(int sig) {
387 /* ------------------ */
388 int main(int argc, char *argv[])
390 char dbdir[MAXPATHLEN + 1];
394 char *dbdpn = _PATH_CNID_DBD;
395 char *host = DEFAULTHOST;
396 u_int16_t port = DEFAULTPORT;
397 struct db_param *dbp;
406 set_processname("cnid_metad");
408 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:")) != -1 ) {
414 host = strdup(optarg);
417 uid = user_to_uid (optarg);
419 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
424 gid =group_to_gid (optarg);
426 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
434 dbdpn = strdup(optarg);
437 logconfig = strdup(optarg);
448 LOG(log_error, logtype_cnid, "main: bad arguments");
463 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
464 (void)ioctl( i, TIOCNOTTY, 0 );
465 setpgid( 0, getpid());
470 setpgid( 0, getpid());
473 case -1 : /* error */
474 LOG(log_error, logtype_cnid, "detach from terminal: %s", strerror(errno));
476 default : /* server */
481 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
486 LOG(log_info, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
488 if (SWITCH_TO_GID(gid) < 0) {
489 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
494 if (SWITCH_TO_UID(uid) < 0) {
495 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
501 signal(SIGPIPE, SIG_IGN);
502 signal(SIGALRM, catch_alarm);
505 rqstfd = usockfd_check(srvfd, 10000000);
506 /* Collect zombie processes and log what happened to them */
507 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
508 for (i = 0; i < MAXVOLS; i++) {
509 if (srv[i].pid == pid) {
513 close(srv[i].control_fd);
517 if (WIFEXITED(status)) {
518 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
519 pid, WEXITSTATUS(status));
521 else if (WIFSIGNALED(status)) {
522 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
523 pid, WTERMSIG(status));
531 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
532 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
533 alarm(5); /* to prevent read from getting stuck */
534 ret = read(rqstfd, &len, sizeof(int));
538 LOG(log_severe, logtype_cnid, "Read(1) bailed with alarm (timeout)");
547 LOG(log_error, logtype_cnid, "error read: %s", strerror(errno));
550 else if (ret != sizeof(int)) {
551 LOG(log_error, logtype_cnid, "short read: got %d", ret);
555 * checks for buffer overruns. The client libatalk side does it too
556 * before handing the dir path over but who trusts clients?
558 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
559 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
564 actual_len = read(rqstfd, dbdir, len);
568 LOG(log_severe, logtype_cnid, "Read(2) bailed with alarm (timeout)");
571 if (actual_len != len) {
572 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
577 if (set_dbdir(dbdir, len) < 0) {
581 if ((dbp = db_param_read(dbdir, METAD)) == NULL) {
582 LOG(log_error, logtype_cnid, "Error reading config file");
585 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);