Skip to content

Instantly share code, notes, and snippets.

@Low-power
Created May 8, 2022 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Low-power/015fa2f5099b903f561a0b7ff4ba8a7d to your computer and use it in GitHub Desktop.
Save Low-power/015fa2f5099b903f561a0b7ff4ba8a7d to your computer and use it in GitHub Desktop.
Append an accounting record for last(1) (wtmpx)
/* Copyright 2015-2022 Rivoreo
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <utmpx.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
static void print_usage(const char *name) {
fprintf(stderr, "Usage: %s\n"
" -l <ut_line>\n"
" -u <ut_user>\n"
" [-h <ut_host>]\n"
" [-t <ut_type>]\n"
" [-p <ut_pid>]\n"
" [-f <wtmpx-file-path>]\n", name);
}
static int put_string(char *target, size_t target_size, char *s) {
#ifdef ALLOW_UNTERMINATED_STRING
size_t len = strlen(s);
#else
size_t len = strlen(s) + 1;
#endif
if(len > target_size) {
#ifdef TRUNCATE_LONG_STRING
len = target_size;
#ifndef ALLOW_UNTERMINATED_STRING
s[len - 1] = 0;
#endif
#else
return -1;
#endif
}
memcpy(target, s, len);
#ifdef ALLOW_UNTERMINATED_STRING
// Still zero-terminate the string if target have space
if(len < target_size) target[len] = 0;
#endif
return 0;
}
int main(int argc, char **argv) {
const char *wtmpx_file_path = WTMPX_FILE;
//const char *wtmpx_file_path = "/var/adm/wtmpx";
//const char *wtmpx_file_path = _PATH_WTMPX;
struct timeval tv;
if(gettimeofday(&tv, NULL) < 0) {
perror("gettimeofday");
return 1;
}
struct utmpx utx = {
.ut_type = USER_PROCESS,
.ut_pid = getppid(),
.ut_tv.tv_sec = tv.tv_sec,
.ut_tv.tv_usec = tv.tv_usec
};
uid_t myuid = getuid();
while(1) {
int c = getopt(argc, argv, "f:t:p:l:u:h:");
if(c == -1) break;
switch(c) {
case 'f':
if(myuid && myuid != geteuid() && setuid(myuid) < 0) {
perror("setuid");
return 1;
}
wtmpx_file_path = optarg;
break;
case 't':
if(!isdigit(*optarg)) {
fprintf(stderr, "%s: Invalid ut_type '%s'\n", argv[0], optarg);
return 1;
}
utx.ut_type = atoi(optarg);
break;
case 'p':
if(!isdigit(*optarg)) {
fprintf(stderr, "%s: Invalid ut_pid '%s'\n", argv[0], optarg);
return 1;
}
utx.ut_pid = strtoul(optarg, NULL, 0);
if(utx.ut_pid == LONG_MAX) {
perror("strtoul");
return 1;
}
break;
case 'l':
if(put_string(utx.ut_line, sizeof utx.ut_line, optarg) < 0) {
fprintf(stderr, "%s: ut_line too long\n", argv[0]);
return -1;
}
break;
case 'u':
if(put_string(utx.ut_user, sizeof utx.ut_user, optarg) < 0) {
fprintf(stderr, "%s: ut_user too long\n", argv[0]);
return 1;
}
break;
case 'h':
if(put_string(utx.ut_host, sizeof utx.ut_host, optarg) < 0) {
fprintf(stderr, "%s: ut_host too long\n", argv[0]);
return 1;
}
break;
case '?':
print_usage(argv[0]);
return -1;
}
}
if(!*utx.ut_line || !*utx.ut_user) {
print_usage(argv[0]);
return -1;
}
updwtmpx(wtmpx_file_path, &utx);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment