userpass.c

来自「在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LE」· C语言 代码 · 共 135 行

C
135
字号
/*
 * FILENAME: userpass.c
 *
 * Copyright 1997- 2000 By InterNiche Technologies Inc. All rights reserved
 *
 * userpass.c user & password management for demo TCP/IP application. 
 * This is used by FTP, WebPort, PPP(pap), and any other network 
 * protocols which need a user name and password.
 *
 * MODULE: MISCLIB
 *
 * ROUTINES: add_user(), check_permit()
 *
 * PORTABLE: yes
 */


#include "ipport.h"
#include "in_utils.h"
#include "userpass.h"

struct userpass   users[NUM_NETUSERS];


/* FUNCTION: add_user()
 *
 * add_user() - Add a username & password to the user database. This 
 * takes an extra parameter (void permissions) which is not used by 
 * the demo package and may may used for extra checking for the 
 * application. Duplicate user/password combinations are accepted in 
 * case two different apps (e.g. FTP and PPP try to register the same 
 * user. a second entry is not created for duplicate calls. 
 *
 * If the username and password match with an existing entry, then
 * we check if "permissions" is present or not. If "permissions" is
 * present, then it is used as a new password. Hence the add_user()
 * call can be used to change the password for an existing user.
 * Refer change_pwd() for sample use.
 * 
 * PARAM1: char * username
 * PARAM2: char * password
 * PARAM3: void * permissions
 *
 * RETURNS: TRUE if user/password combo was accepted, else FALSE 
 */

int
add_user(char * username, 
   char *   password, 
   void *   permissions)   /* NULL if not used */
{
   int   i;

   if (!username) return FALSE;  /* don't allow null user */
      if (!password) password = ""; /* allow null password */

      for (i = 0; i < NUM_NETUSERS; i++)
   {
      if (users[i].username[0] == 0)
         break;   /* found empty entry */
      else  /* users[] entry has data in it, check for duplicate */
      {
         /* permit duplicate name/password entries */
         if ((strcmp(users[i].username, username) == 0) &&
             (strcmp(users[i].password, password) == 0))
         {
            if (permissions) 
            {
               /* assume that in this case permissions points to
                * new password */
               strncpy(users[i].password, (void *)permissions, MAX_USERLENGTH);
            }
            return TRUE;
         }
      }
   }

   if (i >= NUM_NETUSERS)
      return FALSE;

   strncpy(users[i].username, username, MAX_USERLENGTH);
   strncpy(users[i].password, password, MAX_USERLENGTH);
   users[i].permissions = permissions;
   return TRUE;
}



/* FUNCTION: check_permit()
 *
 * check_permit() - This is called from applciations which want to 
 * verify a user/password combination. Two extra parameters are 
 * provided for the application's use. These are: - appcode is one of 
 * the defines in userpass.h. this will identify the calling module. 
 * - permissions. This may be used by apps as needed. 
 *
 * 
 * PARAM1: char *    username
 * PARAM2: char *    password
 * PARAM3: int       appcode
 * PARAM4: void *    permissions
 *
 * RETURNS: TRUE if use is allowed, FALSE if not.
 */

int
check_permit(char * username, 
   char *   password, 
   int      appcode,       /* application which called us */
   void *   permissions)   /* NULL if not used */
{
   int   i;

   for (i = 0; i < NUM_NETUSERS; i++)
   {
      if (strcmp(users[i].username, username) == 0)
         break;
   }

   if (i >= NUM_NETUSERS)
      return FALSE;


   if (strcmp(users[i].password, password) != 0)
      return FALSE;

   /* add any further checking based on appcode & permissions here: */
   USE_ARG(appcode);
   USE_VOID(permissions); 

   return TRUE;   /* this user is allowed in */
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?