|
|
1.1 ! root 1: /* ! 2: * The information contained herein is a trade secret of Mark Williams ! 3: * Company, and is confidential information. It is provided under a ! 4: * license agreement, and may be copied or disclosed only under the ! 5: * terms of that agreement. Any reproduction or disclosure of this ! 6: * material without the express written authorization of Mark Williams ! 7: * Company or persuant to the license agreement is unlawful. ! 8: * ! 9: * An unpublished work by Mark Williams Company, Chicago. ! 10: * All rights reserved. ! 11: */ ! 12: ! 13: /* ! 14: * misc.c ! 15: * ! 16: * Miscelenious functions for crontab and cron. ! 17: * ! 18: * $ 12-9-1991 vlad (Vladimir Smelyansky) ! 19: */ ! 20: #include <stdio.h> ! 21: #include <string.h> ! 22: #include <pwd.h> ! 23: #include <errno.h> ! 24: #include <time.h> ! 25: #include <errno.h> ! 26: #include <sys/ino.h> ! 27: #include <sys/stat.h> ! 28: #include <sys/times.h> ! 29: #include "cron.h" ! 30: ! 31: FILE *fpOpenTable(); /* Open crontable */ ! 32: int iAllow(); /* Check permissions to use crontab */ ! 33: int iInFile(); /* Check if the user is in a file */ ! 34: int lock(); /* Create a lock FIFO */ ! 35: int set_uid(); /* Set user and group ids */ ! 36: void vDelete(); /* Delete user crontable */ ! 37: void vGetName(); /* Get Real User name */ ! 38: void vList(); /* List user crontable */ ! 39: void vMailDisable(); /* Disable the mail messages */ ! 40: void vMailEnable(); /* Enable the mail messages */ ! 41: void vReplace(); /* Replace user crontable */ ! 42: ! 43: char *pcFileName; /* Source table for crontab */ ! 44: extern char acRealUser[MAX_UNAME]; /* Real user name */ ! 45: ! 46: /* ! 47: * Get real user name and id. ! 48: */ ! 49: void vGetName() ! 50: { ! 51: int iRealUid; /* Real user ID */ ! 52: struct passwd *pstPasswd; ! 53: ! 54: /* Find real user name. ! 55: * ! 56: * NOTE: functions getpwuid and getpwnam will /etc/passwd file ! 57: * by calling setpwent. ! 58: */ ! 59: iRealUid = getuid(); /* Get real user id */ ! 60: if ((pstPasswd = getpwuid(iRealUid)) == NULL) { ! 61: fprintf(stderr, "crontab: cannot find user id %d\n", iRealUid); ! 62: exit(1); ! 63: } ! 64: strncpy(acRealUser, pstPasswd->pw_name, MAX_UNAME); ! 65: } ! 66: ! 67: /* ! 68: * Check if user allowed to use cron. ! 69: * Return TRUE if user autorized to use cron, FALSE otherwise. ! 70: */ ! 71: int iAllow() ! 72: { ! 73: FILE *fpAllow, /* /usr/lib/cron/cron.allow */ ! 74: *fpDeny; /* /usr/lib/cron/cron.deny */ ! 75: int iAllowExist = FALSE; /* By default allow doesn't exist*/ ! 76: int iDenyExist = FALSE; /* By default deny doesn't exist*/ ! 77: ! 78: vGetName(); /* Get reqal user name */ ! 79: ! 80: if (!strcmp(acRealUser, "root")) /* Superuser allowed by definition */ ! 81: return TRUE; ! 82: ! 83: /* If allow exist we do not want read anything more */ ! 84: if ((fpAllow = fopen(F_ALLOW, "r")) != NULL) ! 85: return iInFile(fpAllow, acRealUser);/* return TRUE if allow ! 86: * FALSE otherwise */ ! 87: ! 88: if ((fpDeny = fopen(F_DENY, "r")) != NULL) ! 89: return !iInFile(fpDeny, acRealUser);/* return FALSE if deny ! 90: * TRUE otherwise */ ! 91: ! 92: return FALSE; /* If neither exist not autorized */ ! 93: } ! 94: ! 95: /* ! 96: * Return TRUE if one of the lines in file "fp" matches string "pString" ! 97: * exactly, FALSE otherwise. ! 98: */ ! 99: int iInFile(fp, pString) ! 100: FILE *fp; ! 101: char *pString; ! 102: { ! 103: char acLine[MAX_UNAME]; ! 104: ! 105: rewind(fp); /* Just to be safty */ ! 106: ! 107: while (fgets(acLine, MAX_UNAME, fp)) { ! 108: if (acLine[0] != '\0') ! 109: acLine[strlen(acLine)-1] = '\0'; ! 110: if (0 == strcmp(acLine, pString)) ! 111: return TRUE; /* Line was found */ ! 112: } ! 113: /* We didn't find the line */ ! 114: return FALSE; ! 115: } ! 116: ! 117: /* ! 118: * Disable the mail messages. ! 119: */ ! 120: void vMailDisable() ! 121: { ! 122: char *buf; ! 123: ! 124: if ((buf = malloc(strlen(D_MAIN) + MAX_UNAME)) == ! 125: NULL) { ! 126: fprintf(stderr, "crontab: not enough memory\n"); ! 127: exit(1); ! 128: } ! 129: sprintf(buf, "%s/%s", D_MAIN, acRealUser); ! 130: Dprint("vMailDisable: create lock %s\n", buf); ! 131: lock(buf); ! 132: } ! 133: ! 134: /* ! 135: * Enable the mail messages. ! 136: */ ! 137: void vMailEnable() ! 138: { ! 139: char *buf; ! 140: ! 141: if ((buf = malloc(strlen(D_MAIN) + MAX_UNAME)) == NULL) { ! 142: fprintf(stderr, "crontab: not enough memory\n"); ! 143: exit(1); ! 144: } ! 145: sprintf(buf, "%s/%s", D_MAIN, acRealUser); ! 146: Dprint("vMailDisable: remove lock %s\n", buf); ! 147: unlink(buf); ! 148: } ! 149: ! 150: /* ! 151: * Create a cron lock FIFO. Return TRUE on succes, FALSE if lock exist, ! 152: * exit 1 if cannot lock. ! 153: */ ! 154: int lock(file_name) ! 155: char *file_name; ! 156: { ! 157: if (mknod(file_name, IFPIPE, 0) < 0) ! 158: if (errno == EEXIST) ! 159: return FALSE; /* Lock exist */ ! 160: else { ! 161: /* Something bad with system at all */ ! 162: fprintf(stderr, "cron: cannot lock\n"); ! 163: exit(1); ! 164: } ! 165: else ! 166: return TRUE; /* Cron can run */ ! 167: } ! 168: ! 169: /* ! 170: * Set user ID to user_name ! 171: */ ! 172: int set_uid(cpUserName) ! 173: char *cpUserName; ! 174: { ! 175: struct passwd *stpPwd; ! 176: ! 177: Dprint("set_uid: Effective ID before setuid is %d\n", geteuid()); ! 178: ! 179: setpwent(); /* Rewind /etc/passwd. Just to be safety */ ! 180: if ((stpPwd = getpwnam(cpUserName)) == NULL) ! 181: return FALSE; ! 182: else { ! 183: /* You cannot setgid and setuid in the same if statement ! 184: * because there is no garanty in what order they will be ! 185: * evaluated. ! 186: */ ! 187: if (setgid(stpPwd->pw_gid)) { ! 188: fprintf(stderr, ! 189: "cron: cannot set gid %d for user %s\n", ! 190: stpPwd->pw_gid, cpUserName); ! 191: return FALSE; ! 192: } ! 193: if (setuid(stpPwd->pw_uid)) { ! 194: fprintf(stderr, ! 195: "cron: cannot set uid %d for user %s\n", ! 196: stpPwd->pw_uid, cpUserName); ! 197: return FALSE; ! 198: } ! 199: } ! 200: /* We have to cd to $HOME here */ ! 201: chdir(stpPwd->pw_dir); ! 202: return TRUE; ! 203: } ! 204: ! 205: /* ! 206: * List user crontable. ! 207: */ ! 208: void vList() ! 209: { ! 210: FILE *fp; ! 211: char acBuf[MAX_STR_LEN]; ! 212: ! 213: if ((fp = fpOpenTable("r")) == NULL) { ! 214: printf("There is no table for user '%s'\n", acRealUser); ! 215: exit(0); ! 216: } ! 217: ! 218: while (fgets(acBuf, MAX_STR_LEN, fp)) ! 219: printf("%s", acBuf); ! 220: } ! 221: ! 222: /* ! 223: * Delete user crontable. ! 224: */ ! 225: void vDelete() ! 226: { ! 227: char *pcTableName; ! 228: ! 229: if ((pcTableName = malloc(strlen(D_SPOOL) + MAX_UNAME )) == NULL) { ! 230: fprintf(stderr, "crontab: not enough memory\n"); ! 231: exit(1); ! 232: } ! 233: ! 234: sprintf(pcTableName, "%s/%s", D_SPOOL, acRealUser); ! 235: Dprint("vDelete: delete table %s\n", pcTableName); ! 236: unlink(pcTableName); ! 237: } ! 238: ! 239: void vReplace() ! 240: { ! 241: FILE *fpInTable; /* Source table */ ! 242: FILE *fpOutTable; /* Destination */ ! 243: char acBuf[MAX_STR_LEN]; ! 244: time_t tTime; ! 245: ! 246: Dprint("File name is %s\n", pcFileName); ! 247: if (strcmp(pcFileName, "-") != 0) { ! 248: if ((fpInTable = fopen(pcFileName, "r")) == NULL) { ! 249: fprintf(stderr, "crontab: cannot open file '%s'\n", ! 250: pcFileName); ! 251: exit(1); ! 252: } ! 253: } else /* If input file name is '-' read from stdin */ ! 254: fpInTable = stdin; ! 255: ! 256: Dprint("vReplace: source table is %s\n", pcFileName); ! 257: ! 258: if ((fpOutTable = fpOpenTable("w")) == NULL) { ! 259: fprintf(stderr, "crontab: cannot open file '%s/%s'\n", ! 260: D_SPOOL, acRealUser); ! 261: exit(1); ! 262: } ! 263: ! 264: /* Print a crontab logo. ! 265: */ ! 266: time(&tTime); ! 267: fprintf(fpOutTable, "# Cron Version %s. Installed on %s", VERSION, ! 268: ctime(&tTime)); ! 269: fprintf(fpOutTable, "# Source was %s.\n", pcFileName); ! 270: ! 271: while (fgets(acBuf, MAX_STR_LEN, fpInTable)) ! 272: fprintf(fpOutTable, "%s", acBuf); ! 273: } ! 274: ! 275: /* ! 276: * Open user table. Return file pointer if file is open, NULL othewrwise. ! 277: */ ! 278: FILE *fpOpenTable(cmd) ! 279: char *cmd; ! 280: { ! 281: char *pcTableName; ! 282: FILE *fp; ! 283: ! 284: if ((pcTableName = malloc(strlen(D_SPOOL) + MAX_UNAME + 1)) == NULL) { ! 285: system("echo \"cron*: not enough memory\" > /dev/console"); ! 286: } ! 287: ! 288: strcpy(pcTableName, D_SPOOL); ! 289: strcat(pcTableName, "/"); ! 290: strcat(pcTableName, acRealUser); ! 291: ! 292: Dprint("Destination file is %s\n", pcTableName); ! 293: ! 294: fp = fopen(pcTableName, cmd); ! 295: free(pcTableName); ! 296: return(fp); ! 297: } ! 298: ! 299: /* ! 300: * if_log - return TRUE if log file should be used. ! 301: */ ! 302: if_log() ! 303: { ! 304: char buf[64]; ! 305: FILE *fp_log; ! 306: ! 307: if ((fp_log = fopen(F_DEF, "r")) == NULL) ! 308: return FALSE; ! 309: ! 310: fgets(buf, 64, fp_log); ! 311: if (!strncmp(buf, "CRONLOG=YES", 11)) ! 312: return TRUE; ! 313: return FALSE; ! 314: } ! 315:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.