Commits

Maurício Batista committed 055cb99

Adicionada a funcionalidade de alterar o uid real e o uid eferivo

  • Participants
  • Parent commits f4c1b35
  • Branches setuid

Comments (0)

Files changed (4)

-OBJ = builtins.o shell.o shm.o ui.o
+OBJ = builtins.o identity.o shell.o shm.o ui.o
 CC = gcc
-CFLAGS = -march=native -Wall -O2
-LDFLAGS = -Wl,--as-needed -Wl,-O1
+CFLAGS = -march=native -Wall -O0 -g
+LDFLAGS = -Wl,--as-needed -g
 LDLIBS = -lreadline
 
 shm: ${OBJ}
+#include <unistd.h>
+#include <sys/types.h>
+#include <pwd.h>
+
+static uid_t getuserid(char *username);
+
+int change_identity(char *real, char *effective)
+{
+    uid_t ruid, euid;
+
+    ruid = (real) ? getuserid(real) : -1;
+    euid = (effective) ? getuserid(effective) : -1;
+
+    return setreuid(ruid, euid);
+}
+
+uid_t getuserid(char *username)
+{
+    struct passwd *pwd;
+
+    pwd = getpwnam(username);
+    if (pwd)
+        return pwd->pw_uid;
+
+    return -1;
+}
+#ifndef IDENTITY_H
+#define IDENTITY_H
+
+int change_identity(char *real, char *effective);
+
+#endif
 #include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
 #include "ui.h"
+#include "identity.h"
 
-int main(int argc, char *argv[])
+int main(int argc, char **argv)
 {
+    int c;
+    char *real, *effective;
     shell_info_t si;
+
+    opterr = 0;
+    real = NULL;
+    effective = NULL;
+
+    while ((c = getopt(argc, argv, "r:e:")) != -1)
+        switch (c)
+        {
+            case 'r':
+                real = optarg;
+                break;
+            case 'e':
+                effective = optarg;
+            default:
+                break;
+        }
+
+    if (change_identity(real, effective))
+        fprintf(stderr, "%s\n", strerror(errno));
+
     /* inicializa o shell */
     init_shell();
     /* exibe o prompt de comandos */