/* checkide(). Scans the named drive letter and returns the DISK_??? type. */ static int checkide(char letter) { FILE *f = NULL; char filename[STRING_SIZE]; char buffer[STRING_SIZE]; memset(filename, 0, STRING_SIZE); memset(buffer, 0, STRING_SIZE); snprintf(filename, STRING_SIZE - 1, "/proc/ide/hd%c/media", letter); if (!(f = fopen(filename, "r"))) return DISK_EMPTY; if (!(fgets(buffer, STRING_SIZE, f))) { fclose(f); return DISK_EMPTY; } fclose(f); stripnl(buffer); if (strcmp(buffer, "cdrom") == 0) return DISK_CDROM; else if (strcmp(buffer, "disk") == 0) return DISK_HD; else return DISK_UNKNOWN; }
void acquire_board(const char *filename) { std::FILE *fp = std::fopen(filename, "rt"); if (!fp) { std::cout << "Failed to open " << filename << " for input.\n"; std::exit(-1); } // TODO: Allocate dynamically char buffer[MAX_BOARD_SIZE]; while (!std::feof(fp)) { if (std::fgets(buffer, MAX_BOARD_SIZE, fp) == NULL) break; // skip comments (if any) if (buffer[0] == '#') continue; stripnl(buffer); gBoard = new char[strlen(buffer) + 1]; std::strcpy(gBoard, buffer); // Currently, there is only 1 board per file break; } std::fclose(fp); }
void load_index(Hashtable html_rev_index) { FILE *dat_file = fopen("index.dat", "r"); char *lineptr, *ip, *sp; struct int_elem *int_ent; size_t ptrsize = 4096; lineptr = malloc(4096); while ((getline(&lineptr, &ptrsize, dat_file)) != -1) { stripnl(lineptr); if (strlen(lineptr)) { sp = strtok(lineptr, " "); ip = strtok(NULL, ","); int_ent = malloc(sizeof(*int_ent)); sscanf(ip, "%d", &int_ent->value); int_ent->next = NULL; HTInsert(html_rev_index, sp, int_ent); while ((ip = strtok(NULL, ","))) { int_ent->next = malloc(sizeof(*int_ent)); int_ent = int_ent->next; sscanf(ip, "%d", &int_ent->value); int_ent->next = NULL; } } } fclose(dat_file); }
int signalprocess( const char * pid_file, int signal ) { FILE *file = NULL; char buffer[STRING_SIZE]; int pid = 0; memset(buffer, 0, STRING_SIZE); if ((file = fopen(pid_file, "r")) == NULL) { return -1; } if (fgets(buffer, STRING_SIZE - 1, file) == NULL) { fclose(file); return -2; } stripnl(buffer); pid = safeatoi(buffer); if (pid <= 1) { fclose(file); return -3; } fclose(file); int error = kill( pid, signal ); return( error ); }
int getdisksize(char *device) { FILE *f = NULL; char buffer[STRING_SIZE]; char *result = NULL; int count = 0; char *major = NULL, *minor = NULL, *blocks = NULL, *name = NULL; int size = 0; char *newdevice = NULL; if (!(newdevice = strstr(device, "/dev/"))) return 0; newdevice += strlen("/dev/"); memset(buffer, 0, STRING_SIZE - 1); if (!(f = fopen("/proc/partitions", "r"))) return 0; fgets(buffer, STRING_SIZE, f); fgets(buffer, STRING_SIZE, f); while (fgets(buffer, STRING_SIZE, f)) { stripnl(buffer); result = strtok(buffer, " "); count = 0; major = NULL; minor = NULL; blocks = NULL; name = NULL; while (result) { if (count == 0) major = result; else if (count == 1) minor = result; else if (count == 2) blocks = result; else name = result; count++; result = strtok(NULL, " "); } if (major && minor && blocks && name) { if (strcmp(name, newdevice) == 0) { size = atoi(blocks); break; } } } fclose(f); return size; }
void load_dict(Hashtable dict, const char *filename) { char *lineptr = NULL; size_t ptrsize = 512; FILE *dict_file = fopen(filename, "r"); while ((lineptr = malloc(512), getline(&lineptr, &ptrsize, dict_file)) != -1) { stripnl(lineptr); if (strlen(lineptr)) HTInsert(dict, lineptr, lineptr); } }
/* allows a user to run as us */ int main(int argc, char **argv) { int ok, arg, status; FILE *infd; struct utsname udata; struct stat statbuf; struct passwd *pw; char *pc, inbuf[BUFSIZ]; char user[BUFSIZ], mach[BUFSIZ], cmd[BUFSIZ]; char alwduser[BUFSIZ], alwdmach[BUFSIZ], alwdcmd[BUFSIZ]; /* open debug file */ dbgopen(DEBUGFILE); /* check if any cmds were given */ DBGDUMP("%s\n", "starting suid ..."); if (argc < 2) { dbgprintf("%s: no cmds given. (errno=%d)\n", argv[0], EINVAL); fprintf(stderr, "%s: no cmds given. (errno=%d)\n", argv[0], EINVAL); dbgclose(); exit(2); } /* dump ids */ DBGDUMP("uid = %d\n", getuid()); DBGDUMP("euid = %d\n", geteuid()); DBGDUMP("gid = %d\n", getgid()); DBGDUMP("egid = %d\n", getegid()); /* this tool is used specifically to allow set-uid. * if the setuid has not taken, then we should error * out immediately. verify that the effective uid * matches the owning uid for the suid tool. */ DBGDUMP("%s\n", "checking SETUID worked ..."); if (stat(SUID_PATH, &statbuf) != 0) { dbgprintf("%s: stat failed for file %s. (errno=%d)\n", argv[0], SUID_PATH, errno); fprintf(stderr, "%s: stat failed for file %s. (errno=%d)\n", argv[0], SUID_PATH, errno); exit(2); } if (statbuf.st_uid != geteuid()) { dbgprintf("%s: SUID != EUID !!!. SUID=%d, EUID=%d\n", argv[0], statbuf.st_uid, geteuid()); fprintf(stderr, "%s: SET UID FAILED !!!. EUID=%d, SUID=%d\n", argv[0], statbuf.st_uid, geteuid()); dbgclose(); exit(2); } if (statbuf.st_gid != getegid()) { dbgprintf("%s: SGID != EGID !!!. SGID=%d, EGID=%d\n", argv[0], statbuf.st_gid, getegid()); fprintf(stderr, "%s: SET GID FAILED !!!. EGID=%d, SGID=%d\n", argv[0], statbuf.st_gid, getegid()); dbgclose(); exit(2); } /* set environment variable to indicate owner called a tool */ DBGDUMP("statbuf.st_uid = %d\n", statbuf.st_uid); DBGDUMP("statbuf.st_gid = %d\n", statbuf.st_gid); DBGDUMP("getuid = %d\n", getuid()); DBGDUMP("getgid = %d\n", getgid()); if ((statbuf.st_uid == getuid()) && (statbuf.st_gid == getgid())) { DBGDUMP("FILEOWNER ... %s\n", owneryes); (void)putenv(owneryes); } else { DBGDUMP("FILEOWNER ... %s\n", ownerno); (void)putenv(ownerno); } /* get the name of the current machine */ DBGDUMP("%s\n", "calling uname ..."); if (uname(&udata) == -1) { dbgprintf("%s: uname failed. (errno=%d)\n", argv[0], errno); fprintf(stderr, "%s: uname failed. (errno=%d)\n", argv[0], errno); dbgclose(); exit(2); } strcpy(mach, udata.nodename); DBGDUMP("machine is %s\n", mach); /* get name of current user; user REAL UID, not EFFECTIVE UID */ DBGDUMP("calling getpwuid with uid=%d\n", getuid()); if ((pw = getpwuid(getuid())) == NULL) { dbgprintf("%s: unable to determine user name. (errno=%d)\n", argv[0], errno); fprintf(stderr, "%s: unable to determine user name. (errno=%d)\n", argv[0], errno); dbgclose(); exit(2); } strcpy(user, pw->pw_name); DBGDUMP("user is %s\n", user); /* open file of allowed commands */ DBGDUMP("opening file %s for read\n", ALLOWED_CMDS); if ((infd = fopen(ALLOWED_CMDS, "r")) == NULL) { dbgprintf("%s: allows cmds file %s not readable. (errno=%d)\n", argv[0], ALLOWED_CMDS, errno); fprintf(stderr, "%s: allows cmds file %s not readable. (errno=%d)\n", argv[0], ALLOWED_CMDS, errno); dbgclose(); exit(2); } DBGDUMP("allowed cmds file is %s\n", ALLOWED_CMDS); /* scan if cmd is allowed on this machine for this user */ ok = 0; strcpy(cmd, argv[1]); while (fgets(inbuf, BUFSIZ-1, infd) != NULL) { stripnl(inbuf); if (*inbuf == '#' || *inbuf == '\0') continue; sscanf(inbuf, "%s%s%s", alwdmach, alwduser, alwdcmd); if (strcmp(cmd, alwdcmd) == 0 && (strcmp(alwduser, "*") == 0 || strcmp(user, alwduser) == 0) && (strcmp(alwdmach, "*") == 0 || strcmp(mach, alwdmach) == 0)) { ok = 1; break; } } if (!ok) { dbgprintf("%s: cmd %s not allowed on machine %s for user %s.\n", argv[0], cmd, mach, user); fprintf(stderr, "%s: cmd %s not allowed on machine %s for user %s.\n", argv[0], cmd, mach, user); dbgclose(); exit(2); } DBGDUMP("cmd %s is allowed\n", cmd); /* make sure euid is set */ setuid(geteuid()); setgid(getegid()); /* put together one big command */ cmd[0] = '\0'; for (pc = cmd, arg = 1; arg < argc; arg++, pc++) { strcpy(pc, argv[arg]); pc += strlen(argv[arg]); *pc = ' '; } *pc = '\0'; /* run the command */ DBGDUMP("excuting cmd %s ...\n", cmd); status = system(cmd); DBGDUMP("return value is %d ...\n", status); status >>= 8; DBGDUMP("return value after shift is %d ...\n", status); /* close debug file */ dbgclose(); /* return status */ exit(status); }
int main(int argc, char **argv) { char vertag[128], lastlog[128], lasthash[128], *hash = NULL; FILE *stream = NULL; int gotrev = 0, needupdate = 1; vertag[0] = '\0'; lastlog[0] = '\0'; if (argc != 2) { fprintf(stderr, "Usage: %s <path to gitinfo.h>\n", argv[0]); return 1; } // Use git describe --tags to get a version string. If we are sitting directly // on a tag, it returns that tag. Otherwise it returns <most recent tag>-<number of // commits since the tag>-<short hash>. // Use git log to get the time of the latest commit in ISO 8601 format and its full hash. stream = popen("git describe --tags --dirty=-m && git log -1 --format=%ai*%H", "r"); if (NULL != stream) { if (fgets(vertag, sizeof vertag, stream) == vertag && fgets(lastlog, sizeof lastlog, stream) == lastlog) { stripnl(vertag); stripnl(lastlog); gotrev = 1; } pclose(stream); } if (gotrev) { hash = strchr(lastlog, '*'); if (hash != NULL) { *hash = '\0'; hash++; } } if (hash == NULL) { fprintf(stderr, "Failed to get commit info: %s\n", strerror(errno)); strcpy(vertag, "<unknown version>"); lastlog[0] = '\0'; lastlog[1] = '0'; lastlog[2] = '\0'; hash = lastlog + 1; } stream = fopen (argv[1], "r"); if (stream != NULL) { if (!gotrev) { // If we didn't get a revision but the file does exist, leave it alone. fclose (stream); return 0; } // Read the revision that's in this file already. If it's the same as // what we've got, then we don't need to modify it and can avoid rebuilding // dependant files. if (fgets(lasthash, sizeof lasthash, stream) == lasthash) { stripnl(lasthash); if (strcmp(hash, lasthash + 3) == 0) { needupdate = 0; } } fclose (stream); } if (needupdate) { stream = fopen (argv[1], "w"); if (stream == NULL) { return 1; } fprintf(stream, "// %s\n" "//\n" "// This file was automatically generated by the\n" "// updaterevision tool. Do not edit by hand.\n" "\n" "#define GIT_DESCRIPTION \"%s\"\n" "#define GIT_HASH \"%s\"\n" "#define GIT_TIME \"%s\"\n", hash, vertag, hash, lastlog); fclose(stream); fprintf(stderr, "%s updated to commit %s.\n", argv[1], vertag); } else { fprintf (stderr, "%s is up to date at commit %s.\n", argv[1], vertag); } return 0; }