//This tests the chmod function. //Changes the permissions of a given file, and compares it to the expected new permissions //If the permissions do not mathc the expected, returns 0. int chmodTest() { MINODE *test_mip = NULL; INODE *ip = NULL; int ino = 0; int expected1 = 33279; int expected2 = 33188; int expected3 = 33233; strcpy(pathname, ""); cd(pathname); test_mip = running->cwd; printf("\n\n-------- TESTING CHMOD FUNCTION --------\n\n"); strcpy(pathname, "tiny"); ino = getino(test_mip, pathname); test_mip = iget(dev, ino); ip = &test_mip->INODE; printf("tiny's current imode = %d\n", ip->i_mode); printf("Testing >chmod tiny 777\n"); strcpy(third, "777"); chmod_file(pathname); printf("\ntiny's new imode = %d\n", ip->i_mode); printf("tiny's expected imode = %d\n", expected1); if (ip->i_mode != expected1) { printf("TEST FAILED!\n\n"); return 0; } printf("TEST PASSED!\n\n"); ls(""); printf("Testing >chmod tiny 644\n"); strcpy(third, "644"); chmod_file(pathname); printf("\ntiny's new imode = %d\n", ip->i_mode); printf("tiny's expected imode = %d\n", expected2); if (ip->i_mode != expected2) { printf("TEST FAILED!\n\n"); return 0; } printf("TEST PASSED!\n\n"); ls(""); printf("Showing ls for Y\n"); strcpy(pathname, "Y"); ls(pathname); strcpy(pathname, ""); cd(pathname); test_mip = running->cwd; strcpy(pathname, "Y/bigfile"); ino = getino(test_mip, pathname); test_mip = iget(dev, ino); ip = &test_mip->INODE; printf("bigfiles's current imode = %d\n", ip->i_mode); printf("Testing >chmod Y/bigfile 721\n"); strcpy(third, "721"); strcpy(pathname, "Y/bigfile"); chmod_file(pathname); printf("\nbigfile's new imode = %d\n", ip->i_mode); printf("bigfile's expected imode = %d\n", expected3); if (ip->i_mode != expected3) { printf("TEST FAILED!\n\n"); return 0; } printf("TEST PASSED!\n\n"); printf("CLEANNING UP BELOW!\n"); strcpy(third, "644"); strcpy(pathname, "Y/bigfile"); chmod_file(pathname); cd(""); printf("ALL CHMOD TESTS PASSED!\n\n\n"); return 1; }
/** * Executes a rule. Contains evaluation of all conditions prior * to execution. * * @1 Hotplug event structure * @2 The rule to be executed * * Returns: 0 if success, -1 if the whole event is to be * discared, 1 if bail out of this particular rule was required */ int rule_execute(struct hotplug2_event_t *event, struct rule_t *rule) { int i, last_rv, res; char **env; for (i = 0; i < rule->conditions_c; i++) { if (rule_condition_eval(event, &(rule->conditions[i])) != EVAL_MATCH) return 0; } res = 0; last_rv = 0; env = xmalloc(sizeof(char *) * event->env_vars_c); for (i = 0; i < event->env_vars_c; i++) { env[i] = alloc_env(event->env_vars[i].key, event->env_vars[i].value); putenv(env[i]); } for (i = 0; i < rule->actions_c; i++) { switch (rule->actions[i].type) { case ACT_STOP_PROCESSING: res = 1; break; case ACT_STOP_IF_FAILED: if (last_rv != 0) res = 1; break; case ACT_NEXT_EVENT: res = -1; break; case ACT_NEXT_IF_FAILED: if (last_rv != 0) res = -1; break; case ACT_MAKE_DEVICE: last_rv = make_dev_from_event(event, rule->actions[i].parameter[0], strtoul(rule->actions[i].parameter[1], NULL, 0)); break; case ACT_CHMOD: last_rv = chmod_file(event, rule->actions[i].parameter[0], rule->actions[i].parameter[1]); break; case ACT_CHOWN: case ACT_CHGRP: last_rv = chown_chgrp(event, rule->actions[i].type, rule->actions[i].parameter[0], rule->actions[i].parameter[1]); break; case ACT_SYMLINK: last_rv = make_symlink(event, rule->actions[i].parameter[0], rule->actions[i].parameter[1]); break; case ACT_RUN_SHELL: last_rv = exec_shell(event, rule->actions[i].parameter[0]); break; case ACT_RUN_NOSHELL: last_rv = exec_noshell(event, rule->actions[i].parameter[0], rule->actions[i].parameter); break; case ACT_SETENV: last_rv = setenv(rule->actions[i].parameter[0], rule->actions[i].parameter[1], 1); break; case ACT_REMOVE: last_rv = unlink(rule->actions[i].parameter[0]); rmdir_p(rule->actions[i].parameter[0]); break; case ACT_DEBUG: print_debug(event); last_rv = 0; break; } if (res != 0) break; } for (i = 0; i < event->env_vars_c; i++) { unsetenv(event->env_vars[i].key); free(env[i]); } free(env); return res; }
void main(int argc, char *argv[]) { int i = 0, cmd = -1, fd = 0; char line[128], cname[64], temp_pathname[124], pathname[124] = "NULL", basename[124] = "NULL", dirname[124] = "NULL", parameter[124] = "NULL", *device; //GETS DISK NAME TO MOUNT /* printf("Enter the name of your disk image\n"); fgets(device, 128, stdin); device[strlen(device)-1] = 0; // kill the \n char at end */ device = "mydisk"; mount_root(device, &fd); while(1) { //printf("P%d running: ", running.uid); printf("nick's@linux:"); pwd(P0.cwd); printf("$ "); fgets(line, 128, stdin); line[strlen(line)-1] = 0; // kill the \n char at end if (line[0]==0) continue; sscanf(line, "%s %s %s", cname, pathname, parameter); if(strcmp(pathname,"NULL") != 0) strcpy(PATHNAME, pathname); if(strcmp(parameter,"NULL") != 0) strcpy(PARAMETER, parameter); cmd = findCmd(cname); // map cname to an index switch(cmd) { case 0 : menu(); break; case 1 : pwd(P0.cwd);printf("\n"); break; case 2 : ls(); break; case 3 : cd(); break; case 4 : make_dir(); break; case 5 : rmdir(); break; case 6 : creat_file(); break; case 7 : link(); break; case 8 : unlink(); break; case 9 : symlink(); break; case 10: rm_file(); break; case 11: chmod_file(); break; case 12: chown_file(); break; case 13: stat_file(); break; case 14: touch_file(); break; case 20: open_file(); break; //LEVEL 2 case 21: close_file((int)PATHNAME); break; case 22: pfd(); break; case 23: lseek_file(); break; case 24: //access_file(); break; break; case 25: //read_file(); break; case 26: write_file(); break; case 27: cat_file(); break; case 28: cp_file(); break; case 29: mv_file(); break; case 30: mount(); break; //LEVLEL 3 case 31: //umount(); break; case 32: //cs(); break; case 33: //do_fork(); break; case 34: //do_ps(); break; case 40: //sync(); break; case 41: quit(); break; case 42 : system("clear"); break; default: printf("invalid command\n"); break; } //RESET NAMES strcpy(parameter, "NULL"); strcpy(pathname, "NULL"); strcpy(PATHNAME, "NULL"); } }
/*----------------------------------------------------------------------*/ int daf_service_install(char *daf_binary_path, char *argv0) { char cmd[1024]; char msg[1024]; char daf_path[64]; char daf_binary_to_install[64]; char timestamp[16]; char backup_inittab_filename[64]; struct stat fileStat; char *pathname = "/etc/inittab"; if (daf_binary_path == NULL) { if (get_current_executable_path(argv0, daf_binary_to_install, sizeof(daf_binary_to_install)) !=0) { print_msg_to_console("Problem trying to determine path of current executable\n"); return(1); } } else { safecpy(daf_binary_to_install, daf_binary_path, sizeof(daf_binary_to_install)); } // ensure that directories that are needed do actually exist and delelete existing profile // and start up script ensure_directory_path_exists(DAF_SERVICE_BINARY_DIR); ensure_directory_path_exists(DAF_SERVICE_LOG_DIR); delete_file(DAF_SERVICE_START_SCRIPT_PATH); delete_file(DAF_SERVICE_PROFILE_PATH); // are we trying to copy the same file onto itself, -if so then // do not do the copy as it it will fail if (strcmp(daf_binary_to_install, DAF_SERVICE_BINARY_PATH) == 0) { sprintf(msg, "Requested install image %s is identical to install destination: %s\n", daf_binary_to_install, DAF_SERVICE_BINARY_PATH); print_msg_to_console(msg); } else { if (copy_file(daf_binary_to_install, DAF_SERVICE_BINARY_PATH) != 0) { /* make sure the SETuid bit is on so that daf can assume root priviledges if it is run by a non root user */ /* is this a security hole ?? <<<<<<<<<< */ sprintf(msg, "Could not copy %s into installation directory at %s\n", daf_binary_to_install, DAF_SERVICE_BINARY_PATH); print_msg_to_console(msg); return(1); } else { if (chmod_file(DAF_SERVICE_BINARY_PATH, "u+s") != 0) { sprintf(msg, "Could not set setuid permission bit on %s\n", DAF_SERVICE_BINARY_PATH); print_msg_to_console(msg); return(1); } } } if (create_default_daf_service_profile() != 0) { print_msg_to_console("Could not install command profile into standard daf service directory\n"); return(1); } else { print_msg_to_console("daf service command profile installed successfully\n"); } if (create_daf_service_start_script() != 0) { print_msg_to_console("Could not install start script into standard daf service directory\n"); return(1); } else { print_msg_to_console("daf service start script installed successfully\n"); } //-------------------------------------------------------------------------- // //-------------------------------------------------------------------------- safecpy(daf_path, DAF_SERVICE_BINARY_PATH, sizeof(daf_path)); if (is_daf_service_running()) { print_msg_to_console("daf daemon is running - and will now be stopped\n"); if (stop_daf_service_running() != 0) { print_msg_to_console("Problem trying to stop daf daemon\n"); } } print_msg_to_console("Updating /etc/inittab entry\n"); #if defined AIX //-------------------------------------------------------------------------- // remove any previous entry //-------------------------------------------------------------------------- print_msg_to_console("Removing any previous daf service entry in /etc/inittab\n"); safecpy(cmd, "cat /etc/inittab | grep \"daf:\"", sizeof(cmd)); if (run_system_cmd(cmd, 1) == 0) { safecpy(cmd, "rmitab \"daf\"", sizeof(cmd)); if (run_system_cmd_and_print_all_output(cmd) != 0) { safecpy(msg, "Removal of daf service entry in /etc/inittab failed: ", sizeof(msg)); safecat(msg, cmd, sizeof(msg)); safecat(msg, "\n", sizeof(msg)); print_msg_to_console(msg); return(1); } } //-------------------------------------------------------------------------- // Add daf daemon to /etc/inittab //-------------------------------------------------------------------------- safecpy(backup_inittab_filename, "/etc/inittab.", sizeof(backup_inittab_filename)); get_current_time_as_timestamp(timestamp, sizeof(timestamp), '.'); safecat(backup_inittab_filename, timestamp, sizeof(backup_inittab_filename)); sprintf(msg, "Backing up /etc/inittab in %s\n", backup_inittab_filename); print_msg_to_console(msg); if (copy_file("/etc/inittab", backup_inittab_filename) != 0) { safecpy(msg, "Backup of inittab failed: ", sizeof(msg)); safecat(msg, "Could not copy /etc/inittab to %s", backup_inittabl_filename, sizeof(msg)); print_msg_to_console(msg); return(1); } print_msg_to_console("Adding daf service entry to /etc/inittab\n"); safecpy(cmd, "mkitab \"daf:2:once:", sizeof(cmd)); safecat(cmd, DAF_SERVICE_INVOCATION, sizeof(cmd)); safecat(cmd, " >/dev/null 2>&1", sizeof(cmd)); safecat(cmd, "\"", sizeof(cmd)); if (run_system_cmd_and_print_all_output(cmd) != 0) { safecpy(msg, "Update of inittab failed: ", sizeof(msg)); safecat(msg, cmd, sizeof(msg)); safecat(msg, "\n", sizeof(msg)); print_msg_to_console(msg); return(1); } #elif defined LINUX || defined SOLARIS || defined HPUX //-------------------------------------------------------------------------- // Add daf daemon to /etc/inittab // First check to see if /etc/inittab exists - if it does not this may be // Debian or Ubuntu in which case we must create an empty file // (Debian/Ubuntu use the Startup mechanism instead of /etc/inittab but // still support the old /etc/inittab file it is present // see for instance: http://upstart.ubuntu.com/cookbook/ //-------------------------------------------------------------------------- if (!does_file_exist("/etc/inittab")) { sprintf(msg, "/etc/inittab did not exist - assuming Debian/Ubuntu - so creating /etc/init entry\n"); print_msg_to_console(msg); create_etc_init_conf(); return(0); } if (stat(pathname, &fileStat) < 0) { sprintf(msg, "stat(%s) failed - it seems %s is missing !!!!!!!!!!!!!!!!!!!!! \n", pathname, pathname ); print_msg_to_console(msg); return(1); } // Later versions of Redhat etc do not use /etc/inittab (but will still honour it if exists - so if the file is empty, // we just put a single comment line into so that later code that updates the /etc/inittab file will work if (fileStat.st_size == 0) { if (run_system_cmd("cat '* just a comment' >>/etc/inittab", 0) != 0) { safecpy(msg, "cat \"* just a comment\" >>/etc/inittab failed\n", sizeof(msg)); print_msg_to_console(msg); return(1); } } //-------------------------------------------------------------------------- // remove any previous entry, making sure we backup /etc/inittab //-------------------------------------------------------------------------- safecpy(backup_inittab_filename, "/etc/inittab.", sizeof(backup_inittab_filename)); get_current_time_as_timestamp(timestamp, sizeof(timestamp), '.'); safecat(backup_inittab_filename, timestamp, sizeof(backup_inittab_filename)); sprintf(msg, "Backing up /etc/inittab in %s\n", backup_inittab_filename); print_msg_to_console(msg); print_msg_to_console("Removing any previous daf service entry in /etc/inittab\n"); if (copy_file("/etc/inittab", backup_inittab_filename) != 0) { snprintf(msg, sizeof(msg), "Copy of /etc/inittab to %s failed\n", backup_inittab_filename); print_msg_to_console(msg); return(1); } safecpy(cmd, "cat ", sizeof(cmd)); safecat(cmd, backup_inittab_filename, sizeof(cmd)); safecat(cmd, " | grep -v \""DAF_SERVICE_INVOCATION"\" > /etc/inittab", sizeof(cmd)); if (run_system_cmd(cmd, 0) != 0) { safecpy(msg, "Removal of previous daf service in inittab failed: ", sizeof(msg)); safecat(msg, cmd, sizeof(msg)); safecat(msg, "\n", sizeof(msg)); print_msg_to_console(msg); return(1); } safecpy(cmd, "cat ", sizeof(cmd)); safecat(cmd, backup_inittab_filename, sizeof(cmd)); safecat(cmd, " | grep -v \""DAF_SERVICE_INVOCATION"\" > /etc/inittab", sizeof(cmd)); if (run_system_cmd(cmd, 0) != 0) { safecpy(msg, "Removal of previous daf service in inittab failed: ", sizeof(msg)); safecat(msg, cmd, sizeof(msg)); safecat(msg, "\n", sizeof(msg)); print_msg_to_console(msg); return(1); } //-------------------------------------------------------------------------- // Add daf daemon to /etc/inittab //-------------------------------------------------------------------------- print_msg_to_console("Adding daf service entry to /etc/inittab\n"); safecpy(cmd, "echo \"daf:345:once:", sizeof(msg)); safecat(cmd, DAF_SERVICE_INVOCATION, sizeof(cmd)); safecat(cmd, " \" 2>/dev/null >>/etc/inittab", sizeof(cmd)); if (run_system_cmd(cmd, 0) != 0) { safecpy(msg, "Adding daf service entry to /etc/inittab: ", sizeof(msg)); safecat(msg, cmd, sizeof(msg)); safecat(msg, "\n", sizeof(msg)); print_msg_to_console(msg); return(1); } #endif print_msg_to_console("/etc/inittab entry updated successfully\n"); return(0); }