1 | |
---|
2 | /* |
---|
3 | * SUBS.C |
---|
4 | * |
---|
5 | * Copyright 1994 Matthew Dillon (dillon@apollo.backplane.com) |
---|
6 | * May be distributed under the GNU General Public License |
---|
7 | */ |
---|
8 | |
---|
9 | #include "defs.h" |
---|
10 | |
---|
11 | Prototype void logn(int level, const char *ctl, ...); |
---|
12 | Prototype void log9(const char *ctl, ...); |
---|
13 | Prototype void logfd(int fd, const char *ctl, ...); |
---|
14 | Prototype void fdprintf(int fd, const char *ctl, ...); |
---|
15 | Prototype int ChangeUser(const char *user, short dochdir); |
---|
16 | Prototype void vlog(int level, int fd, const char *ctl, va_list va); |
---|
17 | Prototype int slog(char *buf, const char *ctl, int nmax, va_list va, short useDate); |
---|
18 | |
---|
19 | extern short LogLevel; |
---|
20 | |
---|
21 | void |
---|
22 | log9(const char *ctl, ...) |
---|
23 | { |
---|
24 | va_list va; |
---|
25 | |
---|
26 | va_start(va, ctl); |
---|
27 | vlog(9, 2, ctl, va); |
---|
28 | va_end(va); |
---|
29 | } |
---|
30 | |
---|
31 | void |
---|
32 | logn(int level, const char *ctl, ...) |
---|
33 | { |
---|
34 | va_list va; |
---|
35 | |
---|
36 | va_start(va, ctl); |
---|
37 | vlog(level, 2, ctl, va); |
---|
38 | va_end(va); |
---|
39 | } |
---|
40 | |
---|
41 | void |
---|
42 | logfd(int fd, const char *ctl, ...) |
---|
43 | { |
---|
44 | va_list va; |
---|
45 | |
---|
46 | va_start(va, ctl); |
---|
47 | vlog(9, fd, ctl, va); |
---|
48 | va_end(va); |
---|
49 | } |
---|
50 | |
---|
51 | void |
---|
52 | fdprintf(int fd, const char *ctl, ...) |
---|
53 | { |
---|
54 | va_list va; |
---|
55 | char buf[2048]; |
---|
56 | |
---|
57 | va_start(va, ctl); |
---|
58 | vsnprintf(buf, sizeof(buf), ctl, va); |
---|
59 | write(fd, buf, strlen(buf)); |
---|
60 | va_end(va); |
---|
61 | } |
---|
62 | |
---|
63 | void |
---|
64 | vlog(int level, int fd, const char *ctl, va_list va) |
---|
65 | { |
---|
66 | char buf[2048]; |
---|
67 | short n; |
---|
68 | static short useDate = 1; |
---|
69 | |
---|
70 | if (level >= LogLevel) { |
---|
71 | write(fd, buf, n = slog(buf, ctl, sizeof(buf), va, useDate)); |
---|
72 | useDate = (n && buf[n-1] == '\n'); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | int |
---|
77 | slog(char *buf, const char *ctl, int nmax, va_list va, short useDate) |
---|
78 | { |
---|
79 | time_t t = time(NULL); |
---|
80 | struct tm *tp = localtime(&t); |
---|
81 | |
---|
82 | buf[0] = 0; |
---|
83 | if (useDate) |
---|
84 | strftime(buf, 128, "%d-%b-%y %H:%M ", tp); |
---|
85 | vsnprintf(buf + strlen(buf), nmax, ctl, va); |
---|
86 | return(strlen(buf)); |
---|
87 | } |
---|
88 | |
---|
89 | int |
---|
90 | ChangeUser(const char *user, short dochdir) |
---|
91 | { |
---|
92 | struct passwd *pas; |
---|
93 | |
---|
94 | /* |
---|
95 | * Obtain password entry and change privilages |
---|
96 | */ |
---|
97 | |
---|
98 | if ((pas = getpwnam(user)) == 0) { |
---|
99 | logn(9, "failed to get uid for %s", user); |
---|
100 | return(-1); |
---|
101 | } |
---|
102 | setenv("USER", pas->pw_name, 1); |
---|
103 | setenv("HOME", pas->pw_dir, 1); |
---|
104 | setenv("SHELL", "/bin/sh", 1); |
---|
105 | |
---|
106 | /* |
---|
107 | * Change running state to the user in question |
---|
108 | */ |
---|
109 | |
---|
110 | if (initgroups(user, pas->pw_gid) < 0) { |
---|
111 | logn(9, "initgroups failed: %s %s", user, strerror(errno)); |
---|
112 | return(-1); |
---|
113 | } |
---|
114 | if (setregid(pas->pw_gid, pas->pw_gid) < 0) { |
---|
115 | logn(9, "setregid failed: %s %d", user, pas->pw_gid); |
---|
116 | return(-1); |
---|
117 | } |
---|
118 | if (setreuid(pas->pw_uid, pas->pw_uid) < 0) { |
---|
119 | logn(9, "setreuid failed: %s %d", user, pas->pw_uid); |
---|
120 | return(-1); |
---|
121 | } |
---|
122 | if (dochdir) { |
---|
123 | if (chdir(pas->pw_dir) < 0) { |
---|
124 | logn(8, "chdir failed: %s %s", user, pas->pw_dir); |
---|
125 | if (chdir(TMPDIR) < 0) { |
---|
126 | logn(9, "chdir failed: %s %s", user, pas->pw_dir); |
---|
127 | logn(9, "chdir failed: %s " TMPDIR, user); |
---|
128 | return(-1); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | return(pas->pw_uid); |
---|
133 | } |
---|
134 | |
---|
135 | #if 0 |
---|
136 | |
---|
137 | char * |
---|
138 | strdup(const char *str) |
---|
139 | { |
---|
140 | char *ptr = malloc(strlen(str) + 1); |
---|
141 | |
---|
142 | if (ptr) |
---|
143 | strcpy(ptr, str); |
---|
144 | return(ptr); |
---|
145 | } |
---|
146 | |
---|
147 | #endif |
---|