2 * $Id: cnid_metad.c,v 1.7 2009-03-31 12:13:00 franklahm 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 /* Default logging config: log to syslog with level log_note */
120 static char *logconfig = "default log_note";
122 static struct server *test_usockfn(char *dir, char *fn _U_)
125 for (i = 1; i <= MAXSRV; i++) {
126 if (srv[i].name && !strcmp(srv[i].name, dir)) {
133 /* -------------------- */
134 static int send_cred(int socket, int fd)
139 struct cmsghdr *cmsgp = NULL;
144 size = CMSG_SPACE(sizeof fd);
147 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
151 memset(&msgh,0,sizeof (msgh));
154 msgh.msg_name = NULL;
155 msgh.msg_namelen = 0;
160 iov[0].iov_base = &er;
161 iov[0].iov_len = sizeof(er);
163 msgh.msg_control = buf;
164 msgh.msg_controllen = size;
166 cmsgp = CMSG_FIRSTHDR(&msgh);
167 cmsgp->cmsg_level = SOL_SOCKET;
168 cmsgp->cmsg_type = SCM_RIGHTS;
169 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
171 *((int *)CMSG_DATA(cmsgp)) = fd;
172 msgh.msg_controllen = cmsgp->cmsg_len;
175 ret = sendmsg(socket,&msgh, 0);
176 } while ( ret == -1 && errno == EINTR );
178 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
186 /* -------------------- */
187 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
197 up = test_usockfn(dbdir, usockfn);
199 /* we already have a process, send our fd */
200 if (send_cred(up->control_fd, rqstfd) < 0) {
209 /* find an empty slot */
210 for (i = 1; i <= MAXSRV; i++) {
211 if (!srv[i].pid && srv[i].tm + TESTTIME < t) {
218 up->name = strdup(dbdir);
223 LOG(log_error, logtype_cnid, "no free slot");
228 /* we have a slot but no process, check for respawn too fast */
229 if (up->tm + TESTTIME > t) {
240 if (up->count > MAXSPAWN) {
243 LOG(log_error, logtype_cnid, "respawn too fast %s", up->name);
244 /* FIXME should we sleep a little ? */
249 /* create socketpair for comm between parent and child
250 * FIXME Do we really need a permanent pipe between them ?
252 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
253 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
257 if ((pid = fork()) < 0) {
258 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
264 * Child. Close descriptors and start the daemon. If it fails
265 * just log it. The client process will fail connecting
272 for (i = 1; i <= MAXSRV; i++) {
273 if (srv[i].pid && up != &srv[i]) {
274 close(srv[i].control_fd);
278 sprintf(buf1, "%i", sv[1]);
279 sprintf(buf2, "%i", rqstfd);
281 if (up->count == MAXSPAWN) {
282 /* there's a pb with the db inform child
283 * it will run recover, delete the db whatever
285 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
286 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, logconfig, NULL);
289 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, logconfig, NULL);
292 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
301 up->control_fd = sv[0];
305 /* ------------------ */
306 static int set_dbdir(char *dbdir, int len)
313 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
314 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
318 if (dbdir[len - 1] != '/') {
322 strcpy(dbdir + len, DBHOME);
323 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
324 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
330 /* ------------------ */
331 uid_t user_to_uid ( username )
334 struct passwd *this_passwd;
336 /* check for anything */
337 if ( !username || strlen ( username ) < 1 ) return 0;
339 /* grab the /etc/passwd record relating to username */
340 this_passwd = getpwnam ( username );
342 /* return false if there is no structure returned */
343 if (this_passwd == NULL) return 0;
345 /* return proper uid */
346 return this_passwd->pw_uid;
350 /* ------------------ */
351 gid_t group_to_gid ( group )
354 struct group *this_group;
356 /* check for anything */
357 if ( !group || strlen ( group ) < 1 ) return 0;
359 /* grab the /etc/groups record relating to group */
360 this_group = getgrnam ( group );
362 /* return false if there is no structure returned */
363 if (this_group == NULL) return 0;
365 /* return proper gid */
366 return this_group->gr_gid;
370 /* ------------------ */
371 void catch_alarm(int sig) {
375 /* ------------------ */
376 int main(int argc, char *argv[])
378 char dbdir[MAXPATHLEN + 1];
382 char *dbdpn = _PATH_CNID_DBD;
383 char *host = DEFAULTHOST;
384 u_int16_t port = DEFAULTPORT;
385 struct db_param *dbp;
394 set_processname("cnid_metad");
396 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:")) != -1 ) {
402 host = strdup(optarg);
405 uid = user_to_uid (optarg);
407 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
412 gid =group_to_gid (optarg);
414 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
422 dbdpn = strdup(optarg);
425 logconfig = strdup(optarg);
436 LOG(log_error, logtype_cnid, "main: bad arguments");
451 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
452 (void)ioctl( i, TIOCNOTTY, 0 );
453 setpgid( 0, getpid());
458 setpgid( 0, getpid());
461 case -1 : /* error */
462 LOG(log_error, logtype_cnid, "detach from terminal: %s", strerror(errno));
464 default : /* server */
469 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
474 LOG(log_info, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
476 if (SWITCH_TO_GID(gid) < 0) {
477 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
482 if (SWITCH_TO_UID(uid) < 0) {
483 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
489 signal(SIGPIPE, SIG_IGN);
490 signal(SIGALRM, catch_alarm);
493 rqstfd = usockfd_check(srvfd, 10000000);
494 /* Collect zombie processes and log what happened to them */
495 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
496 for (i = 1; i <= MAXSRV; i++) {
497 if (srv[i].pid == pid) {
502 close(srv[i].control_fd);
506 if (WIFEXITED(status)) {
507 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
508 pid, WEXITSTATUS(status));
510 else if (WIFSIGNALED(status)) {
511 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
512 pid, WTERMSIG(status));
520 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
521 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
522 alarm(5); /* to prevent read from getting stuck */
523 ret = read(rqstfd, &len, sizeof(int));
527 LOG(log_severe, logtype_cnid, "Read(1) bailed with alarm (timeout)");
536 LOG(log_error, logtype_cnid, "error read: %s", strerror(errno));
539 else if (ret != sizeof(int)) {
540 LOG(log_error, logtype_cnid, "short read: got %d", ret);
544 * checks for buffer overruns. The client libatalk side does it too
545 * before handing the dir path over but who trusts clients?
547 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
548 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
553 actual_len = read(rqstfd, dbdir, len);
557 LOG(log_severe, logtype_cnid, "Read(2) bailed with alarm (timeout)");
560 if (actual_len != len) {
561 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
566 if (set_dbdir(dbdir, len) < 0) {
570 if ((dbp = db_param_read(dbdir)) == NULL) {
571 LOG(log_error, logtype_cnid, "Error reading config file");
574 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);