static int set_login(const char *driver, const char *database, const char *user, const char *password, const char *host, const char *port, int overwrite) { int i, found; LOGIN login; G_debug(3, "db_set_login(): drv=[%s] db=[%s] usr=[%s] pwd=[%s] host=[%s] port=[%s]", driver, database, user, password, host, port); init_login(&login); if (read_file(&login) == -1) return DB_FAILED; found = FALSE; for (i = 0; i < login.n; i++) { if (strcmp(login.data[i].driver, driver) == 0 && strcmp(login.data[i].database, database) == 0) { if (user) login.data[i].user = G_store(user); else login.data[i].user = G_store(""); if (password) login.data[i].password = G_store(password); else login.data[i].password = G_store(""); found = TRUE; break; } } if (found) { if (overwrite) G_warning(_("DB connection <%s/%s> already exists and will be overwritten"), driver, database ? database : ""); else G_fatal_error(_("DB connection <%s/%s> already exists. " "Re-run '%s' with '--%s' flag to overwrite existing settings."), driver, database ? database : "", G_program_name(), "overwrite"); } if (!found) add_login(&login, driver, database, user, password, host, port, -1); else add_login(&login, driver, database, user, password, host, port, i); if (write_file(&login) == -1) return DB_FAILED; return DB_OK; }
void receive(void *arg) { int done = 0; int cl_sock = (int) arg; char buffer[MAX_BUFFER]; int argc; char *argv[5]; int i; while (!done) { recv (cl_sock, buffer, sizeof(buffer), 0); i = 0; while (buffer[i++]!=';') {} buffer[i]='\0'; printf("Received message: '%s'\n", buffer); parse (buffer, argv, &argc); if ( !strcmp(argv[0], "LOGIN") ) { if (argc < 4) printf("Wrong number of arguments (%d).\n",argc); else add_login (argv[1], argv[2], (int)(atoi(argv[3]))); } else if ( !strcmp(argv[0], "LOGOUT") ) { if (argc < 4) { printf("Wrong number of arguments (%d).\n",argc); close(cl_sock); printf("Thread exiting with error.\n"); pthread_exit(NULL); } else { del_login (argv[1], argv[2], (int)(atoi(argv[3]))); done = 1; } } else if ( !strcmp(argv[0], "HEY") ) { if (argc < 1) printf("Wrong number of arguments (%d).\n",argc); else get_logins(cl_sock); } else { printf("Unrecognized command.\n"); } } close(cl_sock); printf("Thread exiting.\n"); pthread_exit(NULL); }
/* Read the DB login file if it exists return: -1 error (cannot read file) number of items (0 also if file does not exist) */ static int read_file(LOGIN * login) { int ret; const char *file; FILE *fd; char buf[2001], dr[500], db[500], usr[500], pwd[500], host[500], port[500]; login->n = 0; file = login_filename(); G_debug(3, "read_file(): DB login file = <%s>", file); if (access(file, F_OK) != 0) { G_debug(3, "login file does not exist"); return 0; } fd = fopen(file, "r"); if (fd == NULL) { G_warning(_("Unable to read file '%s'"), file); return -1; } while (G_getl2(buf, 2000, fd)) { G_chop(buf); usr[0] = pwd[0] = host[0] = port[0] = '\0'; ret = sscanf(buf, "%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^\n]", dr, db, usr, pwd, host, port); G_debug(3, "ret = %d : drv=[%s] db=[%s] usr=[%s] pwd=[%s] host=[%s], port=[%s]", ret, dr, db, usr, pwd, host, port); if (ret < 2) { G_warning(_("Login file (%s) corrupted (line: %s)"), file, buf); continue; } add_login(login, dr, db, usr, pwd, host, port, -1); } fclose(fd); return (login->n); }
/* Read the DB login file if it exists return: -1 error (cannot read file) number of items (0 also if file does not exist) */ int read_file(LOGIN * login) { int ret; const char *file; struct stat info; FILE *fd; char buf[2001], dr[500], db[500], usr[500], pwd[500]; login->n = 0; file = login_filename(); G_debug(3, "DB login file = <%s>", file); if (stat(file, &info) != 0) { G_debug(3, "login file does not exist"); return 0; } fd = fopen(file, "r"); if (fd == NULL) return -1; while (G_getl2(buf, 2000, fd)) { G_chop(buf); usr[0] = pwd[0] = '\0'; ret = sscanf(buf, "%[^|]|%[^|]|%[^|]|%[^\n]", dr, db, usr, pwd); G_debug(3, "ret = %d : drv=[%s] db=[%s] usr=[%s] pwd=[%s]", ret, dr, db, usr, pwd); if (ret < 2) { G_warning(_("Login file corrupted")); continue; } add_login(login, dr, db, usr, pwd); } fclose(fd); return (login->n); }
/*! \brief Set user/password for driver/database \return DB_OK \return DB_FAILED */ int db_set_login(const char *driver, const char *database, const char *user, const char *password) { int i, found; LOGIN login; G_debug(3, "db_set_login(): drv=[%s] db=[%s] usr=[%s] pwd=[%s]", driver, database, user, password); init_login(&login); if (read_file(&login) == -1) return DB_FAILED; found = 0; for (i = 0; i < login.n; i++) { if (strcmp(login.data[i].driver, driver) == 0 && strcmp(login.data[i].database, database) == 0) { if (user) login.data[i].user = G_store(user); else login.data[i].user = G_store(""); if (password) login.data[i].password = G_store(password); else login.data[i].password = G_store(""); found = 1; break; } } if (!found) add_login(&login, driver, database, user, password); if (write_file(&login) == -1) return DB_FAILED; return DB_OK; }