chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / src / user.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include "file.h"
24 #include "user.h"
25 #include "util.h"
26 #include <pwd.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 char               *
33 username(int uid)
34 {
35    char               *s;
36    static int          usr_uid = -1;
37    static char        *usr_s = NULL;
38    struct passwd      *pwd;
39
40    if (usr_uid < 0)
41       usr_uid = getuid();
42    if ((uid == usr_uid) && (usr_s))
43       return Estrdup(usr_s);
44    pwd = getpwuid(uid);
45    if (pwd)
46      {
47         s = Estrdup(pwd->pw_name);
48         if (uid == usr_uid)
49            usr_s = Estrdup(s);
50         return s;
51      }
52    return Estrdup("unknown");
53 }
54
55 char               *
56 homedir(int uid)
57 {
58    static int          usr_uid = -1;
59    static char        *usr_s = NULL;
60    char               *s;
61    const char         *ss;
62    struct passwd      *pwd;
63
64    if (usr_uid < 0)
65       usr_uid = getuid();
66    if ((uid == usr_uid) && (usr_s))
67      {
68         return Estrdup(usr_s);
69      }
70    pwd = getpwuid(uid);
71    if (pwd)
72      {
73         s = Estrdup(pwd->pw_dir);
74         if (uid == usr_uid)
75            usr_s = Estrdup(s);
76         return s;
77      }
78    ss = getenv("TMPDIR");
79    if (!ss)
80       ss = "/tmp";
81    return Estrdup(ss);
82 }
83
84 char               *
85 usershell(int uid)
86 {
87    char               *s;
88    static int          usr_uid = -1;
89    static char        *usr_s = NULL;
90    struct passwd      *pwd;
91
92    if (usr_uid < 0)
93       usr_uid = getuid();
94    if ((uid == usr_uid) && (usr_s))
95       return Estrdup(usr_s);
96    pwd = getpwuid(uid);
97    if (pwd)
98      {
99         if (!pwd->pw_shell)
100            return Estrdup("/bin/sh");
101         if (strlen(pwd->pw_shell) < 1)
102            return Estrdup("/bin/sh");
103         if (!(canexec(pwd->pw_shell)))
104            return Estrdup("/bin/sh");
105         s = Estrdup(pwd->pw_shell);
106         if (uid == usr_uid)
107            usr_s = Estrdup(s);
108         return s;
109      }
110    return Estrdup("/bin/sh");
111 }