summaryrefslogtreecommitdiff
path: root/src/lib/evil/evil_pwd.c
blob: 03e74149ac96a3863dd884649e4f85eb83c2f65a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>
#include <stdio.h>

#define _POSIX
#include <io.h>
#include <lmcons.h>

#include "evil_private.h"
#include "pwd.h"


static struct passwd pw = { NULL, NULL, 0, 0, 0, NULL, NULL, NULL, NULL, 0, 0 };

struct passwd *
getpwnam(const char *n)
{
   static char user_name[UNLEN + 1];
   static char user_gecos[UNLEN + 4];
   TCHAR       name[UNLEN + 1];
   DWORD       length;
   BOOLEAN     res;
#ifdef UNICODE
   char       *a_name;
# endif /* UNICODE */

   if (!n)
     return NULL;

   length = UNLEN + 1;
   res = GetUserName(name, &length);
   if (!res)
     return NULL;

#ifdef UNICODE
   a_name = evil_wchar_to_char(name);
   if (a_name)
     {
        int l;

        l = strlen(a_name);
        if (l >= PATH_MAX)
          l = PATH_MAX;
        memcpy(user_name, a_name, l);
        user_name[l] = '\0';
        free(a_name);
     }
   else
     return NULL;
#else
   memcpy(user_name, name, strlen(name) + 1);
#endif /* UNICODE */

   if (strcmp(n, user_name) != 0)
     return NULL;

   pw.pw_name = user_name;
   snprintf(user_gecos, sizeof(user_gecos), "%s,,,", user_name);
   pw.pw_gecos = user_gecos;
   pw.pw_dir = getenv("USERPROFILE");
   pw.pw_shell = getenv("SHELL");
   if (!pw.pw_shell)
     pw.pw_shell = "cmd.exe";

   return &pw;
}

struct passwd *
getpwuid(uid_t uid)
{
   return getpwnam(getlogin());
   (void)uid;
}