2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 675
18 * Mass Ave, Cambridge, MA 02139, USA.
24 #define MAP_FAILED ((void *)-1)
29 /***************************************************************
30 Signal function to tell us we timed out.
31 ****************************************************************/
33 static void gotalarm_sig(void)
38 /***************************************************************
39 Lock or unlock a fd for a known lock type. Abandon after waitsecs
41 ****************************************************************/
43 BOOL
do_file_lock(int fd
, int waitsecs
, int type
)
45 SMB_STRUCT_FLOCK lock
;
47 void (*oldsig_handler
)(int);
50 oldsig_handler
= CatchSignal(SIGALRM
, SIGNAL_CAST gotalarm_sig
);
53 lock
.l_whence
= SEEK_SET
;
59 /* Note we must *NOT* use sys_fcntl here ! JRA */
60 ret
= fcntl(fd
, SMB_F_SETLKW
, &lock
);
62 CatchSignal(SIGALRM
, SIGNAL_CAST oldsig_handler
);
65 DEBUG(0, ("do_file_lock: failed to %s file.\n",
66 type
== F_UNLCK
? "unlock" : "lock"));
73 /***************************************************************
74 Lock an fd. Abandon after waitsecs seconds.
75 ****************************************************************/
77 BOOL
file_lock(int fd
, int type
, int secs
, int *plock_depth
)
84 if ((*plock_depth
) == 0) {
85 if (!do_file_lock(fd
, secs
, type
)) {
86 DEBUG(10,("file_lock: locking file failed, error = %s.\n", strerror(errno
)));
94 /***************************************************************
95 Unlock an fd. Abandon after waitsecs seconds.
96 ****************************************************************/
98 BOOL
file_unlock(int fd
, int *plock_depth
)
102 if(*plock_depth
== 1) {
103 ret
= do_file_lock(fd
, 5, F_UNLCK
);
109 DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n", strerror(errno
)));
114 /***************************************************************
115 Locks a file for enumeration / modification.
116 update to be set = True if modification is required.
117 ****************************************************************/
119 void *startfilepwent(char *pfile
, char *s_readbuf
, int bufsize
,
120 int *file_lock_depth
, BOOL update
)
125 DEBUG(0, ("startfilepwent: No file set\n"));
128 DEBUG(10, ("startfilepwent: opening file %s\n", pfile
));
130 fp
= sys_fopen(pfile
, update
? "r+b" : "rb");
133 DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile
));
137 /* Set a buffer to do more efficient reads */
138 setvbuf(fp
, s_readbuf
, _IOFBF
, bufsize
);
140 if (!file_lock(fileno(fp
), (update
? F_WRLCK
: F_RDLCK
), 5, file_lock_depth
)) {
141 DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile
));
146 /* Make sure it is only rw by the owner */
149 /* We have a lock on the file. */
153 /***************************************************************
154 End enumeration of the file.
155 ****************************************************************/
157 void endfilepwent(void *vp
, int *file_lock_depth
)
159 FILE *fp
= (FILE *)vp
;
161 file_unlock(fileno(fp
), file_lock_depth
);
163 DEBUG(7, ("endfilepwent: closed file.\n"));
166 /*************************************************************************
167 Return the current position in the file list as an SMB_BIG_UINT.
168 This must be treated as an opaque token.
169 *************************************************************************/
171 SMB_BIG_UINT
getfilepwpos(void *vp
)
173 return (SMB_BIG_UINT
)sys_ftell((FILE *)vp
);
176 /*************************************************************************
177 Set the current position in the file list from an SMB_BIG_UINT.
178 This must be treated as an opaque token.
179 *************************************************************************/
181 BOOL
setfilepwpos(void *vp
, SMB_BIG_UINT tok
)
183 return !sys_fseek((FILE *)vp
, (SMB_OFF_T
)tok
, SEEK_SET
);
186 /*************************************************************************
187 Gets a line out of a file.
188 line is of format "xxxx:xxxxxx:xxxxx:".
189 lines with "#" at the front are ignored.
190 *************************************************************************/
192 int getfileline(void *vp
, char *linebuf
, int linebuf_size
)
194 /* Static buffers we will return. */
195 FILE *fp
= (FILE *)vp
;
201 DEBUG(0,("getfileline: Bad file pointer.\n"));
206 * Scan the file, a line at a time.
211 fgets(linebuf
, linebuf_size
, fp
);
217 * Check if the string is terminated with a newline - if not
218 * then we must keep reading and discard until we get one.
221 linebuf_len
= strlen(linebuf
);
222 if (linebuf_len
== 0) {
227 if (linebuf
[linebuf_len
- 1] != '\n') {
229 while (!ferror(fp
) && !feof(fp
)) {
236 linebuf
[linebuf_len
- 1] = '\0';
239 #ifdef DEBUG_PASSWORD
240 DEBUG(100, ("getfileline: got line |%s|\n", linebuf
));
242 if ((linebuf
[0] == 0) && feof(fp
)) {
243 DEBUG(4, ("getfileline: end of file reached\n"));
247 if (linebuf
[0] == '#' || linebuf
[0] == '\0') {
248 DEBUG(6, ("getfileline: skipping comment or blank line\n"));
252 p
= (unsigned char *) strchr_m(linebuf
, ':');
254 DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
262 /****************************************************************************
263 Read a line from a file with possible \ continuation chars.
264 Blanks at the start or end of a line are stripped.
265 The string will be allocated if s2 is NULL.
266 ****************************************************************************/
268 char *fgets_slash(char *s2
,int maxlen
,XFILE
*f
)
273 BOOL start_of_line
= True
;
284 maxlen
= MIN(maxlen
,8);
285 s
= (char *)SMB_MALLOC(maxlen
);
294 while (len
< maxlen
-1) {
300 while (len
> 0 && s
[len
-1] == ' ') {
303 if (len
> 0 && s
[len
-1] == '\\') {
305 start_of_line
= True
;
310 if (len
<= 0 && !s2
) {
313 return(len
>0?s
:NULL
);
319 start_of_line
= False
;
324 if (!s2
&& len
> maxlen
-3) {
328 t
= (char *)SMB_REALLOC(s
,maxlen
);
330 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
341 /****************************************************************************
342 Load from a pipe into memory.
343 ****************************************************************************/
345 char *file_pload(char *syscmd
, size_t *size
)
352 fd
= sys_popen(syscmd
);
360 while ((n
= read(fd
, buf
, sizeof(buf
))) > 0) {
361 tp
= SMB_REALLOC(p
, total
+ n
+ 1);
363 DEBUG(0,("file_pload: failed to expand buffer!\n"));
370 memcpy(p
+total
, buf
, n
);
375 /* FIXME: Perhaps ought to check that the command completed
376 * successfully (returned 0); if not the data may be
387 /****************************************************************************
388 Load a file into memory from a fd.
389 ****************************************************************************/
391 char *fd_load(int fd
, size_t *size
)
393 SMB_STRUCT_STAT sbuf
;
396 if (sys_fstat(fd
, &sbuf
) != 0) {
400 p
= (char *)SMB_MALLOC(sbuf
.st_size
+1);
405 if (read(fd
, p
, sbuf
.st_size
) != sbuf
.st_size
) {
412 *size
= sbuf
.st_size
;
418 /****************************************************************************
419 Load a file into memory.
420 ****************************************************************************/
422 char *file_load(const char *fname
, size_t *size
)
427 if (!fname
|| !*fname
) {
431 fd
= open(fname
,O_RDONLY
);
436 p
= fd_load(fd
, size
);
441 /*******************************************************************
442 mmap (if possible) or read a file.
443 ********************************************************************/
445 void *map_file(char *fname
, size_t size
)
451 fd
= open(fname
, O_RDONLY
, 0);
453 DEBUG(2,("map_file: Failed to load %s - %s\n", fname
, strerror(errno
)));
456 p
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
|MAP_FILE
, fd
, 0);
458 if (p
== MAP_FAILED
) {
459 DEBUG(1,("map_file: Failed to mmap %s - %s\n", fname
, strerror(errno
)));
464 p
= file_load(fname
, &s2
);
469 DEBUG(1,("map_file: incorrect size for %s - got %lu expected %lu\n",
470 fname
, (unsigned long)s2
, (unsigned long)size
));
478 /****************************************************************************
479 Parse a buffer into lines.
480 ****************************************************************************/
482 static char **file_lines_parse(char *p
, size_t size
, int *numlines
)
491 for (s
= p
, i
=0; s
< p
+size
; s
++) {
492 if (s
[0] == '\n') i
++;
495 ret
= SMB_MALLOC_ARRAY(char *, i
+2);
500 memset(ret
, 0, sizeof(ret
[0])*(i
+2));
506 for (s
= p
, i
=0; s
< p
+size
; s
++) {
520 /****************************************************************************
521 Load a file into memory and return an array of pointers to lines in the file
522 must be freed with file_lines_free().
523 ****************************************************************************/
525 char **file_lines_load(const char *fname
, int *numlines
)
530 p
= file_load(fname
, &size
);
535 return file_lines_parse(p
, size
, numlines
);
538 /****************************************************************************
539 Load a fd into memory and return an array of pointers to lines in the file
540 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
542 ****************************************************************************/
544 char **fd_lines_load(int fd
, int *numlines
)
549 p
= fd_load(fd
, &size
);
554 return file_lines_parse(p
, size
, numlines
);
557 /****************************************************************************
558 Load a pipe into memory and return an array of pointers to lines in the data
559 must be freed with file_lines_free().
560 ****************************************************************************/
562 char **file_lines_pload(char *syscmd
, int *numlines
)
567 p
= file_pload(syscmd
, &size
);
572 return file_lines_parse(p
, size
, numlines
);
575 /****************************************************************************
576 Free lines loaded with file_lines_load.
577 ****************************************************************************/
579 void file_lines_free(char **lines
)
588 /****************************************************************************
589 Take a list of lines and modify them to produce a list where \ continues
591 ****************************************************************************/
593 void file_lines_slashcont(char **lines
)
597 for (i
=0; lines
[i
];) {
598 int len
= strlen(lines
[i
]);
599 if (lines
[i
][len
-1] == '\\') {
600 lines
[i
][len
-1] = ' ';
602 char *p
= &lines
[i
][len
];
603 while (p
< lines
[i
+1]) {
606 for (j
= i
+1; lines
[j
]; j
++) {
607 lines
[j
] = lines
[j
+1];
616 /****************************************************************************
617 Save a lump of data into a file. Mostly used for debugging.
618 ****************************************************************************/
620 BOOL
file_save(const char *fname
, void *packet
, size_t length
)
623 fd
= open(fname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
627 if (write(fd
, packet
, length
) != (size_t)length
) {