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

Annotation of src/usr.bin/at/perm.c, Revision 1.3

1.3     ! millert     1: /*     $OpenBSD: perm.c,v 1.2 2002/02/16 21:27:44 millert Exp $        */
1.1       millert     2:
                      3: /*
                      4:  * perm.c - check user permission for at(1)
                      5:  * Copyright (C) 1994  Thomas Koenig
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author(s) may not be used to endorse or promote
                     13:  *    products derived from this software without specific prior written
                     14:  *    permission.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
                     17:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     18:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     19:  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
                     20:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     21:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     22:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     23:  * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     24:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     25:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
                     27:
                     28: /* System Headers */
                     29:
                     30: #include <sys/types.h>
                     31: #include <errno.h>
                     32: #include <pwd.h>
                     33: #include <stddef.h>
                     34: #include <stdio.h>
                     35: #include <stdlib.h>
                     36: #include <string.h>
                     37: #include <unistd.h>
                     38:
                     39: /* Local headers */
                     40:
                     41: #include "at.h"
                     42: #include "panic.h"
                     43: #include "pathnames.h"
                     44: #include "privs.h"
                     45:
                     46: /* File scope variables */
                     47:
                     48: #ifndef lint
1.3     ! millert    49: static const char rcsid[] = "$OpenBSD: perm.c,v 1.2 2002/02/16 21:27:44 millert Exp $";
1.1       millert    50: #endif
                     51:
                     52: /* Function declarations */
                     53:
1.2       millert    54: static int check_for_user(FILE *, const char *);
1.1       millert    55:
                     56: /* Local functions */
                     57:
                     58: static int
1.3     ! millert    59: check_for_user(FILE *fp, const char *name)
1.1       millert    60: {
                     61:        char *buffer;
                     62:        size_t len;
                     63:        int found = 0;
                     64:
                     65:        len = strlen(name);
                     66:        if ((buffer = malloc(len + 2)) == NULL)
                     67:                panic("Insufficient virtual memory");
                     68:
                     69:        while (fgets(buffer, len + 2, fp) != NULL) {
                     70:                if (strncmp(name, buffer, len) == 0 && buffer[len] == '\n') {
                     71:                        found = 1;
                     72:                        break;
                     73:                }
                     74:        }
                     75:        (void)fclose(fp);
                     76:        free(buffer);
                     77:        return (found);
                     78: }
                     79:
                     80:
                     81: /* Global functions */
                     82:
                     83: int
1.3     ! millert    84: check_permission(void)
1.1       millert    85: {
                     86:        FILE *fp;
                     87:        uid_t uid = geteuid();
                     88:        struct passwd *pentry;
                     89:
                     90:        if (uid==0)
                     91:                return 1;
                     92:
                     93:        if ((pentry = getpwuid(uid)) == NULL) {
                     94:                perror("Cannot access user database");
                     95:                exit(EXIT_FAILURE);
                     96:        }
                     97:
1.3     ! millert    98:        PRIV_START;
1.1       millert    99:
                    100:        fp = fopen(_PATH_AT_ALLOW, "r");
                    101:
1.3     ! millert   102:        PRIV_END;
1.1       millert   103:
                    104:        if (fp != NULL) {
                    105:                return (check_for_user(fp, pentry->pw_name));
                    106:        } else {
1.3     ! millert   107:                PRIV_START;
1.1       millert   108:
                    109:                fp = fopen(_PATH_AT_DENY, "r");
                    110:
1.3     ! millert   111:                PRIV_END;
1.1       millert   112:
                    113:                if (fp != NULL)
                    114:                        return (!check_for_user(fp, pentry->pw_name));
                    115:                else
                    116:                        perror(_PATH_AT_DENY);
                    117:        }
                    118:        return (0);
                    119: }