--- embedtools/src/cfexec.c 2014/02/05 15:44:05 1.5 +++ embedtools/src/cfexec.c 2017/10/08 22:34:41 1.5.20.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ - * $Id: cfexec.c,v 1.5 2014/02/05 15:44:05 misho Exp $ + * $Id: cfexec.c,v 1.5.20.5 2017/10/08 22:34:41 misho Exp $ * ************************************************************************* The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004 - 2014 +Copyright 2004 - 2017 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -49,7 +49,7 @@ SUCH DAMAGE. cfg_root_t cfg; int Verbose, Timeout, kq; ait_val_t User, Mount, Dev, Chroot; -char szSess[MAXPATHLEN], szConfig[MAXPATHLEN]; +char szSess[MAXPATHLEN], szSLCK[MAXPATHLEN], szConfig[MAXPATHLEN]; extern char compiled[], compiledby[], compilehost[]; @@ -66,6 +66,8 @@ Usage() "\t-d \tOther device [default=/dev/ufs/elwix]\n" "\t-m \tOther mount dir [default=/]\n" "\t-t \tTimeout for autolock mount dir after seconds [default=300]\n" + "\t-L \tService lock and set RW state of device with reason\n" + "\t-U \t\tService unlock and set RO state of device\n" "\n", compiled, compiledby, compilehost); } @@ -124,18 +126,20 @@ mkevent(struct kevent *chg, struct kevent *evt) } VERB(3) printf("Created lock file %s\n", szSess); - kq = kqueue(); - if (kq == -1) { - printf("Error:: can`t execute safe mount #%d - %s\n", errno, strerror(errno)); - close(f); - unlink(szSess); - return -1; - } else { - memset(chg, 0, sizeof(struct kevent)); - memset(evt, 0, sizeof(struct kevent)); + if (chg && evt) { + kq = kqueue(); + if (kq == -1) { + printf("Error:: can`t execute safe mount #%d - %s\n", errno, strerror(errno)); + close(f); + unlink(szSess); + return -1; + } else { + memset(chg, 0, sizeof(struct kevent)); + memset(evt, 0, sizeof(struct kevent)); - EV_SET(chg, f, EVFILT_VNODE, EV_ADD, NOTE_DELETE | NOTE_RENAME | NOTE_REVOKE, - 0, NULL); + EV_SET(chg, f, EVFILT_VNODE, EV_ADD, NOTE_DELETE | NOTE_RENAME | NOTE_REVOKE, + 0, NULL); + } } return f; @@ -152,16 +156,61 @@ cleanexit() cfgUnloadConfig(&cfg); } +static int +s_unlck() +{ + if (access(szSLCK, F_OK)) + return 2; + if (access(szSess, F_OK) && update(MNT_UPDATE | MNT_RDONLY) == -1) + return 8; + + unlink(szSLCK); + VERB(3) printf("Unlock & deleted service lock file %s\n", szSLCK); + return 0; +} + +static int +s_lck(const char *reason) +{ + int f; + char szStr[STRSIZ]; + + if (!access(szSLCK, F_OK)) { + printf("cfexec already held service lock ...\n"); + return 127; + } + + f = open(szSLCK, O_CREAT | O_WRONLY | O_TRUNC, 0644); + if (f == -1) { + printf("Error:: can`t service lock session #%d - %s\n", errno, strerror(errno)); + return 4; + } else { + memset(szStr, 0, sizeof szStr); + snprintf(szStr, sizeof szStr, "[%d] - %s", getpid(), reason); + write(f, szStr, strlen(szStr)); + } + close(f); + + if (update(MNT_UPDATE) == -1) { + unlink(szSLCK); + return 5; + } + + VERB(3) printf("Lock & created service lock file %s\n", szSLCK); + return 0; +} + + int main(int argc, char **argv) { - char ch; + char ch, mod = 0, reason[STRSIZ]; const char *err = NULL; struct kevent chg, evt; struct timespec ts; pid_t pid; - int f, stat = 0; + int f, ret = 0, stat = 0; strlcpy(szConfig, DEFAULT_CONFIG, MAXPATHLEN); /* Load variables from config if exists */ @@ -195,7 +244,7 @@ main(int argc, char **argv) atexit(cleanexit); /* Load variables from arguments if exists */ - while ((ch = getopt(argc, argv, "hvu:c:d:m:t:")) != -1) + while ((ch = getopt(argc, argv, "hvUu:c:d:m:t:L:")) != -1) switch (ch) { case 'v': Verbose++; @@ -212,6 +261,13 @@ main(int argc, char **argv) case 'm': AIT_SET_STR(&Mount, optarg); break; + case 'L': + strlcpy(reason, optarg, sizeof reason); + mod = 1; + break; + case 'U': + mod = -1; + break; case 't': #ifndef HAVE_STRTONUM Timeout = (int) strtol(optarg, NULL, 0); @@ -234,7 +290,19 @@ main(int argc, char **argv) memset(szSess, 0, MAXPATHLEN); snprintf(szSess, MAXPATHLEN, "%s%s-cfexec.LCK", DEFAULT_TMP, AIT_GET_STR(&Mount)); + memset(szSLCK, 0, MAXPATHLEN); + snprintf(szSLCK, MAXPATHLEN, CFEXEC_SLOCK, DEFAULT_TMP); + /* we have request for service lock! */ + if (mod) { + VERB(3) printf("Info(3):: mode=%hhd\n", mod); + if (mod == -1) + ret = s_unlck(); + else + ret = s_lck(reason); + return ret; + } + VERB(3) printf("Info(3):: Chroot=%s SUID=%s Device=%s Mount=%s Timeout=%d Session=%s\n", AIT_GET_STR(&Chroot), AIT_GET_STR(&User), AIT_GET_STR(&Dev), AIT_GET_STR(&Mount), Timeout, szSess); @@ -254,10 +322,10 @@ main(int argc, char **argv) VERB(5) printf("Info(5):: Go safe mount.\n"); setsid(); - if (update(MNT_UPDATE) == -1) + if ((f = mkevent(&chg, &evt)) == -1) return 4; - if ((f = mkevent(&chg, &evt)) == -1) + if (update(MNT_UPDATE) == -1) return 5; if (Timeout) { @@ -271,10 +339,14 @@ main(int argc, char **argv) stat = 7; break; case 0: + if (!access(szSLCK, F_OK)) { + VERB(1) printf("Timeout reached - service locked\n"); + break; + } VERB(1) printf("Timeout reached - secure mount\n"); default: VERB(1) printf("Lock file is deleted - secure mount\n"); - if (update(MNT_UPDATE | MNT_RDONLY) == -1) + if (access(szSLCK, F_OK) && update(MNT_UPDATE | MNT_RDONLY) == -1) stat = 8; } @@ -291,14 +363,20 @@ main(int argc, char **argv) sigprocmask(SIG_BLOCK, &sig, &oldsig); */ - if (update(MNT_UPDATE) == -1) + if ((f = mkevent(NULL, NULL)) == -1) return 4; + else + close(f); + if (update(MNT_UPDATE) == -1) + return 5; + switch ((pid = vfork())) { case -1: printf("Error:: can`t execute safe mount #%d - %s\n", errno, strerror(errno)); - return 5; + stat = 3; + break; case 0: VERB(5) printf("Go to running process %s\n", *argv); if (chroot(AIT_GET_STR(&Chroot)) == -1) { @@ -311,17 +389,19 @@ main(int argc, char **argv) /* chdir("/"); */ execvp(*argv, argv); } - exit(127); + _exit(127); break; default: waitpid(pid, &stat, 0); + stat = WEXITSTATUS(stat); VERB(3) printf("Return code: %d\n", stat); - if (stat == 32512) - stat = 127; - if (update(MNT_UPDATE | MNT_RDONLY) == -1) - return 8; + if (access(szSLCK, F_OK) && update(MNT_UPDATE | MNT_RDONLY) == -1) + stat = 8; + break; } + + unlink(szSess); } return stat;