/* // match the encrypted string, use SHS (default for crypt()) if // 'encrypted' begins with "$2$" (FreeBSD standard); otherwise // pass back to the OS crypt() */ Int match_crypted(cStr * encrypted, cStr * possible) { uChar * ep, * pp, *sp, salt[9]; char p_buf[SHS_OUTPUT_SIZE]; Int sl, el, pl, x; ep = (uChar *) string_chars(encrypted); el = string_length(encrypted); pp = (uChar *) string_chars(possible); pl = string_length(possible); if (el < 3) { cthrow(type_id, "Invalid password format"); return -1; } if (ep[0] == '$' && ep[1] == '2' && ep[2] == '$') { sp = ep + 3; for (x=0; x < 8 && sp[x] != '$'; x++) salt[x] = sp[x]; salt[x] = 0; sp = salt; sl = strlen((char *) sp); shs_crypt((uChar *) pp, pl, sp, sl, p_buf); return (!strcmp((char *) ep, p_buf)); } else { #ifdef USE_OS_CRYPT #ifdef sys_freebsd sp = ep; #else /* assume ancient DES format, with the first two chars as salt */ salt[0] = ep[0]; salt[1] = ep[1]; salt[2] = 0; sp = salt; #endif return (!strcmp((char *) ep, (char *) crypt((char *) pp, (char *) sp))); #else cthrow(type_id, "Driver was not compiled with OS crypt() support."); return -1; #endif } }
void func_reassign_connection(void) { cData * args; Conn * c; Obj * obj; /* Accept a objnum. */ if (!func_init_1(&args, OBJNUM)) return; c = find_connection(cur_frame->object); if (c) { obj = cache_retrieve(args[0].u.objnum); if (!obj) THROW((objnf_id, "Object #%l does not exist.", args[0].u.objnum)) else if (find_connection(obj)) { cthrow(perm_id, "Object %O already has a connection.", obj->objnum); cache_discard(obj); return; } c->objnum = obj->objnum; cache_discard(obj); cur_frame->object->conn = NULL; pop(1); push_int(1); } else {
cStr *build_path(char *fname, struct stat * sbuf, Int nodir) { Int len = strlen(fname); cStr *str = NULL; if (len == 0) THROWN((file_id, "No file specified.")) #ifdef RESTRICTIVE_FILES if (strstr(fname, "../") || strstr(fname, "/..") || !strcmp(fname, "..")) THROWN((perm_id, "Filename \"%s\" is not legal.", fname)) str = string_from_chars(c_dir_root, strlen(c_dir_root)); str = string_addc(str, '/'); str = string_add_chars(str, fname, len); #else if (*fname != '/') { str = string_from_chars(c_dir_root, strlen(c_dir_root)); str = string_addc(str, '/'); str = string_add_chars(str, fname, len); } else { str = string_from_chars(fname, len); } #endif if (sbuf != NULL) { if (stat(str->s, sbuf) < 0) { cthrow(file_id, "Cannot find file \"%s\".", str->s); string_discard(str); return NULL; } if (nodir) { if (S_ISDIR(sbuf->st_mode)) { cthrow(directory_id, "\"%s\" is a directory.", str->s); string_discard(str); return NULL; } } } return str; }
void func_dict_del(void) { cData * args; if (!func_init_2(&args, DICT, 0)) return; if (!dict_contains(args[0].u.dict, &args[1])) { cthrow(keynf_id, "Key (%D) is not in the dictionary.", &args[1]); } else { anticipate_assignment(); args[0].u.dict = dict_del(args[0].u.dict, &args[1]); pop(1); } }
/* which is 1 for max, -1 for min. */ INTERNAL void find_extreme(Int which) { Int arg_start, num_args, i, type; cData *args, *extreme, d; arg_start = arg_starts[--arg_pos]; args = &stack[arg_start]; num_args = stack_pos - arg_start; if (!num_args) { cthrow(numargs_id, "Called with no arguments, requires at least one."); return; } type = args[0].type; if (type != INTEGER && type != STRING && type != FLOAT) { cthrow(type_id, "First argument (%D) not an integer, float or string.", &args[0]); return; } extreme = &args[0]; for (i = 1; i < num_args; i++) { if (args[i].type != type) { cthrow(type_id, "Arguments are not all of same type."); return; } if (data_cmp(&args[i], extreme) * which > 0) extreme = &args[i]; } /* Replace args[0] with extreme, and pop other arguments. */ data_dup(&d, extreme); data_discard(&args[0]); args[0] = d; pop(num_args - 1); }
cBuf *read_binary_file(filec_t * file, Int block) { cBuf *buf = buffer_new(block); /* Patch #6 -- Bruce Mitchner */ if (feof(file->fp)) { cthrow(eof_id, "End of file."); buffer_discard(buf); return NULL; } buf->len = fread(buf->s, sizeof(unsigned char), block, file->fp); return buf; }
cList *open_file(cStr * name, cStr * smode, Obj * obj) { char mode[4]; Int rw = 0; char *s = NULL; filec_t *fnew = file_new(); struct stat sbuf; /* parse the mode first, if the string pointer is NULL, set it readable */ if (smode != NULL) { s = smode->s; if (*s == '+') { rw = 1; fnew->f.readable = fnew->f.writable = 1; s++; } if (*s == '>') { s++; if (*s == '>') { s++; mode[0] = 'a'; } else { mode[0] = 'w'; } fnew->f.writable = 1; } else { if (*s == '<') s++; mode[0] = 'r'; fnew->f.readable = 1; } /* here is where we branch from perl, '-' is used to specify it as a 'binary' file (i.e. use buffers not cold strings) */ if (*s == '-') { s++; fnew->f.binary = 1; } } else { mode[0] = 'r'; fnew->f.readable = 1; } /* most systems ignore this, some need it */ mode[1] = 'b'; if (rw) { mode[2] = '+'; mode[3] = '\0'; } else { mode[2] = '\0'; } fnew->path = build_path(name->s, NULL, DISALLOW_DIR); if (fnew->path == NULL) return NULL; /* redundant, as build_path could have done this, but we have a special case which we need to handle differently */ if (stat(fnew->path->s, &sbuf) == F_SUCCESS) { /* Patch #6 -- Bruce Mitchener */ if (S_ISDIR(sbuf.st_mode)) { cthrow(directory_id, "\"%s\" is a directory.", fnew->path->s); file_discard(fnew, NULL); return NULL; } } fnew->fp = fopen(fnew->path->s, mode); if (fnew->fp == NULL) { if (GETERR() == ERR_NOMEM) panic("open_file(): %s", strerror(GETERR())); cthrow(file_id, "%s (%s)", strerror(GETERR()), name->s); file_discard(fnew, NULL); return NULL; } file_add(fnew); obj->file = fnew; fnew->objnum = obj->objnum; return statbuf_to_list(&sbuf); }