2 * $Id: cnid_metad.c,v 1.2 2005-04-28 20:49:47 bfernhomberg Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYING.
9 /* cnid_dbd metadaemon to start up cnid_dbd upon request from afpd */
13 #endif /* HAVE_CONFIG_H */
21 #endif /* HAVE_UNISTD_H */
22 #include <sys/param.h>
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
29 #ifdef HAVE_SYS_TIME_H
32 #ifdef HAVE_SYS_WAIT_H
40 #include <sys/socket.h>
45 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
46 #endif /* ! WEXITSTATUS */
48 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
49 #endif /* ! WIFEXITED */
51 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
55 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
58 #define WTERMSIG(status) ((status) & 0x7f)
62 #define fork aTaC_fork
65 /* functions for username and group */
72 #define USE_SETRESUID 1
73 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
74 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
79 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
80 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
84 #include <atalk/logger.h>
85 #include <atalk/cnid_dbd_private.h>
90 #define DBHOME ".AppleDB"
98 #define MAXSPAWN 3 /* Max times respawned in.. */
100 #define DEFAULTHOST "localhost"
101 #define DEFAULTPORT 4700
102 #define TESTTIME 22 /* this much seconds apfd client tries to
103 * to reconnect every 5 secondes, catch it
109 time_t tm; /* When respawned last */
110 int count; /* Times respawned in the last TESTTIME secondes */
112 int control_fd; /* file descriptor to child cnid_dbd process */
115 static struct server srv[MAXSRV +1];
117 static struct server *test_usockfn(char *dir, char *fn _U_)
120 for (i = 1; i <= MAXSRV; i++) {
121 if (srv[i].name && !strcmp(srv[i].name, dir)) {
128 /* -------------------- */
129 static int send_cred(int socket, int fd)
134 struct cmsghdr *cmsgp = NULL;
139 size = CMSG_SPACE(sizeof fd);
142 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
146 memset(&msgh,0,sizeof (msgh));
149 msgh.msg_name = NULL;
150 msgh.msg_namelen = 0;
155 iov[0].iov_base = &er;
156 iov[0].iov_len = sizeof(er);
158 msgh.msg_control = buf;
159 msgh.msg_controllen = size;
161 cmsgp = CMSG_FIRSTHDR(&msgh);
162 cmsgp->cmsg_level = SOL_SOCKET;
163 cmsgp->cmsg_type = SCM_RIGHTS;
164 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
166 *((int *)CMSG_DATA(cmsgp)) = fd;
167 msgh.msg_controllen = cmsgp->cmsg_len;
170 ret = sendmsg(socket,&msgh, 0);
171 } while ( ret == -1 && errno == EINTR );
173 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
181 /* -------------------- */
182 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
192 up = test_usockfn(dbdir, usockfn);
194 /* we already have a process, send our fd */
195 if (send_cred(up->control_fd, rqstfd) < 0) {
204 /* find an empty slot */
205 for (i = 1; i <= MAXSRV; i++) {
206 if (!srv[i].pid && srv[i].tm + TESTTIME < t) {
213 up->name = strdup(dbdir);
218 LOG(log_error, logtype_cnid, "no free slot");
223 /* we have a slot but no process, check for respawn too fast */
224 if (up->tm + TESTTIME > t) {
235 if (up->count > MAXSPAWN) {
238 LOG(log_error, logtype_cnid, "respawn too fast %s", up->name);
239 /* FIXME should we sleep a little ? */
244 /* create socketpair for comm between parent and child
245 * FIXME Do we really need a permanent pipe between them ?
247 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
248 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
252 if ((pid = fork()) < 0) {
253 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
259 * Child. Close descriptors and start the daemon. If it fails
260 * just log it. The client process will fail connecting
267 for (i = 1; i <= MAXSRV; i++) {
268 if (srv[i].pid && up != &srv[i]) {
269 close(srv[i].control_fd);
273 sprintf(buf1, "%i", sv[1]);
274 sprintf(buf2, "%i", rqstfd);
276 if (up->count == MAXSPAWN) {
277 /* there's a pb with the db inform child
278 * it will run recover, delete the db whatever
280 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
281 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, NULL);
284 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, NULL);
287 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
296 up->control_fd = sv[0];
300 /* ------------------ */
301 static int set_dbdir(char *dbdir, int len)
308 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
309 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
313 if (dbdir[len - 1] != '/') {
317 strcpy(dbdir + len, DBHOME);
318 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
319 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
325 /* ------------------ */
326 uid_t user_to_uid ( username )
329 struct passwd *this_passwd;
331 /* check for anything */
332 if ( !username || strlen ( username ) < 1 ) return 0;
334 /* grab the /etc/passwd record relating to username */
335 this_passwd = getpwnam ( username );
337 /* return false if there is no structure returned */
338 if (this_passwd == NULL) return 0;
340 /* return proper uid */
341 return this_passwd->pw_uid;
345 /* ------------------ */
346 gid_t group_to_gid ( group )
349 struct group *this_group;
351 /* check for anything */
352 if ( !group || strlen ( group ) < 1 ) return 0;
354 /* grab the /etc/groups record relating to group */
355 this_group = getgrnam ( group );
357 /* return false if there is no structure returned */
358 if (this_group == NULL) return 0;
360 /* return proper gid */
361 return this_group->gr_gid;
365 /* ------------------ */
366 int main(int argc, char *argv[])
368 char dbdir[MAXPATHLEN + 1];
372 char *dbdpn = _PATH_CNID_DBD;
373 char *host = DEFAULTHOST;
374 u_int16_t port = DEFAULTPORT;
375 struct db_param *dbp;
384 set_processname("cnid_metad");
386 while (( cc = getopt( argc, argv, "ds:p:h:u:g:")) != -1 ) {
392 host = strdup(optarg);
395 uid = user_to_uid (optarg);
397 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
402 gid =group_to_gid (optarg);
404 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
412 dbdpn = strdup(optarg);
421 LOG(log_error, logtype_cnid, "main: bad arguments");
436 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
437 (void)ioctl( i, TIOCNOTTY, 0 );
438 setpgid( 0, getpid());
443 setpgid( 0, getpid());
446 case -1 : /* error */
447 LOG(log_error, logtype_cnid, "detach from terminal: %s", strerror(errno));
449 default : /* server */
454 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
459 LOG(log_info, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
461 if (SWITCH_TO_GID(gid) < 0) {
462 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
467 if (SWITCH_TO_UID(uid) < 0) {
468 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
474 signal(SIGPIPE, SIG_IGN);
477 rqstfd = usockfd_check(srvfd, 10000000);
478 /* Collect zombie processes and log what happened to them */
479 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
480 for (i = 1; i <= MAXSRV; i++) {
481 if (srv[i].pid == pid) {
486 close(srv[i].control_fd);
490 if (WIFEXITED(status)) {
491 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
492 pid, WEXITSTATUS(status));
494 else if (WIFSIGNALED(status)) {
495 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
496 pid, WTERMSIG(status));
503 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
504 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
505 ret = read(rqstfd, &len, sizeof(int));
511 LOG(log_error, logtype_cnid, "error read: %s", strerror(errno));
514 else if (ret != sizeof(int)) {
515 LOG(log_error, logtype_cnid, "short read: got %d", ret);
519 * checks for buffer overruns. The client libatalk side does it too
520 * before handing the dir path over but who trusts clients?
522 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
523 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
526 if (read(rqstfd, dbdir, len) != len) {
527 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
532 if (set_dbdir(dbdir, len) < 0) {
536 if ((dbp = db_param_read(dbdir)) == NULL) {
537 LOG(log_error, logtype_cnid, "Error reading config file");
540 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);