得到错误"未定义引用"。你能解释一下它的含义以及我犯错的地方吗?

时间:2015-01-30 13:56:53

标签: c unix

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <string.h>
#include <signal.h>
#include <pwd.h>
#include <sys/types.h>
#include <crypt.h>
#include "pwent.h"

#define TRUE 1
#define FALSE 0
#define LENGTH 16

void sighandler() {
    signal(SIGINT,SIG_IGN);
}

int main(int argc, char *argv[]) {

    mypwent *passwddata; 
    /* see pwent.h */

    char important[LENGTH] = "***IMPORTANT***";

    char user[LENGTH];
    char prompt[] = "password: ";
    char swap_prompt[]="New password: ";
    char again_prompt[]="Again: ";
    char *user_pass;
    char *new_pass;
    char *again_pass;
    int f_login;
    char *en_pass;
        char *envp[] = { NULL };
    char *argvv[] = { "/bin/sh",NULL};

    sighandler();

    while (TRUE) {
        /* check what important variable contains - do not remove, part of buffer overflow test */
        printf("Value of variable 'important' before input of login name: %s\n",
                important);

        printf("login: ");
        fflush(NULL); /* Flush all  output buffers */
        __fpurge(stdin); /* Purge any data in stdin buffer */

        if (fgets(user,16,stdin) == NULL) /* gets() is vulnerable to buffer */
        {
            exit(0); /*  overflow attacks.  */  
        }   

        */* check to see if important variable is intact after input of login name - do not remove */*
        printf("Value of variable 'important' after input of login name: %*.*s\n",
                LENGTH - 1, LENGTH - 1, important);

        user_pass = getpass(prompt);
        passwddata = mygetpwnam(user);

        if (passwddata != NULL) {
            en_pass=crypt(user_pass,passwddata->passwd_salt);

            if (!strcmp(en_pass, passwddata->passwd)) {
                if(passwddata->pwage==10){
                    printf("You need to swap your password!!! \n");
                    do{
                        new_pass=getpass(swap_prompt);
                        again_pass=getpass(again_prompt);
                    }while(strcmp(new_pass,again_pass));
                    printf("Password changed!!! \n");
                    passwddata->passwd=new_pass;
                    passwddata->pwage=0;
                }else{
                    printf(" You're in !\n");
                    printf("Number of failed login is %d\n", passwddata->pwfailed);
                    passwddata->pwfailed=0;
                    passwddata->pwage++;
                }
                mysetpwent(user,passwddata);
                setuid(passwddata->uid);
                execve("/bin/sh",argvv,envp);

            }else{
                if(passwddata->pwfailed==3){
                    printf("You attempted too many times \n");
                    passwddata->pwfailed=0;
                    mysetpwent(user,passwddata);
                    return 0;
                }
                printf("Wrong password, please try again!!! \n");
                f_login++;
                passwddata->pwfailed=f_login;
                mysetpwent(user,passwddata);
            }
        }else{
            printf("Login Incorrect \n");
        }

    }
    return 0;
}

所以我得到了错误&#34;未定义的对mygetpwnam的引用&#34;和&#34;对mysetpwent&#34;的未定义引用。我不确定这究竟意味着什么以及如何纠正它。这是我正在处理的有关unix及其密码系统的任务的一部分。

3 个答案:

答案 0 :(得分:2)

您尝试在代码中调用函数mygetpwnam,并在mysetpwent调用三次,但这些函数未在任何位置定义。因此,您引用了一些未定义的错误。

答案 1 :(得分:0)

&#34; mygetpwnam&#34;和#34; mysetpwent&#34;在pwd.h中定义。打开并检查pwd.h是否有&#34; mygetpwnam&#34;的定义。和#34; mysetpwent&#34;。

答案 2 :(得分:0)

检查 pwent.h 以查看您的函数是否在那里声明(定义应该在 pwent.c 中)。还要确保您没有错误拼写功能名称。你应该告诉我们你如何编译程序。