diff options
| author | mo khan <mo@mokhan.ca> | 2014-07-23 20:53:58 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-07-23 20:53:58 -0600 |
| commit | c9c862da868fe4f2934c5d3afe13907fe3a52b84 (patch) | |
| tree | 899828655d7f58825e45591ee981c8e1dae3d72d | |
| parent | a7e6b519e7bbbb41d79d844ecfcd5da260979c27 (diff) | |
redo notetaker program using setuid to run as root and insert uid of user running program.
| -rw-r--r-- | hacking.h | 18 | ||||
| -rw-r--r-- | notetaker.c | 58 |
2 files changed, 76 insertions, 0 deletions
diff --git a/hacking.h b/hacking.h new file mode 100644 index 0000000..2c9c805 --- /dev/null +++ b/hacking.h @@ -0,0 +1,18 @@ + +void fatal(char *message) +{ + char error_message[100]; + strcpy(error_message, "[!!] Fatal Error "); + strncat(error_message, message, 83); + perror(error_message); + exit(-1); +} + +void *ec_malloc(unsigned int size) +{ + void *ptr; + ptr = malloc(size); + if (ptr == NULL) + fatal("in ec_malloc() on memory allocation"); + return ptr; +} diff --git a/notetaker.c b/notetaker.c new file mode 100644 index 0000000..a1c58f3 --- /dev/null +++ b/notetaker.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <unistd.h> +#include "hacking.h" + +void usage(const char *program_name, char *filename) +{ + printf("Usage: %s <data to add to %s>\n", program_name, filename); + exit(0); +} + +void fatal(char *); +void *ec_malloc(unsigned int); + +int main(int argc, const char *argv[]) +{ + int userid, fd; + char *buffer, *datafile; + + buffer = (char *) ec_malloc(100); + datafile = (char *) ec_malloc(20); + strcpy(datafile, "/var/notes"); + + if (argc < 2) + usage(argv[0], datafile); + + strcpy(buffer, argv[1]); + + printf("[DEBUG] buffer @ %p: \'%s\'\n", buffer, buffer); + printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile); + + fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR); + if (fd == -1) + fatal("in main() while opening file"); + + printf("[DEBUG] file descriptor is %d\n", fd); + userid = getuid(); + + if (write(fd, &userid, 4) == -1) + fatal("in main() while writing userid to file"); + write(fd, "\n", 1); + + if(write(fd, buffer, strlen(buffer)) == -1) + fatal("in main() while writing buffer to file"); + write(fd, "\n", 1); + + if(close(fd) == -1) + fatal("in main() while closing file"); + + printf("Note has been saved.\n"); + free(buffer); + free(datafile); + + return 0; +} |
