static int mdb_update_style(struct menudb_handle *mh, const ChopstixItemCode code, const ChopstixItemStyle *style) { sqlite3_stmt *q = NULL; CHECKSQL(mh); if (sqlite3_prepare(SQLARG(mh)->db, SQL_MDB_UPD_MENUSTYLE, strlen(SQL_MDB_UPD_MENUSTYLE), &q, NULL)) { sql_err(SQLARG(mh), "MQ PREP UPD MENUSTYLE"); goto fail; } /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(q, 1, style->name, strlen(style->name), SQLITE_STATIC)) goto fail; if (sqlite3_bind_int(q, 2, style->num)) goto fail; if (sqlite3_step(q) != SQLITE_DONE) { sql_err(SQLARG(mh), "cannot update menu item"); goto fail; } sqlite3_finalize(q); return 0; fail: if (q) sqlite3_finalize(q); return -1; }
static int mdb_put_item(struct menudb_handle *mh, const ChopstixMenuitem *mi) { sqlite3_stmt *q = NULL; CHECKSQL(mh); if (sqlite3_prepare(SQLARG(mh)->db, SQL_MDB_PUT_MENUITEM, strlen(SQL_MDB_PUT_MENUITEM), &q, NULL)) { sql_err(SQLARG(mh), "MQ PREP PUT MENUITEM"); goto fail; } /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(q, 1, mi->code, strlen(mi->code), SQLITE_STATIC)) goto fail; if (sqlite3_bind_int(q, 2, mi->gen)) goto fail; if (sqlite3_bind_text(q, 3, mi->name, strlen(mi->name), SQLITE_STATIC)) goto fail; if (sqlite3_bind_int(q, 4, mi->price)) goto fail; if (sqlite3_step(q) != SQLITE_DONE) { sql_err(SQLARG(mh), "cannot put menu item"); goto fail; } sqlite3_finalize(q); return 0; fail: if (q) sqlite3_finalize(q); return -1; }
void sql_get_last_id(void) { size_t size; size=strlen(GET) +strlen(conf.db_database) +strlen(conf.db_table) +1; char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if((size=snprintf(buf,size, GET, conf.db_database, conf.db_table))) { if(mysql_real_query(conn,buf,size)) sql_err(conn,buf); MYSQL_RES *res; res=mysql_store_result(conn); if(!res) sql_err(conn,buf); free(buf); MYSQL_ROW row; row=mysql_fetch_row(res); if(!row) id=0; else if(sscanf(row[0],"%li",&id)==0) { fprintf(stderr,"Failed to fetching row counter.\n"); fprintf(stderr,"Starting at 0.\n"); id=0; } mysql_free_result(res); } }
static int cdb_put(struct custdb_handle *ch, const ChopstixCustomer *cust) { char *phonestr, *phoneext, *special; sqlite3_stmt *q = NULL; CHECKSQL(ch); if ((phonestr = phone2str(&cust->phone)) == NULL) goto fail; if ((phoneext = phoneext2str(&cust->phone)) == NULL) goto fail; /* * CUSTOMER */ q = SQLARG(ch)->q.cdb.put_cust; /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(q, 1, phonestr, strlen(phonestr), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 2, phoneext, strlen(phoneext), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 3, cust->name, strlen(cust->name), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 4, cust->addr.addr, strlen(cust->addr.addr), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 5, cust->addr.apt, strlen(cust->addr.apt), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 6, cust->addr.entry, strlen(cust->addr.entry), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 7, cust->isect.cross, strlen(cust->isect.cross), SQLITE_STATIC)) goto fail; if (cust->special) special = *cust->special; else special = ""; if (sqlite3_bind_text(q, 8, special, strlen(special), SQLITE_STATIC)) goto fail; if (sqlite3_step(q) != SQLITE_DONE) { sql_err(SQLARG(ch), "cannot add customer"); goto fail; } sqlite3_reset(q); return 0; fail: sql_err(SQLARG(ch), "cannot bind data to query"); if (q) sqlite3_reset(q); return -1; }
void sql_dump_write(void) { data_dump.logg_id=id; char *to; to=malloc(data_dump.size*2+1); if(!to) exit(EXIT_FAILURE); unsigned long length; length=mysql_real_escape_string(conn,to,data_dump.write_data,data_dump.size); size_t size; size=strlen(BWD) +strlen(conf.db_database) +strlen(conf.db_dump_table) +20 +10 +10 +length +1; char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if((size=snprintf(buf,size, BWD, conf.db_database, conf.db_dump_table, data_dump.logg_id, data_dump.size, data_dump.offset, to))) { if(mysql_real_query(conn,buf,size)) sql_err(conn,buf); } free(to); free(buf); }
void sql_write_err(void) { errors.logg_id=id; char str[512]; memset(str,0,512); int ret; ret=strerror_r(errno,str,512); if(ret!=0) snprintf(str,512,"Unknown error"); snprintf(errors.error_message,512,"ERROR (%s)",str); size_t size; size=strlen(ERR) +strlen(conf.db_database) +strlen(conf.db_error_table) +20 +strlen(errors.error_message) +1; char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if((size=snprintf(buf,size, ERR, conf.db_database, conf.db_error_table, errors.logg_id, errors.error_message))) { if(mysql_real_query(conn,buf,size)) sql_err(conn,buf); } free(buf); }
static int mdb_put_subitem(struct menudb_handle *mh, const ChopstixItemCode code, const ChopstixItemExtra *subitem) { sqlite3_stmt *q = NULL; CHECKSQL(mh); if (sqlite3_prepare(SQLARG(mh)->db, SQL_MDB_PUT_MENUSUBITEM, strlen(SQL_MDB_PUT_MENUSUBITEM), &q, NULL)) { sql_err(SQLARG(mh), "MQ PREP PUT MENUSUBITEM"); goto fail; } /* 1 = first parameter in prepared query */ if (sqlite3_bind_int(q, 1, subitem->qty)) goto fail; if (sqlite3_bind_text(q, 2, subitem->code, strlen(subitem->code), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 3, code, strlen(code), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 4, code, strlen(code), SQLITE_STATIC)) goto fail; if (sqlite3_step(q) != SQLITE_DONE) { sql_err(SQLARG(mh), "cannot put menu subitem"); goto fail; } sqlite3_finalize(q); return 0; fail: if (q) sqlite3_finalize(q); return -1; }
static int mdb_load(struct menudb_handle *mh, ChopstixMenu *menu) { sqlite3_stmt *q = NULL; ChopstixMenuitem *mi; ChopstixItemCode code; CHECKSQL(mh); bzero(menu, sizeof(*menu)); if (sqlite3_prepare(SQLARG(mh)->db, SQL_MDB_LOAD_MENU, strlen(SQL_MDB_LOAD_MENU), &q, NULL)) { sql_err(SQLARG(mh), "MQ PREP LOAD MENU"); goto fail; } while (sqlite3_step(q) == SQLITE_ROW) { if ((mi = realloc(menu->val, (menu->len + 1) * sizeof(ChopstixMenuitem))) == NULL) goto fail; menu->len++; menu->val = mi; (const char *)code = sqlite3_column_text(q, 0); if (mdb_get(mh, code, &menu->val[menu->len - 1]) == -1) goto fail; } sqlite3_finalize(q); return 0; fail: free_ChopstixMenu(menu); if (q) sqlite3_finalize(q); return -1; }
static int mdb_get(struct menudb_handle *mh, const ChopstixItemCode code, ChopstixMenuitem *item) { sqlite3_stmt *q = NULL; CHECKSQL(mh); bzero(item, sizeof(*item)); /* * PRIMARY MENUITEM */ q = SQLARG(mh)->q.mdb.get_menuitem; /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(q, 1, code, strlen(code), SQLITE_STATIC)) { sql_err(SQLARG(mh), "cannot bind code number to query"); goto fail; } if (sqlite3_step(q) == SQLITE_ROW) { item->code = sql_strdup(sqlite3_column_text(q, 0)); item->name = sql_strdup(sqlite3_column_text(q, 1)); item->price = sqlite3_column_int(q, 2); item->gen = sqlite3_column_int(q, 3); item->flags.deleted = sqlite3_column_int(q, 4); } else { sql_err(SQLARG(mh), "GET MENUITEM code=%s", code); errno = ENOENT; goto fail; } sqlite3_reset(q); /* * STYLES */ q = SQLARG(mh)->q.mdb.get_menustyles; /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(q, 1, code, strlen(code), SQLITE_STATIC)) { sql_err(SQLARG(mh), "cannot bind code number to query"); goto fail; } if (sqlite3_bind_text(q, 2, code, strlen(code), SQLITE_STATIC)) { sql_err(SQLARG(mh), "cannot bind code number to query"); goto fail; } while (sqlite3_step(q) == SQLITE_ROW) { ChopstixItemStyle *is; if ((is = realloc(item->styles.val, (item->styles.len + 1) * sizeof(ChopstixItemStyle))) == NULL) goto fail; item->styles.len++; item->styles.val = is; item->styles.val[item->styles.len - 1].name = sql_strdup(sqlite3_column_text(q, 0)); item->styles.val[item->styles.len - 1].num = sqlite3_column_int(q, 1); } sqlite3_reset(SQLARG(mh)->q.mdb.get_menustyles); /* * EXTRAS */ q = SQLARG(mh)->q.mdb.get_menuextras; /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(q, 1, code, strlen(code), SQLITE_STATIC)) { sql_err(SQLARG(mh), "cannot bind code number to query"); goto fail; } if (sqlite3_bind_text(q, 2, code, strlen(code), SQLITE_STATIC)) { sql_err(SQLARG(mh), "cannot bind code number to query"); goto fail; } while (sqlite3_step(q) == SQLITE_ROW) { ChopstixItemExtra *ie; if ((ie = realloc(item->extras.val, (item->extras.len + 1) * sizeof(ChopstixItemExtra))) == NULL) goto fail; item->extras.len++; item->extras.val = ie; item->extras.val[item->extras.len - 1].qty = sqlite3_column_int(q, 0); item->extras.val[item->extras.len - 1].code = sql_strdup(sqlite3_column_text(q, 1)); } sqlite3_reset(SQLARG(mh)->q.mdb.get_menuextras); /* * SUBITEMS (Same as extras) */ q = SQLARG(mh)->q.mdb.get_menusubitems; /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(q, 1, code, strlen(code), SQLITE_STATIC)) { sql_err(SQLARG(mh), "cannot bind code number to query"); goto fail; } if (sqlite3_bind_text(q, 2, code, strlen(code), SQLITE_STATIC)) { sql_err(SQLARG(mh), "cannot bind code number to query"); goto fail; } while (sqlite3_step(q) == SQLITE_ROW) { ChopstixItemExtra *ie; if (item->subitems == NULL) if ((item->subitems = calloc(1, sizeof(*item->subitems))) == NULL) goto fail; if ((ie = realloc(item->subitems->val, (item->subitems->len + 1) * sizeof(ChopstixItemExtra))) == NULL) goto fail; item->subitems->len++; item->subitems->val = ie; item->subitems->val[item->subitems->len - 1].qty = sqlite3_column_int(q, 0); item->subitems->val[item->subitems->len - 1].code = sql_strdup(sqlite3_column_text(q, 1)); } sqlite3_reset(SQLARG(mh)->q.mdb.get_menusubitems); return 0; fail: free_ChopstixMenuitem(item); if (q) sqlite3_reset(q); return -1; }
void sql_write(const char* path,const char* op) { memset(&logg,0,sizeof(logg)); ++id; logg.logg_id=id; get_timestamp(0,logg.time); strncpy(logg.operation,op,32); // REMEMBER: path my be non existing // char *absolute_path; char *relative_path; absolute_path=get_abs_path(path); relative_path=get_rel_path(path); strncpy(logg.file,absolute_path,1024); struct stat stbuf; if(lstat(relative_path,&stbuf)==0) { logg.protection=(unsigned int)stbuf.st_mode; uid_t uid; uid=stbuf.st_uid; size_t buflen; buflen=sysconf(_SC_GETPW_R_SIZE_MAX); if(buflen==-1) exit(EXIT_FAILURE); char *buf; buf=malloc(buflen); if(!buf) exit(EXIT_FAILURE); struct passwd pwd; struct passwd *pwd_result; if(getpwuid_r(uid,&pwd,buf,buflen,&pwd_result)==0) { if(pwd_result) strncpy(logg.owner,pwd.pw_name,256); } free(buf); gid_t gid; gid=stbuf.st_gid; buflen=sysconf(_SC_GETGR_R_SIZE_MAX); if(buflen==-1) exit(EXIT_FAILURE); buf=malloc(buflen); if(!buf) exit(EXIT_FAILURE); struct group grp; struct group *grp_result; if(getgrgid_r(gid,&grp,buf,buflen,&grp_result)==0) { if(grp_result) strncpy(logg.group,grp.gr_name,256); } free(buf); } free(absolute_path); free(relative_path); strncpy(logg.host,conf.hostname,256); strncpy(logg.tag,conf.tagname,64); logg.uid=(unsigned int)fuse_get_context()->uid; logg.gid=(unsigned int)fuse_get_context()->gid; logg.pid=(unsigned int)fuse_get_context()->pid; if(conf.log_username) { uid_t uid; uid=fuse_get_context()->uid; size_t buflen; buflen=sysconf(_SC_GETPW_R_SIZE_MAX); if(buflen==-1) exit(EXIT_FAILURE); char *buf; buf=malloc(buflen); if(!buf) exit(EXIT_FAILURE); struct passwd pwd; struct passwd *result; if(getpwuid_r(uid,&pwd,buf,buflen,&result)==0) { if(result) strncpy(logg.username,pwd.pw_name,256); } free(buf); if(is_str(logg.username)) { if(conf.log_tty||conf.log_remote_host||conf.log_login_time) { setutxent(); struct utmpx *utx; while((utx=getutxent())) { if(!strcmp(logg.username,utx->ut_user)) { if(conf.log_tty) strncpy(logg.tty,utx->ut_line,16); if(conf.log_remote_host) strncpy(logg.remote_host,utx->ut_host,256); if(conf.log_login_time) get_timestamp(utx->ut_tv.tv_sec,logg.login_time); } } endutxent(); } } } if(conf.log_groupname) { gid_t gid; gid=fuse_get_context()->gid; size_t buflen; buflen=sysconf(_SC_GETGR_R_SIZE_MAX); if(buflen==-1) exit(EXIT_FAILURE); char *buf; buf=malloc(buflen); if(!buf) exit(EXIT_FAILURE); struct group grp; struct group *result; if(getgrgid_r(gid,&grp,buf,buflen,&result)==0) { if(result) strncpy(logg.groupname,grp.gr_name,256); } free(buf); } if(conf.log_cmd) { size_t size; size=strlen("/proc//cmdline")+10+1; char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if(snprintf(buf,size,"/proc/%i/cmdline",logg.pid)) { FILE *fp; fp=fopen(buf,"r"); free(buf); if(!fp) return; buf=malloc(1024); if(!buf) exit(EXIT_FAILURE); size_t bytes_r; bytes_r=fread(buf,1,1024,fp); fclose(fp); if(bytes_r==0) { free(buf); return; } strncpy(logg.cmd,buf,1024); if(conf.log_args) { int i=0; while(++i<bytes_r-1) if(buf[i]==0x00) buf[i]=0x20; int str_off=strlen(logg.cmd); strncpy(logg.args,&buf[str_off],1024); free(buf); } } } if(conf.log_ppid) { size_t size; size=strlen("/proc//status")+10+1; char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if(snprintf(buf,size,"/proc/%i/status",logg.pid)) { FILE *fp; fp=fopen(buf,"r"); free(buf); if(!fp) return; char *line=NULL; size_t len=0; while(getline(&line,&len,fp)!=-1) { if(!strncmp(line,"PPid:",5)) { if((strlen(line)-6)<20) sscanf(line,"PPid:\t%i",&logg.ppid); break; } } fclose(fp); free(line); } if(conf.log_ppid_cmd) { size_t size; size=strlen("/proc//cmdline")+10+1; char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if(snprintf(buf,size,"/proc/%i/cmdline",logg.ppid)) { FILE *fp; fp=fopen(buf,"r"); free(buf); if(!fp) return; buf=malloc(1024); if(!buf) exit(EXIT_FAILURE); size_t bytes_r; bytes_r=fread(buf,1,1024,fp); fclose(fp); if(bytes_r==0) { free(buf); return; } strncpy(logg.p_cmd,buf,1024); free(buf); } } } size_t size; size=strlen(INS) +strlen(conf.db_database) +strlen(conf.db_table) +20 +strlen(logg.time) +strlen(logg.host) +strlen(logg.tag) +strlen(logg.operation) +10 +10 +strlen(logg.username) +strlen(logg.groupname) +strlen(logg.tty) +strlen(logg.login_time) +strlen(logg.remote_host) +strlen(logg.cmd) +strlen(logg.args) +10 +10 +strlen(logg.p_cmd) +strlen(logg.file) +10 +strlen(logg.owner) +strlen(logg.group); char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if((size=snprintf(buf,size, INS, conf.db_database, conf.db_table, logg.logg_id, logg.time, logg.host, logg.tag, logg.operation, logg.uid, logg.gid, logg.username, logg.groupname, logg.tty, logg.login_time, logg.remote_host, logg.cmd, logg.args, logg.pid, logg.ppid, logg.p_cmd, logg.file, logg.protection, logg.owner, logg.group))) { if(mysql_real_query(conn,buf,size)) sql_err(conn,buf); } free(buf); }
void test_cfg(void) { fprintf(stdout,"\nConfiguration\n"); fprintf(stdout,"----------------------------------------------------\n"); fprintf(stdout,"hostname :\t\t%s\n",conf.hostname); fprintf(stdout,"tagname :\t\t%s\n",conf.tagname); fprintf(stdout,"mountpoint :\t\t%s\n",conf.mountpoint); fprintf(stdout,"----------------------------------------------------\n"); fprintf(stdout,"allow_other :\t\t"); if(conf.allow_other) fprintf(stdout,"yes\n"); else fprintf(stdout,"no\n"); fprintf(stdout,"default_permissions:\t"); if(conf.default_permissions) fprintf(stdout,"yes\n"); else fprintf(stdout,"no\n"); fprintf(stdout,"nonempty:\t\t"); if(conf.nonempty) fprintf(stdout,"yes\n"); else fprintf(stdout,"no\n"); fprintf(stdout,"----------------------------------------------------\n"); fprintf(stdout,"db_host :\t\t%s\n",conf.db_host); fprintf(stdout,"db_port :\t\t%i\n",conf.db_port); fprintf(stdout,"db_database :\t\t%s\n",conf.db_database); fprintf(stdout,"db_username :\t\t%s\n",conf.db_username); fprintf(stdout,"db_password :\t\t******\n"); fprintf(stdout,"db_table :\t\t%s\n",conf.db_table); fprintf(stdout,"----------------------------------------------------\n"); if(conf.enable_error_messages) { fprintf(stdout,"Error messages enabled.\n"); fprintf(stdout,"db_error_table:\t\t%s\n",conf.db_error_table); } if(conf.enable_write_dump) { fprintf(stdout,"Write dumps enabled.\n"); fprintf(stdout,"db_dump_table:\t\t%s\n",conf.db_dump_table); if(conf.dump_uid) fprintf(stdout,"-- Effective user-id '%i'.\n",conf.dump_uid); if(conf.dump_size) fprintf(stdout,"-- Limit writes to '%i' kilobytes.\n",conf.dump_size); if(is_str(conf.dump_cmd)) fprintf(stdout,"-- Only monitoring the '%s' command.\n",conf.dump_cmd); } fprintf(stdout,"----------------------------------------------------\n"); fprintf(stdout,"Following system calls are monitored :\n"); if(conf.syscall_getattr) fprintf(stdout,"getattr\n"); if(conf.syscall_access) fprintf(stdout,"access\n"); if(conf.syscall_readlink) fprintf(stdout,"readlink\n"); if(conf.syscall_readdir) fprintf(stdout,"readdir\n"); if(conf.syscall_mknod) fprintf(stdout,"mknod\n"); if(conf.syscall_mkdir) fprintf(stdout,"mkdir\n"); if(conf.syscall_unlink) fprintf(stdout,"unlink\n"); if(conf.syscall_rmdir) fprintf(stdout,"rmdir\n"); if(conf.syscall_symlink) fprintf(stdout,"symlink\n"); if(conf.syscall_rename) fprintf(stdout,"rename\n"); if(conf.syscall_link) fprintf(stdout,"link\n"); if(conf.syscall_chmod) fprintf(stdout,"chmod\n"); if(conf.syscall_chown) fprintf(stdout,"chown\n"); if(conf.syscall_truncate) fprintf(stdout,"truncate\n"); if(conf.syscall_utimens) fprintf(stdout,"utimens\n"); if(conf.syscall_open) fprintf(stdout,"open\n"); if(conf.syscall_read) fprintf(stdout,"read\n"); if(conf.syscall_write) fprintf(stdout,"write\n"); if(conf.syscall_statfs) fprintf(stdout,"statfs\n"); if(conf.syscall_fallocate) fprintf(stdout,"fallocate\n"); if(conf.syscall_setxattr) fprintf(stdout,"setxattr\n"); if(conf.syscall_getxattr) fprintf(stdout,"getxattr\n"); if(conf.syscall_listxattr) fprintf(stdout,"listxattr\n"); if(conf.syscall_removexattr) fprintf(stdout,"removexattr\n"); fprintf(stdout,"----------------------------------------------------\n"); fprintf(stdout,"Additional logging enabled:\n"); if(conf.log_username) fprintf(stdout,"username\n"); if(conf.log_groupname) fprintf(stdout,"groupname\n"); if(conf.log_tty) fprintf(stdout,"tty\n"); if(conf.log_login_time) fprintf(stdout,"logintime\n"); if(conf.log_remote_host) fprintf(stdout,"remotehost\n"); if(conf.log_cmd) fprintf(stdout,"cmd\n"); if(conf.log_args) fprintf(stdout,"args\n"); if(conf.log_ppid) fprintf(stdout,"ppid\n"); if(conf.log_ppid_cmd) fprintf(stdout,"ppidcmd\n"); fprintf(stdout,"----------------------------------------------------\n"); conn=mysql_init(NULL); if(!conn) sql_err(conn,NULL); if(!mysql_real_connect(conn,conf.db_host,conf.db_username,conf.db_password,conf.db_database,conf.db_port,NULL,0)) sql_err(conn,NULL); fprintf(stdout,"connectivity \t\t\t[ok]\n"); id=1; logg.logg_id=id; get_timestamp(0,logg.time); strncpy(logg.host,conf.hostname,256); strncpy(logg.tag,"test",64); strncpy(logg.operation,"sql insert",32); logg.uid=800; logg.gid=800; strncpy(logg.username,"test",256); strncpy(logg.groupname,"test",256); strncpy(logg.tty,"pts/0",16); get_timestamp(0,logg.login_time); strncpy(logg.remote_host,"localhost",256); strncpy(logg.cmd,"lifl",1024); strncpy(logg.args,"--test",1024); logg.pid=200; logg.ppid=100; strncpy(logg.p_cmd,"system",1024); strncpy(logg.file,"/path/to/folder",1024); logg.protection=330; strncpy(logg.owner,"admin",256); strncpy(logg.group,"admin",256); size_t size; size=strlen(INS) +strlen(conf.db_database) +strlen(conf.db_table) +20 +strlen(logg.time) +strlen(logg.host) +strlen(logg.tag) +strlen(logg.operation) +10 +10 +strlen(logg.username) +strlen(logg.groupname) +strlen(logg.tty) +strlen(logg.login_time) +strlen(logg.remote_host) +strlen(logg.cmd) +strlen(logg.args) +10 +10 +strlen(logg.p_cmd) +strlen(logg.file) +10 +strlen(logg.owner) +strlen(logg.group); char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if((size=snprintf(buf,size, INS, conf.db_database, conf.db_table, logg.logg_id, logg.time, logg.host, logg.tag, logg.operation, logg.uid, logg.gid, logg.username, logg.groupname, logg.tty, logg.login_time, logg.remote_host, logg.cmd, logg.args, logg.pid, logg.ppid, logg.p_cmd, logg.file, logg.protection, logg.owner, logg.group))) { if(mysql_real_query(conn,buf,size)) sql_err(conn,buf); } ; free(buf); fprintf(stdout,"%s \t\t\t[ok]\n",conf.db_table); if(conf.enable_write_dump) { FILE *fp; fp=fopen("/bin/bash","r"); if(fp) { buf=malloc(1024); if(!buf) exit(EXIT_FAILURE); size=fread(buf,1,1024,fp); fclose(fp); } else { buf=NULL; size=0; } data_dump.size=size; data_dump.offset=0; data_dump.write_data=buf; sql_dump_write(); free(buf); fprintf(stdout,"%s \t\t\t[ok]\n",conf.db_dump_table); } if(conf.enable_error_messages) { sql_write_err(); fprintf(stdout,"%s \t\t\t\t[ok]\n",conf.db_error_table); } mysql_close(conn); }
int main(int argc,char *argv[]) { id=0; umask(0); if(atexit(exit_gracefull)!=0) { fprintf(stderr,"Sorry the program encountered a problem.\nExiting.\n"); exit(EXIT_FAILURE); } static struct fuse_operations op; op.init =lifl_init; op.getattr =xmp_getattr; op.access =xmp_access; op.readlink =xmp_readlink; op.readdir =xmp_readdir; op.mknod =xmp_mknod; op.mkdir =xmp_mkdir; op.symlink =xmp_symlink; op.unlink =xmp_unlink; op.rmdir =xmp_rmdir; op.rename =xmp_rename; op.link =xmp_link; op.chmod =xmp_chmod; op.chown =xmp_chown; op.truncate =xmp_truncate; #ifdef HAVE_UTIMENSAT op.utimens =xmp_utimens, #endif op.open =xmp_open; op.read =xmp_read; op.write =xmp_write; op.statfs =xmp_statfs; op.release =xmp_release; op.fsync =xmp_fsync; #ifdef HAVE_POSIX_FALLOCATE op.fallocate =xmp_fallocate; #endif #ifdef HAVE_SETXATTR op.setxattr =xmp_setxattr; op.getxattr =xmp_getxattr; op.listxattr =xmp_listxattr; op.removexattr =xmp_removexattr; #endif char config_file[255]; strncpy(config_file,DEFAULT_CONF_NAME,255); static struct option long_options[]= { {"help", no_argument, 0, 'h'}, {"config", required_argument, 0, 'c'}, {"test", no_argument, 0, 't'}, {0, 0, 0, 0 } }; while(1) { int option_index=0; int ch=getopt_long(argc,argv,"hc:t",long_options,&option_index); if(ch==-1) break; switch (ch) { case 'h': fprintf(stdout,"-h --help\n"); fprintf(stdout,"-c --config <filename>\n"); fprintf(stdout,"-t --test\n"); exit(EXIT_SUCCESS); case 'c': strncpy(config_file,optarg,255); break; case 't': read_cfg(config_file); test_cfg(); errno=0; exit(EXIT_SUCCESS); } } if(optind<argc) { while(optind<argc) fprintf(stderr,"%s is not a valid argument\n",argv[optind++]); } read_cfg(config_file); conn=mysql_init(NULL); if(!conn) sql_err(conn,NULL); if(!mysql_real_connect(conn,conf.db_host,conf.db_username,conf.db_password,conf.db_database,conf.db_port,NULL,0)) sql_err(conn,NULL); sql_get_last_id(); fprintf(stdout,"Starting up...\n"); sleep(2); chdir(conf.mountpoint); fd0=open(".",0); char *fuse_argv[8]; memset(fuse_argv,0,sizeof(fuse_argv)); int fuse_argc; fuse_argc=0; fuse_argv[fuse_argc++]=argv[0]; fuse_argv[fuse_argc++]=conf.mountpoint; if(conf.allow_other) { fuse_argv[fuse_argc++]="-o"; fuse_argv[fuse_argc++]="allow_other"; } if(conf.default_permissions) { fuse_argv[fuse_argc++]="-o"; fuse_argv[fuse_argc++]="default_permissions"; } if(conf.nonempty) { fuse_argv[fuse_argc++]="-o"; fuse_argv[fuse_argc++]="nonempty"; } fuse_argv[fuse_argc++]="-o"; fuse_argv[fuse_argc++]="use_ino"; fuse_argv[fuse_argc]=NULL; fuse_main(fuse_argc,fuse_argv,&op,NULL); mysql_close(conn); exit(EXIT_SUCCESS); }
static int cdb_get(struct custdb_handle *ch, const ChopstixPhone *phone, ChopstixCustomer *cust) { char *phonestr; int64_t cust_ID = 0; sqlite3_stmt *q = NULL; ChopstixCredit *cc; int dupcount = 0; CHECKSQL(ch); /* get phonestr before zeroing, in case user sends same structure in */ if ((phonestr = phone2str(phone)) == NULL) goto fail; bzero(cust, sizeof(*cust)); /* * CUSTOMER */ q = SQLARG(ch)->q.cdb.get_cust; /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(q, 1, phonestr, strlen(phonestr), SQLITE_STATIC)) { sql_err(SQLARG(ch), "cannot bind phone number to query"); goto fail; } while (sqlite3_step(q) == SQLITE_ROW) { dupcount++; /* start by freeing, incase multiple customers match the same phone */ free_ChopstixCustomer(cust); /* ID, Phone, PhoneExt, Address, Intersection, Special */ cust_ID = sqlite3_column_int64(q, 0); cust->name = sql_strdup(sqlite3_column_text(q, 1)); str2phone(sqlite3_column_text(q, 2), &cust->phone); str2phoneext(sqlite3_column_text(q, 3), &cust->phone); cust->addr.addr = sql_strdup(sqlite3_column_text(q, 4)); cust->addr.apt = sql_strdup(sqlite3_column_text(q, 5)); cust->addr.entry = sql_strdup(sqlite3_column_text(q, 6)); cust->isect.cross = sql_strdup(sqlite3_column_text(q, 7)); if ((cust->special = calloc(1, sizeof(cust->special)))) *cust->special = sql_strdup(sqlite3_column_text(q, 8)); cust->reps = sqlite3_column_int(q, 9); } sqlite3_reset(q); if (dupcount > 1) sql_warn(SQLARG(ch), "%d duplicate entries", dupcount); if (cust_ID == 0) { errno = ENOENT; return -1; } /* * CREDITS */ q = SQLARG(ch)->q.cdb.get_cred; /* 1 = first parameter in prepared query */ if (sqlite3_bind_int(q, 1, cust_ID)) { sql_err(SQLARG(ch), "cannot bind customer ID to query"); goto fail; } if ((cust->credit = calloc(1, sizeof(*cust->credit))) == NULL) goto fail; cust->credit->len = 0; while (sqlite3_step(q) == SQLITE_ROW) { /* increase buffer */ if ((cc = realloc(cust->credit->val, (cust->credit->len + 1) * sizeof(ChopstixCredit))) == NULL) goto fail; cust->credit->len++; cust->credit->val = cc; /* Credit, Remain, Reason */ cust->credit->val[cust->credit->len - 1].credit = sqlite3_column_int(q, 0); cust->credit->val[cust->credit->len - 1].remain = sqlite3_column_int(q, 1); cust->credit->val[cust->credit->len - 1].reason = sql_strdup(sqlite3_column_text(q, 2)); } sqlite3_reset(SQLARG(ch)->q.cdb.get_cred); return 0; fail: free_ChopstixCustomer(cust); if (q) sqlite3_reset(q); return -1; }
static int cdb_update(struct custdb_handle *ch, const ChopstixCustomer *cust) { char *phonestr, *phoneext, *special; sqlite3_stmt *q = NULL, *u = NULL; size_t cpos = 0; CHECKSQL(ch); if ((phonestr = phone2str(&cust->phone)) == NULL) goto fail; if ((phoneext = phoneext2str(&cust->phone)) == NULL) goto fail; /* * CUSTOMER */ q = SQLARG(ch)->q.cdb.upd_cust; /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(q, 1, cust->name, strlen(cust->name), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 2, phoneext, strlen(phoneext), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 3, cust->addr.addr, strlen(cust->addr.addr), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 4, cust->addr.apt, strlen(cust->addr.apt), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 5, cust->addr.entry, strlen(cust->addr.entry), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 6, cust->isect.cross, strlen(cust->isect.cross), SQLITE_STATIC)) goto fail; if (cust->special) special = *cust->special; else special = ""; if (sqlite3_bind_text(q, 7, special, strlen(special), SQLITE_STATIC)) goto fail; if (sqlite3_bind_text(q, 8, phonestr, strlen(phonestr), SQLITE_STATIC)) goto fail; if (sqlite3_step(q) != SQLITE_DONE) { sql_err(SQLARG(ch), "cannot update customer"); goto fail; } sqlite3_reset(q); /* no need to do this if no credits are attached */ if (cust->credit == NULL || cust->credit->len == 0) return 0; /* * CREDITS * * Have to SELECT, compare against the passed array and then UPDATE any * that have changed. */ q = SQLARG(ch)->q.cdb.get_cred_phone; /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(q, 1, phonestr, strlen(phonestr), SQLITE_STATIC)) { sql_err(SQLARG(ch), "cannot bind phone number to query"); goto fail; } while (sqlite3_step(q) == SQLITE_ROW) { /* limit credit positional */ if (cpos >= cust->credit->len) break; /* ensure we are still in order */ if (cust->credit->val[cpos].reason == NULL || sqlite3_column_int(q, 1) != cust->credit->val[cpos].credit) { sql_err(SQLARG(ch), "cannot update customer credit"); break; } /* if nothing needs to change, continue */ if (cust->credit->val[cpos].remain == sqlite3_column_int(q, 2) && strcasecmp(cust->credit->val[cpos].reason, sqlite3_column_text(q, 3)) == 0) { cpos++; continue; } u = SQLARG(ch)->q.cdb.upd_cred; /* 1 = first parameter in prepared query */ if (sqlite3_bind_int(u, 1, cust->credit->val[cpos].remain)) goto fail; if (sqlite3_bind_text(u, 2, cust->credit->val[cpos].reason, strlen(cust->credit->val[cpos].reason), SQLITE_STATIC)) goto fail; if (sqlite3_bind_int(u, 3, sqlite3_column_int(q, 0))) goto fail; if (sqlite3_step(u) != SQLITE_DONE) { sql_err(SQLARG(ch), "cannot update credit %zd", cpos + 1); goto fail; } sqlite3_reset(u); cpos++; } sqlite3_reset(q); for (; cpos < cust->credit->len; cpos++) { u = SQLARG(ch)->q.cdb.put_cred; /* 1 = first parameter in prepared query */ if (sqlite3_bind_text(u, 4, phonestr, strlen(phonestr), SQLITE_STATIC)) goto fail; if (sqlite3_bind_int(u, 1, cust->credit->val[cpos].credit)) goto fail; if (sqlite3_bind_int(u, 2, cust->credit->val[cpos].remain)) goto fail; if (sqlite3_bind_text(u, 3, cust->credit->val[cpos].reason, strlen(cust->credit->val[cpos].reason), SQLITE_STATIC)) goto fail; if (sqlite3_step(u) != SQLITE_DONE) { sql_err(SQLARG(ch), "cannot insert credit %zd", cpos + 1); goto fail; } sqlite3_reset(u); } return 0; fail: sql_err(SQLARG(ch), "cannot bind data to query"); if (u) sqlite3_reset(u); if (q) sqlite3_reset(q); return -1; }