]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/unix.c
Merge master
[netatalk.git] / libatalk / util / unix.c
1 /*
2   Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13 */
14
15 /*!
16  * @file
17  * Netatalk utility functions
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* HAVE_CONFIG_H */
23
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <sys/time.h>
34 #include <time.h>
35
36 #include <atalk/adouble.h>
37 #include <atalk/ea.h>
38 #include <atalk/afp.h>
39 #include <atalk/logger.h>
40 #include <atalk/vfs.h>
41 #include <atalk/util.h>
42 #include <atalk/unix.h>
43 #include <atalk/compat.h>
44
45 /* close all FDs >= a specified value */
46 static void closeall(int fd)
47 {
48     int fdlimit = sysconf(_SC_OPEN_MAX);
49
50     while (fd < fdlimit)
51         close(fd++);
52 }
53
54 /*!
55  * Daemonize
56  *
57  * Fork, exit parent, setsid(), optionally chdir("/"), optionally close all fds
58  *
59  * returns -1 on failure, but you can't do much except exit in that case
60  * since we may already have forked
61  */
62 int daemonize(int nochdir, int noclose)
63 {
64     switch (fork()) {
65     case 0:
66         break;
67     case -1:
68         return -1;
69     default:
70         _exit(0);
71     }
72
73     if (setsid() < 0)
74         return -1;
75
76     switch (fork()) {
77     case 0: 
78         break;
79     case -1:
80         return -1;
81     default:
82         _exit(0);
83     }
84
85     if (!nochdir)
86         chdir("/");
87
88     if (!noclose) {
89         closeall(0);
90         open("/dev/null",O_RDWR);
91         dup(0);
92         dup(0);
93     }
94
95     return 0;
96 }
97
98 static uid_t saved_uid = -1;
99
100 void become_root(void)
101 {
102     saved_uid = geteuid();
103     if (seteuid(0) != 0)
104         AFP_PANIC("Can't seteuid(0)");
105 }
106
107 void unbecome_root(void)
108 {
109     if (saved_uid == -1 || seteuid(saved_uid) < 0)
110         AFP_PANIC("Can't seteuid back");
111     saved_uid = -1;
112 }
113
114 /*!
115  * @brief get cwd in static buffer
116  *
117  * @returns pointer to path or pointer to error messages on error
118  */
119 const char *getcwdpath(void)
120 {
121     static char cwd[MAXPATHLEN + 1];
122     char *p;
123
124     if ((p = getcwd(cwd, MAXPATHLEN)) != NULL)
125         return p;
126     else
127         return strerror(errno);
128 }
129
130 /*!
131  * @brief Request absolute path
132  *
133  * @returns Absolute filesystem path to object
134  */
135 const char *fullpathname(const char *name)
136 {
137     static char wd[MAXPATHLEN + 1];
138
139     if (name[0] == '/')
140         return name;
141
142     if (getcwd(wd , MAXPATHLEN)) {
143         strlcat(wd, "/", MAXPATHLEN);
144         strlcat(wd, name, MAXPATHLEN);
145     } else {
146         strlcpy(wd, name, MAXPATHLEN);
147     }
148
149     return wd;
150 }
151
152 /*!
153  * Takes a buffer with a path, strips slashs, returns basename
154  *
155  * @param p (rw) path
156  *        path may be
157  *          "[/][dir/[...]]file"
158  *        or
159  *          "[/][dir/[...]]dir/[/]"
160  *        Result is "file" or "dir" 
161  *
162  * @returns pointer to basename in path buffer, buffer is possibly modified
163  */
164 char *stripped_slashes_basename(char *p)
165 {
166     int i = strlen(p) - 1;
167     while (i > 0 && p[i] == '/')
168         p[i--] = 0;
169     return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
170 }
171
172 /*!
173  * @brief symlink safe chdir replacement
174  *
175  * Only chdirs to dir if it doesn't contain symlinks.
176  *
177  * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
178  */
179 int lchdir(const char *dir)
180 {
181     char buf[MAXPATHLEN+1];
182     char cwd[MAXPATHLEN+1];
183     char *test;
184     int  i;
185
186     /*
187      dir is a canonical path (without "../" "./" "//" )
188      but may end with a / 
189     */
190     *cwd = 0;
191     if (*dir != '/') {
192         if (getcwd(cwd, MAXPATHLEN) == NULL)
193             return -1;
194     }
195     if (chdir(dir) != 0)
196         return -1;
197
198     /* 
199      * Cases:
200      * chdir request   | realpath result | ret
201      * (after getwcwd) |                 |
202      * =======================================
203      * /a/b/.          | /a/b            | 0
204      * /a/b/.          | /c              | 1
205      * /a/b/.          | /c/d/e/f        | 1
206      */
207     if (getcwd(buf, MAXPATHLEN) == NULL)
208         return 1;
209
210     i = 0;
211     if (*cwd) {
212         /* relative path requested, 
213          * Same directory?
214         */
215         for (; cwd[i]; i++) {
216             if (buf[i] != cwd[i])
217                 return 1;
218         }
219         if (buf[i]) {
220             if (buf[i] != '/')
221                 return 1;
222             i++;
223         }                    
224     }
225
226     test = &buf[i];    
227     for (i = 0; test[i]; i++) {
228         if (test[i] != dir[i]) {
229             return 1;
230         }
231     }
232     /* trailing '/' ? */
233     if (!dir[i])
234         return 0;
235
236     if (dir[i] != '/')
237         return 1;
238
239     i++;
240     if (dir[i])
241         return 1;
242
243     return 0;
244 }
245
246 /*!
247  * Store n random bytes an buf
248  */
249 void randombytes(void *buf, int n)
250 {
251     char *p = (char *)buf;
252     int fd, i;
253     struct timeval tv;
254
255     if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
256         /* generate from /dev/urandom */
257         if (read(fd, buf, n) != n) {
258             close(fd);
259             fd = -1;
260         } else {
261             close(fd);
262             /* fd now != -1, so srandom wont be called below */
263         }
264     }
265
266     if (fd == -1) {
267         gettimeofday(&tv, NULL);
268         srandom((unsigned int)tv.tv_usec);
269         for (i=0 ; i < n ; i++)
270             p[i] = random() & 0xFF;
271     }
272
273     return;
274 }
275
276 int gmem(gid_t gid, int ngroups, gid_t *groups)
277 {
278     int         i;
279
280     for ( i = 0; i < ngroups; i++ ) {
281         if ( groups[ i ] == gid ) {
282             return( 1 );
283         }
284     }
285     return( 0 );
286 }