1 /*
2  * wzdftpd - a modular and cool ftp server
3  * Copyright (C) 2002-2004  Pierre Chifflier
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * As a special exemption, Pierre Chifflier
20  * and other respective copyright holders give permission to link this program
21  * with OpenSSL, and distribute the resulting executable, without including
22  * the source code for OpenSSL in the source distribution.
23  */
24 
25 #ifndef __WZD_USER_H__
26 #define __WZD_USER_H__
27 
28 /** @brief User definition
29  */
30 struct wzd_user_t {
31   uid_t                  uid;
32   u16_t                  backend_id;
33   char                   username[HARD_USERNAME_LENGTH];
34   char                   userpass[MAX_PASS_LENGTH];
35   char                   rootpath[WZD_MAX_PATH];
36   char                   tagline[MAX_TAGLINE_LENGTH];
37   unsigned int           group_num;
38   unsigned int           groups[MAX_GROUPS_PER_USER];
39   u32_t                  max_idle_time;
40   wzd_perm_t             userperms;      /**< @brief default permissions */
41   char                   flags[MAX_FLAGS_NUM];
42   u32_t                  max_ul_speed;
43   u32_t                  max_dl_speed;   /**< @brief bytes / sec */
44   unsigned short         num_logins;     /**< @brief number of simultaneous logins allowed */
45   struct wzd_ip_list_t * ip_list;
46   wzd_stats_t            stats;
47   u64_t                  credits;
48   unsigned int           ratio;
49   unsigned short         user_slots;     /**< @brief user slots for gadmins */
50   unsigned short         leech_slots;    /**< @brief leech slots for gadmins */
51   time_t                 last_login;
52 };
53 
54 /** \brief Allocate a new empty structure for a user
55  */
56 wzd_user_t * user_allocate(void);
57 
58 /** \brief Initialize members of struct \a user
59  */
60 void user_init_struct(wzd_user_t * user);
61 
62 /** \brief Free memory used by a \a user structure
63  */
64 void user_free(wzd_user_t * user);
65 
66 /** \brief Create a new user, giving default parameters
67  * \return The new user, or NULL. If \a err is provided, set it to
68  * the error code.
69  */
70 wzd_user_t * user_create(const char * username, const char * pass, const char * groupname, wzd_context_t * context, wzd_config_t * config, int * err);
71 
72 /** \brief Register a user to the main server
73  * \return The uid of the registered user, or -1 on error
74  */
75 uid_t user_register(wzd_user_t * user, u16_t backend_id);
76 
77 /** \brief Update a registered user atomically. Datas are copied,
78  * and old user is freed.
79  * A pointer to the old user is still valid (change is done in-place)
80  * If the uid had changed, the user will be moved
81  * \return 0 if ok
82  */
83 int user_update(uid_t uid, wzd_user_t * new_user);
84 
85 /** \brief Unregister a user to the main server
86  * The \a user struct must be freed using user_free()
87  * \warning Unregistering a user at runtime can break the server if the user is being used
88  * \return The unregistered user structure, or NULL on error
89  */
90 wzd_user_t * user_unregister(uid_t uid);
91 
92 /** \brief Free memory used to register users
93  * \warning Also free ALL registered users !
94  */
95 void user_free_registry(void);
96 
97 /** \brief Get registered user using the \a uid
98  * \return The user, or NULL
99  */
100 wzd_user_t * user_get_by_id(uid_t uid);
101 
102 /** \brief Get registered user using the \a name
103  * \return The user, or NULL
104  */
105 wzd_user_t * user_get_by_name(const char * username);
106 
107 /** \brief Get list or users register for a specific backend
108  * The returned list is terminated by -1, and must be freed with wzd_free()
109  */
110 uid_t * user_get_list(u16_t backend_id);
111 
112 /** \brief Find the first free uid, starting from \a start
113  */
114 uid_t user_find_free_uid(uid_t start);
115 
116 /** \brief Add an ip to the list of authorized/forbidden ips
117  * \return 0 if ok
118  */
119 int user_ip_add(wzd_user_t * user, const char * ip, int is_authorized);
120 
121 /** \brief List all users in a particular group, optionally filtered by a flag
122  *
123  * Optional: a flag can be specified where only users with this flag set will be returned (use 0 to ignore)
124  * \return
125  *  - a user list terminated by -1, must be freed with wzd_free()
126  *  - NULL if no group with that gid was found
127  */
128 uid_t * group_list_users(gid_t gid, char flag /* optional */);
129 
130 #endif /* __WZD_USER_H__ */
131