HEIMDAL: move code from source4/heimdal* to third_party/heimdal*
[samba.git] / third_party / heimdal / lib / roken / test-getuserinfo.c
1 /*
2  * Copyright (c) 2017 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef WIN32
35 #include <err.h>
36 #endif
37 #include "roken.h"
38
39 static void
40 print1(const char *name, const char *tabs, const char *s2)
41 {
42     (void) printf("%s:%s%s\n", name, tabs, s2 ? s2 : "<NULL>");
43 }
44
45 int
46 main(void)
47 {
48     char buf[MAX_PATH * 2];
49 #ifndef WIN32
50     char buf2[MAX_PATH * 2];
51     int ret = 0;
52     if (!issuid() && getuid() != 0) {
53         const char *s, *s2;
54
55         if (getenv("USER") != NULL && strlen(getenv("USER")) != 0 &&
56             (s = roken_get_username(buf, sizeof(buf))) == NULL) {
57             warnx("roken_get_username() returned NULL but $USER is set");
58             ret++;
59         }
60         if (getenv("USER") != NULL && strlen(getenv("USER")) != 0 && s &&
61             strcmp(getenv("USER"), s) != 0) {
62             warnx("roken_get_username() != getenv(\"USER\")");
63             ret++;
64         }
65
66         if (getenv("HOME") != NULL && strlen(getenv("HOME")) != 0 &&
67             (s = roken_get_homedir(buf, sizeof(buf))) == NULL) {
68             warnx("roken_get_homedir() returned NULL but $HOME is set");
69             ret++;
70         }
71         if (getenv("HOME") != NULL && strlen(getenv("HOME")) != 0 && s &&
72             strcmp(getenv("HOME"), s) != 0) {
73             warnx("roken_get_homedir() != getenv(\"HOME\")");
74             ret++;
75         }
76
77         if (getenv("HOME") != NULL && strlen(getenv("HOME")) != 0 && s &&
78             (s2 = roken_get_appdatadir(buf, sizeof(buf))) == NULL) {
79             warnx("roken_get_appdatadir() returned NULL but $HOME is set "
80                   "and roken_get_homedir() returned not-NULL");
81             ret++;
82         }
83         if (getenv("HOME") != NULL && strlen(getenv("HOME")) != 0 &&
84             s && s2 && strcmp(s, s2) != 0) {
85             warnx("roken_get_homedir() != roken_get_appdatadir()");
86             ret++;
87         }
88         if (getenv("SHELL") != NULL && strlen(getenv("SHELL")) != 0 &&
89             strcmp(getenv("SHELL"), roken_get_shell(buf, sizeof(buf))) != 0) {
90             warnx("roken_get_shell() != getenv(\"SHELL\")");
91             ret++;
92         }
93     }
94 #endif
95
96     print1("Username",      "\t",   roken_get_username(buf, sizeof(buf)));
97     print1("Loginname",     "\t",   roken_get_loginname(buf, sizeof(buf)));
98     print1("Home",          "\t\t", roken_get_homedir(buf, sizeof(buf)));
99     print1("Appdatadir",    "\t",   roken_get_appdatadir(buf, sizeof(buf)));
100     print1("Shell",         "\t\t", roken_get_shell(buf, sizeof(buf)));
101
102 #ifndef WIN32
103     if (!issuid() && getuid() != 0) {
104         const char *s, *s2;
105
106         putenv("USER=h5lfoouser");
107         putenv("HOME=/no/such/dir/h5lfoouser");
108         putenv("SHELL=/no/such/shell");
109         if ((s = roken_get_username(buf, sizeof(buf))) == NULL ||
110             strcmp("h5lfoouser", s) != 0) {
111             warnx("roken_get_username() (%s) did not honor $USER", s);
112             ret++;
113         }
114         if ((s = roken_get_homedir(buf, sizeof(buf))) == NULL ||
115             strcmp("/no/such/dir/h5lfoouser", s) != 0) {
116             warnx("roken_get_homedir() (%s) did not honor $HOME", s);
117             ret++;
118         }
119         s = roken_get_homedir(buf, sizeof(buf));
120         s2 = roken_get_appdatadir(buf2, sizeof(buf2));
121         if (strcmp(s, s2) != 0) {
122             warnx("roken_get_homedir() != roken_get_appdatadir() (%s)",
123                   roken_get_appdatadir(buf, sizeof(buf)));
124             ret++;
125         }
126         if ((s = roken_get_shell(buf, sizeof(buf))) == NULL ||
127             strcmp("/no/such/shell", s) != 0) {
128             warnx("roken_get_shell() (%s) did not honor $SHELL", s);
129             ret++;
130         }
131     }
132     return ret;
133 #endif
134     return 0;
135 }