Skip to content

Commit

Permalink
- crashd: Adding OSX Makefile, using POSIX utmpx rather than utmp
Browse files Browse the repository at this point in the history
  • Loading branch information
stealth committed Mar 7, 2014
1 parent 7f54e76 commit 1fbf0ee
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 108 deletions.
10 changes: 8 additions & 2 deletions Makefile
Expand Up @@ -48,11 +48,17 @@ keys:
openssl genrsa -out authkey.priv -aes256 4096
openssl rsa -in authkey.priv -pubout -out authkey.pub
@echo
@echo
@echo "Your serverkey is in serverkey.{priv,pub} and authentication (user-) key in"
@echo "authkey.{priv,pub}. Copy authkey.pub to ~./crash/authorized_keys on server"
@echo "authkey.{priv,pub}. Copy authkey.pub to ~/.crash/authorized_keys on remote server"
@evho "and use '-i authkey.priv' on the client to connect to it"
@echo
@echo "Your known-host key which belongs to serverkey.priv is in HK_127.0.0.1"
@echo "you can use it with 'crashc -K'"
@echo "and you can use it with '-K HK_127.0.0.1' on the client."
@echo
@echo "For example you can start './crashd' as root on localhost and use"
@echo "'./crashc -K ./HK_127.0.0.1 -H 127.0.0.1 -l user -i authkey.priv'"
@echo "to login."
@echo

crashc: net.o misc.o crashc.o config.o global.o
Expand Down
53 changes: 53 additions & 0 deletions Makefile.osx
@@ -0,0 +1,53 @@
CXX=c++
CFLAGS=-w -O2 -ansi -DHAVE_UNIX98 -pedantic -std=c++11
INC=
LD=c++
STRIP=strip
LIBS=-lssl -lcrypto

all: crashd crashc

clean:
rm -rf *.o

crashc: net.o misc.o crashc.o config.o global.o
$(LD) net.o misc.o crashc.o config.o pty.o global.o pty98.o $(LIBS) -o crashc
$(STRIP) crashc

crashd: server.o session.o net.o misc.o crashd.o config.o pty.o pty98.o global.o log.o
$(LD) server.o session.o net.o misc.o crashd.o config.o pty.o pty98.o global.o log.o $(LIBS) -o crashd
$(STRIP) crashd

server.o: server.cc
$(CXX) $(CFLAGS) -c server.cc

session.o: session.cc
$(CXX) $(CFLAGS) -c session.cc

net.o: net.cc
$(CXX) $(CFLAGS) -c net.cc

misc.o: misc.cc
$(CXX) $(CFLAGS) -c misc.cc

config.o: config.cc
$(CXX) $(CFLAGS) -c config.cc

pty.o: pty.cc
$(CXX) $(CFLAGS) -c pty.cc

pty98.o: pty98.cc
$(CXX) $(CFLAGS) -c pty98.cc

global.o: global.cc
$(CXX) $(CFLAGS) -c global.cc

log.o: log.cc
$(CXX) $(CFLAGS) -c log.cc

crashd.o: crashd.cc
$(CXX) $(CFLAGS) -c crashd.cc

crashc.o: crashc.cc
$(CXX) $(CFLAGS) -c crashc.cc

130 changes: 24 additions & 106 deletions log.cc
Expand Up @@ -37,7 +37,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <utmp.h>
#include <utmpx.h>
#include <fcntl.h>
#include "log.h"
#include "config.h"
Expand Down Expand Up @@ -86,56 +86,29 @@ void logger::login(const string &dev, const string &user, const string &host)
if (config::silent)
return;

struct utmp ut;
memset(&ut, 0, sizeof(ut));
#ifndef __FreeBSD__
ut.ut_pid = getpid();
ut.ut_type = USER_PROCESS;
#endif
struct utmpx utx;
memset(&utx, 0, sizeof(utx));

utx.ut_pid = getpid();
utx.ut_type = USER_PROCESS;

const char *ptr = NULL;
if (strstr(dev.c_str(), "/dev/"))
ptr = dev.c_str() + 5;
else
ptr = dev.c_str();

snprintf(ut.ut_line, sizeof(ut.ut_line), "%s", ptr);
snprintf(ut.ut_name, sizeof(ut.ut_name), "%s", user.c_str());
#ifndef __FreeBSD__
snprintf(ut.ut_id, sizeof(ut.ut_id), "%04x", ut.ut_pid);
#endif
#ifndef __sun__
snprintf(ut.ut_host, sizeof(ut.ut_host), "%s", host.c_str());
#endif
snprintf(utx.ut_line, sizeof(utx.ut_line), "%s", ptr);
snprintf(utx.ut_user, sizeof(utx.ut_user), "%s", user.c_str());
snprintf(utx.ut_id, sizeof(utx.ut_id), "%04x", utx.ut_pid);

struct timeval tv;
timeval tv;
gettimeofday(&tv, NULL);
ut.ut_time = tv.tv_sec;
memcpy(&utx.ut_tv, &tv, sizeof(tv));

#ifdef __sun__
utmpname("/var/adm/utmpx");
#else
#if !defined __FreeBSD__
utmpname("/var/run/utmp");
#endif
#endif

#ifdef __FreeBSD__
::login(&ut);
#else
setutent();
pututline(&ut);
endutent();
#endif

#ifdef __sun__
int fd = open("/var/adm/wtmpx", O_WRONLY|O_APPEND);
#else
int fd = open("/var/log/wtmp", O_WRONLY|O_APPEND);
#endif
if (fd < 0)
return;
write(fd, &ut, sizeof(struct utmp));
close(fd);
setutxent();
pututxline(&utx);
endutxent();
}

#endif
Expand All @@ -152,80 +125,25 @@ void logger::logout(pid_t pid)
if (config::silent)
return;

struct timeval tv;
gettimeofday(&tv, NULL);

struct utmp ut;
memset(&ut, 0, sizeof(ut));

#ifdef __sun__
utmpname("/var/adm/utmpx");
#else
#if !defined __FreeBSD__
utmpname("/var/run/utmp");
#endif
#endif

#ifndef __FreeBSD__
setutent();
struct utmp *t = NULL;
timeval tv;
setutxent();
struct utmpx *t = NULL;
for (;;) {
#if !defined __sun__ && !defined EMBEDDED
getutent_r(&ut, &t);
#else
t = getutent();
#endif
if (!t)
break;
if (t->ut_pid != pid || t->ut_type != USER_PROCESS)
continue;
t->ut_type = DEAD_PROCESS;
t->ut_time = tv.tv_sec;
memset(t->ut_name, 0, sizeof(t->ut_name));
setutent();
pututline(t);
break;
}
endutent();
t = getutxent();

#ifdef __sun__
utmpname("/var/run/wtmpx");
#else
utmpname("/var/log/wtmp");
#endif
setutent();
for (;;) {
#if !defined __sun__ && !defined EMBEDDED
getutent_r(&ut, &t);
#else
t = getutent();
#endif
if (!t)
break;
if (t->ut_pid != pid || t->ut_type != USER_PROCESS)
continue;
t->ut_type = DEAD_PROCESS;
t->ut_time = tv.tv_sec;
t->ut_exit.e_termination = 0;
t->ut_exit.e_exit = 0;
memset(t->ut_name, 0, sizeof(t->ut_name));
#ifndef __sun__
memset(t->ut_host, 0, sizeof(t->ut_host));
#endif
gettimeofday(&tv, NULL);
memcpy(&t->ut_tv, &tv, sizeof(tv));
memset(t->ut_user, 0, sizeof(t->ut_user));
pututxline(t);
break;
}
endutent();
endutxent();

#ifdef __sun__
int fd = open("/var/adm/wtmpx", O_WRONLY|O_APPEND);
#else
int fd = open("/var/log/wtmp", O_WRONLY|O_APPEND);
#endif
if (fd < 0)
return;
write(fd, t, sizeof(struct utmp));
close(fd);
#endif // ! __FreeBSD__
}

#endif
Expand Down

0 comments on commit 1fbf0ee

Please sign in to comment.