kissy/kissy.c
2025-11-18 10:55:03 -03:00

105 lines
3.5 KiB
C

// KISSY (Kissing Interface for Sapphic Smooching over ttY) v1.2
/*
Copyright (C) 2025 Magdalunaa <magdalunaa@linuxposting.xyz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <pwd.h>
#include <stdbool.h>
// mreowww meow nya :3
int main (int argc, char **argv) {
// print help
if (argc < 2 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
printf("%s\n", "Usage: kissy [target] <message>\n\n"
"Kissy is a utility that allows you to kiss currently logged in users who allow messages.\n"
"It works by writing to files in /dev/pts. It needs to run as the group who owns said files (usually tty), using setgid (2755 permissions).");
return 0;
}
// are we running with setgid?
struct stat stat_self;
stat("/proc/self/exe", &stat_self);
if (!(stat_self.st_mode & S_ISGID && !(stat_self.st_mode & S_IWOTH))) {
printf("%s\n", "setgid is not set, or this file is world writable.");
return 3;
}
// try to find the target's uid
struct passwd *pw = getpwnam(argv[1]);
if (pw == NULL) {
printf("%s\n", "Cannot find target");
return 1;
}
uid_t target = pw->pw_uid;
// get current username
struct passwd *pw2 = getpwuid(getuid());
if (pw == NULL) {
printf("%s\n", "Cannot find user");
return 2;
}
char* user = pw2->pw_name;
// if we have a second argument, set a custom kiss message
char* kiss;
if (argc >= 3)
kiss = argv[2];
else
kiss = "*mwah*";
bool kissed = false;
// iterate over all files in directory
DIR *dir = opendir("/dev/pts");
while (1) {
struct dirent *ent = readdir(dir);
if (ent == NULL)
break;
char filepath[strlen("/dev/pts/") + strlen(ent->d_name) + 1];
strcpy(filepath, "/dev/pts/");
strcat(filepath, ent->d_name);
struct stat stats;
stat (filepath, &stats);
// find all terminals with the correct user and that are group-writable
if (stats.st_mode & S_IWGRP && stats.st_uid == target) {
kissed = true;
if (stats.st_gid != getegid()) { // are we running as the tty user?
fprintf(stderr, "%s\n", "Mismatch between file owner group and current effective group. Make sure this program is running with setgid.");
} else {
FILE *tty = fopen(filepath, "a"); // write to terminal!
fprintf(tty, "\a\n%s\n--%s\n", kiss, user);
fclose(tty);
}
}
}
// print a message if a suitable terminal couldn't be found
if (!kissed)
fprintf(stderr, "%s\n", "Couldn't find any suitable terminals. User is not logged in or has not enabled messages.");
}