Custom PAM fails at PAM_CONVERSATION in SSH - c

I've been trying to implement a custom PAM for 2nd Factor Verification. The Custom PAM needs to read factor selected(integer) and pass it on to the program. The below code works fine in SUDO and SU but I'm getting "Connection Closed by host port 22" error while trying with SSH. Can anyone help me sort the issue.
static char *request_pass(pam_handle_t *pamh, int echocode,PAM_CONST char *prompt)
{
// Query user for verification code
PAM_CONST struct pam_message msg = { .msg_style = PAM_PROMPT_ECHO_ON,.msg = prompt };
PAM_CONST struct pam_message *msgs = &msg;
// PAM_CONST struct pam_message msg;
struct pam_conv conv = {re,NULL};
struct pam_response *resp = NULL;
int retval = re(1,&msgs,&resp,"");
return "";
}
int re(int nummsg,PAM_CONST struct pam_message **msg,struct pam_response **resp, void *appdata_ptr)
{
*resp = malloc(sizeof(struct pam_response));
assert(*resp);
(*resp)->resp = calloc(1024,1);
printf("%s^^ ", msg[0]->msg);
fflush(stdout);
assert(fgets((*resp)->resp, 1024, stdin));
char *ret = NULL;
ret = (*resp)->resp;
// Deallocate temporary storage
if (*resp)
{
if (!ret)
{
free((*resp)->resp);
}
free(*resp);
}
factorselect = atoi(ret);
return PAM_SUCCESS;
}
Edit: Below code worked for me(Solved)
char *
s_prompt(void *arg, const char *prompt, char *buf, size_t bufsz)
{
char *p = NULL;
if (pam_prompt((pam_handle_t *)arg, PAM_PROMPT_ECHO_ON, &p,
"%s", prompt) != PAM_SUCCESS || p == NULL) {
return (NULL);
}
strncpy(buf, p, bufsz);
free(p);
return (buf);
}

Related

Manipulating structs with a void function in C

so I've been set a task of creating a faux string struct and implementing all the usual string functions on my faux string struct. I'm stuck on the tests of my strcat implementation called append, with the first test failing (segfault) being the 5th line. My function for creating new structs should be OK because it passed all the tests, but I've included it just incase.
I've already been able to successfully implement length, get, set and copy functions for my faux string structs.
The struct:
struct text {
int capacity;
char *content;
};
typedef struct text text;
My function for creating new structs:
text *newText(char *s) {
printf("new Text from %s\n", s);
int sizeNeeded = (strlen(s)+1);
int sizeGot = 24;
while (sizeNeeded > sizeGot) {
sizeGot = sizeGot * 2;
}
text *out = malloc(sizeGot);
char *c = malloc(sizeGot);
strcpy(c, s);
out->content = c;
out->capacity = (sizeGot);
printf("the capacity is %d\n", sizeGot);
return out;
free(c);
}
My append function:
void append(text *t1, text *t2) {
printf("t1 content is %s, t2 content is %d\n", t1->content, *t2->content);
int sizeNeeded = (t1->capacity + t2->capacity);
int sizeGot = 24;
while (sizeNeeded > sizeGot) {
sizeGot = sizeGot * 2;
}
char *stringy = calloc(sizeGot, 32);
stringy = strcat(t1->content, t2->content);
free(t1);
t1 = newText(stringy);
}
and finally the tests:
void testAppend() {
text *t = newText("car");
text *t2 = newText("pet");
append(t, t2);
assert(like(t, "carpet"));
assert(t->capacity == 24);
text *t3 = newText("789012345678901234");
append(t, t3);
assert(like(t, "carpet789012345678901234"));
assert(t->capacity == 48);
freeText(t);
freeText(t2);
freeText(t3);
}
You are allocating memory in the wrong way. You could fix this by using a flexible array member like this:
typedef struct {
int capacity;
char content[];
} text;
text *out = malloc(sizeof(text) + sizeof(something));
strcpy(out->content, str);
...
And obviously code such as this is nonsense:
return out;
free(c);
}
Enable compiler warnings and listen to them.
Och, some errors you have:
Inside text_new you allocate memory for text *out using text *out = malloc(sizeGot); when sizeGot = 24 is a constant value. You should allocate sizeof(*out) or sizeof(text) bytes of memory for it.
I don't know what for int sizeGot = 24; while (sizeNeeded > sizeGot) the loop inside text_new and append is for. I guess the intention is to do allocations in power of 24. Also it mostly looks like the same code is in both functions, it does look like code duplication, which is a bad thing.
Inside append You pass a pointer to t1, not a double pointer, so if you modify the t1 pointer itself the modification will not be visible outside of function scope. t1 = newText(stringy); is just pointless and leaks memory. You could void append(text **t1, text *t2) and then *t1 = newText(stringy). But you can use a way better approach using realloc - I would expect append to "append" the string, not to create a new object. So first resize the buffer using realloc then strcat(&t1->content[oldcapacity - 1], string_to_copy_into_t1).
int sizeNeeded = (t1->capacity + t2->capacity); is off. You allocate capacity in power of 24, which does not really interact with string length. You need to have strlen(t1->content) + strlen(t2->content) + 1 bytes for both strings and the null terminator.
Try this:
size_t text_newsize(size_t sizeNeeded)
{
// I think this is just `return 24 << (sizeNeeded / 24);`, but not sure
int sizeGot = 24;
while (sizeNeeded > sizeGot) {
sizeGot *= 2;
}
return sizeGot;
}
text *newText(char *s) {
printf("new Text from %s\n", s);
if (s == NULL) return NULL;
int sizeNeeded = strlen(s) + 1;
int sizeGot = text_newsize(sizeNeeded);
text *out = malloc(sizeof(*out));
if (out == NULL) {
return NULL;
}
out->content = malloc(sizeGot);
if (out->content == NULL) {
free(out);
return NULL;
}
strcpy(out->content, s);
out->capacity = sizeGot;
printf("the capacity is %d\n", sizeGot);
return out;
}
and this:
int append(text *t1, text *t2) {
printf("t1 content is %s, t2 content is %s\n", t1->content, t2->content);
int sizeNeeded = strlen(t1->content) + strlen(t2->content) + 1;
if (t1->capacity < sizeNeeded) {
// this could a text_resize(text*, size_t) function
int sizeGot = text_newsize(sizeNeeded);
void *tmp = realloc(t1->content, sizeGot);
if (tmp == NULL) return -ENOMEM;
t1->content = tmp;
t1->capacity = sizeGot;
}
strcat(t1->content, t2->content);
return 0;
}
Some remarks:
Try to handle errors in your library. If you have a function like void append(text *t1, text *t2) let it be int append(text *t1, text *t2) and return 0 on success and negative number on *alloc errors.
Store the size of everything using size_t type. It's defined in stddef.h and should be used to represent a size of an object. strlen returns size_t and sizeof also returns size_t.
I like to put everything inside a single "namespace", I do that by prepending the functions with a string like text_.
I got some free time and decided to implement your library. Below is the code with a simple text object storing strings, I use 24 magic number as allocation chunk size.
// text.h file
#ifndef TEXT_H_
#define TEXT_H_
#include <stddef.h>
#include <stdbool.h>
struct text;
typedef struct text text;
text *text_new(const char content[]);
void text_free(text *t);
int text_resize(text *t, size_t newsize);
int text_append(text *to, const text *from);
int text_append_mem(text *to, const void *from, size_t from_len);
const char *text_get(const text *t);
int text_append_str(text *to, const char *from);
char *text_get_nonconst(text *t);
size_t text_getCapacity(const text *t);
bool text_equal(const text *t1, const text *t2);
#endif // TEXT_H_
// text.c file
//#include "text.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
struct text {
size_t capacity;
char *content;
};
text *text_new(const char content[])
{
text * const t = malloc(sizeof(*t));
if (t == NULL) goto MALLOC_ERR;
const struct text zero = {
.capacity = 0,
.content = NULL,
};
*t = zero;
if (content != NULL) {
const int ret = text_append_str(t, content);
if (ret) {
goto TEXT_APPEND_ERR;
}
}
return t;
TEXT_APPEND_ERR:
free(t);
MALLOC_ERR:
return NULL;
}
void text_free(text *t)
{
assert(t != NULL);
free(t->content);
free(t);
}
int text_resize(text *t, size_t newcapacity)
{
// printf("%s %d -> %d\n", __func__, t->capacity, newcapacity);
// we resize in chunks
const size_t chunksize = 24;
// clap the capacity into multiple of 24
newcapacity = (newcapacity + chunksize - 1) / chunksize * chunksize;
void * const tmp = realloc(t->content, newcapacity);
if (tmp == NULL) return -ENOMEM;
t->content = tmp;
t->capacity = newcapacity;
return 0;
}
int text_append_mem(text *to, const void *from, size_t from_len)
{
if (to == NULL || from == NULL) return -EINVAL;
if (from_len == 0) return 0;
const size_t oldcapacity = to->capacity == 0 ? 0 : strlen(to->content);
const size_t newcapacity = oldcapacity + from_len + 1;
int ret = text_resize(to, newcapacity);
if (ret) return ret;
memcpy(&to->content[newcapacity - from_len - 1], from, from_len);
to->content[newcapacity - 1] = '\0';
return 0;
}
int text_append_str(text *to, const char *from)
{
if (to == NULL || from == NULL) return -EINVAL;
return text_append_mem(to, from, strlen(from));
}
int text_append(text *to, const text *from)
{
if (to == NULL || from == NULL) return -EINVAL;
if (text_getCapacity(from) == 0) return 0;
return text_append_str(to, text_get(from));
}
const char *text_get(const text *t)
{
return t->content;
}
const size_t text_strlen(const text *t)
{
return t->capacity == 0 ? 0 : strlen(t->content);
}
size_t text_getCapacity(const text *t)
{
return t->capacity;
}
bool text_equal_str(const text *t, const char *str)
{
assert(t != NULL);
if (str == NULL && t->capacity == 0) return true;
const size_t strlength = strlen(str);
const size_t t_strlen = text_strlen(t);
if (t_strlen != strlength) return false;
if (memcmp(text_get(t), str, strlength) != 0) return false;
return true;
}
// main.c file
#include <stdio.h>
int text_testAppend(void) {
text *t = text_new("car");
if (t == NULL) return -1;
text *t2 = text_new("pet");
if (t2 == NULL) return -1;
if (text_append(t, t2)) return -1;
assert(text_equal_str(t, "carpet"));
assert(text_getCapacity(t) == 24);
text *t3 = text_new("789012345678901234");
if (t3 == NULL) return -1;
if (text_append(t, t3)) return -1;
assert(text_equal_str(t, "carpet789012345678901234"));
assert(text_getCapacity(t) == 48);
text_free(t);
text_free(t2);
text_free(t3);
return 0;
}
int main()
{
text *t1 = text_new("abc");
text_append_str(t1, "def");
printf("%s\n", text_get(t1));
text_free(t1);
printf("text_testAppend = %d\n", text_testAppend());
return 0;
}

Chain List, story of pointers

I have this following code:
void pushInList(t_chan **chan, char *name, char *nick_user)
{
t_chan *new_channel;
(void)nick_user;
if ((new_channel = malloc(sizeof(t_chan))) == NULL)
return ;
new_channel->name = name;
new_channel->prev = (*chan);
(*chan) = new_channel;
}
display_list(t_chan *chan, int fd)
{
int i;
i = 0;
while (chan != NULL)
{
printf("list : %s\n", chan->name);
chan = chan->prev;
}
}
int create_chanel(int fd, char *buffer, t_chan **chan)
{
char *tmp;
int i;
i = 0;
if ((tmp = get_param(fd, buffer, 2, "NICK")) == NULL)
return (EXIT_FAILURE);
while ((*chan) != NULL)
{
/* FUTUR CHECK OF EXISTING CHANEL*/
(*chan) = (*chan)->prev;
}
if ((*chan) == NULL)
pushInList(chan, tmp, "Kilian");
return (EXIT_SUCCESS);
}
int main()
{
t_chan *chan;
char *test;
chan = NULL;
test = strdup("JOIN Coucou");
create_chanel(4, test, &chan);
test = strdup("JOIN Google");
create_chanel(4, test, &chan);
printf("-------------------\nlast display :\n");
display_list(chan, 4);
}
I don't understand why my list is every time NULL. I passed a pointer but in my main the list don't keep its values.
Can you help me, I don't understand...
It's a chain list of channel, When a client send "Join Coucou", if the channel Coucou doesn't exist I create a new node.
Thank you in advance,
Cordialy

Open LDAP Client in C - How to replace deprecated APIs

I am struggling while converting a small OpenLDAP client coded in C. The original version of this program works well with deprecated APIs.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ldap.h>
/*-------------------
gcc -w -DLDAP_DEPRECATED -o exe/TstLDAPold sources/TstLDAPold.c -lopenldap
--------------------*/
int main(int argc, char ** argv)
{
char* pstrDatabaseName = "I36_XXX";
char * pstrSchemaName = "DEV_XXX";
char* pstrModuleName = "L8XX";
char * pstrHostName = "mado";
// New LDAP Structure
int version, ldapPort, i, rc, entryCount, NodeFound;
char *ldapHost, *searchBase;
char *attribute, *dn, **values;
const char *sortAttribute = "sn";
struct timeval timeOut;
BerElement *ber;
LDAP *ld;
LDAPMessage *searchResult, *entry;
FILE *fp;
const char * loginDN = "cn=ldap_yyyy,cn=yyyyy,cn=Ixxxxx";
const char * password = "ldap_xxx";
ldapHost = (char *) malloc(100);
searchBase = (char *) malloc(500); // New LDAP Structure
(void) strcpy( (char *) ldapHost, (const char *) "myldapserver");
char * strLdapPort = (char *) malloc(10);
if (getenv("LDAP_PORT") != NULL)
{
ldapPort = atoi(getenv("LDAP_PORT"));
strcpy(strLdapPort, getenv("LDAP_PORT"));
}
else
{
ldapPort = 389;
strcpy(strLdapPort, "389");
}
strcpy(searchBase, "HostName="); // Change attrib ModuleName in ProgramName
strcat(searchBase, pstrHostName);
strcpy(searchBase, "ProgramName="); // Change attrib ModuleName in ProgramName
strcat(searchBase, pstrModuleName);
strcat(searchBase, ",SchemaName="); // New LDAP Structure
strcat(searchBase, pstrSchemaName); // New LDAP Structure
strcat(searchBase, ",DatabaseName="); // New LDAP Structure
strcat(searchBase, pstrDatabaseName); // New LDAP Structure
strcat(searchBase, ",cn=RLM,cn=Ixxx");
/* Set LDAP version to 3 */
version = LDAP_VERSION3;
ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &version);
/* Initialize the LDAP session */
/* NEW ROUTINE OPEN LDAP 2.4 */
char * ldapServer = (char *) malloc(200);
strcpy(ldapServer, (const char *) ldapHost);
strcat(ldapServer, (const char *) ":");
strcat(ldapServer, (const char *) strLdapPort);
printf("LDAP Server : %s\n", ldapServer);
//LDAP ld1;
if ((ld = ldap_open(ldapHost, ldapPort)) == NULL)
{
printf("\n LDAP session initialization failed\n");
return (1);
} else {
printf("Open success\n");
}
/* Bind to the server */
rc = ldap_simple_bind_s(ld, loginDN, password);
if (rc != LDAP_SUCCESS)
{
printf(
"ldap_simple_bind_s: %s\nInformations : Login: %s Password: %s\n",
ldap_err2string(rc), loginDN, password);
printf( "Unbind\n" );
ldap_unbind_s(ld);
printf( "Unbinding ok\n" );
return (1);
}
rc = ldap_search_s(ld, searchBase, LDAP_SCOPE_ONELEVEL, "(objectclass=*)",
NULL, 0, &searchResult);
if (rc != LDAP_SUCCESS)
{
printf("ldap_search_ext_s: %s \n", ldap_err2string(rc));
ldap_unbind_s(ld);
return (1);
} else {
printf("Search Success\n");
}
/// DISPLAY RESULTS
NodeFound = 0;
/* Go through the search results by checking entries */
for (entry = ldap_first_entry(ld, searchResult);
entry != NULL & NodeFound == 0; entry = ldap_next_entry(ld, entry))
{
if ((dn = ldap_get_dn(ld, entry)) != NULL)
{
ldap_memfree(dn);
}
for (attribute = ldap_first_attribute(ld, entry, &ber);
attribute != NULL;
attribute = ldap_next_attribute(ld, entry, ber))
{
/* Get values and print. Assumes all values are strings. */
if ((values = ldap_get_values(ld, entry, attribute)) != NULL)
{
printf("attribute : %s \n",attribute );
printf("value : %s\n", values[0] );
if (strcasecmp(attribute, "HostName") == 0)
{
if (strncmp(values[0], pstrHostName, strlen(pstrHostName)) != 0)
{
NodeFound = 0;
} else
{
//strcpy(mstrHostName, values[0]);
NodeFound = 1;
}
}
ldap_value_free(values);
}
ldap_memfree(attribute);
}
}
ldap_msgfree(searchResult);
ldap_unbind_s(ld);
return (0);
}
This code works, it gives :
attribute : ModuleName
value : LIC_058
attribute : BeginDate
value : 19970404
attribute : EndDate
value : 20251231
...
Then, I have changed it to :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ldap.h>
/*-------------------
gcc -o exe/TstLDAP sources/TstLDAP.c -lopenldap
--------------------*/
int main(int argc, char ** argv)
{
char* pstrDatabaseName = "I36_XXX";
char * pstrSchemaName = "DEV_XXX";
char* pstrModuleName = "LXXX";
char * pstrHostName = "mado";
// New LDAP Structure
int version, ldapPort, i, rc, entryCount, NodeFound;
char *ldapHost, *searchBase;
char *attribute, *dn, **values;
const char *sortAttribute = "sn";
struct timeval timeOut;
BerElement *ber;
LDAP *ld;
LDAPMessage *searchResult, *entry;
FILE *fp;
const char * loginDN = "cn=ldap_yyy,cn=yyyy,cn=Ixxxx";
const char * password = "ldap_xxx";
ldapHost = (char *) malloc(100);
searchBase = (char *) malloc(500); // New LDAP Structure
(void) strcpy( (char *) ldapHost, (const char *) "ldap://myldapserver");
char * strLdapPort = (char *) malloc(10);
if (getenv("LDAP_PORT") != NULL)
{
ldapPort = atoi(getenv("LDAP_PORT"));
strcpy(strLdapPort, getenv("LDAP_PORT"));
}
else
{
ldapPort = 389;
strcpy(strLdapPort, "389");
}
strcpy(searchBase, "HostName="); // Change attrib ModuleName in ProgramName
strcat(searchBase, pstrHostName);
strcpy(searchBase, "ProgramName="); // Change attrib ModuleName in ProgramName
strcat(searchBase, pstrModuleName);
strcat(searchBase, ",SchemaName="); // New LDAP Structure
strcat(searchBase, pstrSchemaName); // New LDAP Structure
strcat(searchBase, ",DatabaseName="); // New LDAP Structure
strcat(searchBase, pstrDatabaseName); // New LDAP Structure
strcat(searchBase, ",cn=RLM,cn=Ixxx");
/* Set LDAP version to 3 */
version = LDAP_VERSION3;
ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &version);
/* Initialize the LDAP session */
/* NEW ROUTINE OPEN LDAP 2.4 */
char * ldapServer = (char *) malloc(200);
strcpy(ldapServer, (const char *) ldapHost);
strcat(ldapServer, (const char *) ":");
strcat(ldapServer, (const char *) strLdapPort);
printf("LDAP Server : %s\n", ldapServer);
//LDAP ld1;
if (ldap_initialize(&ld, ldapServer))
{
//logFile("/tmp/licences_manager.log", "LDAP init failed!", "");
printf("LDAP Init failed");
}
else
{
//logFile("/tmp/licences_manager.log", "LDAP init Success!", "");
printf("LDAP Init Success\n");
}
/*
if ((ld = ldap_open(ldapHost, ldapPort)) == NULL)
{
printf("\n LDAP session initialization failed\n");
return (1);
}*/
/* Bind to the server */
struct berval cred;
strcpy(cred.bv_val, password);
cred.bv_len = strlen(password);
rc = ldap_sasl_bind_s(ld, loginDN, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
if (rc != LDAP_SUCCESS)
{
printf("ldap_sasl_bind_s: %s\nInformations : Login: %s Password: %s\n",
ldap_err2string(rc), loginDN, password);
return (1);
} else {
printf( "Binding successful\n");
}
////-------------Phase 2
LDAPControl **serverctrls;
LDAPControl **clientctrls;
struct timeval *timeout;
rc = ldap_search_ext_s( ld,
searchBase,
LDAP_SCOPE_ONELEVEL,
"(objectclass=*)",
NULL,
0,
NULL, // LDAPControl **serverctrls, // NEW
NULL, // LDAPControl **clientctrls, // NEW
NULL, // struct timeval *timeout, //NEW
LDAP_NO_LIMIT, // int sizelimit, // NEW
&searchResult );
if (rc != LDAP_SUCCESS)
{
printf("ldap_search_ext_s: %s \n", ldap_err2string(rc));
//ldap_unbind_s(ld);
return (1);
} else {
printf("Search Success\n");
}
return (0);
}
The first steps seems to work (initialize, bind) but it crashes in phase 2 when I run the search:
LDAP Server : ldap://myldapserver:389
LDAP Init Success
Memory fault(coredump)
It's on Solaris 11 with GCC 4.8 but I don't think it's system related. Am I doing anything wrong with the search API ? I have googled it a lot but I don't find a good reason for it to fail.
Thanks a lot.
Oops, it seems the binding does not work either. I forgot to allocate the cred.bv_val to receive the password. This is the reason of the coredump.

Parsing a read in file and storing it in a binary tree

I am getting a segfault when I try to insert my node into the binary tree. I run the program with gdb and here is what I find out about the segfault, but I dont really know what to change in my insert and create function. Thanks for the help in advance.
Program received signal SIGSEGV, Segmentation fault.
__strcmp_sse42 () at ../sysdeps/x86_64/multiarch/strcmp.S:260
260 movdqu (%rsi), %xmm2
(gdb) where
#0 __strcmp_sse42 () at ../sysdeps/x86_64/multiarch/strcmp.S:260
#1 0x0000000000400a42 in insert_into_commands_tree (node=0x7fffffffe0b0,
data=0x602270) at lab8.c:116
#2 0x00000000004009d7 in create_commands_tree (commands=0x7fffffffe0b0,
file=0x7fffffffe4a1 "commands.dat") at lab8.c:104
#3 0x0000000000400835 in main (argc=2, argv=0x7fffffffe1b8) at lab8.c:48
What my program does is reads in a text file and parses the strings, then it stores them into a binary tree. Then the user types in a command and I search the tree and see if the command is listed in the tree. I am going to post my full code so you all can see it, then post the file I read in and the sample output, and hopefully someone can help me with this segfault error. Thanks a lot in advance. My teacher provided us with the main and tokenizer function too.
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define COMMAND_NAME_LEN 50
#define MAX_SPLIT_SIZE 50
#define MAX_BUFF_SIZE 50
typedef struct Command_ {
char name[COMMAND_NAME_LEN];
int expected_param_count;
struct Command_ *left;
struct Command_ *right;
}Command;
typedef struct StringArray_ {
char **strings;
int size;
}StringArray;
StringArray* tokenizer (char *string, const char* delimiters);
void free_string_array(StringArray *sr);
void create_commands_tree(Command **commands, const char *file);
void insert_into_commands_tree(Command** node, char** data);
Command* get_command(Command *node, const char *command);
Command* create_command(char **data);
void destroy_commands_tree(Command* node);
void display_commands(Command *node);
int main (int argc, char *argv[]) {
if (argc < 2) {
printf("%s is missing commands.dat\n", argv[0]);
return 0;
}
Command* options = NULL;
create_commands_tree(&options,argv[1]);
int checking = 1;
char input_buffer[MAX_BUFF_SIZE];
do {
printf("Command: ");
fgets(input_buffer,MAX_BUFF_SIZE,stdin);
StringArray* parsed_input = tokenizer(input_buffer," \n");
Command* c = get_command(options,parsed_input->strings[0]);
if( c && parsed_input->size == c->expected_param_count) {
if (strcmp(c->name, "quit") == 0){
checking = 0;
}
printf("Valid command used\n");
}
else {
printf("Invalid command, please try again\n");
}
free_string_array(parsed_input);
}while (checking);
destroy_commands_tree(options);
}
void create_commands_tree(Command **commands, const char *file) {
FILE *input;
input = fopen(file, "r");
char strings[100];
StringArray *temp2;
while(fgets(strings,100,input)){
temp2 = tokenizer(strings, "\n");
insert_into_commands_tree(commands,temp2->strings);
}
}
void insert_into_commands_tree(Command** node, char** data) {
if(*node == NULL){
*node = create_command(data);
}
else if( *node != NULL){
if(strcmp(data[0],(*node)->name) < 0)
insert_into_commands_tree(&(*node)->left,data);
else if(strcmp(data[0], (*node)->name) > 0)
insert_into_commands_tree(&(*node)->right,data);
}
}
Command* create_command(char **data) {
Command* new_;
new_ = (Command*)malloc(sizeof(Command));
strncpy(new_->name, data[0], COMMAND_NAME_LEN);
new_->expected_param_count = atoi(data[1]);
new_->right = NULL;
new_->left = NULL;
return new_;
}
Command* get_command(Command *node, const char *command) {
Command *temp = node;
int compare;
if(temp){
compare = strcmp(node->name, command);
if(compare == 0){
return temp;
}
else if(compare < 0){
return (get_command(node->right, command));
}
else{
if(compare > 0){
return (get_command(node->left, command));
}}
}
return temp;
}
void destroy_commands_tree(Command* node) {
if( node == NULL){
return;
}
destroy_commands_tree(node->left);
destroy_commands_tree(node->right);
free(node);
}
void display_commands(Command *node) {
printf("\npickup <item>");
printf("\nhelp ");
printf("\nquit ");
printf("\nload <file>\n\n");
}
StringArray* tokenizer (char *string, const char* delimiters){
StringArray* sr = malloc(sizeof(StringArray));
sr->strings = malloc(MAX_SPLIT_SIZE * sizeof(char *));
size_t len;
char* hold;
(sr->strings)[0] = malloc(MAX_BUFF_SIZE * sizeof(char));
hold = strtok(string, delimiters);
int i;
for(i = 1; i < MAX_SPLIT_SIZE; i++){
hold = strtok(NULL, delimiters);
if(hold == NULL){
sr->size = i + 1;
break;
}
(sr->strings)[i] = malloc(MAX_BUFF_SIZE * sizeof(char));
strcpy((sr->strings)[i], hold);
}
return sr;
}
void free_string_array(StringArray *sr) {
int i;
for(i = 0; i < sr->size; ++i){
free(sr->strings[i]);
}
free(sr->strings);
free(sr);
}
Here is the sample output that was given:
]$ ./a.out commands.dat 
Command: pickup 
Invalid command, please try again 
Command: pickup ball 
Valid command used 
Command: quit 1 
Invalid command, please try again 
Command: load 
Invalid command, please try again 
Command: load bak.sav 
Valid command used 
Command: help
Valid command used
Command: help 2 
Invalid command, please try again 
Command: quit 
Valid command used 
And the file that we read in is as follows:
pickup,2
help,1
quit,1
load,2
There are several issues with this code:
In function insert_into_commands_tree
Like said in a comment above, the condition should be if (*node == NULL). This is probably what caused a segfault.
Furthermore, the if ... else if structure is a bit redundant here, since it is a true/false condition. You can simply use:
void insert_into_commands_tree(Command** node, char** data) {
if(*node == NULL) {
*node = create_command(data);
}
else {
if(strcmp(data[0],(*node)->name) < 0)
insert_into_commands_tree(&(*node)->left,data);
else if(strcmp(data[0], (*node)->name) > 0)
insert_into_commands_tree(&(*node)->right,data);
}
}
(but it's just a small detail)
In function create_commands_tree
What you do here is read a line of the input file with fgets, and separate this line with the toker "\n", which is useless (since fgets already separates the lines). I think you meant:
temp2 = tokenizer(strings, ",");
In function tokenizer
The output of this function is not what it should be. The main issue here is that you don't copy the first token into your StringArray. Below is a valid (and a bit simplified) tokenizer function:
StringArray* tokenizer (char *string, const char* delimiters)
{
StringArray* sr = malloc(sizeof(StringArray));
sr->strings = malloc(MAX_SPLIT_SIZE * sizeof(char *));
char* hold;
int i = 0;
hold = strtok(string, delimiters);
while ((hold != NULL) && (i < MAX_SPLIT_SIZE))
{
(sr->strings)[i] = malloc(MAX_BUFF_SIZE * sizeof(char));
strncpy((sr->strings)[i], hold, MAX_SPLIT_SIZE);
i++;
hold = strtok(NULL, delimiters);
}
sr->size = i;
return sr;
}
With these modifications, your code seems to do what you expect.

Segmentation fault (core dumped) [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am new to C, I am learning structure now.
This tries to create an entire small program to manage a database.
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define MAX_DATA 512
#define MAX_ROWS 100
struct Address {
int id;
int set;
char name[MAX_DATA];
char email[MAX_DATA];
};
struct Database {
struct Address rows[MAX_ROWS];
};
struct Connection {
FILE *file;
struct Database *db;
};
void die(const char *message)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n", message);
}
exit(1);
}
void Address_print(struct Address *addr)
{
printf("%d %s %s\n",
addr->id, addr->name, addr->email);
}
void Database_load(struct Connection *conn)
{
int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die("Failed to load database.");
}
struct Connection *Database_open(const char *filename, char mode)
{
struct Connection *conn = malloc(sizeof(struct Connection));
if(!conn) die("Memory error");
conn->db = malloc(sizeof(struct Database));
if(!conn->db) die("Memory error");
if(mode == 'c') {
conn->file = fopen(filename, "w");
} else {
conn->file = fopen(filename, "r+");
if(conn->file) {
Database_load(conn);
}
}
if(!conn->file) die("Failed to open the file");
return conn;
}
void Database_close(struct Connection *conn)
{
if(conn) {
if(conn->file) fclose(conn->file);
if(conn->db) free(conn->db);
free(conn);
}
}
void Database_write(struct Connection *conn)
{
rewind(conn->file);
int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die("Failed to write database.");
rc = fflush(conn->file);
if(rc == -1) die("Cannot flush database.");
}
void Database_create(struct Connection *conn)
{
int i = 0;
for(i = 0; i < MAX_ROWS; i++) {
// make a prototype to initialize it
struct Address addr = {.id = i, .set = 0};
// then just assign it
conn->db->rows[i] = addr;
}
}
void Database_set(struct Connection *conn, int id, const char *name, const char *email)
{
addr->set = 1;
char *res = strncpy(addr->name, name, MAX_DATA);
addr->name[MAX_DATA] = '\n';
if(!res) die("Name copy failed");
res = strncpy(addr->email, email, MAX_DATA);
addr->email[MAX_DATA] = '\n';
if(!res) die("Email copy failed");
}
void Database_get(struct Connection *conn, int id)
{
struct Address *addr = &conn->db->rows[id];
if(addr->set) {
Address_print(addr);
} else {
die("ID is not set");
}
}
void Database_delete(struct Connection *conn, int id)
{
struct Address addr = {.id = id, .set = 0};
conn->db->rows[id] = addr;
}
void Database_list(struct Connection *conn)
{
int i = 0;
struct Database *db = conn->db;
for(i = 0; i < MAX_ROWS; i++) {
struct Address *cur = &db->rows[i];
if(cur->set) {
Address_print(cur);
}
}
}
int main(int argc, char *argv[])
{
if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]");
char *filename = argv[1];
char action = argv[2][0];
struct Connection *conn = Database_open(filename, action);
int id = 0;
if(argc > 3) id = atoi(argv[3]);
if(id >= MAX_ROWS) die("There's not that many records.");
switch(action) {
case 'c':
Database_create(conn);
Database_write(conn);
break;
case 'g':
if(argc != 4) die("Need an id to get");
Database_get(conn, id);
break;
case 's':
if(argc != 6) die("Need id, name, email to set");
Database_set(conn, id, argv[4], argv[5]);
Database_write(conn);
break;
case 'd':
if(argc != 4) die("Need id to delete");
Database_delete(conn, id);
Database_write(conn);
break;
case 'l':
Database_list(conn);
break;
default:
die("Invalid action, only: c=create, g=get, s=set, d=del, l=list");
}
Database_close(conn);
return 0;
}
It should be like this:
$ make ex17
cc -Wall -g ex17.c -o ex17
$ ./ex17 db.dat c
$ ./ex17 db.dat s 1 someone someemail
$
$ ./ex17 db.dat l
1 someone someemail
But I get this Error message: Segmentation fault (core dumped).
$ make ex17
cc -Wall -g ex17.c -o ex17
$ ./ex17 db.dat c
$ ./ex17 db.dat s 1 someone someemail
Segmentation fault (core dumped)
I think I've got something wrong in "Database_set". I want to solve this problem but I can not. I want to know what I did wrong. Appreciate any help on this error.
In the line
void Database_set(struct Connection *conn, int id, const char *name, const char *email)
{
addr->set = 1;
Where did you define addr? I don't even understand how this compiles...
Presumably you need to keep a count of the number of valid records in your database, and point addr to the first unused record when you add a new one. This requires an update to your database structure as well as to the functions used for adding and deleting. Or you loop through the database to the first address that is not set. Either way, your function needs to declare addr and set it to something useful before using it as a pointer.
The following lines of code may help (these would be the first lines of the Database_set function):
struct Address *addr;
int ii=0;
while(conn->db->rows[ii].set) ii++;
addr = conn->db->rows + ii;
addr->set = 1;
addr->id = id;
You also need to make the changes shown by craig65535. There are quite possibly other problems with your code, but with these additions I can execute the instructions you gave in your question; it compiles, it runs, it doesn't complain. It's a start.
Oh - and it was able to list the database with the l command...
Assuming addr is of type struct Address *, this is totally wrong:
addr->name[MAX_DATA] = '\n';
addr->name only contains MAX_DATA bytes, so the maximum index you can write to is addr->name[MAX_DATA-1]. Also, you want that string to be null-terminated, so you want:
addr->name[MAX_DATA-2] = '\n';
addr->name[MAX_DATA-1] = '\0';
The same goes for addr->email.

Resources