2 * $Id: cnid_metad.c,v 1.1.4.15.2.3 2008-08-31 09:52:14 didg 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>
43 #include <sys/ioctl.h>
46 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
47 #endif /* ! WEXITSTATUS */
49 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
50 #endif /* ! WIFEXITED */
52 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
56 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
59 #define WTERMSIG(status) ((status) & 0x7f)
63 #define fork aTaC_fork
66 /* functions for username and group */
73 #define USE_SETRESUID 1
74 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
75 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
80 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
81 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
85 #include <atalk/logger.h>
86 #include <atalk/cnid_dbd_private.h>
91 #define DBHOME ".AppleDB"
96 volatile sig_atomic_t alarmed = 0;
100 #define MAXSPAWN 3 /* Max times respawned in.. */
102 #define DEFAULTHOST "localhost"
103 #define DEFAULTPORT 4700
104 #define TESTTIME 22 /* this much seconds apfd client tries to
105 * to reconnect every 5 secondes, catch it
111 time_t tm; /* When respawned last */
112 int count; /* Times respawned in the last TESTTIME secondes */
114 int control_fd; /* file descriptor to child cnid_dbd process */
117 static struct server srv[MAXSRV +1];
119 static struct server *test_usockfn(char *dir, char *fn _U_)
122 for (i = 1; i <= MAXSRV; i++) {
123 if (srv[i].name && !strcmp(srv[i].name, dir)) {
130 /* -------------------- */
131 static int send_cred(int socket, int fd)
136 struct cmsghdr *cmsgp = NULL;
141 size = CMSG_SPACE(sizeof fd);
144 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
148 memset(&msgh,0,sizeof (msgh));
151 msgh.msg_name = NULL;
152 msgh.msg_namelen = 0;
157 iov[0].iov_base = &er;
158 iov[0].iov_len = sizeof(er);
160 msgh.msg_control = buf;
161 msgh.msg_controllen = size;
163 cmsgp = CMSG_FIRSTHDR(&msgh);
164 cmsgp->cmsg_level = SOL_SOCKET;
165 cmsgp->cmsg_type = SCM_RIGHTS;
166 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
168 *((int *)CMSG_DATA(cmsgp)) = fd;
169 msgh.msg_controllen = cmsgp->cmsg_len;
172 ret = sendmsg(socket,&msgh, 0);
173 } while ( ret == -1 && errno == EINTR );
175 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
183 /* -------------------- */
184 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
194 up = test_usockfn(dbdir, usockfn);
196 /* we already have a process, send our fd */
197 if (send_cred(up->control_fd, rqstfd) < 0) {
206 /* find an empty slot */
207 for (i = 1; i <= MAXSRV; i++) {
208 if (!srv[i].pid && srv[i].tm + TESTTIME < t) {
215 up->name = strdup(dbdir);
220 LOG(log_error, logtype_cnid, "no free slot");
225 /* we have a slot but no process, check for respawn too fast */
226 if (up->tm + TESTTIME > t) {
237 if (up->count > MAXSPAWN) {
240 LOG(log_error, logtype_cnid, "respawn too fast %s", up->name);
241 /* FIXME should we sleep a little ? */
246 /* create socketpair for comm between parent and child
247 * FIXME Do we really need a permanent pipe between them ?
249 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
250 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
254 if ((pid = fork()) < 0) {
255 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
261 * Child. Close descriptors and start the daemon. If it fails
262 * just log it. The client process will fail connecting
269 for (i = 1; i <= MAXSRV; i++) {
270 if (srv[i].pid && up != &srv[i]) {
271 close(srv[i].control_fd);
275 sprintf(buf1, "%i", sv[1]);
276 sprintf(buf2, "%i", rqstfd);
278 if (up->count == MAXSPAWN) {
279 /* there's a pb with the db inform child
280 * it will run recover, delete the db whatever
282 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
283 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, NULL);
286 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, NULL);
289 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
298 up->control_fd = sv[0];
302 /* ------------------ */
303 static int set_dbdir(char *dbdir, int len)
310 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
311 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
315 if (dbdir[len - 1] != '/') {
319 strcpy(dbdir + len, DBHOME);
320 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
321 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
327 /* ------------------ */
328 uid_t user_to_uid ( username )
331 struct passwd *this_passwd;
333 /* check for anything */
334 if ( !username || strlen ( username ) < 1 ) return 0;
336 /* grab the /etc/passwd record relating to username */
337 this_passwd = getpwnam ( username );
339 /* return false if there is no structure returned */
340 if (this_passwd == NULL) return 0;
342 /* return proper uid */
343 return this_passwd->pw_uid;
347 /* ------------------ */
348 gid_t group_to_gid ( group )
351 struct group *this_group;
353 /* check for anything */
354 if ( !group || strlen ( group ) < 1 ) return 0;
356 /* grab the /etc/groups record relating to group */
357 this_group = getgrnam ( group );
359 /* return false if there is no structure returned */
360 if (this_group == NULL) return 0;
362 /* return proper gid */
363 return this_group->gr_gid;
367 /* ------------------ */
368 void catch_alarm(int sig) {
372 /* ------------------ */
373 int main(int argc, char *argv[])
375 char dbdir[MAXPATHLEN + 1];
379 char *dbdpn = _PATH_CNID_DBD;
380 char *host = DEFAULTHOST;
381 u_int16_t port = DEFAULTPORT;
382 struct db_param *dbp;
391 set_processname("cnid_metad");
393 while (( cc = getopt( argc, argv, "ds:p:h:u:g:")) != -1 ) {
399 host = strdup(optarg);
402 uid = user_to_uid (optarg);
404 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
409 gid =group_to_gid (optarg);
411 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
419 dbdpn = strdup(optarg);
428 LOG(log_error, logtype_cnid, "main: bad arguments");
443 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
444 (void)ioctl( i, TIOCNOTTY, 0 );
445 setpgid( 0, getpid());
450 setpgid( 0, getpid());
453 case -1 : /* error */
454 LOG(log_error, logtype_cnid, "detach from terminal: %s", strerror(errno));
456 default : /* server */
461 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
466 LOG(log_info, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
468 if (SWITCH_TO_GID(gid) < 0) {
469 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
474 if (SWITCH_TO_UID(uid) < 0) {
475 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
481 signal(SIGPIPE, SIG_IGN);
482 signal(SIGALRM, catch_alarm);
485 rqstfd = usockfd_check(srvfd, 10000000);
486 /* Collect zombie processes and log what happened to them */
487 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
488 for (i = 1; i <= MAXSRV; i++) {
489 if (srv[i].pid == pid) {
494 close(srv[i].control_fd);
498 if (WIFEXITED(status)) {
499 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
500 pid, WEXITSTATUS(status));
502 else if (WIFSIGNALED(status)) {
503 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
504 pid, WTERMSIG(status));
512 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
513 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
514 alarm(5); /* to prevent read from getting stuck */
515 ret = read(rqstfd, &len, sizeof(int));
519 LOG(log_severe, logtype_cnid, "Read(1) bailed with alarm (timeout)");
528 LOG(log_error, logtype_cnid, "error read: %s", strerror(errno));
531 else if (ret != sizeof(int)) {
532 LOG(log_error, logtype_cnid, "short read: got %d", ret);
536 * checks for buffer overruns. The client libatalk side does it too
537 * before handing the dir path over but who trusts clients?
539 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
540 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
545 actual_len = read(rqstfd, dbdir, len);
549 LOG(log_severe, logtype_cnid, "Read(2) bailed with alarm (timeout)");
552 if (actual_len != len) {
553 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
558 if (set_dbdir(dbdir, len) < 0) {
562 if ((dbp = db_param_read(dbdir)) == NULL) {
563 LOG(log_error, logtype_cnid, "Error reading config file");
566 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);