2 * $Id: cnid_metad.c,v 1.1.4.7 2003-12-01 22:23:12 lenneis Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYRIGHT.
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.. */
99 #define TESTTIME 20 /* this much seconds */
101 #define DEFAULTHOST "localhost"
102 #define DEFAULTPORT 4700
107 time_t tm; /* When respawned last */
108 int count; /* Times respawned in the last TESTTIME secondes */
112 static struct server srv[MAXSRV +1];
114 static struct server *test_usockfn(char *dir, char *fn)
117 for (i = 1; i <= MAXSRV; i++) {
118 if (srv[i].name && !strcmp(srv[i].name, dir)) {
125 /* -------------------- */
126 static int send_cred(int socket, int fd)
131 struct cmsghdr *cmsgp = NULL;
132 char buf[CMSG_SPACE(sizeof fd)];
135 memset(&msgh,0,sizeof (msgh));
136 memset(buf,0,sizeof (buf));
138 msgh.msg_name = NULL;
139 msgh.msg_namelen = 0;
144 iov[0].iov_base = &er;
145 iov[0].iov_len = sizeof(er);
147 msgh.msg_control = buf;
148 msgh.msg_controllen = sizeof(buf);
150 cmsgp = CMSG_FIRSTHDR(&msgh);
151 cmsgp->cmsg_level = SOL_SOCKET;
152 cmsgp->cmsg_type = SCM_RIGHTS;
153 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
155 *((int *)CMSG_DATA(cmsgp)) = fd;
156 msgh.msg_controllen = cmsgp->cmsg_len;
159 ret = sendmsg(socket,&msgh, 0);
160 } while ( ret == -1 && errno == EINTR );
162 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
168 /* -------------------- */
169 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
176 up = test_usockfn(dbdir, usockfn);
178 /* we already have a process, send our fd */
179 if (send_cred(up->sv[0], rqstfd) < 0) {
188 /* find an empty slot */
189 for (i = 1; i <= MAXSRV; i++) {
190 if (!srv[i].pid && srv[i].tm + TESTTIME < t) {
196 up->name = strdup(dbdir);
201 LOG(log_error, logtype_cnid, "no free slot");
206 /* we have a slot but no process, check for respawn too fast */
207 if (up->tm + TESTTIME > t) {
213 if (up->count >= MAXSPAWN) {
215 LOG(log_error, logtype_cnid, "respawn too fast %s", up->name);
216 /* FIXME should we sleep a little ? */
221 /* create socketpair for comm between parent and child
222 * FIXME Do we really need a permanent pipe between them ?
224 if (socketpair(PF_UNIX, SOCK_STREAM, 0, up->sv) < 0) {
225 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
229 if ((pid = fork()) < 0) {
230 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
235 * Child. Close descriptors and start the daemon. If it fails
236 * just log it. The client process will fail connecting
248 if (execlp(dbdpn, dbdpn, dbdir, NULL) < 0) {
249 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
260 /* ------------------ */
261 static int set_dbdir(char *dbdir, int len)
268 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
269 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
273 if (dbdir[len - 1] != '/') {
277 strcpy(dbdir + len, DBHOME);
278 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
279 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
285 /* ------------------ */
286 uid_t user_to_uid ( username )
289 struct passwd *this_passwd;
291 /* check for anything */
292 if ( !username || strlen ( username ) < 1 ) return 0;
294 /* grab the /etc/passwd record relating to username */
295 this_passwd = getpwnam ( username );
297 /* return false if there is no structure returned */
298 if (this_passwd == NULL) return 0;
300 /* return proper uid */
301 return this_passwd->pw_uid;
305 /* ------------------ */
306 gid_t group_to_gid ( group )
309 struct group *this_group;
311 /* check for anything */
312 if ( !group || strlen ( group ) < 1 ) return 0;
314 /* grab the /etc/groups record relating to group */
315 this_group = getgrnam ( group );
317 /* return false if there is no structure returned */
318 if (this_group == NULL) return 0;
320 /* return proper gid */
321 return this_group->gr_gid;
325 /* ------------------ */
326 int main(int argc, char *argv[])
328 char dbdir[MAXPATHLEN + 1];
332 char *dbdpn = _PATH_CNID_DBD;
333 char *host = DEFAULTHOST;
334 int port = DEFAULTPORT;
335 struct db_param *dbp;
344 while (( cc = getopt( argc, argv, "ds:p:h:u:g:")) != -1 ) {
350 host = strdup(optarg);
353 uid = user_to_uid (optarg);
355 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
360 gid =group_to_gid (optarg);
362 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
370 dbdpn = strdup(optarg);
379 LOG(log_error, logtype_cnid, "main: bad arguments");
394 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
395 (void)ioctl( i, TIOCNOTTY, 0 );
396 setpgid( 0, getpid());
401 setpgid( 0, getpid());
404 case -1 : /* error */
405 LOG(log_error, logtype_cnid, "detach from terminal: %s", strerror(errno));
407 default : /* server */
412 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
417 LOG(log_info, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
419 if (SWITCH_TO_GID(gid) < 0) {
420 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
425 if (SWITCH_TO_UID(uid) < 0) {
426 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
432 signal(SIGPIPE, SIG_IGN);
435 /* Collect zombie processes and log what happened to them */
436 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
437 for (i = 1; i <= MAXSRV; i++) {
438 if (srv[i].pid == pid) {
448 if (WIFEXITED(status)) {
449 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
450 pid, WEXITSTATUS(status));
452 else if (WIFSIGNALED(status)) {
453 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
454 pid, WTERMSIG(status));
459 if ((rqstfd = usockfd_check(srvfd, 10000000)) <= 0)
461 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
462 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
463 ret = read(rqstfd, &len, sizeof(int));
469 LOG(log_error, logtype_cnid, "error read: %s", strerror(errno));
472 else if (ret != sizeof(int)) {
473 LOG(log_error, logtype_cnid, "short read: got %d", ret);
477 * checks for buffer overruns. The client libatalk side does it too
478 * before handing the dir path over but who trusts clients?
480 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
481 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
484 if (read(rqstfd, dbdir, len) != len) {
485 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
490 if (set_dbdir(dbdir, len) < 0) {
494 if ((dbp = db_param_read(dbdir)) == NULL) {
495 LOG(log_error, logtype_cnid, "Error reading config file");
498 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);