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.
25 /***************************************************************
26 Signal function to tell us we timed out.
27 ****************************************************************/
29 static void gotalarm_sig(void)
34 /***************************************************************
35 Lock or unlock a fd for a known lock type. Abandon after waitsecs
37 ****************************************************************/
39 BOOL do_file_lock(int fd, int waitsecs, int type)
41 SMB_STRUCT_FLOCK lock;
45 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
48 lock.l_whence = SEEK_SET;
54 ret = fcntl(fd, SMB_F_SETLKW, &lock);
56 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
59 DEBUG(0, ("do_file_lock: failed to %s file.\n",
60 type == F_UNLCK ? "unlock" : "lock"));
68 /***************************************************************
69 Lock an fd. Abandon after waitsecs seconds.
70 ****************************************************************/
72 BOOL file_lock(int fd, int type, int secs, int *plock_depth)
79 if ((*plock_depth) == 0)
81 if (!do_file_lock(fd, secs, type)) {
82 DEBUG(10,("file_lock: locking file failed, error = %s.\n",
91 /***************************************************************
92 Unlock an fd. Abandon after waitsecs seconds.
93 ****************************************************************/
95 BOOL file_unlock(int fd, int *plock_depth)
100 ret = do_file_lock(fd, 5, F_UNLCK);
105 DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n",
110 /***************************************************************
111 locks a file for enumeration / modification.
112 update to be set = True if modification is required.
113 ****************************************************************/
115 void *startfilepwent(char *pfile, char *s_readbuf, int bufsize,
116 int *file_lock_depth, BOOL update)
122 DEBUG(0, ("startfilepwent: No file set\n"));
125 DEBUG(10, ("startfilepwent: opening file %s\n", pfile));
127 fp = sys_fopen(pfile, update ? "r+b" : "rb");
130 DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile));
134 /* Set a buffer to do more efficient reads */
135 setvbuf(fp, s_readbuf, _IOFBF, bufsize);
137 if (!file_lock(fileno(fp), (update ? F_WRLCK : F_RDLCK), 5, file_lock_depth))
139 DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile));
144 /* Make sure it is only rw by the owner */
147 /* We have a lock on the file. */
151 /***************************************************************
152 End enumeration of the file.
153 ****************************************************************/
154 void endfilepwent(void *vp, int *file_lock_depth)
156 FILE *fp = (FILE *)vp;
158 file_unlock(fileno(fp), file_lock_depth);
160 DEBUG(7, ("endfilepwent: closed file.\n"));
163 /*************************************************************************
164 Return the current position in the file list as an SMB_BIG_UINT.
165 This must be treated as an opaque token.
166 *************************************************************************/
167 SMB_BIG_UINT getfilepwpos(void *vp)
169 return (SMB_BIG_UINT)sys_ftell((FILE *)vp);
172 /*************************************************************************
173 Set the current position in the file list from an SMB_BIG_UINT.
174 This must be treated as an opaque token.
175 *************************************************************************/
176 BOOL setfilepwpos(void *vp, SMB_BIG_UINT tok)
178 return !sys_fseek((FILE *)vp, (SMB_OFF_T)tok, SEEK_SET);
181 /*************************************************************************
182 gets a line out of a file.
183 line is of format "xxxx:xxxxxx:xxxxx:".
184 lines with "#" at the front are ignored.
185 *************************************************************************/
186 int getfileline(void *vp, char *linebuf, int linebuf_size)
188 /* Static buffers we will return. */
189 FILE *fp = (FILE *)vp;
196 DEBUG(0,("getfileline: Bad file pointer.\n"));
201 * Scan the file, a line at a time.
207 fgets(linebuf, linebuf_size, fp);
214 * Check if the string is terminated with a newline - if not
215 * then we must keep reading and discard until we get one.
218 linebuf_len = strlen(linebuf);
219 if (linebuf_len == 0)
225 if (linebuf[linebuf_len - 1] != '\n')
228 while (!ferror(fp) && !feof(fp))
239 linebuf[linebuf_len - 1] = '\0';
242 #ifdef DEBUG_PASSWORD
243 DEBUG(100, ("getfileline: got line |%s|\n", linebuf));
245 if ((linebuf[0] == 0) && feof(fp))
247 DEBUG(4, ("getfileline: end of file reached\n"));
251 if (linebuf[0] == '#' || linebuf[0] == '\0')
253 DEBUG(6, ("getfileline: skipping comment or blank line\n"));
257 p = (unsigned char *) strchr_m(linebuf, ':');
260 DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
269 /****************************************************************************
270 read a line from a file with possible \ continuation chars.
271 Blanks at the start or end of a line are stripped.
272 The string will be allocated if s2 is NULL
273 ****************************************************************************/
274 char *fgets_slash(char *s2,int maxlen,XFILE *f)
279 BOOL start_of_line = True;
284 if (maxlen <2) return(NULL);
288 maxlen = MIN(maxlen,8);
289 s = (char *)malloc(maxlen);
292 if (!s) return(NULL);
296 while (len < maxlen-1)
304 while (len > 0 && s[len-1] == ' ')
308 if (len > 0 && s[len-1] == '\\')
311 start_of_line = True;
318 return(len>0?s:NULL);
323 start_of_line = False;
327 if (!s2 && len > maxlen-3)
332 t = (char *)Realloc(s,maxlen);
334 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
344 /****************************************************************************
345 load from a pipe into memory
346 ****************************************************************************/
347 char *file_pload(char *syscmd, size_t *size)
354 fd = sys_popen(syscmd);
355 if (fd == -1) return NULL;
360 while ((n = read(fd, buf, sizeof(buf))) > 0) {
361 tp = Realloc(p, total + n + 1);
363 DEBUG(0,("file_pload: failed to exand buffer!\n"));
368 memcpy(p+total, buf, n);
375 if (size) *size = total;
380 /****************************************************************************
381 load a file into memory from a fd.
382 ****************************************************************************/
384 char *fd_load(int fd, size_t *size)
386 SMB_STRUCT_STAT sbuf;
389 if (sys_fstat(fd, &sbuf) != 0) return NULL;
391 p = (char *)malloc(sbuf.st_size+1);
394 if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
400 if (size) *size = sbuf.st_size;
405 /****************************************************************************
406 load a file into memory
407 ****************************************************************************/
408 char *file_load(const char *fname, size_t *size)
413 if (!fname || !*fname) return NULL;
415 fd = open(fname,O_RDONLY);
416 if (fd == -1) return NULL;
418 p = fd_load(fd, size);
426 /*******************************************************************
427 mmap (if possible) or read a file
428 ********************************************************************/
429 void *map_file(char *fname, size_t size)
436 fd = open(fname, O_RDONLY, 0);
438 DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
441 p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
443 if (p == MAP_FAILED) {
444 DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
450 p = file_load(fname, &s2);
451 if (!p || s2 != size) {
452 DEBUG(1,("incorrect size for %s - got %d expected %d\n",
463 /****************************************************************************
464 parse a buffer into lines
465 ****************************************************************************/
466 static char **file_lines_parse(char *p, size_t size, int *numlines)
473 for (s = p, i=0; s < p+size; s++) {
474 if (s[0] == '\n') i++;
477 ret = (char **)malloc(sizeof(ret[0])*(i+2));
482 memset(ret, 0, sizeof(ret[0])*(i+2));
483 if (numlines) *numlines = i;
486 for (s = p, i=0; s < p+size; s++) {
492 if (s[0] == '\r') s[0] = 0;
499 /****************************************************************************
500 load a file into memory and return an array of pointers to lines in the file
501 must be freed with file_lines_free().
502 ****************************************************************************/
503 char **file_lines_load(const char *fname, int *numlines)
508 p = file_load(fname, &size);
511 return file_lines_parse(p, size, numlines);
514 /****************************************************************************
515 load a fd into memory and return an array of pointers to lines in the file
516 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
518 ****************************************************************************/
519 char **fd_lines_load(int fd, int *numlines)
524 p = fd_load(fd, &size);
527 return file_lines_parse(p, size, numlines);
531 /****************************************************************************
532 load a pipe into memory and return an array of pointers to lines in the data
533 must be freed with file_lines_free().
534 ****************************************************************************/
535 char **file_lines_pload(char *syscmd, int *numlines)
540 p = file_pload(syscmd, &size);
543 return file_lines_parse(p, size, numlines);
546 /****************************************************************************
547 free lines loaded with file_lines_load
548 ****************************************************************************/
549 void file_lines_free(char **lines)
557 /****************************************************************************
558 take a lislist of lines and modify them to produce a list where \ continues
560 ****************************************************************************/
561 void file_lines_slashcont(char **lines)
565 for (i=0; lines[i];) {
566 int len = strlen(lines[i]);
567 if (lines[i][len-1] == '\\') {
568 lines[i][len-1] = ' ';
570 char *p = &lines[i][len];
571 while (p < lines[i+1]) *p++ = ' ';
572 for (j = i+1; lines[j]; j++) lines[j] = lines[j+1];
581 save a lump of data into a file. Mostly used for debugging
583 BOOL file_save(const char *fname, void *packet, size_t length)
586 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
590 if (write(fd, packet, length) != length) {