1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Samba system utilities
|
---|
4 | Copyright (C) Andrew Tridgell 1992-1998
|
---|
5 | Copyright (C) Jeremy Allison 1998-2002
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 | #include "system/network.h"
|
---|
23 | #include "system/filesys.h"
|
---|
24 |
|
---|
25 | /*
|
---|
26 | The idea is that this file will eventually have wrappers around all
|
---|
27 | important system calls in samba. The aims are:
|
---|
28 |
|
---|
29 | - to enable easier porting by putting OS dependent stuff in here
|
---|
30 |
|
---|
31 | - to allow for hooks into other "pseudo-filesystems"
|
---|
32 |
|
---|
33 | - to allow easier integration of things like the japanese extensions
|
---|
34 |
|
---|
35 | - to support the philosophy of Samba to expose the features of
|
---|
36 | the OS within the SMB model. In general whatever file/printer/variable
|
---|
37 | expansions/etc make sense to the OS should be acceptable to Samba.
|
---|
38 | */
|
---|
39 |
|
---|
40 | /**************************************************************************
|
---|
41 | A wrapper for gethostbyname() that tries avoids looking up hostnames
|
---|
42 | in the root domain, which can cause dial-on-demand links to come up for no
|
---|
43 | apparent reason.
|
---|
44 | ****************************************************************************/
|
---|
45 |
|
---|
46 | _PUBLIC_ struct hostent *sys_gethostbyname(const char *name)
|
---|
47 | {
|
---|
48 | #ifdef REDUCE_ROOT_DNS_LOOKUPS
|
---|
49 | char query[256], hostname[256];
|
---|
50 | char *domain;
|
---|
51 |
|
---|
52 | /* Does this name have any dots in it? If so, make no change */
|
---|
53 |
|
---|
54 | if (strchr(name, '.'))
|
---|
55 | return(gethostbyname(name));
|
---|
56 |
|
---|
57 | /* Get my hostname, which should have domain name
|
---|
58 | attached. If not, just do the gethostname on the
|
---|
59 | original string.
|
---|
60 | */
|
---|
61 |
|
---|
62 | gethostname(hostname, sizeof(hostname) - 1);
|
---|
63 | hostname[sizeof(hostname) - 1] = 0;
|
---|
64 | if ((domain = strchr(hostname, '.')) == NULL)
|
---|
65 | return(gethostbyname(name));
|
---|
66 |
|
---|
67 | /* Attach domain name to query and do modified query.
|
---|
68 | If names too large, just do gethostname on the
|
---|
69 | original string.
|
---|
70 | */
|
---|
71 |
|
---|
72 | if((strlen(name) + strlen(domain)) >= sizeof(query))
|
---|
73 | return(gethostbyname(name));
|
---|
74 |
|
---|
75 | slprintf(query, sizeof(query)-1, "%s%s", name, domain);
|
---|
76 | return(gethostbyname(query));
|
---|
77 | #else /* REDUCE_ROOT_DNS_LOOKUPS */
|
---|
78 | return(gethostbyname(name));
|
---|
79 | #endif /* REDUCE_ROOT_DNS_LOOKUPS */
|
---|
80 | }
|
---|
81 |
|
---|
82 | _PUBLIC_ struct in_addr sys_inet_makeaddr(int net, int host)
|
---|
83 | {
|
---|
84 | struct in_addr in;
|
---|
85 | struct in_addr in2;
|
---|
86 | in = inet_makeaddr(net, host);
|
---|
87 | in2.s_addr = in.s_addr;
|
---|
88 | return in2;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**************************************************************************
|
---|
92 | Wrapper for fork. Ensures we clear our pid cache.
|
---|
93 | ****************************************************************************/
|
---|
94 |
|
---|
95 | static pid_t mypid = (pid_t)-1;
|
---|
96 |
|
---|
97 | _PUBLIC_ pid_t sys_fork(void)
|
---|
98 | {
|
---|
99 | pid_t forkret = fork();
|
---|
100 |
|
---|
101 | if (forkret == (pid_t)0) {
|
---|
102 | /* Child - reset mypid so sys_getpid does a system call. */
|
---|
103 | mypid = (pid_t) -1;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return forkret;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**************************************************************************
|
---|
110 | Wrapper for getpid. Ensures we only do a system call *once*.
|
---|
111 | ****************************************************************************/
|
---|
112 |
|
---|
113 | _PUBLIC_ pid_t sys_getpid(void)
|
---|
114 | {
|
---|
115 | if (mypid == (pid_t)-1)
|
---|
116 | mypid = getpid();
|
---|
117 |
|
---|
118 | return mypid;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | _PUBLIC_ int sys_getpeereid( int s, uid_t *uid)
|
---|
123 | {
|
---|
124 | #if defined(HAVE_PEERCRED)
|
---|
125 | struct ucred cred;
|
---|
126 | socklen_t cred_len = sizeof(struct ucred);
|
---|
127 | int ret;
|
---|
128 |
|
---|
129 | ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
|
---|
130 | if (ret != 0) {
|
---|
131 | return -1;
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (cred_len != sizeof(struct ucred)) {
|
---|
135 | errno = EINVAL;
|
---|
136 | return -1;
|
---|
137 | }
|
---|
138 |
|
---|
139 | *uid = cred.uid;
|
---|
140 | return 0;
|
---|
141 | #else
|
---|
142 | #if defined(HAVE_GETPEEREID)
|
---|
143 | gid_t gid;
|
---|
144 | return getpeereid(s, uid, &gid);
|
---|
145 | #endif
|
---|
146 | errno = ENOSYS;
|
---|
147 | return -1;
|
---|
148 | #endif
|
---|
149 | }
|
---|
150 |
|
---|
151 | _PUBLIC_ int sys_getnameinfo(const struct sockaddr *psa,
|
---|
152 | int salen,
|
---|
153 | char *host,
|
---|
154 | size_t hostlen,
|
---|
155 | char *service,
|
---|
156 | size_t servlen,
|
---|
157 | int flags)
|
---|
158 | {
|
---|
159 | /*
|
---|
160 | * For Solaris we must make sure salen is the
|
---|
161 | * correct length for the incoming sa_family.
|
---|
162 | */
|
---|
163 |
|
---|
164 | if (salen == sizeof(struct sockaddr_storage)) {
|
---|
165 | salen = sizeof(struct sockaddr_in);
|
---|
166 | #if defined(HAVE_IPV6)
|
---|
167 | if (psa->sa_family == AF_INET6) {
|
---|
168 | salen = sizeof(struct sockaddr_in6);
|
---|
169 | }
|
---|
170 | #endif
|
---|
171 | }
|
---|
172 | return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
|
---|
173 | }
|
---|
174 |
|
---|
175 | _PUBLIC_ int sys_connect(int fd, const struct sockaddr * addr)
|
---|
176 | {
|
---|
177 | socklen_t salen = (socklen_t)-1;
|
---|
178 |
|
---|
179 | if (addr->sa_family == AF_INET) {
|
---|
180 | salen = sizeof(struct sockaddr_in);
|
---|
181 | } else if (addr->sa_family == AF_UNIX) {
|
---|
182 | salen = sizeof(struct sockaddr_un);
|
---|
183 | }
|
---|
184 | #if defined(HAVE_IPV6)
|
---|
185 | else if (addr->sa_family == AF_INET6) {
|
---|
186 | salen = sizeof(struct sockaddr_in6);
|
---|
187 | }
|
---|
188 | #endif
|
---|
189 |
|
---|
190 | return connect(fd, addr, salen);
|
---|
191 | }
|
---|