goutmp/goutmp_bsd.go

137 lines
3.3 KiB
Go
Raw Normal View History

2020-04-26 00:50:02 +00:00
// +build bsd
2018-06-01 20:15:15 +00:00
// Golang bindings for basic login/utmp accounting
2018-06-27 21:57:26 +00:00
package goutmp
2018-06-01 20:15:15 +00:00
//#include <stdio.h>
//#include <stdlib.h>
//#include <sys/file.h>
//#include <string.h>
//#include <unistd.h>
//#include <stdint.h>
//#include <time.h>
//#include <pwd.h>
//
2020-04-26 00:50:02 +00:00
//#include <utmpx.h>
2018-06-01 20:15:15 +00:00
//
//typedef char char_t;
//
2020-04-26 00:50:02 +00:00
//void pututmpx(struct utmpx* entry, char* uname, char* ptsname, char* host) {
2018-06-01 20:15:15 +00:00
// entry->ut_type = USER_PROCESS;
// entry->ut_pid = getpid();
// strcpy(entry->ut_line, ptsname + strlen("/dev/"));
2018-06-01 20:15:15 +00:00
//
// strcpy(entry->ut_id, ptsname + strlen("/dev/pts/"));
//
2020-04-26 00:50:02 +00:00
// //entry->ut_time = time(NULL);
// strcpy(entry->ut_user, uname);
2018-06-01 20:15:15 +00:00
// strcpy(entry->ut_host, host);
2020-04-26 00:50:02 +00:00
// //entry->ut_addr = 0;
// setutxent();
// pututxline(entry);
2018-06-01 20:15:15 +00:00
//}
//
2020-04-26 00:50:02 +00:00
//void unpututmpx(struct utmpx* entry) {
2018-06-01 20:15:15 +00:00
// entry->ut_type = DEAD_PROCESS;
2020-04-26 00:50:02 +00:00
// entry->ut_line[0] = '\0';
// //entry->ut_time = 0;
// entry->ut_user[0] = '\0';
// setutxent();
// pututxline(entry);
2018-06-01 20:15:15 +00:00
//
2020-04-26 00:50:02 +00:00
// endutxent();
2018-06-01 20:15:15 +00:00
//}
//
2020-04-26 00:50:02 +00:00
//#if 0
2018-06-01 20:15:15 +00:00
//int putlastlogentry(int64_t t, int uid, char* line, char* host) {
// int retval = 0;
// FILE *f;
// struct lastlog l;
//
// strncpy(l.ll_line, line, UT_LINESIZE);
// l.ll_line[UT_LINESIZE-1] = '\0';
// strncpy(l.ll_host, host, UT_HOSTSIZE);
// l.ll_host[UT_HOSTSIZE-1] = '\0';
//
// l.ll_time = (time_t)t;
// //printf("l: ll_line '%s', ll_host '%s', ll_time %d\n", l.ll_line, l.ll_host, l.ll_time);
2018-06-01 20:15:15 +00:00
//
// /* Write lastlog entry at fixed offset (uid * sizeof(struct lastlog) */
// if( NULL != (f = fopen("/var/log/lastlog", "rw+")) ) {
// if( !fseek(f, (uid * sizeof(struct lastlog)), SEEK_SET) ) {
// int fd = fileno(f);
// if( write(fd, &l, sizeof(l)) == sizeof(l) ) {
// retval = 1;
2018-06-09 05:55:50 +00:00
// //int32_t stat = system("echo ---- lastlog ----; lastlog");
2018-06-01 20:15:15 +00:00
// }
// }
// fclose(f);
// }
// return retval;
//}
2020-04-26 00:50:02 +00:00
//#else
//int putlastlogentry(int64_t t, int uid, char* line, char* host) {
// return 0;
//}
//#endif
2018-06-01 20:15:15 +00:00
import "C"
import (
2018-06-28 02:08:56 +00:00
"fmt"
"net"
"os/user"
"strings"
"time"
2018-06-01 20:15:15 +00:00
)
// UtmpEntry wraps the C struct utmp
2018-06-09 03:30:00 +00:00
type UtmpEntry struct {
2020-04-26 00:50:02 +00:00
entry C.struct_utmpx
2018-06-09 03:30:00 +00:00
}
2018-06-28 02:08:56 +00:00
// return remote client hostname or IP if host lookup fails
// addr is expected to be of the format given by net.Addr.String()
// eg., "127.0.0.1:80" or "[::1]:80"
2018-06-28 02:08:56 +00:00
func GetHost(addr string) (h string) {
if !strings.Contains(addr, "[") {
h = strings.Split(addr, ":")[0]
2018-06-28 02:08:56 +00:00
} else {
h = strings.Split(strings.Split(addr, "[")[1], "]")[0]
}
hList, e := net.LookupAddr(h)
//fmt.Printf("lookupAddr:%v\n", hList)
if e == nil {
2018-06-28 02:08:56 +00:00
h = hList[0]
}
return
}
2018-06-27 22:18:28 +00:00
// Put a username and the originating host/IP to utmp
func Put_utmp(user, ptsName, host string) UtmpEntry {
var entry UtmpEntry
2018-06-01 20:15:15 +00:00
//log.Println("Put_utmp:host ", host, " user ", user)
2020-04-26 00:50:02 +00:00
C.pututmpx(&entry.entry, C.CString(user), C.CString(ptsName), C.CString(host))
return entry
2018-06-01 20:15:15 +00:00
}
2018-06-27 22:18:28 +00:00
// Remove a username/host entry from utmp
2018-06-09 03:30:00 +00:00
func Unput_utmp(entry UtmpEntry) {
2020-04-26 00:50:02 +00:00
C.unpututmpx(&entry.entry)
2018-06-01 20:15:15 +00:00
}
2018-06-27 22:18:28 +00:00
// Put the login app, username and originating host/IP to lastlog
func Put_lastlog_entry(app, usr, ptsname, host string) {
u, e := user.Lookup(usr)
if e != nil {
return
}
var uid uint32
fmt.Sscanf(u.Uid, "%d", &uid)
2018-06-01 20:15:15 +00:00
t := time.Now().Unix()
_ = C.putlastlogentry(C.int64_t(t), C.int(uid), C.CString(app), C.CString(host))
//stat := C.putlastlogentry(C.int64_t(t), C.int(uid), C.CString(app), C.CString(host))
//fmt.Println("stat was:",stat)
2018-06-01 20:15:15 +00:00
}