[BACK]Return to fileops.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sudo

Annotation of src/usr.bin/sudo/fileops.c, Revision 1.9

1.1       millert     1: /*
1.8       millert     2:  * Copyright (c) 1999-2005,2007,2009 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       millert     3:  *
1.4       millert     4:  * Permission to use, copy, modify, and distribute this software for any
                      5:  * purpose with or without fee is hereby granted, provided that the above
                      6:  * copyright notice and this permission notice appear in all copies.
                      7:  *
                      8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                      9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.3       millert    15:  *
                     16:  * Sponsored in part by the Defense Advanced Research Projects
                     17:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
                     18:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
1.1       millert    19:  */
                     20:
1.5       millert    21: #include <config.h>
1.1       millert    22:
1.2       millert    23: #include <sys/types.h>
                     24: #include <sys/param.h>
1.4       millert    25: #include <sys/time.h>
1.2       millert    26: #ifdef HAVE_FLOCK
                     27: # include <sys/file.h>
                     28: #endif /* HAVE_FLOCK */
1.1       millert    29: #include <stdio.h>
1.6       millert    30: #ifdef HAVE_STRING_H
                     31: # include <string.h>
                     32: #else
                     33: # ifdef HAVE_STRINGS_H
                     34: #  include <strings.h>
                     35: # endif
                     36: #endif /* HAVE_STRING_H */
                     37: #include <ctype.h>
                     38: #include <limits.h>
1.1       millert    39: #ifdef HAVE_UNISTD_H
1.2       millert    40: # include <unistd.h>
1.1       millert    41: #endif /* HAVE_UNISTD_H */
                     42: #include <fcntl.h>
1.5       millert    43: #if TIME_WITH_SYS_TIME
                     44: # include <time.h>
                     45: #endif
                     46: #ifndef HAVE_TIMESPEC
                     47: # include <emul/timespec.h>
                     48: #endif
1.1       millert    49:
                     50: #include "sudo.h"
                     51:
1.6       millert    52: #ifndef LINE_MAX
                     53: # define LINE_MAX 2048
                     54: #endif
                     55:
1.1       millert    56: /*
1.4       millert    57:  * Update the access and modify times on an fd or file.
1.1       millert    58:  */
                     59: int
1.4       millert    60: touch(fd, path, tsp)
                     61:     int fd;
1.1       millert    62:     char *path;
1.4       millert    63:     struct timespec *tsp;
1.1       millert    64: {
1.4       millert    65:     struct timeval times[2];
1.1       millert    66:
1.4       millert    67:     if (tsp != NULL) {
                     68:        times[0].tv_sec = times[1].tv_sec = tsp->tv_sec;
                     69:        times[0].tv_usec = times[1].tv_usec = tsp->tv_nsec / 1000;
                     70:     }
                     71:
                     72: #if defined(HAVE_FUTIME) || defined(HAVE_FUTIMES)
                     73:     if (fd != -1)
                     74:        return(futimes(fd, tsp ? times : NULL));
                     75:     else
                     76: #endif
                     77:     if (path != NULL)
                     78:        return(utimes(path, tsp ? times : NULL));
                     79:     else
                     80:        return(-1);
1.1       millert    81: }
                     82:
                     83: /*
                     84:  * Lock/unlock a file.
                     85:  */
                     86: #ifdef HAVE_LOCKF
                     87: int
                     88: lock_file(fd, lockit)
                     89:     int fd;
                     90:     int lockit;
                     91: {
                     92:     int op = 0;
                     93:
                     94:     switch (lockit) {
                     95:        case SUDO_LOCK:
                     96:            op = F_LOCK;
                     97:            break;
                     98:        case SUDO_TLOCK:
                     99:            op = F_TLOCK;
                    100:            break;
                    101:        case SUDO_UNLOCK:
                    102:            op = F_ULOCK;
                    103:            break;
                    104:     }
                    105:     return(lockf(fd, op, 0) == 0);
                    106: }
                    107: #elif HAVE_FLOCK
                    108: int
                    109: lock_file(fd, lockit)
                    110:     int fd;
                    111:     int lockit;
                    112: {
                    113:     int op = 0;
                    114:
                    115:     switch (lockit) {
                    116:        case SUDO_LOCK:
                    117:            op = LOCK_EX;
                    118:            break;
                    119:        case SUDO_TLOCK:
                    120:            op = LOCK_EX | LOCK_NB;
                    121:            break;
                    122:        case SUDO_UNLOCK:
1.2       millert   123:            op = LOCK_UN;
1.1       millert   124:            break;
                    125:     }
                    126:     return(flock(fd, op) == 0);
                    127: }
                    128: #else
                    129: int
                    130: lock_file(fd, lockit)
                    131:     int fd;
                    132:     int lockit;
                    133: {
                    134: #ifdef F_SETLK
                    135:     int func;
                    136:     struct flock lock;
                    137:
                    138:     lock.l_start = 0;
                    139:     lock.l_len = 0;
                    140:     lock.l_pid = getpid();
                    141:     lock.l_type = (lockit == SUDO_UNLOCK) ? F_UNLCK : F_WRLCK;
                    142:     lock.l_whence = SEEK_SET;
1.8       millert   143:     func = (lockit == SUDO_LOCK) ? F_SETLKW : F_SETLK;
1.1       millert   144:
                    145:     return(fcntl(fd, func, &lock) == 0);
                    146: #else
                    147:     return(TRUE);
                    148: #endif
                    149: }
                    150: #endif
1.6       millert   151:
                    152: /*
                    153:  * Read a line of input, remove comments and strip off leading
                    154:  * and trailing spaces.  Returns static storage that is reused.
                    155:  */
                    156: char *
                    157: sudo_parseln(fp)
                    158:     FILE *fp;
                    159: {
                    160:     size_t len;
                    161:     char *cp = NULL;
                    162:     static char buf[LINE_MAX];
                    163:
                    164:     if (fgets(buf, sizeof(buf), fp) != NULL) {
                    165:        /* Remove comments */
                    166:        if ((cp = strchr(buf, '#')) != NULL)
                    167:            *cp = '\0';
                    168:
                    169:        /* Trim leading and trailing whitespace/newline */
                    170:        len = strlen(buf);
1.7       millert   171:        while (len > 0 && isspace((unsigned char)buf[len - 1]))
1.6       millert   172:            buf[--len] = '\0';
                    173:        for (cp = buf; isblank(*cp); cp++)
                    174:            continue;
                    175:     }
                    176:     return(cp);
                    177: }