static char * np_timestr(const u64 sec, const u64 nsec) { const time_t t = sec; char *s = "0"; if (sec > 0) { s = ctime(&t); _chomp(s); } return s; }
// * [ugoa]*([-+=]([rwxXst]*|[ugo]))+ extern "C" t_std_error std_user_chmod(const char *path, const char *perm_str) { //todo wrap this initialization with a mutex static std_mutex_lock_create_static_init_fast(lock); std_mutex_lock(&lock); static const mode_t all_mode_mask = _perm_map.find('a')->second.find('w')->second | _perm_map.find('a')->second.find('r')->second | _perm_map.find('a')->second.find('x')->second ; std_mutex_unlock(&lock); struct stat buf; if (stat(path, &buf)!=0) { int err = errno; EV_LOG(ERR,COM,0,"COM-USER-PERM","path element doesn't exist %s",path); return STD_ERR(COM,FAIL,err); } STD_ASSERT(path!=nullptr && perm_str!=nullptr); mode_t mod = 0; while(*perm_str!=0) { char op = '+'; mode_t _ch_mod = 0; if (!_chomp(perm_str,op,_ch_mod)) { EV_LOG(ERR,COM,0,"COM-USER-PERM","permission string invalid %s",perm_str); return STD_ERR(COM,PARAM,*perm_str); } if (op=='=') { mod = _ch_mod; } else { if (mod==0) mod = buf.st_mode & all_mode_mask; } if (op=='+') { mod |= _ch_mod; } if (op=='-') { mod = mod & (~_ch_mod); } } if (chmod(path,mod)!=0) { int err = errno; EV_LOG(ERR,COM,0,"COM-USER-PERM","Unable to change the file permissions %s:%x (%d)",path,mod,err); return STD_ERR(COM,FAIL,err); } return STD_ERR_OK; }
// parses openkubus-passwd file // returns 0 on success; if successfull user, key, data and num are filled with // data from file int _parse_passwd(FILE *file, const char *user, char **pw, char **offset, char **num) { int found = 0, i; const char *delim = " "; int len_user = strlen(user); while(fgets(line, BUFFER, file) != NULL) { int len = strlen(line); // line too long if(_chomp(line) == 0) continue; if(len > len_user) { if(strncmp(user, line, len_user) == 0) { for(i = 0; i < len; i++) if(line[i] == '\t') line[i] = ' '; if(strtok(line, delim) == NULL) continue; if((*pw = strtok(NULL, delim)) == NULL) continue; if((*offset = strtok(NULL, delim)) == NULL) continue; if((*num = strtok(NULL, delim)) == NULL) continue; found = 1; break; } } } if(!found) return 1; else return 0; }