2 * This file is part of the OpenPTS project.
4 * The Initial Developer of the Original Code is International
5 * Business Machines Corporation. Portions created by IBM
6 * Corporation are Copyright (C) 2010 International Business
7 * Machines Corporation. All Rights Reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the Common Public License as published by
11 * IBM Corporation; either version 1 of the License, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * Common Public License for more details.
19 * You should have received a copy of the Common Public License
20 * along with this program; if not, a copy can be viewed at
21 * http://www.opensource.org/licenses/cpl1.0.php.
26 * \brief UUID wrapper (Generic part, OPENPTS_UUID)
27 * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
29 * cleanup 2012-01-05 SM
35 * Program UUID Description When Files
36 * ---------------------------------------------------------------------------------------------------
37 * ptsc CID Colelctor ID System install => /var/lib/openpts/uuid (UUID of sign key)
38 * RM RM ID RM Gen xid in RM, path /var/lib/openpts/$UUID/rm_files
39 * RunID ID of this daemon Daemon start => /var/lib/openpts/run_uuid TODO ptsc requires uuid file
40 * ---------------------------------------------------------------------------------------------------
41 * openpts VID Verifier ID 1st run => 'HOME/.openpts/uuid
42 * CID Colelctor ID Enrollment => 'HOME/.openpts/$UUID (dir name)
43 * RM Colelctor RM ID Enrollment => 'HOME/.openpts/$UUID/rm_uuid
44 * NEWRM Colelctor New RM ID Update => 'HOME/.openpts/$UUID/newrm_uuid
45 * OLDRM Colelctor Old RM ID Update => 'HOME/.openpts/$UUID/oldrm_uuid
46 * ---------------------------------------------------------------------------------------------------
48 * Unit Test: check_uuid.c
57 #include <sys/types.h>
68 #define SEP_LINE "------------------------------------------------------------------------------------"
71 /******************************/
73 /******************************/
76 * Create new OPENPTS_UUID, no contents
78 * @return OPENPTS_UUID
80 OPENPTS_UUID *newOpenptsUuid() {
83 uuid = xmalloc(sizeof(OPENPTS_UUID)); // BYTE[16]
85 LOG(LOG_ERR, "no memory");
88 memset(uuid, 0, sizeof(OPENPTS_UUID));
94 * Create new OPENPTS_UUID, with contents
96 * @return OPENPTS_UUID
98 OPENPTS_UUID *newOpenptsUuid2(PTS_UUID *pts_uuid) {
102 if (pts_uuid == NULL) {
103 LOG(LOG_ERR, "null input");
107 uuid = xmalloc(sizeof(OPENPTS_UUID)); // BYTE[16]
109 LOG(LOG_ERR, "no memory");
112 memset(uuid, 0, sizeof(OPENPTS_UUID));
114 uuid->uuid = xmalloc_assert(16);
115 memcpy(uuid->uuid, pts_uuid, 16);
117 uuid->str = getStringOfUuid(uuid->uuid);
118 uuid->time = getDateTimeOfUuid(uuid->uuid);
119 uuid->status = OPENPTS_UUID_UUID_ONLY;
125 * init UUID from file
126 * status = OPENPTS_UUID_EMPTY
127 * @return OPENPTS_UUID
129 OPENPTS_UUID *newOpenptsUuidFromFile(char * filename) {
134 if (filename == NULL) {
135 LOG(LOG_ERR, "null input");
139 uuid = newOpenptsUuid();
141 LOG(LOG_ERR, "no memory");
145 /* set the filename */
146 uuid->filename = smalloc_assert(filename);
148 /* load the filename */
149 rc = readOpenptsUuidFile(uuid);
150 if (rc != PTS_SUCCESS) {
151 LOG(LOG_ERR, "newOpenptsUuidFromFile() - readOpenptsUuidFile() fail rc=%d\n", rc);
152 freeOpenptsUuid(uuid);
162 void freeOpenptsUuid(OPENPTS_UUID *uuid) {
165 LOG(LOG_ERR, "null input\n");
169 if (uuid->filename != NULL) {
170 xfree(uuid->filename);
172 if (uuid->uuid != NULL) {
175 if (uuid->str != NULL) {
178 if (uuid->time != NULL) {
188 * @retval PTS_SUCCESS
189 * @retval PTS_INTERNAL_ERROR
191 int genOpenptsUuid(OPENPTS_UUID *uuid) {
194 LOG(LOG_ERR, "null input");
198 /* check the status */
199 if (uuid->status == OPENPTS_UUID_EMPTY) {
200 // hold UUID only, no binding with the file
201 uuid->status = OPENPTS_UUID_UUID_ONLY;
202 } else if (uuid->status == OPENPTS_UUID_FILENAME_ONLY) {
203 // TODO Re genenation happen, before load the UUID from file
204 DEBUG("genOpenptsUuid() %s filled, before load the UUID from file\n", uuid->str);
205 uuid->status = OPENPTS_UUID_FILLED;
206 } else if (uuid->status == OPENPTS_UUID_FILLED) {
207 // TODO Re genenation happen
208 uuid->status = OPENPTS_UUID_CHANGED;
209 LOG(LOG_ERR, "genOpenptsUuid() %s - changed\n", uuid->str);
210 } else if (uuid->status == OPENPTS_UUID_CHANGED) {
211 // TODO Re genenation happen
212 uuid->status = OPENPTS_UUID_CHANGED;
213 LOG(LOG_ERR, "genOpenptsUuid() %s - changed again\n", uuid->str);
214 } else if (uuid->status == OPENPTS_UUID_UUID_ONLY) {
215 // TODO Re genenation happen
216 uuid->status = OPENPTS_UUID_UUID_ONLY;
217 LOG(LOG_ERR, "genOpenptsUuid() %s - changed again (no binding to the file)\n", uuid->str);
219 LOG(LOG_ERR, "genOpenptsUuid() - bad status\n");
224 if (uuid->uuid != NULL) {
227 if (uuid->str != NULL) {
230 if (uuid->time != NULL) {
235 uuid->uuid = newUuid();
236 uuid->str = getStringOfUuid(uuid->uuid);
237 uuid->time = getDateTimeOfUuid(uuid->uuid);
240 DEBUG("genOpenptsUuid() - %s\n", uuid->str);
246 * read UUID from file(uuid->filename), and fill OPENPTS_UUID
248 * @retval PTS_SUCCESS
249 * @retval PTS_INTERNAL_ERROR
251 int readOpenptsUuidFile(OPENPTS_UUID *uuid) {
252 int rc = PTS_SUCCESS;
259 LOG(LOG_ERR, "null input");
262 if (uuid->filename == NULL) {
263 LOG(LOG_ERR, "null input");
267 DEBUG("readOpenptsUuidFile() : %s\n", uuid->filename);
269 // TODO check UUID status?
270 if (uuid->status == OPENPTS_UUID_FILENAME_ONLY) {
273 // reload UUID from same? file
274 DEBUG("reload UUID, current UUID=%s, filename=%s\n",
275 uuid->str, uuid->filename);
279 if (uuid->uuid != NULL) {
282 if (uuid->str != NULL) {
285 if (uuid->time != NULL) {
290 if ((fp = fopen(uuid->filename, "r")) == NULL) {
291 // DEBUG("readUuidFile - UUID File %s open was failed\n", filename);
292 /* we don't want double free errors - we may have already freed these up
293 above. this was a genuine issue that caused multiple pointers to
294 reference the same area of memory. */
298 return PTS_DENIED; // TODO
302 memset(line, 0, BUF_SIZE);
305 if (fgets(line, BUF_SIZE, fp) != NULL) {
307 /* remove LR at the end otherwise getUuidFromString() go bad */
308 for (i = 0; i < BUF_SIZE; i++) {
309 if (line[i] == 0x0a) {
315 uuid->uuid = getUuidFromString(line);
316 if (uuid->uuid == NULL) {
317 LOG(LOG_ERR, "readUuidFile() - UUID is NULL, file %s\n", uuid->filename);
318 rc = PTS_INTERNAL_ERROR;
321 uuid->str = getStringOfUuid(uuid->uuid);
322 if (uuid->str == NULL) {
323 LOG(LOG_ERR, "readUuidFile() - STR UUID is NULL, file %s\n", uuid->filename);
324 rc = PTS_INTERNAL_ERROR;
327 uuid->time = getDateTimeOfUuid(uuid->uuid);
328 if (uuid->time == NULL) {
329 LOG(LOG_ERR, "readUuidFile() - TIME UUID is NULL, file %s\n", uuid->filename);
330 rc = PTS_INTERNAL_ERROR;
333 uuid->status = OPENPTS_UUID_FILLED;
335 ERROR(NLS(MS_OPENPTS, OPENPTS_UUID_READ_FAILED, "Failed to read the UUID file\n"));
345 * @retval PTS_SUCCESS
346 * @retval PTS_INTERNAL_ERROR
348 int writeOpenptsUuidFile(OPENPTS_UUID *uuid, int overwrite) {
351 int mode = S_IRUSR | S_IWUSR | S_IRGRP;
355 LOG(LOG_ERR, "null input");
358 if (uuid->filename == NULL) {
359 LOG(LOG_ERR, "null input\n");
362 if ((uuid->status != OPENPTS_UUID_FILLED) && (uuid->status != OPENPTS_UUID_CHANGED)) {
363 LOG(LOG_ERR, "writeOpenptsUuidFile() - uuid->status = %d (!= FILLED or CHANGED)\n", uuid->status);
364 // 1 => OPENPTS_UUID_FILENAME_ONLY, UUID is missing
365 return PTS_INTERNAL_ERROR;
367 if (uuid->str == NULL) {
368 LOG(LOG_ERR, "writeOpenptsUuidFile() - uuid->str == NULL\n");
369 return PTS_INTERNAL_ERROR;
373 if (overwrite == 1) {
375 if ((fp = fopen(uuid->filename, "w")) == NULL) {
376 ERROR(NLS(MS_OPENPTS, OPENPTS_UUID_FILE_OPEN_FAILED,
377 "Failed to open UUID file %s\n"), uuid->filename);
378 return PTS_INTERNAL_ERROR;
382 if ((fd = open(uuid->filename, O_CREAT | O_EXCL | O_WRONLY, mode)) == -1) {
383 if (errno == EEXIST) {
384 /* exist, keep the current UUID file */
385 ERROR(NLS(MS_OPENPTS, OPENPTS_UUID_FILE_EXISTS,
386 "The UUID file '%s' already exists\n"), uuid->filename);
387 // return PTS_SUCCESS; // TODO
388 return OPENPTS_FILE_EXISTS;
390 ERROR(NLS(MS_OPENPTS, OPENPTS_UUID_FILE_OPEN_FAILED,
391 "Failed to open UUID file %s\n"), uuid->filename);
392 return PTS_INTERNAL_ERROR;
395 if ((fp = fdopen(fd, "w")) == NULL) {
396 ERROR(NLS(MS_OPENPTS, OPENPTS_UUID_FILE_OPEN_FAILED,
397 "Failed to open UUID file %s\n"), uuid->filename);
398 return PTS_INTERNAL_ERROR;
403 fprintf(fp, "%s", uuid->str);
405 fclose(fp); // this close fd also
407 DEBUG("writeOpenptsUuidFile() %s -> %s\n", uuid->str, uuid->filename);